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. zip filter map 列表生成器

    map map(function, list): 就是对list 中的每一个元素都调用function函数进行处理,返回一个map的对象 list一下就可以生成一个列表 或者for循环该对象就可以输出 ...

  2. Django之stark组件1

    stark组件 stark组件是根据Django admin为原型写的一个组件,能够让我们告别增删改查.stark组件是可插拔试的组件, 移植性强,而且只用配置文件就能够得到想要的数据 一.stark ...

  3. 我的Android进阶之旅------>Android之动画之Frame Animation实例

    ============================首先看看官网上关于Frame animation的介绍================================ 地址:http://de ...

  4. PAT 1059. C语言竞赛(20)

    C语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛.既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽: 0. 冠军将赢得一份“神秘大奖”(比如很巨大的一本学生研究论文集……). 1. 排名为素数的学生 ...

  5. 如何使用Django实现用户登录验证

    最初开始搞用户登录验证的时候感觉没什么难的,不就是增删改查中的查询数据库么,但是还是遇到许多小问题,而且感觉在查询数据库的时候,要把前端的数据一条一条的进行比对,会导致我的代码很丑,而且方式很不智,所 ...

  6. IE下获取不到Response添加的cookie的解决方法

    原博客地址: http://blog.csdn.net/wjdd1/article/details/16341189 在百度上查询了好久也没有查询到结果,于是自己用ie的开发者工具进行跟踪,JSESS ...

  7. 使用微软官方U盘制作软件来安装纯净版windows

    第一步:下载一个制作U启的工具;windows-usb-dvd-download-tool 微软官网:https://www.microsoft.com/en-us/download/windows- ...

  8. Windows存储管理之磁盘结构详解

    Windows磁盘结构: Windows的主流磁盘结构分为MBR和GPT两种.MBR是早期Windows的唯一选择,但是随着物理磁盘的容量不断增大.GPT结构成为目前的主流,最大支持超过2TB的容量, ...

  9. LeetCode:最长回文子串【5】

    LeetCode:最长回文子串[5] 题目描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: ...

  10. 图片加载Picasso

    https://github.com/square/picasso 基本用法 // 基本用法 // 普通加载图片 Picasso.with(PicassoActivity.this) .load(&q ...