秋实大哥是一个儒雅之人,昼听笙歌夜醉眠,若非月下即花前。
所以秋实大哥精心照料了很多花朵。现在所有的花朵排成了一行,每朵花有一个愉悦值。
秋实大哥每天要对着某一段连续的花朵歌唱,然后这些花朵的愉悦值都会增加一个相同的值vvv(vvv可能为负)。
同时他想知道每次他唱完歌后这一段连续的花朵的愉悦值总和是多少。
Standard Input
第一行有一个整数nnn,表示花朵的总数目。
第二行包含nnn个整数a_ia\_ia_i,表示第iii朵花初始的愉悦值。
第三行包含一个整数mmm,表示秋实大哥唱了mmm天的歌。
接下来mmm行,每行包含三个整数lll rrr vvv,表示秋实大哥对着[l,r][l, r][l,r]这个区间内的花朵歌唱,每朵花的愉悦值增加了vvv。
1≤n,m,a_i,∣v∣≤1000001\leq n,m,a\_i,|v|\leq 1000001≤n,m,a_i,∣v∣≤100000,1≤l≤r≤n。1\leq l\leq r\leq n。1≤l≤r≤n。
Standard Output
输出共mmm行,第iii行表示秋实大哥完成第iii天的歌唱后,那一段花朵的愉悦值总和。
Samples
Input Output 3 0 0 0 3 1 2 1 1 2 -1 1 3 1 2 0 3
Problem ID 1057 Problem Title 秋实大哥与花 Time Limit 1000 ms Memory Limit 64 MiB Output Limit 64 MiB Source 2015 UESTC Training for Data Structures
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define mod 1000000007
#define MAXN 100005
LL a[MAXN],lazy[MAXN<<2],tree[MAXN<<2];
inline void pushUp(int root){
tree[root]=tree[root<<1]+tree[root<<1|1];
}
void pushDown(int root,int le){
if(lazy[root]){
lazy[root<<1]+=lazy[root];
lazy[root<<1|1]+=lazy[root];
tree[root<<1]+=lazy[root]*(le-(le>>1));
tree[root<<1|1]+=lazy[root]*(le>>1);
lazy[root]=0;
}
}
void build(int l,int r,int root){
lazy[root]=0;
if(l==r){
tree[root]=a[l];
return;
}
int m=(l+r)>>1;
build(l,m,root<<1);
build(m+1,r,root<<1|1);
pushUp(root);
}
void update(int L,int R,LL c,int l,int r,int root){
if(l>=L&&r<=R){
lazy[root]+=c;
tree[root]+=c*(r-l+1);
return;
}
pushDown(root,r-l+1);
int m=(l+r)>>1;
if(m>=L) update(L,R,c,l,m,root<<1);
if(m<R) update(L,R,c,m+1,r,root<<1|1);
pushUp(root);
}
LL query(int L,int R,int l,int r,int root){
if(l>=L&&r<=R)
return tree[root];
pushDown(root,r-l+1);
int m=(l+r)>>1;
LL re=0;
if(m>=L) re+=query(L,R,l,m,root<<1);
if(m<R) re+=query(L,R,m+1,r,root<<1|1);
return re;
}
int main(){
int n,q;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
build(1,n,1);
cin>>q;
while(q--){
int l,r,d;
scanf("%d%d%d",&l,&r,&d);
update(l,r,d,1,n,1);
cout<<query(l,r,1,n,1)<<endl;
}
return 0;
}