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]这个区间内每 ...
随机推荐
- Java 继承初探
Java继承的基础 Java中,被继承的类叫做超类,继承超类的类叫子类.(一个子类亦可以是另一个类的超类) 继承一个类,只需要用关键字 extends 把一个类的定义合并到另一个类中就可以了. 例子中 ...
- Python+Selenium操作select下拉框
首先需要倒入Select模块: from selenium.webdriver.support.select import Select 常用方法: 通过索引定位:select_by_index() ...
- 牛客网Java刷题知识点之拥塞发生的主要原因、TCP拥塞控制、TCP流量控制、TCP拥塞控制的四大过程(慢启动、拥塞避免、快速重传、快速恢复)
不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 ...
- elasticSearch请求流程图
- 新建maven工程index.jsp页面报错
引入servlet依赖jar <dependency><groupId>javax.servlet</groupId><artifactId>servl ...
- 从Zero到Hero,一文掌握Python关键代码
# 01基础篇 # 变量 #int one=1 some_number=100 print("one=",one) #print type1 print("some_nu ...
- Java Socket通信示例
Socket分为ServerSocket和Socket两大类: 其中ServerSocket用于服务器端,可以通过accept方法监听请求,监听到请求后返回Socket: Socket用户具体完成数据 ...
- EFCodeFirst 各种命令整理
1.Enable-Migrations (创建迁移目录:Migrations,如果有多个数据上下文可以用 -ContextTypeName 命令迁移对应的数据上下文 ) 2.Add-Migratio ...
- Web程序中使用EasyUI时乱码问题
今天偶然遇见使用easyUI时,弹窗和分页都是乱码的问题,耗费了很长的时间来解决,以此记住这个坑. 相信大家都会在使用easyUI时都会设置这样一句: 那么就有可能出现设置中文后的乱码问题,如下图: ...
- XML深入了解(XML JavaSprint)
XMLHttpRequest 对象 XMLHttpRequest 对象用于在后台与服务器交换数据. XMLHttpRequest 对象是开发者的梦想,因为您能够: 在不重新加载页面的情况下更新网页 在 ...