树状数组(区间更新,但点查询)

来源:互联网 发布:linux 文件上传权限 编辑:程序博客网 时间:2024/06/03 21:06



#include<iostream>#include<cstring>using namespace std;const int MAX=100010;int T, N, M;int c[MAX];inline int lowBit(int x){    return x & (-x);}void add(int pos, int v){    while(pos<=MAX-5)    {        c[pos]+=v;        pos+=lowBit(pos);    }}int getSum(int pos){    int sum=0;    while(pos>0)    {        sum+=c[pos];        pos-=lowBit(pos);    }    return sum;}void addInterval(int l, int r){    add(l, 1);    add(r+1, -1);}int main(){    ios::sync_with_stdio(0);    cin>>T;    int cnt=1;    int l, r, ans;    while(T--)    {        cin>>N>>M;        memset(c, 0, sizeof(c));        for(int i=1; i<=N; i++)        {            cin>>l>>r;            addInterval(l, r);        }        cout<<"Case #"<<cnt++<<":"<<endl;        for(int i=1; i<=M; i++)        {            cin>>l;            ans=getSum(l);            cout<<ans<<endl;        }    }    return 0;}


原创粉丝点击