HDU 1698 Just a Hook(线段树区间置换)
2016-07-27 21:27
330 查看
Just a Hook
Crawling in process...
Crawling failed
Time Limit:2000MS
Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
Input
Output
Sample Input
Sample Output
Hint
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.
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.
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.题意:屠夫有一条长的钩子,钩子由n个小钩子组成,每次操作将一个区间的小钩子变成金(3)银(2)铜(1)三者之一,最后取出所有区间的钩子总长度
思路:区间置换好区间跟新一个思考,简单的区间更新;
AC code:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
#define N 1000100
struct node
{
LL l;
LL r;
LL sum;
LL lazy; //lazy标记记录更新值
}tree[N << 2];
void pushdown(LL root, LL len)
{
if (tree[root].lazy)
{ //更新子区间标记
tree[root * 2].lazy = tree[root].lazy;
tree[root * 2 + 1].lazy = tree[root].lazy; //更新子区间和
tree[root * 2].sum = tree[root].lazy*(len - (len / 2));
tree[root * 2 + 1].sum = tree[root].lazy*(len / 2);
tree[root].lazy = 0; //lazy标记清零
}
}
void build(LL root, LL l, LL r)
{
tree[root].l = l;
tree[root].r = r;
tree[root].lazy = 0; //初始化lazy
if (l == r)
{
tree[root].sum = 1; // 钩子最初全为铜
return;
}
LL mid = (tree[root].l + tree[root].r) / 2;
build(root * 2, l, mid);
build(root * 2 + 1, mid + 1, r);
tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
}
void update(LL root, LL l, LL r, LL c)
{
if (l <= tree[root].l &&tree[root].r <= r)
{
tree[root].lazy = c; //更新最大区间的lazy标记,用的时候才传递 防止超时
tree[root].sum = c*(tree[root].r - tree[root].l + 1); //同时维护sum
return;
}
pushdown(root, tree[root].r - tree[root].l + 1); //更新儿子节点的标记,防止二次更新使区间和,lazy标记被覆盖
LL mid = (tree[root].l + tree[root].r) / 2;
if (l <= mid)
{
update(root * 2, l, r, c);
}
if (r > mid)
{
update(root * 2 + 1, l, r, c);
}
tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
}
LL query(LL root, LL l, LL r)
{
LL ans = 0;
LL mid = (tree[root].l + tree[root].r) / 2;
if (l <= tree[root].l &&tree[root].r <= r)
{
return tree[root].sum;
}
pushdown(root, tree[root].r - tree[root].l + 1); //将父亲的lazy标记传递下去,更新儿子节点,维护查找的区间和准确
if (l <= mid)
{
ans += query(root * 2, l, r);
}
if (r > mid)
{
ans += query(root * 2 + 1, l, r);
}
tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
return ans;
}
int main()
{
LL i, x, y, z, n, m, t, k = 1;
scanf("%lld", &t);
while (t--)
{
scanf("%lld", &n);
build(1, 1, n);
scanf("%lld", &m);
for (i = 1; i <= m; i++)
{
scanf("%lld%lld%lld", &x, &y, &z);
update(1, x, y, z);
}
printf("Case %lld: The total value of the hook is %lld.\n", k++, query(1, 1, n));
}
return 0;
}
Crawling in process...
Crawling failed
Time Limit:2000MS
Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
Input
Output
Sample Input
Sample Output
Hint
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.
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.
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.题意:屠夫有一条长的钩子,钩子由n个小钩子组成,每次操作将一个区间的小钩子变成金(3)银(2)铜(1)三者之一,最后取出所有区间的钩子总长度
思路:区间置换好区间跟新一个思考,简单的区间更新;
AC code:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
#define N 1000100
struct node
{
LL l;
LL r;
LL sum;
LL lazy; //lazy标记记录更新值
}tree[N << 2];
void pushdown(LL root, LL len)
{
if (tree[root].lazy)
{ //更新子区间标记
tree[root * 2].lazy = tree[root].lazy;
tree[root * 2 + 1].lazy = tree[root].lazy; //更新子区间和
tree[root * 2].sum = tree[root].lazy*(len - (len / 2));
tree[root * 2 + 1].sum = tree[root].lazy*(len / 2);
tree[root].lazy = 0; //lazy标记清零
}
}
void build(LL root, LL l, LL r)
{
tree[root].l = l;
tree[root].r = r;
tree[root].lazy = 0; //初始化lazy
if (l == r)
{
tree[root].sum = 1; // 钩子最初全为铜
return;
}
LL mid = (tree[root].l + tree[root].r) / 2;
build(root * 2, l, mid);
build(root * 2 + 1, mid + 1, r);
tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
}
void update(LL root, LL l, LL r, LL c)
{
if (l <= tree[root].l &&tree[root].r <= r)
{
tree[root].lazy = c; //更新最大区间的lazy标记,用的时候才传递 防止超时
tree[root].sum = c*(tree[root].r - tree[root].l + 1); //同时维护sum
return;
}
pushdown(root, tree[root].r - tree[root].l + 1); //更新儿子节点的标记,防止二次更新使区间和,lazy标记被覆盖
LL mid = (tree[root].l + tree[root].r) / 2;
if (l <= mid)
{
update(root * 2, l, r, c);
}
if (r > mid)
{
update(root * 2 + 1, l, r, c);
}
tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
}
LL query(LL root, LL l, LL r)
{
LL ans = 0;
LL mid = (tree[root].l + tree[root].r) / 2;
if (l <= tree[root].l &&tree[root].r <= r)
{
return tree[root].sum;
}
pushdown(root, tree[root].r - tree[root].l + 1); //将父亲的lazy标记传递下去,更新儿子节点,维护查找的区间和准确
if (l <= mid)
{
ans += query(root * 2, l, r);
}
if (r > mid)
{
ans += query(root * 2 + 1, l, r);
}
tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
return ans;
}
int main()
{
LL i, x, y, z, n, m, t, k = 1;
scanf("%lld", &t);
while (t--)
{
scanf("%lld", &n);
build(1, 1, n);
scanf("%lld", &m);
for (i = 1; i <= m; i++)
{
scanf("%lld%lld%lld", &x, &y, &z);
update(1, x, y, z);
}
printf("Case %lld: The total value of the hook is %lld.\n", k++, query(1, 1, n));
}
return 0;
}
相关文章推荐
- 练习场 1253 Turing equation
- tjut 4630
- 从1到n整数中1出现的次数
- 10009---怎么查看linux版本
- Memcache的最佳实践方案
- 认识Servlet
- 10009---压力测试之badboy和Jmeter的简单使用方法
- HDU 1241 Oil Deposits(深搜基础模板)
- 什么东西只花十分钟,但是受益终身
- laravel5数据库操作
- Ionic环境搭建
- 使用class-dump导出其他应用头文件
- JSTL的配置与使用
- 奇怪的问题,列名无效
- Codeforces-448【A数学函数ceil】【B思维】【D二分】
- Redis 和 Memcache 的性能对比基础篇
- 项目安排[动态规划]
- 基于spark的DataFrame实战
- Struts2的三种参数传递方式
- HDOJ 1551 Cable master