HDU-OJ-1698 Just a Hook
来源:互联网 发布:明天教室网络课怎么样 编辑:程序博客网 时间:2024/05/01 17:57
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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.
/****************************************/ #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <stack> #include <queue> #include <vector> #include <map> #include <string> #include <iostream> using namespace std;/****************************************/#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1const int N = 100010;int tree_sum[N<<2], lazy[N<<2];int A, B, val;void Up(int rt) {tree_sum[rt] = tree_sum[rt<<1] + tree_sum[rt<<1|1];}void Down(int rt, int len) {if(lazy[rt]) {//本层有懒惰标记说明有事儿还没完成lazy[rt<<1|1] = lazy[rt<<1] = lazy[rt];tree_sum[rt<<1] = (len-(len>>1)) * lazy[rt];//下放标记之后,更新左子树、右子树tree_sum[rt<<1|1] = (len>>1) * lazy[rt];lazy[rt] = 0;//本层没事儿了,反正有事儿也是下一层的事儿,不用我管}}void build(int l, int r, int rt) {tree_sum[rt] = 1;lazy[rt] = 0;if(l == r)return ;int m = (l+r) >> 1;build(lson);build(rson);Up(rt);}void update(int l, int r, int rt) {if(A <= l&&r <= B) {//倘若当前区间在目标区间之内,则更新懒惰标记即可,不会进入下面的懒惰下放及更新lazy[rt] = val;//设置懒惰标记,表示该段下面所有结点共享的val值tree_sum[rt] = val * (r-l+1);return ;}//不在目标区间之内,继续向下找Down(rt, r-l+1);//倘若该点拥有懒惰标记,下放懒惰标记并且更新儿子,不然的话正确数据会被覆盖int m = (l+r) >> 1;if(A <= m)update(lson);if(B > m)update(rson);Up(rt);//向上更新是必须的}int main() {int cas;scanf("%d", &cas);for(int i = 1; i <= cas; i++) {int n, op;scanf("%d%d", &n, &op);build(1, n, 1);while(op--) {scanf("%d%d%d", &A, &B, &val);update(1, n, 1);}printf("Case %d: The total value of the hook is %d.\n", i, tree_sum[1]);}return 0;}
0 0
- HDU-OJ-1698 Just a Hook
- hdu oj 1698 just a hook 线段树
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- Hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 just a hook
- hdu 1698 Just a Hook
- HDU 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- HDU-1698-Just a Hook
- hdu 1698 Just a Hook
- hdu 1698 Just a Hook
- HDU 1698 Just a Hook
- HDU 1698 - Just a Hook
- 解决 CentOS 6.4 升级 Python2.7 后 Ibus 输入法无法使用问题
- contrastive divergence 算法
- widow下svn上传项目时的文件可执行权限问题
- Fragment和Activity
- 关于s3c2440+linux2.6.34.14KGDB的使用
- HDU-OJ-1698 Just a Hook
- 母猪的产后护理
- CAN总线负载率原理及计算
- HDU 1896 Stones ——STL 优先队列
- 数据库同步-重置同步数据
- 深入学习JDK 线程池(之七)
- 寄存器编址
- c++-结构体内存对齐
- Cocos2d学习之路五(Box2d使用CCPhysicsSprite时编译不通过解决方法)