Just a hook
Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 11844 accepted submission (s): 5882
Problem descriptionin the game of Dota, Pudge's meat hook is actually the most horrible thing for most of the heroes. the hook is made up of several consecutive metallic sticks which are of the same length.
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to n. for each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after creating the operations.
You may consider the original hook is made up of cupreous sticks.
Inputthe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1 <=n <= 100,000, which is the number of the sticks of Pudge's meat hook and the second line contains an integer Q, 0 <= q <= 100,000, which is the number of the operations.
Next Q lines, each line contains three integers x, y, 1 <= x <= Y <= N, Z, 1 <= z <= 3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where z = 1 represents the cupreous kind, Z = 2 represents the silver kind and Z = 3 represents
The golden kind.
Outputfor each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
11021 5 25 9 3
Sample output
Case 1: the total value of the hook is 24. this topic uses the latency marker of the Line Segment tree. In fact, it is simple to say that during this query, I will not change the value first, and wait for the next query, by the way, I just need to update these latencies. In this way, we can fully use the updat () function. The latency mark is the essence of the Line Segment tree and must be understood!# Include <iostream> # include <stdio. h> using namespace STD; # define n 500000int L [n <2], flag [n <2]; // L is the total flag Flag status void build (INT num, int S, int e) {flag [num] = 0; L [num] = 1; if (S = e) return; int mid = (S + E)> 1; build (Num <1, S, mid ); build (Num <1 | 1, Mid + 1, e); L [num] = L [num <1] + L [num <1 | 1];} void Update (INT num, int S, int e, int A, int B, int c) {if (a <= S & B> = E) {flag [num] = C; L [num] = C * (E-S + 1); return;} If (flag [num]) // if it is 0, unchanged. Update {flag [num <1] = Flag [num <1 | 1] = Flag [num] next time. L [num <1] = (E-S + 1-(E-S + 1)/2) * flag [num]; L [num <1 | 1] = (e-S + 1)/2) * flag [num]; flag [num] = 0; // mark the status as not updated after the query.} int mid = (S + E)/2; If (mid> = A) Update (Num <1, S, mid, A, B, C); If (mid <B) Update (Num <1 | 1, Mid + 1, E, A, B, C ); L [num] = L [num <1] + L [num <1 | 1];} int main () {int T, TT, M, N,, b, C; scanf ("% d", & T); For (TT = 1; TT <= T; TT ++) {scanf ("% d ", & N); Build (1,1, n); scanf ("% d", & M); While (M --) {scanf ("% d ", & A, & B, & C); Update (1,1, n, a, B, c);} printf ("case % d: the total value of the hook is % d. \ n ", TT, L [1]);} return 0 ;}