1 //线段树入门,单点修改,区间查询 2 #include <cstdio> 3 const int maxn = 3*1e6+5; 4 struct node 5 { 6 int l,r; 7 node *pl, *pr; 8 int s; 9 }; 10 node t[maxn]; 11 int cnt = 0; 12 int mid(node *p) 13 { 14 return (p->l + p->r)/2; 15 } 16 void build(node *p,int l,int r) 17 { 18 p->l = l; p->r = r; 19 p->s = 0; 20 if(l == r) return; 21 p->pl = t + ++cnt; 22 p->pr = t + ++cnt; 23 build(p->pl,l,(l+r)/2); 24 build(p->pr,(l+r)/2+1,r); 25 } 26 void insert(node *p,int i,int v) 27 { 28 p->s += v; 29 if(p->l == i && p->r == i) return; 30 if(i > mid(p)) insert(p->pr,i,v); 31 else insert(p->pl,i,v); 32 //p->s += p->pl->s + p->pr->s; 33 } 34 int query(node *p,int l,int r) 35 { 36 if(p->l == l && p->r == r) return p->s; 37 if(r <= mid(p)) return query(p->pl,l,r); 38 else if(l > mid(p)) return query(p->pr,l,r); 39 else return query(p->pl,l,mid(p)) + query(p->pr,mid(p)+1,r); 40 } 41 int main() 42 { 43 int n,m,x,y,z; 44 char op[20]; 45 scanf("%d%d",&n,&m); 46 build(t,1,n); 47 for(int i = 1; i <= n; ++i) 48 { 49 scanf("%d",&x); 50 insert(t,i,x); 51 } 52 while(m--) 53 { 54 scanf("%s%d%d",op,&x,&y); 55 if(op[0] == ‘Q‘) 56 printf("%d\n",query(t,x,y)); 57 else 58 insert(t,x,y); 59 } 60 return 0; 61 }
时间: 2024-11-01 23:26:07