[ZOJ1610]Count the Colors
Description
画一些颜色段在一行上,一些较早的颜色就会被后来的颜色覆盖了。
你的任务就是要数出你随后能看到的不同颜色的段的数目。
Input
每组测试数据第一行只有一个整数n, 1 <= n <= 8000,等于填色的次数
接下来的n行每行有三个非负整数,他们之间用一个空格分开。
x1 x2 c
x1和x2表示填色段最左边的点和最右边的点, c表示填进的颜色。
所有数字都是在[0..8000]这个范围里的整数。
输入有多组测试数据,最后以文件结束符为结束。
Output
输出的每一行都是最后能看到的颜色的数字,还有这种颜色的段数。
如果这种颜色最后不能见到,就不要打印出来。
每组数据后面跟一个空行。
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
线段树维护当前节点的颜色,如果颜色相同则为一个颜色,颜色不同为-1,无颜色为-2,最后统计的时候先序遍历递归即可
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=8e3;
int tree[N*5+100],kind[N*5+100];
int cnt;
int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*f;
}
void updata(int p){//标记下传更新
if (tree[p]>=0){
tree[p*2]=tree[p];
tree[p*2+1]=tree[p];
}
tree[p]=-2;
}
void change(int p,int l,int r,int x,int y,int t){
if (tree[p]==t) return;//剪枝
if (x<=l&&r<=y){
tree[p]=t;
return;
}
updata(p);
int mid=(l+r)>>1;
if (x<mid) change(p*2,l,mid,x,y,t);
if (y>mid) change(p*2+1,mid,r,x,y,t);
}
void get(int p){//统计答案
if (tree[p]>=0){
if (tree[p]!=cnt){
kind[tree[p]]++;
cnt=tree[p];
}
return;
}
if (tree[p]==-1){cnt=-1;return;}
get(p*2);get(p*2+1);
}
int main(){
int n;
while (~scanf("%d",&n)){
memset(tree,255,sizeof(tree));
memset(kind,0,sizeof(kind));
for (int i=1;i<=n;i++){
int x=read(),y=read(),t=read();
change(1,0,N,x,y,t);
}
cnt=-1;
get(1);
for (int i=0;i<=N;i++) if (kind[i]) printf("%d %d\n",i,kind[i]);
printf("\n");
}
return 0;
}
[ZOJ1610]Count the Colors的更多相关文章
- ZOJ1610 Count the Colors —— 线段树 区间染色
题目链接:https://vjudge.net/problem/ZOJ-1610 Painting some colored segments on a line, some previously p ...
- 线段树专题—ZOJ1610 Count the Colors(涂区间,直接tag标记)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- ZOJ-1610 Count the Colors ( 线段树 )
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Description Painting some co ...
- ZOJ-1610 Count the Colors(线段树染色,求染色段)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 https://vjudge.net/contest/318019# ...
- [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)
题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=1610 题意:给一个长8000的绳子,向上染色.一共有n段被染色,问染 ...
- 线段树专题—ZOJ1610 Count the Colors
题意:给一个n,代表n次操作,接下来每次操作表示把[l.r]区间的线段涂成k的颜色当中,l,r,k的范围都是0到8000 分析:事实上就是拿线段树维护一段区间的颜色,整体用到的是线段树的区间更新把,可 ...
- Count the Colors(线段树染色)
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- zoj 1610 Count the Colors
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610 Count the Colors Time Limit:2000MS ...
随机推荐
- MySQL入门笔记 - 视图
参考书籍<MySQL入门很简单> 1.视图定义 视图是从一个或者多个表中导出来的虚拟的表,透过这个窗口可以看到系统专门提供的数据,使用户可以只关心对自己有用的数据,方便用户对数据操作,同时 ...
- [转]LINUX新建和增加SWAP分区
以前做过增加swap分区的事情,今天一个同事问到我如何做,故记个笔记整理一下吧.另外,以前我写过“交换分区swap的大小分配”,大家也可先看一下. 我们都知道在安装Linux系统时在分区时可以分配sw ...
- CentOS里route命令详解
Route 功能简述:linux系统中的route命令能够用于IP路由表的显示和操作.它的主要作用是创建一个静态路由让指定一个主机或者一个网络通过一个网络接口,如eth0.当使用"add&q ...
- libevent API 介绍
基本应用场景也是使用 libevnet 的基本流程,下面来考虑一个最简单的场景,使用livevent 设置定时器,应用程序只需要执行下面几个简单的步骤即可. 1)首先初始化 libevent 库,并保 ...
- JSP中的编译指令和动作指令的差别
JSP中的编译指令和动作指令的差别 1.编译指令是通知Servlet引擎的处理消息.而动作指令仅仅是执行时的脚本动作 2.编译指令是在将JSP编译成Servlet时起作用,而动作指令可替换成JSP脚本 ...
- oracle随机数
1.从表中随机取记录 select * from (select * from staff order by dbms_random.random) where rownum < 4 表示从ST ...
- 【转载】TCP和TCP/IP的区别
TCP/IP协议(Transmission Control Protocol/Internet Protocol)叫做传输控制/网际协议, 又叫网络通讯协议,这个协议是Internet国际互联网络的基 ...
- 安装SQLserver2008时出现的错误
1.SQLserver2008提示必须重新启动计算机才干够继续安装.解决方法例如以下: 在開始->执行中输入regedit,到HKEY_LOCAL_MACHINE\SYSTEM\CurrentC ...
- POJ3159 Candies —— 差分约束 spfa
题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS Memory Limit: 131072K Total Submiss ...
- 织梦发布的文章如何批量替换文章"来源"和"作者"?
做的网站中已经采集好并已生成HTML了的文章或以前已发布的文章如何快速批量替换所有“来源”和“作者”呢?第一步:打开:dede模板网站(后台目录)\templets\article_add.htm ( ...