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操作减少修改的次数( ...
随机推荐
- bootspring网站项目,Date类型插入数据库始终比正确时间早一天问题的解决
bug描述 昨天的Date插入不进去问题解决后,一直没发现其实插入的时间一直比正确的时间早一天 输出sql语句,发现insert语句还是对的,不知道为什么插入数据库之后结果就早了一天 https:// ...
- wps左侧显示目录
单击视图----文档结构图,在下拉选项中选择靠左即可,如图所示
- [codevs1050]棋盘染色 2
[codevs1050]棋盘染色 2 试题描述 有一个5*N的棋盘,棋盘中的一些格子已经被染成了黑色,你的任务是对最少的格子染色,使得所有的黑色能连成一块. 输入 第一行一个整数N(<=100) ...
- POJ 1904:King's Quest【tarjan】
题目大意:给出一个二分图的完美匹配(王子和公主的烧死名单表),二分图x部和y部均只有n个点,问对于每一个x部的点,他能选择哪些点与之匹配 使得与之匹配后,剩余图的最大匹配仍然是n 思路:这题是大白书3 ...
- HDU3572:Task Schedule【最大流】
上了一天课 心塞塞的 果然像刘老师那么说 如果你有挂科+4级没过 那基本上是WF队 题目大意:有时间补吧 思路:给每个任务向每个时间点连边容量为1 每个时间点向汇点连边 容量为机器的个数 源点向每个任 ...
- Foundation框架的一些实用方法:替换字符串,去空格,反转
//定义一个可变字符串, Format后面可以跟字符串类型,也可以传入C语言的字符串数组 NSMutableString *str = [NSMutableString stringWithForma ...
- BZOJ 4894 有向图 外向生成树个数
4894: 天赋 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 191 Solved: 150[Submit][Status][Discuss] D ...
- hdu - 5007 Post Robot (水题)
http://acm.hdu.edu.cn/showproblem.php?pid=5007 #include<iostream> #include<stdio.h> #inc ...
- Maven使用tomcat7-maven-plugin插件run时出现错误: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component
错误如下: A child container failed during startjava.util.concurrent.ExecutionException: org.apache.catal ...
- sql 语句哪里添加单引号问题
1.sql 语句哪里添加单引号问题,哪些地方必须加双引号,否则sql语句会报错? :涉及varchar的值的时候,必须有单引号包括varchar值.int等其他字段类型,则不需要加单引号包括. 如: ...