Hdu1698 just a hook line segment tree: Segment replacement, total interval summation

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/u012860063

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1698

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.

Question: There are T groups of test data. n is the hook length (1 <= n <= 100000), and m is the number of operations. Initially, the value of each hook is 1. The operation consists of three numbers: X, Y, and Z, y] The Hook turns into Z (1 stands for copper, 2 silver, 3 gold ).

The Code is as follows:

// Line segment tree function: Update: Segment replacement (because only the total interval is queried once, the information of Node 1 can be directly output) # include <cstdio> # include <algorithm> using namespace STD; # define lson L, M, RT <1 # define rson m + 1, R, RT <1 | 1 // The difference between lson and rson indicates that the Left and Right sons of the node represent the root of the current subtree ), that is, the current node const int maxn = 111111; // maxn is the maximum range given by the question, and the number of nodes is four times, specifically, the number of nodes should be twice the minimum 2x greater than maxn int Col [maxn <2]; int sum [maxn <2]; void pushup (int rt) // update the information of the current node to the parent node {sum [RT] = sum [RT <1] + sum [RT <1 | 1];} void Pushdown (int rt, int m) // update the information of the current node to the son node {If (COL [RT]) {Col [RT <1] = Col [RT]; Col [RT <1 | 1] = Col [RT]; sum [RT <1] = Col [RT] * (m-(M> 1 )); sum [RT <1 | 1] = Col [RT] * (M> 1); Col [RT] = 0 ;}} void build (int l, int R, int RT) {Col [RT] = 0; sum [RT] = 1; // initialize each node as 1if (L = r) {// scanf ("% LLD", & sum [RT]); return;} int M = (L + r)> 1; build (lson ); build (rson); pushup (RT);} void Update (int l, int R, int C, int L, int R, int RT) {If (L <= L & R <= r) {Col [RT] = C; sum [RT] = C * (R-l + 1); return ;} pushdown (RT, R-l + 1); int M = (L + r)> 1; if (L <= m) Update (L, R, C, lson); If (M <r) Update (L, R, C, rson); pushup (RT);}/* int query (int l, int R, int l, int R, int RT) {If (L <= L & R <= r) {return sum [RT];} Pushdown (RT, R-l + 1 ); int M = (L + r)> 1; int ret = 0; If (L <= m) RET + = query (L, R, lson ); if (M <r) RET + = query (L, R, rson); return ret;} */INT main () {int N, Q, T, K = 0; int A, B, C; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & Q ); // n is the number of nodes build (1, n, 1); // build while (Q --) // Q is the number of inquiries {/* char op [2]; int A, B, C; scanf ("% s", OP); If (OP [0] = 'q') {scanf ("% d ", & A, & B); printf ("% LLD \ n", query (A, B, 1, n, 1 ));} else {scanf ("% d", & A, & B, & C); // C is the value update (A, B, c, 1, n, 1);} */scanf ("% d", & A, & B, & C ); // C is the update (A, B, C, 1, n, 1);} printf ("case % d: the total value of the hook is % d. \ n ", ++ K, sum [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.