ZOJ1610 Count the Colors —— 线段树 区间染色
题目链接:https://vjudge.net/problem/ZOJ-1610
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.
<b< dd="">
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.
<b< dd="">
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
<b< dd="">
Sample Output
1 1
2 1
3 1
1 1
0 2
1 1
题解:
问最终用多少段颜色相同的区域。经典的区间染色问题。
写法一:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; int val[MAXN<<];
int num[MAXN], color[MAXN]; void push_down(int u)
{
if(val[u]>=)
{
val[u*] = val[u*+] = val[u];
val[u] = -;
}
} void set_val(int u, int l, int r, int x, int y, int _val)
{
if(x<=l && r<=y)
{
val[u] = _val;
return;
} push_down(u);
int mid = (l+r)>>;
if(x<=mid) set_val(u*, l, mid, x, y, _val);
if(y>=mid+) set_val(u*+, mid+, r, x, y, _val);
} void query(int u, int l, int r)
{
if(l==r)
{
color[l] = val[u];
return;
} push_down(u);
int mid = (l+r)>>;
query(u*, l, mid);
query(u*+, mid+, r);
} int main()
{
int m;
while(scanf("%d", &m)!=EOF)
{
memset(val, -, sizeof(val));
for(int i = ; i<=m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
if(x<y) set_val(, , , x+, y, z);
} query(, , );
memset(num, , sizeof(num));
for(int i = ; i<=; i++)
if(color[i]!=- && (i== || color[i]!=color[i-]))
num[color[i]]++; for(int i = ; i<=; i++)
if(num[i])
printf("%d %d\n", i, num[i]);
printf("\n");
}
}
写法二:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; int val[MAXN<<];
int num[MAXN]; void push_down(int u)
{
if(val[u]>=)
{
val[u*] = val[u*+] = val[u];
val[u] = -;
}
} void set_val(int u, int l, int r, int x, int y, int _val)
{
if(x<=l && r<=y)
{
val[u] = _val;
return;
} push_down(u);
int mid = (l+r)>>;
if(x<=mid) set_val(u*, l, mid, x, y, _val);
if(y>=mid+) set_val(u*+, mid+, r, x, y, _val);
} int pre;
void query(int u, int l, int r)
{
if(l==r)
{
if(val[u]>= && val[u]!=pre)
num[val[u]]++;
pre = val[u];
return;
} push_down(u);
int mid = (l+r)>>;
query(u*, l, mid);
query(u*+, mid+, r);
} int main()
{
int m;
while(scanf("%d", &m)!=EOF)
{
memset(val, -, sizeof(val));
for(int i = ; i<=m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
if(x<y) set_val(, , , x+, y, z);
} memset(num, , sizeof(num));
pre = -;
query(, , );
for(int i = ; i<=; i++)
if(num[i])
printf("%d %d\n", i, num[i]);
printf("\n");
}
}
ZOJ1610 Count the Colors —— 线段树 区间染色的更多相关文章
- [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)
题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=1610 题意:给一个长8000的绳子,向上染色.一共有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 Colors 线段树区间更新/暴力
Count the Colors Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
- ZOJ-1610 Count the Colors ( 线段树 )
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Description Painting some co ...
- 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 ZOJ - 1610 传送门 线段树区间染色求染色的片段数 #include <cstdio> #include <iostream> #in ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- HDU3974 Assign the task(多叉树转换为线段+线段树区间染色)
题目大意:有n个人,给你他们的关系(老板和员工),没有直属上司的人就是整个公司的领导者,这意味着n个人形成一棵树(多叉树).当一个人被分配工作时他会让他的下属也做同样的工作(并且立即停止手头正在做的工 ...
- hdu 5023(线段树区间染色,统计区间内颜色个数)
题目描述:区间染色问题,统计给定区间内有多少种颜色? 线段树模板的核心是对标记的处理 可以记下沿途经过的标记,到达目的节点之后一块算,也可以更新的时候直接更新到每一个节点 Lazy操作减少修改的次数( ...
随机推荐
- Redis 主从复制与哨兵
Redis 可以使用从属服务器来实现读写分离提高吞吐量或在主服务器故障时接替主服务器以提高可用性. 每个 Redis 服务器实例都可以配置多个 slave 节点,slave 服务器也可以拥有次级 sl ...
- Device eth0 does not seem to be present,delaying initialization问题
1.打开/etc/udev/rules.d/70-persistent-net.rules: cat /etc/udev/rules.d/70-persistent-net.rules 文件内容如图: ...
- [WPF自定义控件]Window(窗体)的UI元素及行为
1. 前言 本来打算写一篇<自定义Window>的文章,但写着写着发觉内容太多,所以还是把使用WindowChrome自定义Window需要用到的部分基础知识独立出来,于是就形成了这篇文章 ...
- MyISAM和InnoDB索引实现对比
MyISAM索引实现 MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址.如图: 这里设表一共有三列,假设我们以Col1为主键,则上图是一个MyISAM表的主索引 ...
- cf839c Journey
大水题 #include <iostream> #include <cstdio> using namespace std; int n, du[100005], hea[10 ...
- 大数据学习——mapreduce共同好友
数据 commonfriends.txt A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I D:A,E,F,L E:B,C,D,M,L F:A,B,C,D,E,O,M G:A,C,D ...
- 在C#代码中应用Log4Net系列教程(附源代码)地址
在博客园看到一篇关于Log4Net使用教程,比较详细,感谢这位热心的博主 博客园地址:http://www.cnblogs.com/kissazi2/archive/2013/10/29/339359 ...
- 公钥加密算法那些事 | RSA 与 ECC 系统对比
一.背景 据记载,公元前 400 年,古希腊人发明了置换密码.1881 年世界上的第一个电话保密专利出现.在第二次世界大战期间,德国军方启用「恩尼格玛」密码机,密码学在战争中起着非常重要的作用. 随着 ...
- NOI导刊2010提高(06) 黑匣子
题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两种: ...
- <转>C#中线程的学习
原文发布时间为:2008-11-15 -- 来源于本人的百度文章 [由搬家工具导入] http://hi.baidu.com/cyap/blog/category/%B6%E0%CF%DF%B3%CC ...