hdu 1698 Just a Hook
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.
![](http://acm.hdu.edu.cn/data/images/C116-1010-1.JPG)
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.
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.
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.
线段树简单题,权当练习一下英文。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #define lc root<<1 7 #define rc root<<1|1 8 #define mid ((l+r)>>1) 9 const int Max_N = 100010; 10 struct Node { int v, add; }; 11 struct SegTree { 12 Node seg[Max_N << 2]; 13 inline void built(int root, int l, int r) { 14 seg[root].add = 0; 15 if (l == r) { 16 seg[root].v = 1; 17 return; 18 } 19 built(lc, l, mid); 20 built(rc, mid + 1, r); 21 seg[root].v = seg[lc].v + seg[rc].v; 22 } 23 inline void push_down(int root, int len) { 24 if (seg[root].add != 0) { 25 int &x = seg[root].add; 26 seg[lc].add = seg[rc].add = x; 27 seg[lc].v = (len - (len >> 1)) * x; 28 seg[rc].v = (len >> 1) * x; 29 x = 0; 30 } 31 } 32 inline void update(int root, int l, int r, int x, int y, int add) { 33 if (x > r || y < l) return; 34 if (x <= l && y >= r) { 35 seg[root].add = add; 36 seg[root].v = (r - l + 1) * add; 37 return; 38 } 39 push_down(root, r - l + 1); 40 update(lc, l, mid, x, y, add); 41 update(rc, mid + 1, r, x, y, add); 42 seg[root].v = seg[lc].v + seg[rc].v; 43 } 44 inline int query() { 45 return seg[1].v; 46 } 47 }seg; 48 int main() { 49 #ifdef LOCAL 50 freopen("in.txt", "r", stdin); 51 freopen("out.txt", "w+", stdout); 52 #endif 53 int t, n, m, a, b, c, k = 1; 54 scanf("%d", &t); 55 while (t--) { 56 scanf("%d %d", &n, &m); 57 seg.built(1, 1, n); 58 while (m--) { 59 scanf("%d %d %d", &a, &b, &c); 60 seg.update(1, 1, n, a, b, c); 61 } 62 printf("Case %d: The total value of the hook is %d.\n", k++, seg.query()); 63 } 64 return 0; 65 }
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步