题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
题意:中文题
解析:线段树,单点更新,单点查询
#include <bits/stdc++.h>
using namespace std;
const int maxn = 50000+100;
struct node
{
int l,r;
int sum;
}tree[4* maxn];
int a[maxn];
void push_up(int i)
{
tree[i].sum = tree[i<<1].sum + tree[i<<1|1].sum;
}
void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].sum = 0;
if(l==r)
{
tree[i].sum = a[l];
return ;
}
int mid = (l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
push_up(i);
}
void update(int i,int pos,int val)
{
int l = tree[i].l;
int r = tree[i].r;
if(l==pos && r==pos)
{
tree[i].sum += val;
return ;
}
int mid = (l+r)>>1;
if(mid>=pos)
update(i<<1,pos,val);
else
update(i<<1|1,pos,val);
push_up(i);
}
int query(int i,int l,int r)
{
int L = tree[i].l;
int R = tree[i].r;
if(l<=L && R<=r)
return tree[i].sum;
int mid = (L+R)>>1;
int ans = 0;
if(mid<r)
ans += query(i<<1|1,l,r);
if(mid>=l)
ans += query(i<<1,l,r);
return ans;
}
int main(void)
{
int t,case_t = 1;
cin>>t;
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,1,n);
char op[100];
printf("Case %d:\n",case_t++);
while(~scanf("%s",op) && op[0]!='E')
{
int x,y;
scanf("%d %d",&x,&y);
if(op[0]=='Q')
printf("%d\n",query(1,x,y));
else if(op[0]=='A')
update(1,x,y);
else
update(1,x,-y);
}
}
return 0;
}