Description

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

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

#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
struct node{
int r,l;
int color;
}a[MAXN*]; void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].color=-;//-1表示没有涂色
if(l==r) return ;
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
} void update(int rt,int l,int r,int val)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].color=val;
return ;
} if(a[rt].color>=)//color>=0表示子树均为一个颜色,lazy思想
{
a[rt<<].color=a[(rt<<)|].color=a[rt].color;
} int mid=(a[rt].l+a[rt].r)>>; if(r<=mid) update(rt<<,l,r,val);
else if(mid<l) update((rt<<)|,l,r,val);
else{
update(rt<<,l,mid,val);
update((rt<<)|,mid+,r,val);
}
a[rt].color=-;//-2表示子树为多种颜色
} int Color[MAXN];
int temp;
void query(int rt)
{
if(a[rt].color==-)
{
temp=-;
return ;
}
if(a[rt].color!=-)
{
if(temp!=a[rt].color)
{
Color[a[rt].color]++;
temp=a[rt].color;
}
return ;
} if(a[rt].l==a[rt].r) return ;
int mid=(a[rt].l+a[rt].r)>>;
query(rt<<);
query((rt<<)|);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int maxn=-;
temp=-;
memset(Color,,sizeof(Color));
build(,,);
while(n--)
{
int l,r,c;
scanf("%d%d%d",&l,&r,&c);
update(,l+,r,c);
if(c>maxn) maxn=c;
}
query();
for(int i=;i<=maxn;i++)
{
if(Color[i]) printf("%d %d\n",i,Color[i]);
}
printf("\n");
} }

ZOJ1610(经典线段树涂色问题)的更多相关文章

  1. POJ2777(线段树涂色问题)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42828   Accepted: 12973 Des ...

  2. ZOJ - 1610 经典线段树染色问题

    这个是一个经典线段树染色问题,不过题目给的是左右左右坐标,即[0,3]包含0-1这一段 1-2这一段 2-3这一段,和传统的染色不太一样,不过其实也不用太着急. 我们把左边的坐标+1,即可,那么[0, ...

  3. zoj1610(线段树)

    题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 题意:在0-8000长的线段里面,按先后次序依次覆盖颜色, ...

  4. pku 2777(经典线段树染色问题)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41202   Accepted: 12458 Des ...

  5. 经典线段树 UVALive 3938/UVA 1400

    题意:就是相当于动规里面的求最大连续子串,不同的是,这里需要读入一个区间x,y,输出的区间 a,b 且x<=a<=b<=y,使得a b的连续子串最长,而且询问次数达到了10的五次方. ...

  6. poj-2828 Buy Tickets(经典线段树)

    /* Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 10207 Accepted: 4919 Descr ...

  7. poj 2528 poster经典线段树+lazy+离散化

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; #def ...

  8. 【ACM/ICPC2013】线段树题目集合(一)

    前言:前一段时间在网上找了一个线段树题目列表,我顺着做了一些,今天我把做过的整理一下.感觉自己对线段树了解的还不是很深,自己的算法能力还要加强.光练代码能力还是不够的,要多思考.向队友学习,向大牛学习 ...

  9. 树链剖分+线段树 HDOJ 5029 Relief grain(分配粮食)

    题目链接 题意: 分粮食我就当成涂色了.有n个点的一棵树,在a到b的路上都涂上c颜色,颜色可重复叠加,问最后每一个点的最大颜色数量的颜色类型. 思路: 首先这题的输出是每一个点最后的情况,考虑离线做法 ...

随机推荐

  1. 【python】-- 信号量(Semaphore)、event(红绿灯例子)

    信号量(Semaphore) 之前讲的线程锁(互斥锁) 同时只允许一个线程更改数据,而Semaphore是同时允许一定数量的线程更改数据 ,比如厕所有3个坑,那最多只允许3个人上厕所,后面的人只能等里 ...

  2. 20179209《Linux内核原理与分析》安全类实验答疑

    实验一 题目 Nmap 配合 Metasploit 进行端口扫描 问题 Nmap怎么配合Metasploit进行端口扫描? 回答 这里的Nmap配合Metasploit进行端口扫描是指在Metaspl ...

  3. BZOJXXXX: [IOI2000]邮局——四边形不等式优化初探

    貌似$BZOJ$上并没有这个题... 是嫌这个题水了么... 还是要氪金权限号??? 这里附上洛谷的题面:洛谷P4767 [IOI2000]邮局 题目描述 高速公路旁边有一些村庄.高速公路表示为整数轴 ...

  4. Python整型int、浮点float常用方法

    #!/usr/bin/env python # -*- coding:utf-8 -*- # Python整型int.浮点float # abs(x) # 返回数字的绝对值,如abs(-10) 返回 ...

  5. l如何把SQLServer表数据导出CSV文件,并带列名

    http://jingyan.baidu.com/article/4b07be3c466b5d48b280f37f.html 微信公众号:

  6. MySQL一些常见查询方式

    1.查询端口号命令: show global variables like 'port'; 2.查看版本号: select version(); 3.查看默认安装的MySQL的字符集 show var ...

  7. 【整理学习Hadoop】Hadoop学习基础之一:服务器集群技术

            服务器集群就是指将很多服务器集中起来一起进行同一种服务,在客户端看来就像是只有一个服务器.集群可以利用多个计算机进行并行计算从而获得很高的计算速度,也可以用多个计算机做备份,从而使得任 ...

  8. 改善程序与设计的55个具体做法 day3

    条款07:为多态基类声明virtual析构函数 任何一本C++语法教材上都会讲这一点(如果没讲,扔掉它),这么做到原因是可以让delete pBase操作能够正确的执行子类的析构函数. 需要说明的是当 ...

  9. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  10. 简化Hadoop命令

    1. 安装客户端(通过端用户可以方便的和集群交互) 2. 简化Hadoop命令 修改~/.bashrcalias hadoop='/home/work/hadoop/client/hadoop-cli ...