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 ...
随机推荐
- 杭电oj题目分类
基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058.1 ...
- python中计算程序用时的方法
import time start = time.clock() ...... end = time.clock() print(end - start)
- Sqoop导入HBase,并借助Coprocessor协处理器同步索引到ES
1.环境 Mysql 5.6 Sqoop 1.4.6 Hadoop 2.5.2 HBase 0.98 Elasticsearch 2.3.5 2.安装(略过) 3.HBase Coprocessor实 ...
- Python安装常见问题(1):zipimport.ZipImportError: can't decompress data
在CentOS以及其他的Linux系统中遇到安装包安装错误的原因,大多数都是因为缺少依赖包导致的,所以对于错误:zipimport.ZipImportError: can’t decompress d ...
- POJ1258:Agri-Net(最小生成树模板题)
http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of hi ...
- POJ3414—Pots(bfs加回溯)
http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memor ...
- JDK eclipse selenium 安装以及环境变量的配置
原文地址https://www.cnblogs.com/zmhsoup/p/5249663.html [自动化学习笔记]_环境搭建Selenium2+Eclipse+Java+TestNG_(一) 目 ...
- Summary: Stack Overflow Error
What is a stack overflow error? Parameters and local variables are allocated on the stack (with refe ...
- linux 统计wc
说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选项含义如下: - c 统计字节 ...
- 网络编程—代码—TCP网络传输
一.TCP:用字符流传输信息 ------------------------------------------------------------------------------ [用字符流的 ...