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<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<algorithm> #include<cstdlib> #include<set> #include<queue> #include<stack> #include<vector> #include<map> #define N 50010 #define Mod 10000007 #define lson l,mid,idx<<1 #define rson mid+1,r,idx<<1|1 #define lc idx<<1 #define rc idx<<1|1 const double EPS = 1e-11; const double PI = acos ( -1.0 ); const double E = 2.718281828; typedef long long ll; const int INF = 1000010; using namespace std; int tree[N<<2]; int n; void push_up(int idx) { tree[idx]=tree[lc]+tree[rc]; } void build(int l,int r,int idx) { if(l==r) { scanf("%d",&tree[idx]); return; } int mid=(l+r)>>1; build(lson); build(rson); push_up(idx); } void update(int l,int r,int idx,int x,int k) { if(l==r) { tree[idx]+=k; return; } int mid=(l+r)>>1; if(x<=mid) update(lson,x,k); if(x>mid) update(rson,x,k); push_up(idx); } ll query(int l,int r,int idx,int x,int y) { if(x<=l&&y>=r) return tree[idx]; int mid=(l+r)>>1; ll sum=0; if(x<=mid) sum+=query(lson,x,y); if(y>mid) sum+=query(rson,x,y); return sum; } int main() { //freopen("in.txt","r",stdin); int t; while(cin>>t) { int ca=1; while(t--) { cin>>n; build(1,n,1); //getchar(); int x,y,k; char s[16]; printf("Case %d:\n",ca++); while(1) { scanf("%s",s); if(s[0]=='E') break; scanf("%d%d",&x,&y); if(s[0]=='Q') { printf("%I64d\n",query(1,n,1,x,y)); } else if(s[0]=='A') update(1,n,1,x,y); else update(1,n,1,x,-y); } } } return 0; }
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/42495853