Just a Hook(线段树,成段更新)

Just a Hook

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 4
Problem Description
In 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 performing the operations.
You may consider the original hook is made up of cupreous sticks.
 
Input
The 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.
 
Output
For 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
1
10
2
1 5 2
5 9 3
 
Sample Output
Case 1: The total value of the hook is 24.
 
Source
2008 “Sunline Cup” National Invitational Contest
 
 
AC Code:
 1 #include <iostream>
 2 #include <cstdio>
 3   
 4 using namespace std;
 5   
 6 const int MAXN = 100000;
 7 struct Node
 8 {
 9     int left, right;
10     int tolVal;  //区间[left, right]的total value
11     int val;  //当父节点的val非0时往下更新,此时val是区间上的每一段的value
12 }tree[MAXN << 2];
13   
14 void Build(int rt, int left, int right)
15 {
16     tree[rt].left = left;
17     tree[rt].right = right;
18     tree[rt].val = 0;
19     if(left == right)
20     {
21         tree[rt].tolVal = 1;
22         return ;
23     }
24     int mid = (left + right) >> 1;
25     Build(rt << 1, left, mid);
26     //当区间长度为奇数时,mid为区间的中点,归到左子树
27     Build(rt << 1 | 1, mid + 1, right);
28     tree[rt].tolVal = tree[rt << 1 | 1].tolVal + tree[rt << 1].tolVal;
29 }
30   
31 void Update(int rt, int from, int to, int newVal)
32 {
33     if(from <= tree[rt].left && tree[rt].right <= to)
34     {
35         tree[rt].val = newVal;
36         tree[rt].tolVal = (tree[rt].right - tree[rt].left + 1) * newVal;
37         return ;
38     }
39     int mid = (tree[rt].left + tree[rt].right) >> 1;
40     //Pushdown:往下传递tolVal的变化
41     if(tree[rt].val)
42     {
43         tree[rt << 1].val = tree[rt << 1 | 1].val = tree[rt].val;
44         //注意,此处tree[rt].val != newVal,而是上一次没有往下传的newVal,此时补回更新。
45         tree[rt << 1].tolVal = (tree[rt << 1].right - tree[rt << 1].left + 1) * tree[rt].val;
46         tree[rt << 1 | 1].tolVal = (tree[rt << 1 | 1].right - tree[rt << 1 | 1].left + 1) * tree[rt].val;
47         tree[rt].val= 0;
48     }
49     if(from <= mid)  //建树(Build函数)时,mid归到左子树,故这里==mid时也归到左树
50         Update(rt << 1, from, to, newVal);
51     if(to > mid)
52         Update(rt << 1 | 1, from, to, newVal);
53     tree[rt].tolVal = tree[rt << 1].tolVal + tree[rt << 1 | 1].tolVal;
54     //回溯时再更新当前结点的tolVal
55 }
56   
57 int main()
58 {
59     int T, N, Q, X, Y, Z, ca = 1;
60     scanf("%d", &T);
61     while(T--)
62     {
63         scanf("%d %d", &N, &Q);
64         Build(1, 1, N);
65         while(Q--)
66         {
67             scanf("%d %d %d", &X, &Y, &Z);
68             Update(1, X, Y, Z);
69         }
70         printf("Case %d: The total value of the hook is %d.\n", ca++, tree[1].tolVal);
71     }
72     return 0;
73 }
原文地址:https://www.cnblogs.com/cszlg/p/2932403.html