hdu 1698 Just a Hook(线段树之 成段更新)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit:
32768/32768 K (Java/Others)
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.
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.
1
10
2
1 5 2
5 9 3
Case 1: The total value of the hook is 24.
#include<cstdio> #define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
const int N = 100010;
struct node
{
int l;
int r;
int sum;
int color;
}a[N<<2]; void PushUp(int root)
{
a[root].sum = a[root<<1].sum + a[root<<1|1].sum;
}
void PushDown(int len, int root)
{
if(a[root].color)
{
a[root<<1].color = a[root<<1|1].color = a[root].color;
a[root<<1].sum = (len - (len>>1)) * a[root].color;
a[root<<1|1].sum = (len>>1) * a[root].color;
a[root].color = 0;
}
}
void build_tree(int l, int r, int root)
{
a[root].l = l;
a[root].r = r;
a[root].color = 0; if(l == r)
{
a[root].sum = 1;
return ;
}
int mid = (l + r) >> 1;
build_tree(lson);
build_tree(rson);
PushUp(root);
}
void update(int l, int r, int root, int z)
{
if(l <= a[root].l && r >= a[root].r)
{
a[root].color = z;
a[root].sum = (a[root].r - a[root].l + 1) * z;
return;
}
PushDown(a[root].r - a[root].l + 1, root);
int mid = (a[root].l + a[root].r) >> 1;
if(l <= mid) update(l, r, root<<1, z);
if(r > mid) update(l, r, root<<1|1, z);
PushUp(root);
} int main()
{
int T, n, m, cas = 0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
build_tree(1, n, 1);
scanf("%d",&m);
int x, y, z;
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
update(x, y, 1, z);
}
int ans = a[1].sum;
printf("Case %d: The total value of the hook is %d.\n", ++cas, ans);
}
return 0;
}
hdu 1698 Just a Hook(线段树之 成段更新)的更多相关文章
- Codeforces295A - Greg and Array(线段树的成段更新)
题目大意 给定一个序列a[1],a[2]--a[n] 接下来给出m种操作,每种操作是以下形式的: l r d 表示把区间[l,r]内的每一个数都加上一个值d 之后有k个操作,每个操作是以下形式的: x ...
- HDU 1698 Just a Hook(线段树成段更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 1698 just a hook 线段树,区间定值,求和
Just a Hook Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)
题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3, 初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- HDU 1698 Just a Hook 线段树+lazy-target 区间刷新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 1698 Just a Hook(线段树区间更新查询)
描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...
随机推荐
- 基础知识(10)- 部署应用程序和applet
10.1 JAR文件 10.1.1 清单文件 10.1.2 可运行JAR文件 10.1.3 资源 10.1.4 密封 10.2 Java Web Start 10.2.1 沙箱 10.2. ...
- Flash Builder 条件编译的实现
最近项目需要开发多个版本, 而Flash又没有像C++ 那样的 #ifdef, 来让一套代码支持多个版本的编译发布; 经过研究, 终于知道Flash Builder如何支持条件编译: 1. 在项目 ...
- Swift - 浮点数转换成整数(四舍五入与直接截断)
1,直接截去小数部分转换成整数 使用强制转换会将浮点部分去除,把整数部分转换为整数. 1 var i = Int(23.50) //23 2,四舍五入转换成整数 lroundf是一个全局函数,作用是将 ...
- Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind at ...
- Test oracle db iops
Today, i need to test one database's iops and do something for oracle db's io test. How to test the ...
- 搭建实时同步data guard的最高可用-切换主备
搭建实时同步data guard的最高可用-切换主备 首先保证主库在归档模式下:错过N次了 准备二台机器(hostname gw hosts ech0)host-only [root@node1 ~] ...
- C#写的客户端连接 php的服务器端的小例子
C#写的客户端连接 php的服务器端的小例子 php的server 端 <?php // server.php set_time_limit( 0 ); ob_implicit_flush(); ...
- Windbg 32位版本和64位版本的选择
习惯了Vsiual Studio的兄弟们可能会因为先入为主的原因以为所有的调试器都应该像它那样,其实不然,当你安装Debugging Tools for Windows的时候,你将发现有两个系列的工具 ...
- Qt窗口操作函数(最大化,全屏,隐藏最大化,最小化)
Qt窗口中的一些小技术总结 //Qt主窗口没有最小化,最大化按钮且最大化显示 int main(int argc, char *argv[]) { QApplication a(argc, argv ...
- [Windows Phone]模仿魔兽3技能按钮SkillButton
简介: 模仿魔兽3技能按钮,带CD效果.使用的时候可以当做普通按钮使用,同时也支持Binding. 音效紧耦合在控件内部,因为控件本身目的就是模拟魔兽3的技能按钮,所以不考虑音效的扩展. Demo结构 ...