维护与扫描

【树状数组】【扫描】LuoguP2184 贪婪大陆 

LuoguP2184 贪婪大陆 参考实现
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,q;
struct tree
{
    int t[N];
    void add(int x,int k)
    {while(x<=n) t[x]+=k,x+=x&(-x);}
    int query(int x)
    {
        int res=0;
        while(x) res+=t[x],x-=x&(-x);
        return res;
    }
}l,r;
int main()
{
    scanf("%d%d",&n,&q);
    int op,x,y;
    for(int i=1;i<=q;i++)
    {
        scanf("%d%d%d",&op,&x,&y);
        if(op==1) l.add(x,1),r.add(y,1);
        else printf("%d\n",(l.query(y)-r.query(x-1)));
    }
    return 0;
}