题目链接:
http://acm.uestc.edu.cn/#/problem/show/1057
题意:
题解:
代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define MS(a) memset(a,0,sizeof(a)) 5 #define MP make_pair 6 #define PB push_back 7 const int INF = 0x3f3f3f3f; 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; 9 inline ll read(){ 10 ll x=0,f=1;char ch=getchar(); 11 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 12 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 13 return x*f; 14 } 15 ////////////////////////////////////////////////////////////////////////// 16 const int maxn = 1e5+10; 17 18 int n,a[maxn],q; 19 20 struct node{ 21 int l,r; 22 ll sum,lazy; 23 void update(ll x){ 24 sum += 1LL * (r-l+1)*x; 25 lazy += x; 26 } 27 }tree[maxn<<2]; 28 29 void pushup(int rt){ 30 tree[rt].sum = tree[rt<<1].sum+tree[rt<<1|1].sum; 31 } 32 33 void pushdown(int rt){ 34 int lazyval = tree[rt].lazy; 35 if(lazyval){ 36 tree[rt<<1].update(lazyval); 37 tree[rt<<1|1].update(lazyval); 38 tree[rt].lazy = 0; 39 } 40 } 41 42 43 void build(int rt,int l,int r){ 44 tree[rt].l = l,tree[rt].r = r; 45 tree[rt].sum = tree[rt].lazy = 0; 46 if(l == r){ 47 tree[rt].sum = a[l]; 48 }else{ 49 int mid = (l+r)/2; 50 build(rt<<1,l,mid); 51 build(rt<<1|1,mid+1,r); 52 pushup(rt); 53 } 54 } 55 56 void update(int rt, int l,int r,ll val){ 57 int L = tree[rt].l, R = tree[rt].r; 58 if(l<=L && R<=r){ 59 tree[rt].update(val); 60 }else{ 61 pushdown(rt); 62 int mid = (L+R) / 2; 63 if(mid >= l) update(rt<<1,l,r,val); 64 if(r > mid) update(rt<<1|1,l,r,val); 65 pushup(rt); 66 } 67 } 68 69 ll query(int rt,int l,int r){ 70 int L = tree[rt].l, R = tree[rt].r; 71 if(l<=L && R<=r){ 72 return tree[rt].sum; 73 }else{ 74 pushdown(rt); 75 ll ans = 0; 76 int mid = (L+R) / 2; 77 if(mid >= l) ans += query(rt<<1,l,r); 78 if(r > mid) ans += query(rt<<1|1,l,r); 79 pushup(rt); 80 return ans; 81 } 82 } 83 84 int main(){ 85 n = read(); 86 for(int i=1; i<=n; i++) 87 a[i] = read(); 88 build(1,1,n); 89 q = read(); 90 for(int i=1; i<=q; i++){ 91 int l,r,val; 92 scanf("%d%d%d",&l,&r,&val); 93 update(1,l,r,val); 94 printf("%I64d ",query(1,l,r)); 95 } 96 97 return 0; 98 }