标签:
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<queue> 5 #include<stack> 6 #include<vector> 7 #include<iomanip> 8 #include<algorithm> 9 #include<string> 10 #include<cstring> 11 #include<cstdlib> 12 #include<bitset> 13 #define LL long long 14 using namespace std; 15 struct data 16 { 17 int left,right; 18 long long sum; 19 int l,r; 20 }; 21 data te[400005]; 22 int n,q,a,b,w,cnt,p; 23 long long ans; 24 int change[400005]//here,c[200005]; 25 void update(int x) 26 { 27 te[x].sum=te[te[x].left].sum+te[te[x].right].sum; 28 } 29 int build(int s,int t) 30 { 31 cnt++; 32 int k=cnt; 33 te[k].l=s;te[k].r=t; 34 if (s==t) 35 { 36 te[k].sum=c[s]; 37 return k; 38 } 39 int mid=(s+t)>>1; 40 te[k].left=build(s,mid); 41 te[k].right=build(mid+1,t); 42 update(k); 43 return k; 44 } 45 void paint(int x,int k){ 46 te[x].sum+=((LL)te[x].r-te[x].l+1)*k; 47 change[x]+=k; 48 } 49 void pushdown(int x){ 50 paint(te[x].left,change[x]); 51 paint(te[x].right,change[x]); 52 change[x]=0; 53 } 54 void insert(int x) 55 { 56 if (x!=0) 57 { 58 if (change[x]!=0) 59 pushdown(x); 60 if (te[x].l>=a&&te[x].r<=b) 61 { 62 te[x].sum+=((LL)te[x].r-te[x].l+1)*w; 63 change[x]=w; 64 return ; 65 } 66 int mid=(te[x].l+te[x].r)>>1; 67 if (a<=mid) insert(te[x].left); 68 if (b>mid) insert(te[x].right); 69 update(x); 70 return ; 71 } 72 } 73 long long findsum(int x) 74 { 75 if (x!=0) 76 { 77 if (change[x]!=0) 78 pushdown(x); 79 if (te[x].l>b||te[x].r<a) return 0; 80 if (te[x].l>=a&&te[x].r<=b) return te[x].sum; 81 int mid=(te[x].l+te[x].r)>>1; 82 ans=findsum(te[x].left)+findsum(te[x].right); 83 return ans; 84 } 85 } 86 int main () 87 { 88 freopen ("1082.in","r",stdin); 89 //freopen ("1082.out","w",stdout); 90 scanf ("%d",&n); 91 for (int i=1;i<=n;i++) scanf ("%d",&c[i]); 92 build(1,n); 93 scanf ("%d",&q); 94 for (int i=1;i<=q;i++) 95 { 96 scanf ("%d",&p); 97 if (p==1) 98 { 99 scanf ("%d%d%d",&a,&b,&w); 100 insert(1); 101 } 102 if (p==2) 103 { 104 scanf ("%d%d",&a,&b); 105 printf ("%lld\n",findsum(1)); 106 } 107 } 108 return 0; 109 }
标签:
原文地址:http://www.cnblogs.com/Ang-Eric/p/5782581.html