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操作减少修改的次数( ...
随机推荐
- MyBatis 3 分页
利用MyBatis 3进行分页,选定的数据库表c_province,有3列,id列,provinceid列,province列,用Oracle数据库.首先建立一个对应的实体类,Province有3个属 ...
- bootspring + mybaits +mysql Date 类型的处理
mysql 中有date 类型的属性,java实体类中对应的属性是java.sql.Date 类的. 最初的bug是怎么新增,joinDate 值都是null. 千辛万苦学会了用String转Date ...
- python接口自动化-有token的接口项目使用unittest框架设计
获取token 在做接口自动化的时候,经常会遇到多个用例需要用同一个参数token,并且这些测试用例跨.py脚本了. 一般token只需要获取一次就行了,然后其它使用unittest框架的测试用例全部 ...
- JS获取所有LI中第三个<SPAN>
- 624. Maximum Distance in Arrays
Problem statement Given m arrays, and each array is sorted in ascending order. Now you can pick up t ...
- 45个android实例源码分享
分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html andriod闹钟源代码 http://www.apkbus.c ...
- 神秘数(bzoj 4408)
Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13}, 1 = 1 2 = 1+1 3 = 1+1+1 4 = 4 5 = ...
- 汕头市赛srm8 C-3
n<=100000个点m<=300000条边有权无向联通图,给出K<=10000个特殊点求K个点中任意两点最短路的最小值. 方法一:K小,随便搞.先构造最短路树,在最短路树上Dijk ...
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings [dp 前缀和 ]
传送门 D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 me ...
- msp430项目编程02
msp430中项目---液晶1602显示 1.液晶1602工作原理 2.电路原理说明 3.代码(静态显示) 4.代码(动态显示) 5.项目总结 msp430项目编程 msp430入门学习