Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4578
----------------------------------------------------------------------------------
A more complex line-of-tree problem
It is strongly recommended that the segment tree be written with fewer students to add the Operation $1,2,3$ one by one.
Direct write three operation thought may be disorderly
The problem is to ask for interval $1$ to $3$ and how to update for 1 operation needs deduction
Assume that the interval length is $len-$-A $ sum1$ $sum 2$ and respectively, and the 3$
After the update is $ Sum1 ' $ $sum 2 ' $ $sum 3 ' $ then
$sum 1 ' = sum1 + c * len$
$sum 2 ' = sum2 + sum1 * C * 2 + c * C * len$
$sum 3 ' = sum3 + sum2 * C * 3 + sum1 * c * c * 3 + c * C * c * len$
The rest is okay, just be patient. Analyze the code volume only $2k+$
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 Const intN =100010, mod =10007;7 intsum[3][n <<2], flag[3][n <<2];8 intN, M;9 voidUpdateintXintLintRintTlintTrintOintc);Ten voidBuildintXintTlintTR) One { Asum[0][X] = sum[1][X] = sum[2][X] =0; -flag[0][X] = flag[2][X] =0; -flag[1][X] =1; the if(TL = =TR) - return; - intMid = (tl + TR) >>1; -Build (x <<1, TL, mid); +Build (x <<1|1, Mid +1, tr); - } + voidPushdown (intXintTlintTR) A { at intMid = (tl + TR) >>1; - for(inti =2; I >=0; --i) - if(Flag[i][x] &&! (i = =1&& Flag[i][x] = =1)) - { -Update (x <<1, TL, Mid, TL, Mid, I, flag[i][x]); -Update (x <<1|1, Mid +1, TR, Mid +1, TR, I, flag[i][x]); inFLAG[I][X] = (i = =1?1:0); - } to } + voidUpdateintXintLintRintTlintTrintOintc) - { the intMid = (tl + TR) >>1; * if(L <= tl && TR <=R) $ {Panax Notoginseng if(O = =0) - { theflag[0][X] = (flag[0][X] + c)%MoD; +sum[2][X] = (sum[2][X] + sum[1][X] * c *3%MoD A+ sum[0][X] * C% mod * c *3% mod + c * C% mod * C% mod * the(Tr-tl +1)% MoD)%MoD; +sum[1][X] = (sum[1][X] + sum[0][X] * c *2% mod + c * C% mod * -(Tr-tl +1)% MoD)%MoD; $sum[0][X] = (sum[0][X] + c * (Tr-tl +1)% MoD)%MoD; $ } - Else if(O = =1) - { theflag[0][X] = flag[0][X] * C%MoD; -flag[1][X] = flag[1][X] * C%MoD;Wuyisum[0][X] = sum[0][X] * C%MoD; thesum[1][X] = sum[1][X] * C% mod * C%MoD; -sum[2][X] = sum[2][X] * C% mod * C% mod * C%MoD; Wu } - Else About { $flag[0][X] =0; -flag[1][X] =1; -flag[2][X] =C; -sum[0][X] = (TR-TL +1) * C%MoD; Asum[1][X] = (TR-TL +1) * C% mod * C%MoD; +sum[2][X] = (TR-TL +1* C% mod * C% mod * C%MoD; the } - return; $ } the pushdown (x, TL, TR); the if(L <=mid) theUpdate (x <<1, L, R, TL, Mid, O, c); the if(Mid <R) -Update (x <<1|1, L, R, Mid +1, TR, o, c); in for(inti =0; I <3; ++i) theSUM[I][X] = (sum[i][x <<1] + sum[i][x <<1|1]) %MoD; the } About intQueryintXintLintRintTlintTrintp) the { the if(L <= tl && TR <=R) the returnSum[p][x]; + pushdown (x, TL, TR); - intMid = (tl + TR) >>1, re=0; the if(L <=mid)BayiRe + = query (x <<1, L, R, TL, Mid, p); the if(Mid <R) theRe + = query (x <<1|1, L, R, Mid +1, TR, p); - returnRe%MoD; - } the intMain () the { the while(SCANF ("%d%d", &n, &m), N) the { -Build1,1, n); the into, x, y, C; the while(m--) the {94scanf"%d%d%d%d", &o, &x, &y, &c); the if(O! =4) theUpdate1, X, Y,1, N, O-1, c); the Else98printf"%d\n", Query (1, X, Y,1, N, C-1)); About } - }101 return 0;102}
HDU 4578 Transformation