Dzy Loves colorstime limit:2000msmemory limit:262144kbthis problem would be judged onCodeforces. Original id:445e
64-bit integer IO format: %i64d Java class name: (any) Dzy loves colors, and he enjoys painting.
On a colorful day, Dzy gets a colorful ribbon, which consists Of n units (the Y is numbered From 1 to N from left to right). The color of The i -th unit of the Ribbon Is I at first. It is colorful enough, but we still consider that the colorfulness of each unit is 0 at F Irst.
Dzy loves painting, we know. He takes up a paintbrush with color x and uses it-draw a line on the Ribbon. In such a case some contiguous units is painted. Imagine the color of unit I currently is y. When it was painted by this paintbrush, the color of the unit becomes x, and the colorfulness of the unit incr Eases by | X - y|.
Dzy wants to perform m operations, each operation can be one of the following:
- Paint all the units with numbers between l and R (both inclusive) with color x.
- Ask the sum of colorfulness of the units between l and R (both inclusive).
Can you help Dzy?
Input
The first line contains the space-separated integers n, m (1≤ n, m ≤10< c18>5).
Each of the next m lines begins with a integer type (1≤ type ≤ 2), which repre Sents the type of this operation.
If Type = 1, there'll is 3 more integers l, R, x (1≤ l ≤ R ≤ n; 1 ≤ x ≤108) in the describing an operation 1.
If Type = 2, there'll is 2 more integers l, R (1≤ l ≤ R ≤ n) in the describing an operation 2.
Output
For each operation 2, print a line containing the answer-sum of colorfulness.
Sample Input
Input
3 3
1 1 2 4
1 2 3 5
2 1 3
Output
8
Input
3 4
1 1 3 4
2 1 1
2 2 2
2 3 3
Output
3
2
1
Input
10 6
1 1 5 3
1 2 7 9
1 10 10 11
1 3 8 12
1 1 10 3
2 1 10
Output
129
Hint
In the first sample, the color of each unit was initially [1, 2, 3], and the colorfulness is [0, 0, 0].
After the first operation, colors become [4, 4, 3], colorfulness become [3, 2, 0].
After the second operation, colors become [4, 5, 5], colorfulness become [3, 3, 2].
The answer to the only operation of type 2 is 8.
Sourcecodeforces Round #254 (Div. 2) Problem solving: line tree
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 Const intMAXN =100010;5 structnode{6 intLt,rt,color;7 LL Sum,len,add;8}tree[maxn<<2];9 voidPushup (intv) {TenTree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum; One if(tree[v<<1].color = = tree[v<<1|1].color) ATree[v].color = tree[v<<1].color; - ElseTree[v].color =0; - } the voidPushdown (intv) { - if(tree[v].add) { -tree[v<<1].add + =Tree[v].add; -tree[v<<1|1].add + =Tree[v].add; +tree[v<<1].sum + = tree[v].add*tree[v<<1].len; -tree[v<<1|1].sum + = tree[v].add*tree[v<<1|1].len; +Tree[v].add =0; A } at if(tree[v].color) { -tree[v<<1].color = tree[v<<1|1].color =Tree[v].color; -Tree[v].color =0; - } - } - voidBuildintLtintRtintv) { intree[v].lt =lt; -Tree[v].rt =RT; toTree[v].len = Rt-lt +1; +Tree[v].add =0; -Tree[v].sum =0; the if(LT = =RT) { *Tree[v].color =lt; $ return;Panax Notoginseng } - intMid = (lt + rt) >>1; theBuild (lt,mid,v<<1); +Build (mid+1,rt,v<<1|1); A Pushup (v); the } + voidUpdateintLtintRtintColorintv) { - if(LT <= tree[v].lt && rt >= Tree[v].rt &&Tree[v].color) { $Tree[v].add + = ABS (Tree[v].color-color); $Tree[v].sum + = ABS (tree[v].color-color) *Tree[v].len; -Tree[v].color =color; - return; the } - Pushdown (v);Wuyi if(LT <= tree[v<<1].RT) Update (lt,rt,color,v<<1); the if(Rt >= tree[v<<1|1].lt) Update (lt,rt,color,v<<1|1); - Pushup (v); Wu } -LL Query (intLtintRtintv) { About if(LT <= tree[v].lt && RT >= Tree[v].rt)returntree[v].sum; $ Pushdown (v); -LL sum =0; - if(LT <= tree[v<<1].RT) sum + = query (lt,rt,v<<1); - if(Rt >= tree[v<<1|1].lt) sum + = query (lt,rt,v<<1|1); A Pushup (v); + returnsum; the } - intMain () { $ intN,m,op,x,y,color; thescanf"%d%d",&n,&m); theBuild1N1); the while(m--){ thescanf"%d%d%d",&op,&x,&y); - if(OP = =1){ inscanf"%d",&color); theUpdate (X,y,color,1); the}Elseprintf"%i64d\n", query (x, Y,1)); About } the return 0; the}
View Code
Codeforces 445E Dzy Loves Colors