HDU1698 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.
思路:
区间覆盖,还是用lazy标签,没看到最后一个句点疯狂WA
代码:
#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#define ll long long
const int N=100005;
const int MOD=20071027;
using namespace std;
int node[N<<2],lazy[N<<2];
void push_down(int rt,int l,int r){
int m=(l+r)/2;
if(lazy[rt]!=-1){
node[rt<<1]=lazy[rt]*(m-l+1);
node[rt<<1|1]=lazy[rt]*(r-(m+1)+1);
lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
lazy[rt]=-1;
}
}
void update(int rt,int l,int r,int L,int R,int v){ //l r搜索区间
if(L<=l && R>=r){
node[rt]=v*(r-l+1);
lazy[rt]=v;
return;
}
push_down(rt,l,r);
int m=(l+r)/2;
node[rt]=0;
if(L<=m){
update(rt<<1,l,m,L,R,v);
}
if(R>m){
update(rt<<1|1,m+1,r,L,R,v);
}
node[rt]+=node[rt<<1]+node[rt<<1|1];
}
int main(){
int n,q,a,b,c,T,num=1;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(node,-1,sizeof(node));
int len=n<<2;
for(int i=0;i<len;i++){
lazy[i]=1;
}
scanf("%d",&q);
node[1]=n;
while(q--){
scanf("%d%d%d",&a,&b,&c);
update(1,1,n,a,b,c);
}
printf("Case %d: The total value of the hook is %d.\n",num++,node[1]);
}
return 0;
}
HDU1698 Just a Hook(线段树&区间覆盖)题解的更多相关文章
- 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)
学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...
- hdu1698 Just a hook 线段树区间更新
题解: 和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新). 单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间. 这里就体现了线段树的另一个重要思想:延迟标记. ...
- HDU1698 Just a Hook —— 线段树 区间染色
题目链接:https://vjudge.net/problem/HDU-1698 In the game of DotA, Pudge’s meat hook is actually the most ...
- hdu1698 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: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)
http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- [NOI2015] 软件包管理器【树链剖分+线段树区间覆盖】
Online Judge:Luogu-P2146 Label:树链剖分,线段树区间覆盖 题目大意 \(n\)个软件包(编号0~n-1),他们之间的依赖关系用一棵含\(n-1\)条边的树来描述.一共两种 ...
- Mayor's posters POJ - 2528 线段树区间覆盖
//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...
随机推荐
- Elasticsearch 的坑爹事——记录一次mapping field修改过程(转)
原文:http://www.cnblogs.com/Creator/p/3722408.html 本文记录一次Elasticsearch mapping field修改过程 团队使用Elasticse ...
- Servlet----------在 Servlet 中的xml配置
今天弄了大半天,才弄好了,还是请教了别人,主要原因在于把web.xml文件放在了WEB-INF文件夹下面了,正常的情况是在WebRoot下面的. 还有一个,我是在MyEclipse中操作的,起初不知道 ...
- Linuxer-"Linux开发人员自己的媒体"第五月稿件和赠书名单
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/juS3Ve/article/details/78859630 Linuxer已经从一个单纯的读者服务 ...
- zkSNARK 零知识验证
参考文献 ZCash7篇,有社区翻译版,但还是推荐看原汁原味的 https://z.cash/blog/snark-explain.html Vitalik3篇,小天才作者我就不介绍了,这三篇 ...
- Python随机数生成方法
假设你对在Python生成随机数与random模块中最经常使用的几个函数的关系与不懂之处.以下的文章就是对Python生成随机数与random模块中最经常使用的几个函数的关系,希望你会有所收获,以下就 ...
- 阿里云安装docker 指定版本
sh docker-install.sh 1.12.6 #ubuntu16.4 测试通过 #!/bin/sh set -e # # This script is meant for quick &am ...
- mysql 实时统计脚本 QPS,TPS和线程连接数等
#!/bin/bash mysqladmin -uroot -p'root' extended-status -i1|awk 'BEGIN{local_switch=0;print "QPS ...
- MVC html.beginform & ajax.beginform
1.指定表单提交方式和路径等 @using (Html.BeginForm("Index", "Home", FormMethod.Get, new { nam ...
- 安插,复制,替换和删除ul中的li
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- for和foreach的区别
public class Program { public static void Main() { Program program = new Program(); program.For();// ...