1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std;
inline int Read(){ register int r=0,b=1,c=getchar(); while(c<'0' || '9'<c) b=c=='-'?-1:b,c=getchar(); while('0'<=c && c<='9') r=(r<<1)+(r<<3)+(c^'0'),c=getchar(); return r*b; }
typedef long long ll; const int N=1e5+10; #define lowbit(x) ((x)&(-(x)))
int n,m; ll tary[N];
void Modify(int pos,ll key){ while(pos<=n){ tary[pos]+=key; pos+=lowbit(pos); } } ll Query(int pos){ ll ret=0; while(pos){ ret+=tary[pos]; pos-=lowbit(pos); } return ret; } ll Calc(ll a,ll b){return ceil((a+b)/2.0);} int main(){ n=Read(); ll psi=0,neg=0; for(int i=1,las=0,now=0;i<=n;i++,las=now){ now=Read(); Modify(i,now),Modify(i+1,-now); if(i>1) psi+=max(0,now-las),neg+=min(0,now-las); } printf("%lld\n",Calc(Query(1),psi)); m=Read(); while(m--){ int le=Read(),ri=Read(),key=Read(); ll tmp; if(le>1) tmp=Query(le)-Query(le-1),psi-=max(tmp,0ll),neg-=min(tmp,0ll); if(ri<n) tmp=Query(ri+1)-Query(ri),psi-=max(tmp,0ll),neg-=min(tmp,0ll); Modify(le,key),Modify(ri+1,-key); if(le>1) tmp=Query(le)-Query(le-1),psi+=max(tmp,0ll),neg+=min(tmp,0ll); if(ri<n) tmp=Query(ri+1)-Query(ri),psi+=max(tmp,0ll),neg+=min(tmp,0ll); printf("%lld\n",Calc(Query(1),max(psi,Query(n)-Query(1)-neg))); } return 0; }
|