Question: Enter n, m; n is given to the position. At the beginning, the color of the I position is I, and the colorfulness is 0.
There are m operations, one is to update the color of the segmented area to X, for the updated area, each location (so that the color before position I is not updated is color [I]) colorfulness of | color [I]-x |;
Another operation is to query a range [L, R] and output the total colorfulness of the range.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 using namespace STD; 7 # define lson L, M, RT <1 8 # define rson m + 1, R, RT <1 | 1 9 typedef long ll; 10 const int n = 111111; 11 int n, m, color [n <2]; 12 ll Delta [n <2], sum [n <2]; 13 14 void build (int l, int R, int RT) 15 {16 color [RT] = delta [RT] = sum [RT] = 0; 17 if (L = r) {18 color [RT] = L; // The I position color is I 19 return; 20} 21 int M = (L + r)> 1; 22 build (lson); 23 build (rson); 24} 25 26 void up (int l, int R, int RT) 27 {28 sum [RT] = sum [RT <1] + sum [RT <1 | 1] + 1ll * Delta [RT] * (R-l + 1); 29} 30 31 void down (int rt) 32 {33 If (color [RT]> 0) 34 {35 color [RT <1] = color [RT <1 | 1] = color [RT]; 36 color [RT] = 0; 37} 38} 39 40 void check (int x, int L, int R, int RT) 41 {42 if (color [RT]> 0) 43 {44 Delta [RT] + = ABS (X-color [RT]); 45 sum [RT] + = 1ll * ABS (X-color [RT]) * (R-l + 1); 46} 47 else 48 {49 int M = (L + r)> 1; 50 check (x, lson); check (X, rson); 51 up (L, R, RT); 52} 53 color [RT] = x; 54} 55 56 void Update (int l, int R, int X, int L, int R, int RT) 57 {58 If (L <= L & R> = r) {59 check (x, L, R, RT ); 60 return; 61} 62 down (RT); 63 int M = (L + r)> 1; 64 if (L <= m) Update (L, R, X, lson); 65 if (r> m) Update (L, R, X, rson); 66 up (L, R, RT ); 67} 68 69 ll query (int l, int R, int L, int R, int RT) 70 {71 If (L <= L & R> = r) return sum [RT]; 72 int M = (L + r)> 1; 73 ll ans = 0; 74 if (r <= m) 75 ans + = query (L, R, lson) + 1ll * Delta [RT] * (R-L + 1); 76 else if (L> m) 77 ans + = query (L, R, rson) + 1ll * Delta [RT] * (R-L + 1); 78 else 79 ans + = query (L, M, lson) + query (m + 1, R, rson) + 1ll * Delta [RT] * (R-L + 1); 80 return ans; 81} 82 83 int main () 84 {85 // freopen ("in.txt", "r", stdin); 86 while (scanf ("% d", & N, & M)> 0) 87 {88 build (1, n, 1); 89 int type, L, R, X; 90 while (M --) 91 {92 scanf ("% d", & type, & L, & R); 93 If (type = 1) 94 {95 scanf ("% d", & X); 96 Update (L, R, X, 1, n, 1 ); 97} 98 else 99 printf ("% i64d \ n", query (L, R, 1, n, 1); 100} 101} 102 return 0; 103}