ZOJ-1610 Count the Colors(线段树染色,求染色段)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
https://vjudge.net/contest/318019#problem/F
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
Sample Output
这题题意不太好懂
题目大意:
在范围[0,8000]内,给n个操作,把[x,y]段染成颜色c,注意是线段不是点,最后按颜色从小到大输出每种颜色连续的线段的个数,有颜色的才输出;
先给出我参考的博客
https://www.cnblogs.com/kuangbin/archive/2012/08/10/2631449.html
https://www.cnblogs.com/thunder-110/p/10302782.html
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
const int maxn=1e5+;
using namespace std;
//ios::sync_with_stdio(false);
// cin.tie(NULL); int n;
int last;//flag判断相邻两个区间的颜色是否相同
struct node
{
int l;
int r;
int color;
}SegTree[<<]; int ans[<<]; void PushDown(int rt)
{
if(SegTree[rt].color>)
{
SegTree[rt<<].color=SegTree[rt].color;
SegTree[rt<<|].color=SegTree[rt].color;
SegTree[rt].color=-;
}
} void Build(int l,int r,int rt)
{
SegTree[rt].l=l;
SegTree[rt].r=r;
SegTree[rt].color=;//多样例时必须加
if(l==r)
{
return;
}
int mid=(l+r)>>;
Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
} void Update(int L,int R,int C,int rt)
{
int l=SegTree[rt].l;
int r=SegTree[rt].r;
if(L<=l&&R>=r)
{
SegTree[rt].color=C;
return ;
}
PushDown(rt);//向下更新lazy
int mid=(l+r)>>;
if(L<=mid)//当寻找一个区间的时候,路径上的值全改成C
Update(L,R,C,rt<<);
if(R>mid)//当寻找一个区间的时候,路径上的值全改成C
Update(L,R,C,rt<<|);
SegTree[rt].color=-;//寻找到了之后,把回头的路径全部改成-1,说明如果顺着这些点下来,一定能找到区间
} void Query(int rt)
{
if(last==SegTree[rt].color)
return;
if(SegTree[rt].color==)//一次也没有被涂过
{
last=;
return;
}
if(SegTree[rt].color>)
{
if(last!=SegTree[rt].color)//不是同一块海报
{
last=SegTree[rt].color;
ans[SegTree[rt].color]++;
}
return;
}
//接下来是如果SegTree[rt].color== -1 ,表示顺着这个点 一定能找到区间
Query(rt<<);
Query(rt<<|);
} int main()
{
while(~scanf("%d",&n))
{
memset(ans,,sizeof(ans));
Build(,,);
for(int i=;i<=n;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
Update(a,b-,c+,);
}
Query();
for(int i=;i<=;i++)
{
if(ans[i])
printf("%d %d\n",i-,ans[i]);
}
printf("\n");
}
return ;
}
暴力解法:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<stack>
#include<ctype.h>
#include<stdlib.h>
#include<map>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int M=;
int main()
{
int n,t[M];
while(~scanf("%d",&n))
{
memset(t,-,sizeof(t));
int j,i,a,b,c,max1=-,max2=-;
int ans[M]= {};
for(i=; i<n; i++)
{
scanf("%d %d %d",&a,&b,&c);
max1=max(max1,b);//记录染有颜色的最右端
max2=max(max2,c);//记录染颜色的最大数值 for(j=a+; j<=b; j++)//注意这里,题目中的说的是线段,所以我们每次不标记最左端
t[j]=c;
}
for(i=; i<max1; i++)
{
if(t[i]!=t[i+]&&t[i]!=-)
ans[t[i]]++;
}
ans[t[max1]]++;
for(i=; i<=max2; i++)
{
if(ans[i]>)
printf("%d %d\n",i,ans[i]);
}
printf("\n");
}
return ;
}
ZOJ-1610 Count the Colors(线段树染色,求染色段)的更多相关文章
- 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 (线段树成段更新)
题意 : 给出 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: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)
题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=1610 题意:给一个长8000的绳子,向上染色.一共有n段被染色,问染 ...
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
- Count the Colors(线段树,找颜色段条数)
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】
任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...
- ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
随机推荐
- Maven - pom.xml 文件
章节 Maven – 简介 Maven – 工作原理 Maven – Repository(存储库) Maven – pom.xml 文件 Maven – 依赖管理 Maven – 构建生命周期.阶段 ...
- C++ 模板练习1
//特定的模板友元关系 #include "stdafx.h" #include <iostream> using namespace std; template< ...
- Windbg 大改版,值得期待
早上从twitter上面看到一篇文章,看到windbg会提供一个Time Travel Debugging(TTD) 功能,该功能会在未来的版本引入. Time travel debugging: I ...
- 文献阅读报告 - Social LSTM:Human Trajectory Prediction in Crowded Spaces
概览 简述 文献所提出的模型旨在解决交通中行人的轨迹预测(pedestrian trajectory prediction)问题,特别是在拥挤环境中--人与人交互(interaction)行为常有发生 ...
- MySQL高负载优化
MySQL配置文件优化 [client] port = #客户端端口号为3306 socket = /data//mysql.sock # default-character-set = utf8 # ...
- html+css新特性
audio 音频 viedeo 视频 <audio src = ""02.pogg" controls = "controls"> lo ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 运算符
要介绍 MySQL 的运算符及运算符的优先级. MySQL 主要有以下几种运算符: 算术运算符 比较运算符 逻辑运算符 位运算符 算术运算符 MySQL 支持的算术运算符包括: 在除法运算和模运算中, ...
- 苹果智能AR挡风玻璃靠谱吗?
在过去十年,外界给苹果的形象一直是"伟大的硬件公司",他们的产品在外观方面往往比内涵更加引人注目,兼具娱乐性和艺术性, iPhone/iPad/iPod莫不如此,所以,当坊间传闻苹 ...
- 用Emoji和照片挑战大众点评,YOBO玩转新点评方式能引领潮流吗?
对于一家企业来说,要想获得长久生命力的必备元素是什么?是技术底蕴和海量资金?但诺基亚.摩托罗拉和黑莓等巨头的崩塌,已经证明再稳固的基础都有可能只是沙子做的.是让人工智能.云计算.大数据等前沿技术赋能于 ...
- spring容器抽象的具体实现
1.BeanFactory 接口与 ApplicationContext 接口 (1)spring 提供了两种类型的IOC容器实现.BeanFactory 和 ApplicationContext ( ...