F - Count the Colors(线段树)
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.
输入:
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.
输出:
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
题意:这个题目和D - Mayor’s posters这个题目很相似,只是这个不需要映射,而那个题目需要
要注意的就是:我们的线段树只能存点,我们不能把一个[1,4]区间用四个点去存放,因为实际上区间长度为3,如果我们用[1,4]内所有点去存就表示长度为三
解决方法:
我们只需要再给出的区间在其左边界加一,或是在右边界减一从而使其区间中的点变得和区间长度一样
上代码;
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=8005;
struct node
{
int l,r,sum;
}a[maxn<<2];
int x1[maxn],x2[maxn],c[maxn];
int mark[maxn];
int sum[maxn];
int lenn;
void build(int o,int l,int r)
{
a[o].l=l,a[o].r=r,a[o].sum=-1;
int ls=o<<1,rs=o<<1|1,mid=(l+r>>1);
if(l==r)
{
a[o].sum=-1;
return ;
}
build(ls,l,mid);
build(rs,mid+1,r);
}
void pushdown(int o)
{
if(a[o].sum!=-1)
{
a[o<<1].sum=a[o].sum;
a[o<<1|1].sum=a[o].sum;
a[o].sum=-1;
}
}
void update(int o,int l,int r,int c)
{
int ls=o<<1,rs=o<<1|1,mid=(a[o].l+a[o].r)>>1;
if(l<=a[o].l&&a[o].r<=r)
{
a[o].sum=c;
return ;
}
pushdown(o);
if(l<=mid) update(ls,l,r,c);
if(r>mid) update(rs,l,r,c);
}
void query(int o,int l,int r)
{
int ls=o<<1,rs=o<<1|1,mid=(a[o].l+a[o].r)>>1;
if(a[o].l==a[o].r)
{
mark[lenn++]=a[o].sum;
return ;
}
pushdown(o);
if(l<=mid) query(ls,l,r);
if(r>mid) query(rs,l,r);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int maxx=0;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&x1[i],&x2[i],&c[i]);
int x=max(x1[i]+1,x2[i]);
maxx=max(maxx,x);
}
memset(mark,-1,sizeof(mark));
build(1,1,maxx);
for(int i=0;i<n;i++) update(1,x1[i]+1,x2[i],c[i]);
lenn=0;
query(1,1,maxx);
int x;
memset(sum,0,sizeof(sum));
for(int i=0;i<lenn;)
{
if(mark[i]==-1)
{
i++;
continue;
}
x=mark[i];
while(x==mark[++i]&&i<lenn) ;
sum[x]++;
}
for(int i=0;i<=maxx+10;i++)
{
if(sum[i]!=0)
printf("%d %d\n",i,sum[i]);
}
printf("\n"); /注意不要忘了最后还有一个换行
}
}
F - Count the Colors(线段树)的更多相关文章
- 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 ...
- [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)
题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=1610 题意:给一个长8000的绳子,向上染色.一共有n段被染色,问染 ...
- 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 ( 线段树 )
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Description Painting some co ...
- ZOJ1610 Count the Colors —— 线段树 区间染色
题目链接:https://vjudge.net/problem/ZOJ-1610 Painting some colored segments on a line, some previously p ...
- Count the Colors 线段树
题目 参考博客地址 题意: n范围[1,8000] , li 和 ri 的范围[0,8000]. n个操作,每个操作是把 [li , ri]内的点修改成一个颜色c. n个操作过后,按颜色从小到大 ...
- F - Count the Colors
F - Count the Colors ZOJ - 1610 思路:调了一个小时,但是发现自己线段树木有写错,颜色统计出了错误.但是不明白自己颜色统计为什么错了. 求大佬指点迷津.思路很简单,就 ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
随机推荐
- 【C++】《Effective C++》第九章
杂项讨论 条款53:不要轻忽编译器的警告 请记住 严肃对待编译器发出的警告信息.努力在你的编译器的最高(最严苛)警告级别下争取"无任何警告"的容易. 不要过度依赖编译器的报警能力, ...
- Linux学习笔记 | 配置Samba
Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通 ...
- pidof
pidof 服务名称,就可以查看到服务占用的进程号
- 【Zabbix】配置zabbix agent向多个server发送数据
1.背景: server端: 172.16.59.197 ,172.16.59.98 agent 端: hostname:dba-test-hzj02 IP:172.16.59.98 2.方式: 配 ...
- Golang应用性能问题排查分析
背景 公司有一个使用golang开发的采集模块,负责调用多个外部系统采集数据:最近做了一次架构上的调整,将采集模块分成api.job两个子模块,并部署到容器中,拆分前部署在虚机上. 现象 部分采集任务 ...
- MongoDB查询优化--explain,慢日志
引入 与Mysql数据库一样,MongoDB也有自己的查询优化工具,explain和慢日志 explain shell命令格式 db.collection.explain().<method(. ...
- LSTM+CRF进行序列标注
为什么使用LSTM+CRF进行序列标注 直接使用LSTM进行序列标注时只考虑了输入序列的信息,即单词信息,没有考虑输出信息,即标签信息,这样无法对标签信息进行建模,所以在LSTM的基础上引入一个标签转 ...
- 前端工程构建之谈:gulp3要不要升级到Gulp4
关于升级还是不升级,这是一个哲学问题. gulp4的语法更加现代,支持ES6的大部分写法,使用exports的方式去暴露任务组合,更加灵活和便捷. gulp4同时也提供了很多强大的API,例如para ...
- 时序数据库 Apache-IoTDB 源码解析之元数据索引块(六)
上一章聊到 TsFile 索引块的详细介绍,以及一个查询所经过的步骤.详情请见: 时序数据库 Apache-IoTDB 源码解析之文件索引块(五) 打一波广告,欢迎大家访问 IoTDB 仓库,求一波 ...
- 【Coredump】调试之旅
测试反馈,core了. 拿到环境,发现6和11,一个是重复释放,一个是非法指针. 用GDB一挂 ,发现 1 GNU gdb (GDB) 7.5 2 Copyright (C) 2012 Free So ...