2019.9.4 清点人数

树状数组水题(逃

题目传送门

 

裸的树状数组 哪个车厢上下人就更新数据 走到哪个车厢就查

上代码

#include<iostream>
#include<cstdio>
#include<cstring>
#define lowbit(x) x&(-x)
#define int long long
using namespace std;
int n,k,m,p;
char ch;
int c[1000050];
void updata(int i,int x)
{
    while(i<=n)
    {
        c[i]+=x;
        i+=lowbit(i);
    }
}
int query(int i)
{
    int res=0;
    while(i>=1)
    {
        res+=c[i];
        i-=lowbit(i);
    }
    return res;
}
signed main()
{
    scanf("%lld%lld",&n,&k);
    for(int i=1;i<=k;i++)
    {
        cin>>ch;
        if(ch=='A')
        {
            scanf("%lld",&m);
            printf("%lld\n",query(m));
        }
        if(ch=='B')
        {
            scanf("%lld%lld",&m,&p);
            updata(m,p);
        }
        if(ch=='C')
        {
            scanf("%lld%lld",&m,&p);
            updata(m,-1*p);
        }
    }
    return 0;
}

 

posted @ 2019-09-04 18:43  lqxssf  阅读(198)  评论(0编辑  收藏  举报