http://acm.hdu.edu.cn/showproblem.php?pid=1698

Just a Hook

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.

 
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.
 
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 1000010
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
int sum[N<<],add[N<<];
struct node
{
int l,r;
int value;
}tree[N<<];
/*
Update 区间更新
Query 直接求根节点的sum
使用Lazy标记
Lazy标记的学习:http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html
*/
void PushUp(int rt)
{
//自底向上更新
sum[rt]=sum[rt<<]+sum[rt<<|];
} void BuildTree(int L,int R,int rt)
{
add[rt]=;
if(L==R){
sum[rt]=;
return ;
}
int m=(L+R)>>;
BuildTree(lson);
BuildTree(rson);
PushUp(rt);
} void PushDown(int rt,int m)
{
//处理lazy标记,自顶向下更新
if(add[rt]){
add[rt<<]=add[rt];
add[rt<<|]=add[rt];
sum[rt<<]=add[rt]*(m-(m>>));
sum[rt<<|]=add[rt]*(m>>);
add[rt]=;
}
} void Update(int l,int r,int c,int L,int R,int rt)
{
if(add[rt]==c) return ;
if(L>=l&&R<=r){
add[rt]=c;
sum[rt]=c*(R-L+);
return ;
}
PushDown(rt,R-L+);
int m=(R+L)>>;
if(l<=m) Update(l,r,c,lson);
if(r>m) Update(l,r,c,rson);
PushUp(rt);
} int main()
{
int cas=;
int t;
scanf("%d",&t);
while(t--){
int n,q;
scanf("%d%d",&n,&q);
BuildTree(,n,);
while(q--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
Update(a,b,c,,n,);
}
printf("Case %d: The total value of the hook is %d.\n",++cas,sum[]);
}
return ;
}

HDU 1398:Just a Hook(线段树区间更新)的更多相关文章

  1. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  2. 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 ...

  3. hdu - 1689 Just a Hook (线段树区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...

  4. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

  5. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  6. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  7. Just a Hook 线段树 区间更新

    Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

  8. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  9. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  10. [HDU] 1698 Just a Hook [线段树区间替换]

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. MyBatis Generator 详解 专题

    idea中有plugin可提高效率: http://www.henryxi.com/use-idea-mybatis-plugin-generate-mapper-files eg: <?xml ...

  2. Java之线程池管理

    JDK5后建议使用ExecutorService与Excutors来创建与管理线程池, 不再建议直接使用Thread. 开始不明白原因, 今天知道结果了:使用Thread.currnetThread. ...

  3. vagrant up default: Warning: Authentication failure. Retrying...的一些解决办法

    vagrant up default: Warning: Authentication failure. Retrying...的一些解决办法 一般看到这个信息时,虚拟机已经启动成功,可以中断命令后v ...

  4. WPF ScrollViewer(滚动条) 自定义样式表制作 (改良+美化)

    原文:WPF ScrollViewer(滚动条) 自定义样式表制作 (改良+美化) 注释直接写在代码里了   不太理解意思的 可以先去看看我上一篇  WPF ScrollViewer(滚动条)  自定 ...

  5. android x86 7.0 32bit调试apk时出现的错误

    detected problems with app native libraries libavcodec.so:text relocationslibavutil.solibswresample. ...

  6. SQLServer 远程服务器不存在,未被指定为有效的发布服务器,或您无权查看可用的发布服务器

    原文:SQLServer 远程服务器不存在,未被指定为有效的发布服务器,或您无权查看可用的发布服务器 创建了事务发布,在初始化时出现错误,查看相关代理信息如下: 日志读取器代理错误: 状态: 0,代码 ...

  7. ArcGIS数据生产与精细化制图之中国年降水量分布图的制作

    原文:ArcGIS数据生产与精细化制图之中国年降水量分布图的制作 楼主按:在今年的Esri中国用户大会上,我听了几场关于ArcGIS用于制图方面的讲座,也在体验区与Esri中国的技术老师有一些交流.一 ...

  8. 处理 Windows Phone 应用中的“后退”按钮 (XAML)

    与电脑不同,所有 Windows Phone 设备都有“后退”按钮,它允许用户在应用的页面之间向后导航.如果用户在转到应用的第一页时再次按“后退”按钮,操作系统会挂起你的应用并将用户导航到应用启动前的 ...

  9. Android零基础入门第44节:ListView数据动态更新

    原文:Android零基础入门第44节:ListView数据动态更新 经过前面几期的学习,关于ListView的一些基本用法大概学的差不多了,但是你可能发现了,所有ListView里面要填充的数据都是 ...

  10. uwp开发————换背景图片

    原文:uwp开发----换背景图片 用后台代码来实现对容器背景的切换,用本地图片作为背景. 把需要的图片素材放到Assets文件夹下 前台xaml代码如下: <Grid x:Name=" ...