ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】
任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
Count the Colors
Time Limit: 2 Seconds Memory Limit: 65536 KB
Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.
Your task is counting the segments of different colors you can see at last.
Input
The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:
x1 x2 c
x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.
All the numbers are in the range [0, 8000], and they are all integers.
Input may contain several data set, process to the end of file.
Output
Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.
If some color can't be seen, you shouldn't print it.
Print a blank line after every dataset.
Sample Input
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1
Sample Output
1 1
2 1
3 1
1 1
0 2
1 1
题意概括:
多测试样例,每个有 N 次操作,对区间 [a, b] 涂色,是涂区间是涂区间,不是涂区间的点!
是很别扭,举个栗子:涂 1-2 和 3-4,如果是涂点那么 1、2、3、4都涂了,如果是涂区间那么 2-3 区间没有涂。

解题思路:
线段树更新修改区间(把原来的数组看成一个点,两点之间的差是区间),暴力单点查询每点的颜色值,统计每种可以看得见的颜色有几段。
AC code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long int
#define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
using namespace std;
const int MAXN = ;
struct date{
int L, R, val;
}node[MAXN];
int lyt[MAXN<<];
int cnt[MAXN];
int N, max_R, max_C; void PushDown(int root)
{
if(lyt[root] >= ){
lyt[root<<] = lyt[root<<|] = lyt[root];
lyt[root] = -;
}
}
void Update(int L, int R, int l, int r, int root, int val)
{
if(L <= l && r <= R){lyt[root] = val;return;}
int mid = (l+r)>>;
PushDown(root);
if(L <= mid) Update(L, R, lson, val);
if(R > mid) Update(L, R, rson, val);
}
int Query(int pos, int l, int r, int root)
{
if(l == r) return lyt[root];
int ret;
PushDown(root);
int mid = (l+r)>>;
if(pos <= mid) ret = Query(pos, lson);
if(pos > mid) ret = Query(pos, rson);
return ret;
}
void init()
{
max_R = ; max_C = ;
memset(cnt, , sizeof(cnt));
memset(lyt, -, sizeof(lyt));
}
int main()
{
while(~scanf("%d", &N)){
init(); //初始化
for(int i = ; i < N; i++){ //输入
scanf("%d%d%d", &node[i].L, &node[i].R, &node[i].val);
node[i].L++;
max_R = max(node[i].R, max_R);
max_C = max(max_C, node[i].val);
}
for(int i = ; i < N; i++){ //建树
if(node[i].R >= node[i].L)
Update(node[i].L, node[i].R, , max_R, , node[i].val);
}
int last_color=-, now_color;
for(int i = ; i <= max_R; i++)
{
now_color = Query(i, , max_R, );
if(now_color == -){last_color = -; continue;} ///没有染色
if(now_color != last_color) cnt[now_color]++; ///该颜色断层
last_color = now_color;
}
for(int i = ; i <= max_C; i++){
if(cnt[i]>) printf("%d %d\n", i, cnt[i]);
}
puts("");
}
return ;
}
ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】的更多相关文章
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)
1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...
- 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 (线段树+区间更新+暴力计数)
题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...
- ZOJ - 1610 Count the Colors(线段树区间更新)
https://cn.vjudge.net/problem/ZOJ-1610 题意 给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000. ...
- zoj 1610 Count the Colors(线段树延迟更新)
所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...
- ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- ZOJ 1610 Count the Colors 【线段树】
<题目链接> 题目大意: 在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每 ...
随机推荐
- Activemq API使用(整合spring)
整合spring之后,主要用的就是org.springframework.jms.core.JmsTemplate的API了,在spring-jms-xxx.jar中. 引入整合需要的jar包: &l ...
- oracle 笔记---(六)__表空间
查看表空间的大小 select tablespace_name,block_size,contents from dba_tablespaces; 查看表空间对应的数据文件 select file_n ...
- LINUX学习之一:
学好linux的基础:C语言(GNU C语言与GCC):硬件基础:熟悉操作系统内核代码,熟悉多线程和网络知识.分驱动开发(驱动程序模型即框架)和应用程序开发,目标是驱动开发 驱动开发特点: 不能使用标 ...
- JQuery脚本-通过禁用按钮防止表单重复提交
<script type="text/javascript"> /* jquer 脚本,避免重复提交 隐藏点击的submit,后在他之后插入同名button伪装成被隐藏 ...
- awk - Unix, Linux Command---reference
http://www.tutorialspoint.com/unix_commands/awk.htm NAME gawk - pattern scanning and processing lang ...
- rsync的配置
1. 安装rsync,如果要讲1.1.1.1的数据备份到2.2.2.2上,则在1.1.1.1上配置如下: /etc/rsyncd.conf uid = nobody gid = nobody use ...
- 【Shell】shell 判断文件夹或文件是否存在
1.文件夹不存在则创建,文件夹是directory if [ ! -d "/data/" ];then mkdir /data else echo "文件夹已经存在&qu ...
- Bootstrap入门(第一天)
一直都想认真的学习一下Bootstrap,但是由于种种原因,一直没有行动,虽然期间有使用过Bootstrap,但是都没有系统的学习过.最近工作室(学校老师的工作室)安排了一个前端任务让我跟进,主要是根 ...
- ES6 克隆对象
浅克隆:只能克隆原始对象自身的值,不能克隆它继承的值 方法一: function clone(origin) { return Object.assign({}, origin); } 方法二: fu ...
- 写C#代码时用到的中文简体字 、繁体字 对应的转化 (收藏吧)
简体字 下面有与之对应的繁体字 private const String Jian = "啊阿埃挨哎唉哀皑癌蔼矮艾碍爱隘鞍氨安俺按暗岸胺案肮昂盎凹敖熬翱袄傲奥懊澳芭捌扒叭吧笆疤巴拔跋靶 ...