A Simple Problem with Integers
http://poj.org/problem?id=3468
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> struct node { int l,r; __int64 sum; __int64 add; }tree[100000*3]; void build(int left,int right,int root) { int mid=(left+right)/2; if(left==right) { tree[root].l=left; tree[root].r=right; tree[root].add=0; scanf("%I64d",&tree[root].sum); return ; } tree[root].l=left; tree[root].r=right; tree[root].add=0; build(left,mid,2*root); build(mid+1,right,2*root+1); tree[root].sum=tree[2*root].sum+tree[2*root+1].sum; } void modify(int left,int right,__int64 add,int root) { int mid=(tree[root].l+tree[root].r)/2; if(tree[root].l==left&&tree[root].r==right) { tree[root].sum+=add*(tree[root].r-tree[root].l+1); tree[root].add+=add; return; } if(tree[root].add) { tree[2*root].add+=tree[root].add; tree[2*root+1].add+=tree[root].add; tree[2*root].sum+=tree[root].add*(tree[2*root].r-tree[2*root].l+1); tree[2*root+1].sum+=tree[root].add*(tree[2*root+1].r-tree[2*root+1].l+1); tree[root].add=0; } if(right<=mid) modify(left,right,add,2*root); else if(left>=mid+1) modify(left,right,add,2*root+1); else { modify(left,mid,add,2*root); modify(mid+1,right,add,2*root+1); } tree[root].sum=tree[2*root].sum+tree[2*root+1].sum; } __int64 search(int left,int right,int root) { int mid=(tree[root].l+tree[root].r)/2; __int64 sum; if(left==tree[root].l&&right==tree[root].r) return sum=tree[root].sum; if(tree[root].add) { tree[2*root].add+=tree[root].add; tree[2*root+1].add+=tree[root].add; tree[2*root].sum+=tree[root].add*(tree[2*root].r-tree[2*root].l+1); tree[2*root+1].sum+=tree[root].add*(tree[2*root+1].r-tree[2*root+1].l+1); tree[root].add=0; } if(right<=mid) return sum=search(left,right,2*root); else if(left>=mid+1) return sum=search(left,right,2*root+1); else return sum=search(left,mid,2*root)+search(mid+1,right,2*root+1); } int main() { int n,q,i; char str[2]; int a,b; __int64 c; scanf("%d%d",&n,&q); build(1,n,1); for(i=1;i<=q;i++) { scanf("%s%d%d",str,&a,&b); if(str[0]=='Q') { printf("%I64d\n",search(a,b,1)); } else { scanf("%I64d",&c); modify(a,b,c,1); } } return 0; }