树状数组+差分

 1 #include <iostream>
 2 using namespace std;
 3 int n,m;
 4 int d[100000];
 5 
 6 int lowbit(int x)  
 7 {
 8     return x&(-x);
 9 }
10 int query(int x)
11 {
12     int res=0;
13     while(x)
14     {
15         res+=d[x];
16         x-=lowbit(x);
17     }
18     return res;
19 }
20 void update(int x,int v)
21 {
22     while(x<=n)
23     {
24         d[x]+=v;
25         x+=lowbit(x);
26     }
27 
28 }
29 //以上的函数属于树状数组的函数
30 int main()
31 {
32     cin>>n>>m;
33     for(int i=1;i<=m;i++)
34     {
35         int a,b,c;
36         cin>>c;
37         if(c==0)
38         {
39             cin>>a>>b;
40             update(a,1);   //差分
41             update(b+1,-1);  //左边界改变一个数值,有边界改变相应的相反的数
42         }
43         if(c==1)
44         {
45             cin>>a;
46             cout<<query(a)<<endl;
47         }
48     }
49     return 0;
50 }

 

posted @ 2022-02-07 15:36  九点的日落  阅读(42)  评论(0)    收藏  举报