传送门

 

用两个树状数组,cr 维护 1....x 中 r 的数量

         cl 维护 1....x 中 l 的数量

求答案的时候只需要求 y 前面 被作为左端点 的个数 - x 前面 被作为右端点的个数

 

——代码

 1 #include <cstdio>
 2 
 3 using namespace std;
 4 
 5 const int MAXN = 1000001;
 6 int n, m;
 7 int cl[MAXN], cr[MAXN];
 8 
 9 inline void add1(int x) { for(; x <= n; x += x & -x) cl[x]++; }
10 inline void add2(int x) { for(; x <= n; x += x & -x) cr[x]++; }
11 inline int query1(int x) { int ans = 0; for(; x; x -= x & -x) ans += cl[x]; return ans; }
12 inline int query2(int x) { int ans = 0; for(; x; x -= x & -x) ans += cr[x]; return ans; }
13 
14 int main()
15 {
16     int i, j, x, y, z;
17     scanf("%d %d", &n, &m);
18     for(i = 1; i <= m; i++)
19     {
20         scanf("%d %d %d", &z, &x, &y);
21         if(z == 1)
22         {
23             add1(x);
24             add2(y);
25         }
26         else printf("%d\n", query1(y) - query2(x - 1));
27     }
28     return 0;
29 }
View Code

相关文章:

  • 2021-12-25
  • 2021-08-27
  • 2021-05-29
  • 2021-11-23
  • 2021-08-27
  • 2021-08-27
猜你喜欢
  • 2021-12-30
  • 2021-08-27
  • 2021-08-28
  • 2021-08-27
  • 2021-10-16
  • 2022-02-19
  • 2021-12-21
相关资源
相似解决方案