题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
题意:区间查询,单点更新。
题解:树状数组模板。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=50000+10;
int c[MAX];
int N;
string op;
int lowbit(int x)
{
return x&(-x);
}
//查询【1,x】区间和
int query(int x)
{
int s=0;
while(x>0)
{
s+=c[x];
x-=lowbit(x);
}
return s;
}
//单点更新,对节点x加data
void update(int x,int data)
{
while(x<=N)
{
c[x]+=data;
x+=lowbit(x);
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
int tcase=1;
while(T--)
{
scanf("%d",&N);
memset(c,0,sizeof(c));
for(int i=1;i<=N;i++)
{
int val;
scanf("%d",&val);
update(i,val);
}
printf("Case %d:\n",tcase++);
while(cin>>op&&op!="End")
{
int a,b;
scanf("%d%d",&a,&b);
if(op=="Query") printf("%d\n",query(b)-query(a-1));
if(op=="Add") update(a,b);
if(op=="Sub") update(a,-b);
}
}
return 0;
}