HDU 1166

单点,easy

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 const int MM=50000;//10^6
 8 int num[MM<<2];
 9 void buildtree(int l,int r,int id)
10 {
11     if(l==r)
12     {
13         scanf("%d",&num[id]);return;
14     }
15     else
16     {
17         int mid=(l+r)>>1;
18         buildtree(l,mid,id<<1);
19         buildtree(mid+1,r,id<<1|1);
20     }num[id]=num[id<<1]+num[id<<1|1];
21 }
22 int query(int L,int R,int l,int r,int id)
23 {
24     if(L<=l&&r<=R)return num[id];
25     else
26     {
27         int mid=(l+r)>>1; int res=0;
28         if(L<=mid)res+=query(L,R,l,mid,id<<1);
29         if(R>mid)res+=query(L,R,mid+1,r,id<<1|1);
30         return res;
31     }
32     
33 }
34 void update(int pos,int e,int l,int r,int id)
35 {
36     if(l==r)
37     {
38         num[id]+=e;return;
39     }
40     else
41     {
42         int mid=(l+r)>>1;
43         if(pos<=mid)update(pos,e,l,mid,id<<1);
44         else if(pos>mid)update(pos,e,mid+1,r,id<<1|1);
45         num[id]=num[id<<1]+num[id<<1|1];
46     }
47 }
48 int main()
49 {
50     int t,n,cas,i,x,y;
51     char str[10];
52     scanf("%d",&t);
53     for(cas=1;cas<=t;cas++)
54     {
55         printf("Case %d:
",cas);
56         scanf("%d",&n);
57         buildtree(1,n,1);
58         memset(str,0,sizeof str);
59         while(scanf("%s",str))
60         {
61             if(strcmp(str,"End")==0)break;
62             else if(strcmp(str,"Add")==0)
63             {
64                 scanf("%d %d",&x,&y);
65                 update(x,y,1,n,1);
66             }
67             else if(strcmp(str,"Sub")==0)
68             {
69                 scanf("%d %d",&x,&y);
70                 update(x,-y,1,n,1);
71             }
72             else
73             {
74                 scanf("%d %d",&x,&y);
75                 printf("%d
",query(x,y,1,n,1) );
76             }
77         }
78         
79     }
80 }
原文地址:https://www.cnblogs.com/sylvialucy/p/4135512.html