HDU1698 Just a Hook
来源:互联网 发布:floyd算法流程 编辑:程序博客网 时间:2024/05/18 03:55
题目:
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.
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
11021 5 25 9 3
Sample Output
Case 1: The total value of the hook is 24.
因为本题初始化比较简单,所以没有用build函数
代码:
#include<iostream>using namespace std;int n;int sum[400000];int lazy[400000];void down(int key, int low, int high){int mid = (low + high) / 2;sum[key * 2] = (mid - low + 1)*lazy[key];lazy[key * 2] = lazy[key];sum[key * 2 + 1] = (high - mid)*lazy[key];lazy[key * 2 + 1] = lazy[key];lazy[key] = 0;}void update(int key, int low, int high, int x, int y, int value){if (low == x && high == y){sum[key] = (high - low + 1)*value;lazy[key] = value;return;}if (lazy[key])down(key, low, high);int mid = (low + high) / 2;if (mid < x)update(key * 2 + 1, mid + 1, high, x, y,value);else if (mid >= y)update(key * 2, low, mid, x, y, value);else{update(key * 2, low, mid, x, mid, value);update(key * 2 + 1, mid + 1, high, mid + 1, y, value);}sum[key] = sum[key * 2] + sum[key * 2 + 1];}int query(int key, int low, int high, int x, int y){if (low == x && high == y)return sum[key];down(key, low, high);int mid = (low + high) / 2;if (mid < x)return query(key * 2 + 1, mid + 1, high, x, y);if (mid >= y)return query(key * 2, low, mid, x, y); return query(key * 2, low, mid, x, mid) + query(key * 2 + 1, mid + 1, high, mid + 1, y);}int main(){int t, q, x, y, laz;cin >> t;for (int cas = 1; cas <= t; cas++){cin >> n >> q;sum[1] = n;lazy[1] = 1;for (int i = 1; i <= q; i++){scanf("%d%d%d", &x, &y, &laz);update(1, 1, n, x, y, laz);}printf("Case %d: The total value of the hook is %d.\n", cas,query(1,1,n,1,n));}return 0;}因为这个题目的查询比较特殊,所以query其实也不需要。
2 0
- hdu1698 Just a Hook
- hdu1698 Just a Hook
- [HDU1698]Just a Hook
- hdu1698 Just a Hook
- hdu1698 Just a Hook
- HDU1698 Just a Hook
- hdu1698 Just a Hook
- HDU1698 Just a Hook
- hdu1698-Just a Hook
- hdu1698 Just a Hook
- Just a Hook hdu1698
- hdu1698 Just a Hook
- hdu1698 Just a Hook
- HDU1698 Just a Hook
- HDU1698 Just a Hook
- HDU1698 Just a Hook
- HDU1698-Just a Hook
- HDU1698 Just a Hook
- Hdu-2089 不要62(数位DP)
- html5--video播放
- 《The Docker Book》读书笔记
- 股票经验分享新手入门K线之三空的判断和操作
- MySQL从入门到精通_4索引的操作
- HDU1698 Just a Hook
- 跑步新手入门须知
- DIY智能插座(六) -- 利用网易云信实现外网控制
- CSDN代码仓库的使用
- 跑步应该怎么跑?怎么跑最科学?
- java中String类源代码学习
- 跑步的正确姿势
- HDU-3652 B-number(数位DP+记忆化搜索)
- android中getLocationInWindow 和 getLocationOnScreen的区别