#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;
int number[50001];
struct node
{
int l,r,sum;
}p[800000];
void build(int k,int l,int r)
{
p[k].l=l;
p[k].r=r;
if(r==l)
{
p[k].sum=number[l];
return ;
}
int mid=(l+r)/2;
build(k*2,l,mid);
build(k*2+1,mid+1,r);
p[k].sum=p[2*k].sum+p[2*k+1].sum;
}
void changepoint(int i,int x,int y )
{
if(p[i].l==p[i].r)
{
p[i].sum+=y;
return ;
}
else
{
p[i].sum+=y;
if(x<=p[i*2].r)
changepoint(2*i,x,y);
else
changepoint(2*i+1,x,y);
}
}
int query(int k,int l,int r)
{
int mid;
if(p[k].l==l&&p[k].r==r)
{
return p[k].sum;
}
mid=(p[k].l+p[k].r)/2;
if(r<=mid)
return query(k*2,l,r);
else if(mid<l)
return query(k*2+1,l,r);
else
return query(k*2,l,mid)+query(k*2+1,mid+1,r);
}
int main()
{
int n;
cin>>n;
int kk=0;
while(n--)
{
kk++;
int num;
scanf("%d",&num);
for(int i=1; i<=num; i++)
{
scanf("%d",&number[i]);
}
build(1,1,num);
printf("Case %d:\n",kk);
string ll;
while(cin>>ll)
{
if(ll[0]=='E')
break;
int a,b;
scanf("%d%d",&a,&b);
if(ll[0]=='Q')
{
int ans=query(1,a,b);
cout<<ans<<endl;
}
else if(ll[0]=='A')
{
changepoint(1,a,b);
}
else
{
changepoint(1,a,-b);
}
}
}
}
HDU 1166 (线段树)
最新推荐文章于 2019-08-24 20:29:00 发布