Address: poj 3468
When I came back from playing basketball, I felt a little impulsive .. The brainless game made eight wa requests .. The R-l is written as l-R during the update...
This is the bare question of Interval Update. Interval Update adds a lazy tag and a latency tag. The Lazy tag is updated downward only when it is queried downward. All others are based on the Line Segment tree.
The Code is as follows:
#include <iostream>#include <cstdio>#include <cstring>#include <math.h>#include <stack>using namespace std;#define LL long long#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1const int MAXN=1e5+10;LL sum[MAXN<<2];LL lazy[MAXN<<2];void PushUp(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void PushDown(int rt, int m){ if(lazy[rt]) { lazy[rt<<1]+=lazy[rt]; lazy[rt<<1|1]+=lazy[rt]; sum[rt<<1]+=lazy[rt]*(m-(m>>1)); sum[rt<<1|1]+=lazy[rt]*(m>>1); lazy[rt]=0; }}void build(int l, int r, int rt){ lazy[rt]=0; if(l==r) { scanf("%lld",&sum[rt]); return ; } int mid=(l+r)>>1; build(lson); build(rson); PushUp(rt);}void update(int ll, int rr, int x, int l, int r, int rt){ if(ll<=l&&rr>=r) { lazy[rt]+=x; sum[rt]+=(LL)x*(r-l+1); return ; } PushDown(rt, r-l+1); int mid=(l+r)>>1; if(ll<=mid) update(ll,rr,x,lson); if(rr>mid) update(ll,rr,x,rson); PushUp(rt);}LL query(int ll, int rr, int l, int r, int rt){ if(ll<=l&&rr>=r) { return sum[rt]; } PushDown(rt,r-l+1); LL ans=0; int mid=(l+r)>>1; if(ll<=mid) ans+=query(ll,rr,lson); if(rr>mid) ans+=query(ll,rr,rson); return ans;}int main(){ int n, q, i, a, b, c; char ch[3]; scanf("%d%d",&n,&q); build(1,n,1); while(q--) { getchar(); scanf("%s",&ch); if(ch[0]=='Q') { scanf("%d%d",&a,&b); LL ans=query(a,b,1,n,1); printf("%lld\n",ans); } else { scanf("%d%d%d",&a,&b,&c); update(a,b,c,1,n,1); } } return 0;}