标签:
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
Case 1: 6 33 59
//单点更新,区间求和
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 50005;
struct tree{
int l,r;
int num;
}t[4*N];
void build(int l,int r,int root){
t[root].l=l;
t[root].r=r;
t[root].num=0;
if(l==r){
return;
}
int mid=(l+r)/2;
build(l,mid,2*root);
build(mid+1,r,2*root+1);
}
void update(int val,int id,int root){
if(t[root].l==t[root].r&&t[root].l==id){
t[root].num+=val;
return;
}
int mid=(t[root].l+t[root].r)/2;
if(id<=mid)
update(val,id,2*root);
else
update(val,id,2*root+1);
t[root].num=t[2*root].num+t[2*root+1].num;
}
int ans;
void find(int l,int r,int root){
if(t[root].l==l&&t[root].r==r){
ans+=t[root].num;
return;
}
int mid=(t[root].l+t[root].r)/2;
if(r<=mid)
find(l,r,2*root);
else if(l>mid)
find(l,r,2*root+1);
else{
find(l,mid,2*root);
find(mid+1,r,2*root+1);
}
}
int main(){
int t,n,i,j;
int Case=1;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
build(1,n,1);
for(i=1;i<=n;i++){
int x;
scanf("%d",&x);
update(x,i,1);
}
char s[10];
printf("Case %d:\n",Case++);
while(scanf("%s",s)){
int x,y;
if(s[0]=='E') break;
scanf("%d %d",&x,&y);
if(s[0]=='A') update(y,x,1);
else if(s[0]=='S') update(-y,x,1);
else{
ans=0;
find(x,y,1);
printf("%d\n",ans);
}
}
}
return 0;
}标签:
原文地址:http://blog.csdn.net/qq_27717967/article/details/51355543