Just a Hook
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
题目类型:线段树
算法分析:这是一道线段树区间更新和区间求和的问题,需要使用lazy标记,当需要修改被lazy标记的节点的子节点时,要将lazy标记向其子节点传并修改子节点的sum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 10000007; const int maxn = 100000; int lazy[maxn<<2]; int sum[maxn<<2]; void PushUp (int rt) { sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void PushDown (int rt, int l, int m, int r) { if (lazy[rt]) { lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt]; sum[rt<<1] = lazy[rt] * (m - l + 1); sum[rt<<1|1] = lazy[rt] * (r - m); lazy[rt] = 0; } } void Build (int rt, int l, int r) { sum[rt] = 1; lazy[rt] = 0; if (l == r) { return ; } int m = (l + r) >> 1; PushDown (rt, l, m, r); Build (lson); Build (rson); PushUp (rt); } void UpDate (int rt, int l, int r, int L, int R, int add) { if (L <= l && r <= R) { lazy[rt] = add; sum[rt] = add * (r - l + 1); return ; } int m = (l + r) >> 1; PushDown (rt, l, m, r); if (L <= m) UpDate (lson, L, R, add); if (R > m) UpDate (rson, L, R, add); PushUp (rt); } int main() { // freopen ("aaa.txt", "r", stdin); int t, flag = 1; scanf ("%d", &t); while (t--) { int n; scanf ("%d", &n); Build (1, 1, n); int q; scanf ("%d", &q); int i, val_a, val_b, val; for (i = 0; i < q; i++) { scanf ("%d%d%d", &val_a, &val_b, &val); UpDate (1, 1, n, val_a, val_b, val); } printf ("Case %d: The total value of the hook is %d.\n", flag++, sum[1]); } return 0; } |
- « 上一篇:hdu1671
- hdu1712:下一篇 »