今天刚学了树状数组,兴冲冲地A了模板1、2之后,选标签找到这道题,却样例没过,去看了第一篇题解没看出什么错<@_@>
代码:
#include<bits/stdc++.h>
using namespace std;
int n,c[100005];
int lowbit(int x){return x&(-x);}
void update(int x,int v)
{
while(x<=n)
{
c[x]+=v;
x+=lowbit(x);
}
return;
}
int query(int x)
{
int res=0;
while(x>0)
{
res+=c[x];
x-=lowbit(x);
}
return res;
}
int main()
{
int n,w;
scanf("%d%d",&n,&w);
while(w--)
{
char op;
int a,b;
cin>>op;
scanf("%d%d",&a,&b);
if(op=='x')
update(a,b);
if(op=='y')
printf("%d\n",query(b)-query(a-1));
}
return 0;
}
样例输出:
0
0