标签:+= bsp turn pre printf struct 更新 using style
题目链接:http://poj.org/problem?id=3468
题意:单点更新,区间求和。
题解:裸题
1 //POJ 3468 A Simple Problem with Integers 2 //单点更新 区间求和 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 typedef long long LL; 9 const int N=100000+10; 10 LL ans,n,m; 11 12 struct Tree 13 { 14 LL l,r; 15 LL sum,add; 16 }; 17 Tree tree[4*N]; 18 19 void pushup(LL x) //向上更新 20 { 21 LL tmp=x<<1; 22 tree[x].sum=tree[tmp].sum+tree[tmp+1].sum; 23 } 24 25 void pushdown(LL x) //向下更新 26 { 27 LL tmp=x<<1; 28 tree[tmp].add+=tree[x].add; 29 tree[tmp+1].add+=tree[x].add; 30 tree[tmp].sum+=tree[x].add*(tree[tmp].r-tree[tmp].l+1); 31 tree[tmp+1].sum+=tree[x].add*(tree[tmp+1].r-tree[tmp+1].l+1); 32 tree[x].add=0; 33 } 34 35 void build(LL l,LL r,LL x) 36 { 37 tree[x].l=l; 38 tree[x].r=r; 39 tree[x].add=0; 40 if(l==r) 41 { 42 scanf("%lld",&tree[x].sum); 43 return ; 44 } 45 LL tmp=x<<1; 46 LL mid=(l+r)>>1; 47 build(l,mid,tmp); 48 build(mid+1,r,tmp+1); 49 pushup(x); 50 } 51 52 void update(LL l,LL r,LL c,LL x) 53 { 54 if(r<tree[x].l||l>tree[x].r) return ; 55 if(l<=tree[x].l&&r>=tree[x].r) 56 { 57 tree[x].add+=c; 58 tree[x].sum+=c*(tree[x].r-tree[x].l+1); 59 return ; 60 } 61 if(tree[x].add) pushdown(x); 62 LL tmp=x<<1; 63 update(l,r,c,tmp); 64 update(l,r,c,tmp+1); 65 pushup(x); 66 } 67 68 void query(LL l,LL r,LL x) 69 { 70 if(r<tree[x].l||l>tree[x].r) return ; 71 if(l<=tree[x].l&&r>=tree[x].r) 72 { 73 ans+=tree[x].sum; 74 return ; 75 } 76 if(tree[x].add) pushdown(x); 77 LL tmp=x<<1; 78 LL mid=(tree[x].l+tree[x].r)>>1; 79 if(r<=mid) query(l,r,tmp); 80 else if(l>mid) query(l,r,tmp+1); 81 else 82 { 83 query(l,mid,tmp); 84 query(mid+1,r,tmp+1); 85 } 86 } 87 88 89 int main(){ 90 char op[5]; 91 LL A,B,C; 92 scanf("%lld %lld",&n,&m); 93 build(1,n,1); 94 for(int i=1;i<=m;i++){ 95 scanf("%s",op); 96 if(op[0]==‘Q‘){ 97 ans=0; 98 scanf("%lld%lld",&A,&B); 99 query(A,B,1); 100 printf("%lld\n",ans); 101 } 102 else if(op[0]==‘C‘){ 103 scanf("%lld%lld%lld",&A,&B,&C); 104 update(A,B,C,1); 105 } 106 } 107 return 0; 108 }
POJ 3468 A Simple Problem with Integers(线段树 单点更新+区间求和 )
标签:+= bsp turn pre printf struct 更新 using style
原文地址:http://www.cnblogs.com/Leonard-/p/7624797.html