HDU 1166 敌兵布阵【线段树 单点更新】

题意:给出n个数,a1,a2,a3,,,,,an,再给出一些操作

add i  j 表示给第i个节点增加j

sub i  j 表示给第i个节点减少j

query i j 表示询问第i个节点到第j个节点的和

 

套的别人的模板写的---还是要再多自己写写的说-------

  1 #include<iostream>  

  2 #include<cstdio>  

  3 #include<cstring> 

  4 #include <cmath> 

  5 #include<stack>

  6 #include<vector>

  7 #include<map> 

  8 #include<set>

  9 #include<queue> 

 10 #include<algorithm>  

 11 using namespace std;

 12 

 13 typedef long long LL;

 14 const int INF = (1<<30)-1;

 15 const int mod=1000000007;

 16 const int maxn=1000005;

 17 

 18 int a[maxn];

 19 

 20 struct node{

 21     int l,r;

 22     int s;

 23 };

 24 

 25 node tree[4*maxn];

 26 

 27 int calc(int x,int y) { return x + y;}

 28 

 29 void build_tree(int i,int l,int r){

 30     if(l==r){

 31         tree[i].l=l;

 32         tree[i].r=r;

 33         tree[i].s=a[l];

 34         return;

 35     }

 36     int mid=(l+r)/2;

 37     build_tree(2*i,l,mid);

 38     build_tree(2*i+1,mid+1,r);

 39     tree[i].l=l;

 40     tree[i].r=r;

 41     tree[i].s=calc(tree[2*i].s,tree[2*i+1].s);

 42 }

 43 

 44 void update1(int i,int s,int w){

 45     if(tree[i].l==tree[i].r){

 46         tree[i].s+=w;

 47         return;

 48     }

 49     int mid=(tree[i].l + tree[i].r)/2;

 50     if(s<=mid) update1(2*i,s,w);

 51     else update1(2*i+1,s,w);

 52     

 53     tree[i].s=calc(tree[2*i].s,tree[2*i+1].s);

 54 }

 55 

 56 void update2(int i,int s,int w){

 57     if(tree[i].l==tree[i].r){

 58         tree[i].s-=w;

 59         return;

 60     }

 61     int mid=(tree[i].l + tree[i].r)/2;

 62     if(s<=mid) update2(2*i,s,w);

 63     else update2(2*i+1,s,w);

 64     

 65     tree[i].s=calc(tree[2*i].s,tree[2*i+1].s);

 66 }

 67 

 68 int sum(int i,int l,int r){

 69 //    printf("tree[%d].s=%d\n",i,tree[i].s);

 70     if(tree[i].l==l&&tree[i].r==r){

 71         return tree[i].s;

 72     }

 73     else{

 74         int mid=(tree[i].l+tree[i].r)/2;

 75         if(r<=mid) return sum(2*i,l,r);

 76         if(l>mid) return sum(2*i+1,l,r);

 77     

 78         return calc(sum(2*i,l,mid),sum(2*i+1,mid+1,r));

 79     }

 80 }

 81 

 82 int main(){

 83     int T;

 84     scanf("%d",&T);

 85     int kase=0;

 86     while(T--){

 87         int n;

 88         scanf("%d",&n);

 89         for(int i=1;i<=n;i++) scanf("%d",&a[i]);

 90         

 91         build_tree(1,1,n);

 92         

 93             char s[15];

 94         int x,y;

 95         printf("Case %d:\n",++kase);

 96         

 97         while(scanf("%s",s)!=EOF){

 98             

 99             if(s[0] == 'E') break;

100             if(s[0] == 'A'){

101                 scanf("%d %d",&x,&y);

102                 update1(1,x,y);

103             }

104             if(s[0]=='S'){

105                 scanf("%d %d",&x,&y);

106                 update2(1,x,y);

107             }

108             if(s[0] == 'Q'){

109                 scanf("%d %d",&x,&y);

110                 printf("%d\n",sum(1,x,y));

111             }

112         }    

113     }

114     return 0;

115 }
View Code

 

你可能感兴趣的:(HDU)