hdu 1166 lineup segment tree Jones

#include <the iostream>
 the using  namespace STD;
 const  int MAX = 50000 ;
 int Tree [(MAX + . 1 ) * . 4 ]; // n-th leaf have 2 * n-4 * n nodes 
int A [MAX + . 1 ];
 int n-;
 void getup ( int the root) {
     return  void (Tree [the root] = Tree [* the root 2 ] + Tree [* the root 2 + . 1 ]); // two children are root * 2 and root * 2 + 1 
}
 void btree ( int L, int R & lt, intthe root) { // Contribution 
    IF (L == R & lt) { 
        Tree [the root] = A [L];
         return ; 
    } 
    int MID = (L + R & lt) >> . 1 ; 
    btree (L, MID, the root * 2 ); 
    btree (mid + . 1 , R & lt, the root * 2 + . 1 ); // since mid rounded down must be added to the end of an otherwise not 
    getup (the root); 
} 
int MyQuery ( int L, int R & lt, int L, int R & lt, int ) {root // whole idea is certainly intervals and can be expressed in binary interval
    if(l<=L&&r>=R) return tree[root];
    int mid=(L+R)>>1;
    long long sum=0;
    if(l<=mid) sum+=myquery(l,r,L,mid,root*2);//两边相加
    if(r>mid) sum+=myquery(l,r,mid+1,R,root*2+1);
    return sum;
}
void add(int i,int value,int l,int r,int root){//重点!!  更新的过程
    if(l==r) {tree[root]+=value;return ;}
    int mid=(l+r)>>1;
    if(i<=mid) add(i,value,l,mid,root*2);
    else add(i,value,mid+1,r,root*2+1);
    getup(root);
}
int main(){
    int t;
    cin>>t;
    int i=1;
    while(i<=t){
        int flag=1;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        btree(1,n,1);
        char ss[10];
       while(~scanf("%s",ss)){
        if(ss[0]=='A'){
            int i,value;
            scanf("%d %d",&i,&value);
            add(i,value,1,n,1);
        }
        if(ss[0]=='S'){
            int i,value;
            scanf("%d %d",&i,&value);
            add(i,-1*value,1,n,1);
        }
        if(ss[0]=='Q'){
            int a ,b;
            scanf("%d%d",&a,&b);
            if(flag){printf("Case %d:\n",i);flag=0;}
            printf("%d\n",myquery(a,b,1,n,1));
        }
        if(ss[0]=='E') break;
       }
        i++;
    }
    }

Topic links:? Acm.hdu.edu.cn/status.php user = YZBPXX & pid = 1166 & status = 5

Guess you like

Origin www.cnblogs.com/yzbpxx/p/11069956.html