Just a Hook HDU

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.

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

1
10
2
1 5 2
5 9 3

Sample Output

Case 1: The total value of the hook is 24.

最简单的区间更新。
 1 #pragma warning(disable:4996)
 2 #include<cstdio>
 3 #include<string>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 
 9 #define lson root<<1
10 #define rson root<<1|1
11 #define ll long long 
12 
13 const int maxn = 100005;
14 
15 int n, q, T;
16 
17 struct node { int l, r, sum, add; } Tree[4 * maxn];
18 
19 void Pushup(int root) {
20     Tree[root].sum = Tree[lson].sum + Tree[rson].sum;
21 }
22 
23 void Pushdown(int l,int r,int root) {
24     int mid = (l + r) >> 1;
25     Tree[lson].add = Tree[root].add;
26     Tree[rson].add = Tree[root].add;
27     Tree[lson].sum = Tree[root].add * (mid - l + 1);
28     Tree[rson].sum = Tree[root].add * (r - mid);
29     Tree[root].add = 0;
30 }
31 
32 void Build(int l, int r, int root) {
33     Tree[root].l = l;
34     Tree[root].r = r;
35     Tree[root].add = 0;
36     if (l == r) {
37         Tree[root].sum = 1;
38         return;
39     }
40     int mid = (l + r) >> 1;
41     Build(l, mid, lson);
42     Build(mid + 1, r, rson);
43     Pushup(root);
44 }
45 
46 void Update(int L, int R, int l, int r, int root, int x) {
47     if (l > R || r < L) return;
48     if (L <= l && r <= R) {
49         Tree[root].add = x;
50         Tree[root].sum = x * (r - l + 1);
51         return;
52     }
53     if (Tree[root].add) Pushdown(l, r, root);
54     int mid = (l + r) >> 1;
55     Update(L, R, l, mid, lson, x);
56     Update(L, R, mid + 1, r, rson, x);
57     Pushup(root);
58 }
59 
60 
61 
62 int main()
63 {
64     scanf("%d", &T);
65     for (int t = 1; t <= T; t++) {
66         scanf("%d", &n);
67         Build(1, n, 1);
68         scanf("%d", &q);
69         for (int i = 1; i <= q; i++) {
70             int x, y, d;
71             scanf("%d%d%d", &x, &y, &d);
72             Update(x, y, 1, n, 1, d);
73         }
74         printf("Case %d: ", t);
75         printf("The total value of the hook is %d.
", Tree[1].sum);
76     }
77     return 0;
78 }
原文地址:https://www.cnblogs.com/zgglj-com/p/8847261.html