HDU 1698 Just a Hook (线段树区间更新入门题)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 36705 Accepted Submission(s):
17920
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.
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.
the total value of the hook after the operations. Use the format in the
example.
10
2
1 5 2
5 9 3
#include<iostream>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int M = ;
int sum[M<<],lazy[M<<];
void pushup(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
} void pushdown(int rt,int m){
if(lazy[rt]){
lazy[rt<<] = lazy[rt<<|] = lazy[rt];
sum[rt<<] = (m-(m>>))*lazy[rt];
sum[rt<<|] = (m>>)*lazy[rt];
lazy[rt] = ;
}
} void build(int l,int r,int rt){
lazy[rt] = ;
sum[rt] = ;
if(l == r) return ;
int m = (l + r) >>;
build(lson);
build(rson);
pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l&&R>=r){
lazy[rt] = c;
sum[rt] = c * (r - l + );
return ;
}
pushdown(rt,r - l + );
int m = (l + r)>>;
if(L <= m) update(L,R,c,lson);
if(R > m) update(L,R,c,rson);
pushup(rt);
} int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return sum[rt];
}
pushdown(rt,r - l + );
int m = (l + r)>>;
int ret = ;
if(L <= m) ret += query(L,R,lson);
if(R > m) ret += query(L,R,rson);
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
int t,n,q,l,r,c;
while(cin>>t){
for(int i = ;i <= t; i ++){
cin>>n;
build(,n,);
cin>>q;
while(q--){
cin>>l>>r>>c;
update(l,r,c,,n,);
}
cout<<"Case "<<i<<": The total value of the hook is ";
cout<<query(,n,,n,)<<"."<<endl;
}
}
}
HDU 1698 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 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- 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 ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- 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.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 ...
随机推荐
- 关于CAN总线的被动错误标志的问题?
关于CAN总线的被动错误标志的问题? 关于CAN总线的被动错误标志,协议中的描述是"处于被动错误状态的单元检测出错误时,输出被动错误标志". 对此有几个疑问: 1.被动错误标志的发 ...
- .NET Core中向已存在文件的特定位置写入数据
本例使用.NET Core向一个文本文件中的特定位置写入数据,来模拟文件上传中的断点续传是如何在服务器端实现的. 新建一个.NET Core控制台项目FileContinueToWrite,其Prog ...
- 20155220 Exp2 后门原理与实践
20155220 Exp2 后门原理与实践 1.Windows获得Linux Shell 在windows下,打开CMD,使用ipconfig指令查看本机IP 然后使用ncat.exe程序,ncat. ...
- jdbc获取blob类型乱码
一.使用场景: mysql数据库字段类型为longblob,在数据库里看中文字符正常,java读取字串的时候发现中文乱码 使用到了activeMq 二.排查: (1)修改eclipse的环境编码为ut ...
- 为你的机器学习模型创建API服务
1. 什么是API 当调包侠们训练好一个模型后,下一步要做的就是与业务开发组同学们进行代码对接,以便这些‘AI大脑’们可以顺利的被使用.然而往往要面临不同编程语言的挑战,例如很常见的是调包侠们用Pyt ...
- Kubernetes学习之路(二十三)之资源指标和集群监控
目录 1.资源指标和资源监控 2.Weave Scope监控集群 (1)Weave Scope部署 (2)使用 Scope (3)拓扑结构 (4)实时资源监控 (5)在线操作 (6)强大的搜索功能 2 ...
- P4385 [COCI2009]Dvapravca
首先特判掉蓝点数量\(<2\)的情况.没有蓝点答案就是\(n\),有一个蓝点可以枚举一个红点,选择过这个蓝点和红点的一条线和在无穷远处的平行线(即这条线对应的两个半平面). 这里认为过一个点是与 ...
- sqlyog mysql 外键引用列找不到想要的字段的原因
这是因为引用列必须为一个主键才行
- 面向 Kubernetes 编程: Kubernetes 是下一代操作系统
转自:https://github.com/answer1991/articles/blob/master/Kubernetes-is-the-next-generation-os.md 摘要 此文章 ...
- 华为云Istio服务网格,让应用治理智能化、可视化