ZOJ 1610 Count the Colors 【线段树】
<题目链接>
题目大意:
在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每种颜色的色块数量是多少。
解题分析:
首先要注意,这是对区间进行更新,。所以update的时候是对输入区间[a,b]的左闭右开或者是左开右闭进行处理。然后本题思路非常清晰,就是按照输入顺序更新线段树对应区间,然后对[0,8000]这个区间从左向右进行色块的统计。PS:虽然最后还是要将所有lazy下放到所有的叶子节点,进行色块数量的暴力统计,但是pushdown函数确实能够减少 update 的一些复杂度。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define Lson rt<<1,l,mid
#define Rson rt<<1|1,mid+1,r
const int M =8e3;
int tr[M<<],lazy[M<<],last;
int col[M<<];
void Pushdown(int rt){
if(lazy[rt]!=-){
int tmp=lazy[rt];
lazy[rt<<]=lazy[rt<<|]=tmp;
tr[rt<<]=tr[rt<<|]=tmp;
lazy[rt]=-;
}
}
void update(int rt,int l,int r,int L,int R,int c){ //区间染色
if(L<=l&&r<=R){
lazy[rt]=c;
tr[rt]=c;
return;
}
Pushdown(rt);
int mid=(l+r)>>;
if(L<=mid)update(Lson,L,R,c);
if(R>mid)update(Rson,L,R,c);
}
void query(int rt,int l,int r){ //运用递归(类似于dfs),达到从左到右逐步查询的效果,当遇到染色的点时,判断其是否与前一个点相同,如果不同,就说明该颜色的区块
if(l==r){
if(tr[rt]!=last&&tr[rt]!=-)col[tr[rt]]++;
last=tr[rt];
return;
}
Pushdown(rt);
int mid=(l+r)>>;
query(Lson);
query(Rson);
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
memset(tr,-,sizeof(tr));
memset(lazy,-,sizeof(lazy));
memset(col,,sizeof(col));
while(n--){
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
update(,,M,x+,y,c); //由于本题是区间更新,所以不能直接更新[a,b]这个闭区间的所有点,而是对a~b的左开右闭或者左闭右开区间进行更新,类似于图的将边权转化为点权
}
last=-;
query(,,M);
for(int i=;i<=M;i++){
if(col[i])printf("%d %d\n",i,col[i]);
}
printf("\n");
}
return ;
}
2018-09-22
ZOJ 1610 Count the Colors 【线段树】的更多相关文章
- zoj 1610 Count the Colors 线段树区间更新/暴力
Count the Colors Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
- ZOJ 1610 Count the Colors (线段树成段更新)
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
- ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
- ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】
任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...
- ZOJ 1610.Count the Colors-线段树(区间染色、区间更新、单点查询)-有点小坑(染色片段)
ZOJ Problem Set - 1610 Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting s ...
- ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- zoj 1610 Count the Colors(线段树延迟更新)
所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...
随机推荐
- Flex布局新旧混合写法详解
flex是个非常好用的属性,如果说有什么可以完全代替 float 和 position ,那么肯定是非它莫属了(虽然现在还有很多不支持 flex 的浏览器).然而国内很多浏览器对 Flex 的支持都不 ...
- 获取checkbox勾选的id
需求描述:做批量删除或者批量修改的时候需要获得多个id,根据checkbox勾选来获取对应的d 两种方法: //html代码<table id="table1"> &l ...
- Python基础之面向对象进阶一
一.isinstance(obj,cls)和issubclass(sub,super) 1.isinstance(obj,cls)检查obj是否是类 cls 的对象 class A: pass obj ...
- django的查看sql语句setting设置
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'cons ...
- git push -u 用法
在我们第一次提交git的时候: 发现上面用了这个-u参数,也没作解释,特意搜索了下这个-u的用法,加了参数-u后,以后即可直接用git push 代替git push origin master gi ...
- go包之logrus显示日志文件与行号
前言: logrus是go中比较好的一个log模块.github上的很多开源项目都在使用这个模块, 我在写这个博文时, github上的logrus的stars数已经有8214了.最近在用这个模块时, ...
- Android定位元素与操作
一.常用识别元素的工具 uiautomator:Android SDK自带的一个工具,在tools目录下 monitor:Android SDK自带的一个工具,在tools目录下 Appium Ins ...
- spring cloud Config--server
概述 使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spr ...
- mysql出现10060错误
报错内容如下 Can't connect to MySQL server (10060) 提示不能连接 mysql服务. 首先检查任务管理器 看看mysqld.exe进程有没有启动.如果启动了就强制结 ...
- 三 os模块
os模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相 ...