[线段树]hdu1698 Just a Hook(区间更新、延迟标记
来源:互联网 发布:linux测试网速的命令 编辑:程序博客网 时间:2024/05/21 20:34
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 28346 Accepted Submission(s): 14047
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 <iostream>#include <stdio.h>//区间更新、区间查询const int N = 100000 + 10;struct{ int val; int mark; int l;//区间下界 int r;//区间上界}Tree[N*4];int pushup(int root){ return Tree[root<<1].val + Tree[root<<1|1].val;}void build(int root, int left, int right){ Tree[root].mark = 0; Tree[root].l = left; Tree[root].r = right; if(left == right) { Tree[root].val = 1; return; } int mid = (left + right) >> 1; build(root<<1, left, mid); build(root<<1|1, mid+1, right); Tree[root].val = pushup(root);}void pushdown(int root){ if(Tree[root].mark != 0) { int mid = (Tree[root].l + Tree[root].r) >> 1; Tree[root<<1].mark = Tree[root].mark; Tree[root<<1|1].mark = Tree[root].mark; Tree[root<<1].val = Tree[root].mark * (mid-Tree[root].l+1); Tree[root<<1|1].val = Tree[root].mark * (Tree[root].r-mid); Tree[root].mark = 0; }}void update(int root, int l, int r, int value){ if(l > Tree[root].r || r < Tree[root].l) return; if(l <= Tree[root].l && r >= Tree[root].r) { Tree[root].mark = value; Tree[root].val = value * (Tree[root].r-Tree[root].l+1); return; } pushdown(root); update(root<<1, l, r, value); update(root<<1|1, l , r, value); Tree[root].val = pushup(root);}int main(){ int no = 0; int t, n, m; int x, y, z; scanf("%d", &t); while(t-->0) { scanf("%d%d", &n, &m); build(1, 1, n); while(m-->0) { scanf("%d%d%d", &x, &y, &z); update(1, x, y, z); } printf("Case %d: The total value of the hook is %d.\n", ++no, Tree[1].val); } return 0;}
0 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--线段树区间更新
- 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
- Android ViewPager实现引导页
- MySQL Server优化
- 牢记25个最佳的SSH命令
- 不能被工程师群殴(一)
- [HDU5659]knight
- [线段树]hdu1698 Just a Hook(区间更新、延迟标记
- OAuth2授权(Client Credentials)
- HDU 1872 稳定排序
- 实施数据库审计-DBA负责的安全和审计工作以及标准数据库审计
- 对比objdump和readelf
- Oracle简单入门(连载)(1)
- Q1.Two Sum
- 进程和线程关系及区别
- MUI的上拉加载功能实现