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 ...
随机推荐
- cocos2d-x游戏开发系列教程-前言
cocos2d-x游戏开发前景: 最近企业对于Cocos2D-X开发人才的用人需求很大,而且所提供的薪资相当可观. 为满足广大向往游戏开发行业同学的需求,特推出适合新手的Cocos2D-X手游开发教程 ...
- HDU4939Stupid Tower Defense (有思想的dp)
Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- javascript(七)document.write
<h1>test</h1> <button type="button" onclick="my_function">点击me ...
- hdu 4707 Pet 2013年ICPC热身赛A题 dfs水题
题意:linji的仓鼠丢了,他要找回仓鼠,他在房间0放了一块奶酪,按照抓鼠手册所说,这块奶酪可以吸引距离它D的仓鼠,但是仓鼠还是没有出现,现在给出一张关系图,表示各个房间的关系,相邻房间距离为1,而且 ...
- Cocos2d-x CCProgressTimer
CCProgressTimer,创建使用这个节点可以大致实现两个作用的效果: 其一:在游戏中几乎大部分的游戏启动界面都是游戏加载画面,那么用到的一般是进度条提示加载进度,其使用的就是CCProgres ...
- 在SharePoint 2010中部署RBS (转)
一.RBS(Remote BLOB Storage)简单介绍 在SharePoint的大部分企业应用案例中,SharePoint都是要承担着非常繁重的文件管理工作,这些文件类型包含了Word文档,Ex ...
- RAC 备份到本地不同设备
- Linux搭建Tomcat环境
安装Tomcat 1)下载apache-tomcat-7.0.42.tar.gz http://tomcat.apache.org/download-70.cgi 2)#tar -zxv ...
- DB2错误码解释对照
表 2. SQLSTATE 类代码 类 代码 含义 要获得子代码, 参阅... 00 完全成功完成 表 3 01 警告 表 4 02 无数据 表 5 07 动态 SQL 错误 表 6 ...
- 在安装mysql出现的错误以及解决方法
因为手贱更新了一下驱动,结果导致无线网卡出了问题.然而就算是从官网上下载了驱动各种折腾也没有弄好,心里特别堵.无奈只有重装系统这一条路了.这里表示特别难过,因为电脑上东西实在太多了,而且各种环境变量. ...