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. A vectorized example

    http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture4.pdf

  2. 学院名单-211院校研招学院-中国教育在线(www.eol.cn)170915164402

    [数据结果] 学校数.学院数:112,2657. [数据来源] 中国教育在线(www.eol.cn)211院校研招学院. http://www.eol.cn/html/ky/gxmd/211.shtm ...

  3. python+NLTK 自然语言学习处理七:N-gram标注

    在上一章中介绍了用pos_tag进行词性标注.这一章将要介绍专门的标注器. 首先来看一元标注器,一元标注器利用一种简单的统计算法,对每个标识符分配最有可能的标记,建立一元标注器的技术称为训练. fro ...

  4. activiti 基础搭建

    首先在eclipse内添加activiti的插件,https://blog.csdn.net/qq_22701869/article/details/79537971 1.创建一个maven的java ...

  5. Struts2之ModelDriven的使用

    http://www.cnblogs.com/luoyanli/archive/2012/11/20/2778361.html 我们可以根据Action属性的不同将它分为两类:Field-Driven ...

  6. linux awk数组相关操作介绍

    用awk进行文本处理,少不了就是它的数组处理.那么awk数组有那些特点,一般常见运算又会怎么样呢.我们先看下以下的一些介绍,结合样例我们会解说下它的不同之处.在 awk 中数组叫做关联数组(assoc ...

  7. 《程序员代码面试指南》第二章 链表问题 将单链表每K个节点之间逆序

    样例 链表1-2-3-4-5-6-7-8-9-10 K=3 ,结果 3-2-1-6-5-4-9-8-7-10 java代码 /** * @Description:将单链表每K个节点之间逆序 * @Au ...

  8. STM32 JTDO JREST复用为普通IO

    一.开启AFIO的时钟(必须保证先打开AFIO,否则无效) RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); 二.禁用JTAG,使能SWD GP ...

  9. python3 函数 二

    1.函数嵌套 1.1函数嵌套定义 :在一个函数的内部,又定义另外一个函数. def f1():     x=1     def f2():         print('from f2')     f ...

  10. P3338 [ZJOI2014]力(FFT)

    题目 P3338 [ZJOI2014]力 做法 普通卷积形式为:\(c_k=\sum\limits_{i=1}^ka_ib_{k-i}\) 其实一般我们都是用\(i=0\)开始的,但这题比较特殊,忽略 ...