Rip Van Winkle is fed up with everything except programming. One day he found a problem whichrequired to perform three types of update operations (A, B, C), and one query operation S Over an arraydata[]. Initially all elements of data is equal to 0. Though Rip Van Winkle is going-to-sleep for 20years, and he code is also super slow, you need to perform the same update Operations and output theresult for the query operation S on an efficient.
Long Long data[250001];
void A (int st, int nd) {
for (int i = st; i \le nd; i++) data[i] = Data[i] + (i-st + 1);
}
void B (int st, int nd) {
for (int i = st; i \le nd; i++) data[i] = Data[i] + (nd-i + 1);
}
void C (int st, int nd, int x) {
for (int i = st; i \le nd; i++) data[i] = x;
}
Long Long S (int st, int nd) {
Long long res = 0;
for (int i = st; i \le nd; i++) res + = Data[i];
return res;
}
Input
The first line of input would contain T (≤4) denoting the number of operations. Each of the NEXTT lines starts with a character (' A ', ' B ', ' C ' or ' S '), which indicates the type of operation. The Character ' A ', ' B ' or ' S ' would be followed by the integers, st and nd with the same line. Character ' C ' is followed by threeintegers, St, ND and X. It ' s assumed that, 1≤st≤nd≤250000 and 105≤x≤105. The meaningsof these integers is explained by the code of Rip Van Winkle.
Output
Starting with the character ' s ', print S (St, ND) as defined in the code.
Sample Input
7
A 1 4
B 2 3
S 1 3
C 3 4-2
S 2 4
B 1 3
S 2 4
Sample Output
9
0
3
This question is the interval update, this question is more troublesome, has done for a long time. First maintain the L,R,ADD1 with the line segment tree (the value of the left end of the segment), ADD2 (the value of the right end of the segment), step (the tolerance of the interval, minus the left side of the right), sum (interval sum), flag (determine whether the interval number is the same), value (the value of the interval number is the same
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < math.h> #include <vector> #include <map> #include <set> #include <queue> #include <stack > #include <string> #include <algorithm>using namespace std; #define MAXN 250060#define ll long longll sum; struct NODE{LL l,r,add1,add2,step,sum,flag,v;//full change to Long long}b[8*maxn];void build (ll l,ll r,ll i) {ll mid;b[i].l=l;b[i]. R=r;b[i].add1=b[i].add2=b[i].step=0;b[i].flag=0;b[i].sum=b[i].v=0;if (l==r) return;mid= (l+r)/2;build (l,mid,i*2); Build (mid+1,r,i*2+1);} void Pushdown (LL i) {ll mid,add1,add2,add3,add4;mid= (B[I].L+B[I].R)/2;if (B[I].L==B[I].R) return;if (b[i].flag==0) {b[ I*2].FLAG=B[I*2+1].FLAG=0;B[I*2].V=B[I*2+1].V=B[I].V; B[i*2].add1=b[i*2].add2=b[i*2+1].add1=b[i*2+1].add2=0;b[i*2].step=b[i*2+1].step=0;b[i*2].sum= (B[i*2].r-b[i*2]. l+1) *b[i*2].v;b[i*2+1].sum= (b[i*2+1].r-b[i*2+1].l+1) *b[i*2+1].v;b[i].flag=-1;} if (b[i].add1 | | b[i].add2 | | b[i].step) {ADD1=B[I].add1; add2=b[i].add1+b[i].step* (MID-B[I].L); add3=add2+b[i].step;add4=b[i].add2; b[i*2].add1+=add1;b[i*2].add2+=add2;b[i*2+1].add1+=add3;b[i*2+1].add2+=add4;b[i*2].sum+= (b[i*2].r-b[i*2].l+1) * (ADD1+ADD2)/2;b[i*2+1].sum+= (b[i*2+1].r-b[i*2+1].l+1) * (ADD3+ADD4)/2;b[i*2].step+=b[i].step;b[i*2+1].step+=b[i] . step;b[i].add1=b[i].add2=b[i].step=0;}} void UpdateA (ll l,ll r,ll add,ll i) {ll mid,add1,add2;if (b[i].l==l && b[i].r==r) {add1=add;add2=add+b[i].r-b[i]. l;b[i].add1+=add1;b[i].add2+=add2;b[i].step+=1;b[i].sum+= (b[i].r-b[i].l+1) * (ADD2+ADD1)/2;//b[i].flag=-1; Note here that the b[i].flag=-1 cannot be made, because for all numbers the same interval, its add1,add2 can not be 0 return;} Pushdown (i); mid= (B[I].L+B[I].R)/2;if (r<=mid) UpdateA (l,r,add,i*2); else if (L>mid) UpdateA (l,r,add,i*2+1); Else{updatea (l,mid,add,i*2); UpdateA (mid+1,r,add+mid+1-l,i*2+1);} B[i].sum=b[i*2].sum+b[i*2+1].sum;} void Updateb (ll l,ll r,ll add,ll i) {ll mid,add1,add2;if (b[i].l==l && b[i].r==r) {add1=add+b[i].r-b[i].l;add2= Add;b[i].add1+=add1;b[i].add2+=add2;b[i].step-=1;b[i].sum+= (b[i].r-b[i].l+1) * (ADD2+ADD1)/2;return; Pushdown (i); mid= (B[I].L+B[I].R)/2;if (r<=mid) updateb (l,r,add,i*2); else if (L>mid) updateb (l,r,add,i*2+1); Else{updateb (l,mid,add+r-mid,i*2); Updateb (mid+1,r,add,i*2+1);} B[i].sum=b[i*2].sum+b[i*2+1].sum;} void Updatec (ll l,ll r,ll num,ll i) {ll mid;if (b[i].l==l && b[i].r==r) {b[i].add1=b[i].add2=b[i].step=0;b[i].sum = (b[i].r-b[i].l+1) *num;b[i].flag=0;b[i].v=num;return;} Pushdown (i); mid= (B[I].L+B[I].R)/2;if (r<=mid) Updatec (l,r,num,i*2); else if (L>mid) Updatec (l,r,num,i*2+1); Else{updatec (l,mid,num,i*2); Updatec (mid+1,r,num,i*2+1);} B[i].sum=b[i*2].sum+b[i*2+1].sum;} void question (ll l,ll r,ll i) {ll mid;if (b[i].l==l && b[i].r==r) {Sum+=b[i].sum;return;} Pushdown (i); mid= (B[I].L+B[I].R)/2;if (r<=mid) question (l,r,i*2); else if (l>mid) question (l,r,i*2+1); else{ Question (l,mid,i*2); question (mid+1,r,i*2+1);}} int main () {ll n,m,i,j;while (scanf ("%lld", &n)!=eof) {build (1,250000,1); for (i=1;i<=n;i++) {char s[10];ll c,d,e; scanf ("%s", s), if (s[0]== ' A ') {scanf ("%lld%lld ", &c,&d); UpdateA (c,d,1,1);} else if (s[0]== ' B ') {scanf ("%lld%lld", &c,&d); Updateb (c,d,1,1);} else if (s[0]== ' C ') {scanf ("%lld%lld%lld", &c,&d,&e); Updatec (c,d,e,1);} else if (s[0]== ' s ') {scanf ("%lld%lld", &c,&d), Sum=0;question (c,d,1);p rintf ("%lld\n", Sum);}}} return 0;} /*10B 1 4 A 1 4S 2 2B 1 3C 1 4 1 B 1 4C 3 3 3 a 2 3S 2 3B 3 3*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Uva 12436 Rip Van Winkle ' s Code