HDU 1689 Just a Hook (线段树)
2016-06-09 17:54
155 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
10
2
1 5 2
5 9 3
[align=left]Sample Output[/align]
Case 1: The total value of the hook is 24.
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
10
2
1 5 2
5 9 3
[align=left]Sample Output[/align]
Case 1: The total value of the hook is 24.
#include <cstdio> #include <cstring> #define N 100010 struct node { int l, r, sum, lazy; } tree[4 * N]; int n, q; inline int L(int x) { return x << 1; } inline int R(int x) { return (x << 1) + 1; } void build(int i, int left, int right) { tree[i].l = left; tree[i].r = right; tree[i].lazy = 0; if (left == right) { tree[i].sum = 1; return; } int mid = tree[i].l + (tree[i].r - tree[i].l) / 2; build(L(i), left, mid); build(R(i), mid + 1, right); tree[i].sum = tree[L(i)].sum + tree[R(i)].sum; } void push(int i) { if (!tree[i].lazy) return; tree[L(i)].sum = (tree[L(i)].r - tree[L(i)].l + 1) * tree[i].lazy; tree[L(i)].lazy = tree[i].lazy; tree[R(i)].sum = (tree[R(i)].r - tree[R(i)].l + 1) * tree[i].lazy; tree[R(i)].lazy = tree[i].lazy; tree[i].lazy = 0; } void update(int i, int left, int right, int key) { if (left <= tree[i].l && right >= tree[i].r) { tree[i].sum = (tree[i].r - tree[i].l + 1) * key; tree[i].lazy = key; return; } push(i); int mid = tree[i].l + (tree[i].r - tree[i].l) / 2; if (left <= mid) update(L(i), left, right, key); if (right > mid) update(R(i), left, right, key); tree[i].sum = tree[L(i)].sum + tree[R(i)].sum; } int query(int i, int left, int right) { if (left <= tree[i].l && right >= tree[i].r) return tree[i].sum; push(i); int mid = tree[i].l + (tree[i].r - tree[i].l) / 2; int ans = 0; if (left <= mid) ans += query(L(i), left, right); if (right > mid) ans += query(R(i), left, right); return ans; } int main() { int T; scanf("%d", &T); for (int iT = 1; iT <= T; iT++) { scanf("%d%d", &n, &q); build(1, 1, n); for (int i = 1; i <= n; i++) update(1, i, i, 1); int x, y, z; for (int i = 1; i <= q; i++) { scanf("%d%d%d", &x, &y, &z); update(1, x, y, z); } printf("Case %d: The total value of the hook is %d.\n", iT, query(1, 1, n)); } return 0; }
相关文章推荐
- SQLPLUS的 set配置(结合spool)
- MySQL 语句优化
- hadoop pipes应用别的语言
- IntelliJ IDEA 的 20 个代码自动完成的特性
- AngularJs directive指令实例
- ThinkPHP中的$this->success()与$this->error()方法的使用
- 电商之梳理varnish相关知识---http协议加速器
- 制作Linux版PE系统
- Mac OSX 10.10 Yosemite编译OpenJDK 9
- Struts:No result defined for action daiwei.struts.action.UserAction and result success
- Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- ListView的使用技巧集合(三)
- 常用排序算法Java代码
- 车势科技,高调进军汽车VR,会是下一个BAT?
- 电商之梳理Nginx(7层)相关知识---反向代理以及负载均衡
- 设计模式(二):构造器模式与模块模式
- iOS UI 技巧: 视图无法被点击
- Intersection of Two Linked Lists
- okhttp 使用完全解析
- IOS7笔记-8、协议、block、动画