HDU 1398:Just a Hook(线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698
Just a Hook
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.
#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(线段树区间更新)的更多相关文章
- (简单) 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(线段树区间更新查询)
描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- 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 ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- redis 从0 到 1 键值相关命令 服务器相关命令
keys * 获取所有的key 忽略其数据类型 数据为空 返回(empty list or set) keys a* .*b 获取以a开头 或者 以b结尾的key 返回(empty list ...
- Gradle编译失败 generating the main dex list
编译打包的时候出现这个错误,信息很少. * What went wrong: Execution failed for task ':camCard_Asia_Trunk:transformClass ...
- 芒果TV For Windows10 成长历史 & 迭代历史 & 新闻报道
芒果TV 是国内领先的基于Windows10操作系统并支持Windows10全系列设备的视频应用和内容服务商. Win10商店版<芒果TV>是湖南快乐阳光互动娱乐传媒有限公司专门为Wind ...
- Qt Widget 利用 Qt4.5 实现酷炫透明窗体
本文讲述的是Qt Widget 利用 Qt4.5 实现酷炫透明窗体,QWidget类中的每一个窗口部件都是矩形,并且它们按Z轴顺序排列的.一个窗口部件可以被它的父窗口部件或者它前面的窗口部件盖住一部分 ...
- .NET重思(二)接口和抽象类的取舍
不得不说,接口和抽象类好像啊~两者都不可以实例化,并且未实现的部分都是由派生类实现的. 他们主要有这么个区别: (1)抽象类的派生类可以是派生类,换言之,抽象成员在派生类中不一定完全实现,而接口要求其 ...
- 80%的岗位是没有太多能力上的要求的(少部分聪明的人开始觉醒,这部分一定是那些主动追求、主动学习的人;30岁现象能区分真正专业和不学无术的人)good
不要沦陷程序员的30岁问题 热门> 就是学习能力和工作热情态度的问题. 我之前也跟作者一样思考过这个问题,答案是否定的. 在知识积累的行业,年纪越大,越吃香,比如金融,医学,IT.就怕3 ...
- Delphi中用MessageBox()API函数做倒计时对话框(使用Hook安装CBTHookCallback,计时器更改文字,SetWindowText API真正修改文字,引用未知函数)good
API有隐藏的MessageBoxTimeOut函数可以做计时对话框,缺点是不能显示还剩下多少秒关闭. const IDTIMEDOUT = 32000; function MessageBoxTim ...
- 如何用C++操作无线网卡开启共享热点WiFi?
首先需要笔记本具备AP热点功能,记得写好的程序必须用管理员身份运行. 准备工作需要先做好 //查看是否支持的承载网络 netsh wlan show drivers //设置网络模式为allow ne ...
- SQLite实现内存键值存储
SQLite数据文件往Linux内存文件系统/dev/shm/data.sqlite3一放,就是内存级读写性能的SQL系统.用SQLite实现内存键值存储:CREATE TABLE IF NOT EX ...
- <iOS 导航栏>第一节:导航栏透明方法实现代码
说下导航栏的透明方法: 很多应用需要导航栏随着向上滑动,逐渐从透明变成不透明,很炫酷,大部分应用都在使用导航栏渐变效果,现附上代码然后直接将实现,一会讲下如何来实现,这一部分直接上代码. ...