Test instructions: There are n operations, the type of operation has a a B to add the value of the interval [A, a] in turn 1 2 ... b-a+1, the operation of B a B is to add the value of the interval [b] in turn b-a+1 ... 2 1, Operation c A B x indicates that the value of the interval [A, a] is replaced by X, and the operation s a B requires all the numbers in the output interval [a, a].
The key: A and B operations are to add a arithmetic progression interval, so to use the segment tree to maintain the arithmetic progression of each interval to add the first and the tolerance, note that the operation C X can also take negative and 0, so you need to add a tag array flag.
#include <cstdio> #include <cstring> #include <algorithm> #define LL long longusing namespace std;Const intN =250005; ll Tree[n <<2], Setv[n <<2], A0[n <<2], D[n <<2];intFlag[n <<2];char op[5];void Pushdown (ll K, LL Left, LL Right) {if(Flag[k]) {LLLen= Right- Left+1; Setv[k *2] = Setv[k *2+1] = Setv[k]; Flag[k *2] = Flag[k *2+1] = Flag[k]; Tree[k *2] = setv[k] * (Len-Len/2); Tree[k *2+1] = setv[k] * (Len/2); D[k *2] = D[k *2+1] = A0[k *2] = A0[k *2+1] =0; FLAG[K] =0; }if(A0[k] | | d[k]) {D[k *2] + = D[k]; D[k *2+1] + = D[k]; A0[k *2] + = A0[k]; llMid= ( Right+ Left) /2; LL A1 = A0[k] + d[k] * (Mid- Left+1); ll nl =Mid- Left+1, nr = Right-Mid; Tree[k *2] + = a0[k] * NL + NL * (NL-1) /2* D[k]; A0[k *2+1] + = A1; Tree[k *2+1] + = A1 * nr + Nr * (NR-1) /2* D[k]; A0[K] = d[k] =0; }}void Pushup (ll k) {Tree[k] = Tree[k *2] + tree[k *2+1];} void Modify (ll K, LL Left, LL Right, LL L, LL R, LL DD) {if(L <= Left&& Right<= R) {LL St;if(DD >=0) st = Left-L +1;ElseSt = R- Left+1; A0[k] + = st, D[k] + = DD; ll n = Right- Left+1; Tree[k] + = St * n + N * (N-1) /2* DD; Return } pushdown (k, Left, Right);int Mid= ( Left+ Right) /2;if(L <=Mid) Modify (k *2, Left,Mid, L, R, DD);if(R >Mid) Modify (k *2+1,Mid+1, Right, L, R, DD); Pushup (k);} void Modify2 (ll K, LL Left, LL Right, LL L, LL R, ll X) {if(L <= Left&& Right<= R) {Flag[k] =1; SETV[K] = x; TREE[K] = x * ( Right- Left+1); A0[K] = d[k] =0; Return } pushdown (k, Left, Right); llMid= ( Left+ Right) /2;if(L <=Mid) Modify2 (k *2, Left,Mid, L, R, X);if(R >Mid) Modify2 (k *2+1,Mid+1, Right, L, R, X); Pushup (k);} ll query (ll K, LL Left, LL Right, LL L, ll R) {if(L <= Left&& Right<= R) return tree[k]; Pushdown (k, Left, Right); llMid= ( Left+ Right) /2, res =0;if(L <=Mid) Res + = Query (k *2, Left,Mid, l, R);if(R >Mid) Res + = Query (k *2+1,Mid+1, Right, l, R); Pushup (k); return res;}intMain () {intT ll X, L, R; scanf"%d", &t); memset (Tree,0, sizeof (tree)); Memset (A0,0, sizeof (A0)); memset (D,0, sizeof (d)); memset (Flag,0, sizeof (flag)); while(t--) {scanf ("%s%lld%lld", OP, &l, &r);if(op[0] ==' A ')Modify1,1N1, L, R,1);Else if(op[0] ==' B ')Modify1,1N1, L, R,-1);Else if(op[0] ==' C ') {scanf"%lld", &x); Modify2 (1,1N1, L, R, X); }Elseprintf"%lld\n", Query (1,1N1, L, R)); } return0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 12436 (interval update)