Problem a HDU 1698 just a hook

Source: Internet
Author: User
Tags dota
Just a hook

Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)

Total submission (s): 11857 accepted submission (s): 5890


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.


Source2008 "sunline Cup" national invitational contest


Recommendwangyeps: Thanks to Bobo's solution report, the line segment tree is a masterpiece of Bobo.

Question. Friends who have played Dota games should know such a hero. This topic discusses his weapons. Its weapon is a long chain. It consists of materials of different values. Now, Q operations are performed on its chain. Change the chain value between [x, y] to Z each time. Is the total value of the chain after Q operations

# Include <stdio. h> struct node // defines the line segment tree interval structure {int L, R, V, sum, en; // L, R represents the Left and Right endpoints respectively. The value of the V chain. sum is the total value of the [L, R] chain. // Whether the value of the [L, R] chain is the same} SBT [400010]; // 4 times the value is appropriate. Void btree (int l, int R, int K) // use [L, R] to create a line segment tree. K is the point of the {int mid, ls, RS; // mid record line segment. Ls, RS son SBT [K]. L = L; SBT [K]. R = r; SBT [K]. V = 1; // value is initialized to 1, SBT [K]. en = 1; // initialize to the same if (L = r) // if it reaches the leaf node. Initial sum {SBT [K]. sum = 1; return;} ls = k <1; // calculate the left and right son number rs = k <1 | 1; Mid = (L + r)> 1; btree (L, mid, ls); // recursively construct a line segment tree (Mid + 1, R, RS); SBT [K]. sum = SBT [ls]. sum + SBT [RS]. SUM; // The total value of the recursive calculation interval [L, R]} void Update (int l, int R, int K, int d) // Convert [L, r] value is set to d {int mid, ls, RS; // same as if (SBT [K]. L = L & SBT [K]. R = r) // If the interval matches. Mark the returned result directly. {SBT [K]. en = 1; SBT [K]. V = D; // update value SBT [K]. sum = (SBT [K]. r-SBT [K]. L + 1) * D; // calculate the total value return;} ls = k <1; RS = k <1 | 1; Mid = (SBT [K]. L + SBT [K]. r)> 1; if (SBT [K]. en) // if the value of the interval before modification is the same {SBT [K]. en = 0; // unmark SBT [ls]. en = SBT [RS]. en = 1; // downstream SBT [ls]. V = SBT [RS]. V = SBT [K]. v; // update value SBT [K]. V = 0; SBT [ls]. sum = (SBT [ls]. r-SBT [ls]. L + 1) * SBT [ls]. v; // update the total value of left and right sons SBT [RS]. sum = (SBT [RS]. r-SBT [RS]. L + 1) * SBT [RS]. v;} If (r <= mid) // recursive Update (L, R, ls, d); else if (L> mid) Update (L, R, RS, d); else {Update (L, mid, ls, d); Update (Mid + 1, R, RS, d);} SBT [K]. sum = SBT [ls]. sum + SBT [RS]. SUM; // recursive calculation of the total value of the updated interval [L, R]} int Qu (int l, int R, int K) // query [L, total value between R] {int mid, ls, RS; If (SBT [K]. en | (SBT [K]. L = L & SBT [K]. R = r) // return SBT [k] If the queried range exactly matches or has the same value. SUM; // directly return the total value ls = k <1; RS = k <1 | 1; Mid = (L + r)> 1; if (L> mid) // recursively query return Qu (L, R, RS); else if (r <= mid) return Qu (L, R, ls); else return Qu (L, mid, ls) + Qu (Mid + 1, R, RS);} int main () {int t, x, y, z, N, Q, I, J; // For example, scanf ("% d", & T); for (I = 1; I <= T; I ++) {scanf ("% d ", & N); btree (1, n, 1); scanf ("% d", & Q); For (j = 1; j <= Q; j ++) {scanf ("% d", & X, & Y, & Z); Update (X, Y, 1, Z );} printf ("case % d: the total value of the hook is % d. \ n ", I, Qu (1, n, 1);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.