[关闭]
@ZCDHJ 2018-08-27T03:11:27.000000Z 字数 639 阅读 455

VIJOS 1448

树状数组


VIJOS

大概就是用树状数组来维护。

每次用区间 ,中的树的种类数减去 中完全在这个区间中的种类数就是答案了。

那么每次维护两个树状数组,一个维护左端点的个数,一个维护右端点的个数。

  1. #include <iostream>
  2. #include <cstdio>
  3. const int MAXN = 5e4 + 5;
  4. int N, M;
  5. int BIT[2][MAXN];
  6. inline int read()
  7. {
  8. register int x = 0;
  9. register char ch = getchar();
  10. while(!isdigit(ch)) ch = getchar();
  11. while(isdigit(ch))
  12. {
  13. x = x * 10 + ch - '0';
  14. ch = getchar();
  15. }
  16. return x;
  17. }
  18. inline void Modify(int t, int x)
  19. {
  20. while(x <= N)
  21. {
  22. ++BIT[t][x];
  23. x += x & (-x);
  24. }
  25. }
  26. inline int Query(int t, int x)
  27. {
  28. int res = 0;
  29. while(x > 0)
  30. {
  31. res += BIT[t][x];
  32. x -= x & (-x);
  33. }
  34. return res;
  35. }
  36. int main()
  37. {
  38. N = read();
  39. M = read();
  40. while(M--)
  41. {
  42. int opt = read(), l = read(), r = read();
  43. if(opt == 1)
  44. {
  45. Modify(0, l);
  46. Modify(1, r);
  47. }
  48. else printf("%d\n", Query(0, r) - Query(1, l - 1));
  49. }
  50. return 0;
  51. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注