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,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...
随机推荐
- eclipse 安装教程
eclipse 安装教程 一:安装包下载: 链接: https://pan.baidu.com/s/1qZtt62o 密码: 4ak2 注:若 下载链接失效,请看本文公告的QQ群,请联系群主. 二:安 ...
- 限制 Confluence 6 WebDAV 客户端的写入权限
在早期的 WebDAV 插件中分离了 WebDAV 客户端的写入权限(不能使用,创建/修改,编辑和删除操作)是分开配置的.但是在新版版本的插件中,我们将这些权限合并到了一起. WebDAV 客户端现在 ...
- Java编程之前的复习和练习
日期:2018.7.14 星期六 博客期:001 今天先是试着写一下博客,最近去青海旅游了,学习时间有点少,但空余时间还是有学习的,不管怎么样吧!先说一下我的这几天的成果——“Bignum”类,虽然很 ...
- React基础知识备忘
section-1 //react组件 export class Halo extends React.Component{ constructor(...args){ super(...args); ...
- 【深度学习】吴恩达网易公开课练习(class1 week2)
知识点汇总 作业内容:用logistic回归对猫进行分类 numpy知识点: 查看矩阵维度: x.shape 初始化0矩阵: np.zeros((dim1, dim2)) 去掉矩阵中大小是1的维度: ...
- labelme连续将文件夹中的json文件进行可视化的指令
for /r C:\Users\Fourmi\Desktop\ZP0 %i in (*.json) do labelme_json_to_dataset %i
- 繁简字转换(C#)
1.首先引入: using Microsoft.VisualBasic; 2.转换方法: //繁体转简体 public static string Traditional2Simplified(str ...
- 2019-3-9,Servlet转跳链接详解
//以下代码,可以传递request和response对象及其属性和变量至指定页面 request.getRequestDispatcher("showAttribut.jsp") ...
- C/C++遍历二维数组,列优先(column-major)比行优先(row-major)慢,why?
C/C++遍历二维数组,列优先(column-major)比行优先(row-major)慢,why? 简单粗暴的答案:存在Cache机制! 稍微啰嗦一点:CPU访问内存(读/写,遍历数组的话主要是读) ...
- Senparc.Weixin微信开发(1) 开发验证
官方系列教程 http://www.cnblogs.com/szw/archive/2013/05/20/3089479.html 登录微信公众平台后-左侧找到开发--启用服务器配置 这样,我们才可以 ...