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]这个区间内每 ...
随机推荐
- spring对异步的支持
spring中异步方法的配置 1.在web.xml文件中设置org.springframework.web.servlet.DispatcherServlet的async-supported属性为tr ...
- js动态实现时分秒
<div id="time" style="color: #96C2DD;</div> <script type="text/ ...
- Python归纳 | 爬虫基础知识
1. urllib模块库 Urllib是python内置的HTTP请求库,urllib标准库一共包含以下子包: urllib.error 由urllib.request引发的异常类 urllib.pa ...
- 数据库和AI的一次火花
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由宗文 发表于云+社区专栏 | 导语 通过历史数据,基于时间序列来预测未来. 我们生活中很多数据是有时间维度的.比如说天气或者股票价格. ...
- nyoj 211——Cow Contest——————【floyd传递闭包】
Cow Contest 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1.. ...
- SQL Server 数据库定时自动备份(转)
本文转载自:http://www.cnblogs.com/zhangq723/archive/2012/03/13/2394102.html 作者:清风寻梦 在SQL Server中出于数据安全的考虑 ...
- 2017年10月30日 vs初级教学
Console.Write("Hello World!"); / / 插入 Hello World! Console.WriteLine("Hell ...
- 自己动手实现STL 01:内存配置器的实现(stl_alloc.h)
一.前言 在STL中,容器是其中的重中之重,基本的STL中的算法,仿函数等都是围绕着容器实现的功能.而,内存配置器,是容器的实现的基础.所以,我第一次要去编写便是内存配置器的实现.在STL中,内存配置 ...
- SQL:exec sp_executesql 用法
--這種是無效的過程 declare @sql nvarchar(500), @where nvarchar(500),@i nvarchar(64),@p nvarchar(50),@id int ...
- 原生js实现星星闪烁的效果
星星闪烁的原理其实很简单: html代码: <body style="background:#000"> <div id="stars_box" ...