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

ZOJ挂了,我还没交没不知道对不对。应该没问题,按照kuangbin大神的模板改的。这个题又是区间更新,不过看数据范围不用离散化。

因为每个节点都代表区间的端点,kuangbin大神的处理方法在建树的时候与普通线段树模板做了微小的改动。

代码如下:

 #include <bits/stdc++.h>

 using namespace std;
#define M 8010
struct segTree
{
int l,r,col;
}tree[M<<];
int color[M],temp,n;
void buildTree (int now,int l,int r)
{
tree[now].l=l,tree[now].r=r;
tree[now].col=-;//-1代表没有颜色
if (l+==r)
return ;
int m=((l+r)>>);
buildTree(now<<,l,m);
buildTree(now<<|,m,r);//因为节点代表端点,二分建树时是[l,m],[m,r],而不是[m+1,r]。
}
void upDate (int now,int l,int r,int c)
{
if (l==r||l>r)
return ;
if (tree[now].col==c)
return ;
if (l<=tree[now].l&&tree[now].r<=r)
{
tree[now].col=c;
return ;
}
if (tree[now].col>=)
{
tree[now<<].col=tree[now].col;
tree[now<<|].col=tree[now].col;
tree[now].col=-;
}
int mid=(tree[now].l+tree[now].r)>>;
if (r<=mid)
upDate(now<<,l,r,c);
else if (l>=mid)
upDate(now<<|,l,r,c);
else
{
upDate(now<<,l,mid,c);
upDate(now<<|,mid,r,c);
}
tree[now].col=-;//-2代表有多种颜色
}
void Count (int now)
{
if (tree[now].col==-)
{
temp=-;//temp是前一段的颜色
return ;
}
if (tree[now].col!=-)
{
if (tree[now].col!=temp)
{
color[tree[now].col]++;
temp=tree[now].col;
}
return ;
}
if (tree[now].l+!=tree[now].r)
{
Count(now<<);
Count(now<<|);
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d",&n))
{
int x,y,z,maxn=;
buildTree(,,);
for (int i=;i<n;++i)
{
scanf("%d%d%d",&x,&y,&z);
upDate(,x,y,z);
maxn=max(maxn,z);
}
temp=-;
memset(color,,sizeof color);
Count();
for (int i=;i<=maxn;++i)
{
if (color[i])
printf("%d %d\n",i,color[i]);
}
printf("\n");
}
return ;
}

ZOJ 1610 Count the Colors (线段树区间更新与统计)的更多相关文章

  1. zoj 1610 Count the Colors 线段树区间更新/暴力

    Count the Colors Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...

  2. ZOJ 1610 Count the Color(线段树区间更新)

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

  3. ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  4. ZOJ 1610 Count the Colors (线段树成段更新)

    题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...

  5. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  6. ZOJ 1610.Count the Colors-线段树(区间染色、区间更新、单点查询)-有点小坑(染色片段)

    ZOJ Problem Set - 1610 Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting s ...

  7. ZOJ 5638——Prime Query——————【线段树区间更新,区间查询,单点更新】

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  8. poj 2777 Count Color(线段树 区间更新)

    题目:http://poj.org/problem?id=2777 区间更新,比点更新多一点内容, 详见注释,  参考了一下别人的博客.... 参考博客:http://www.2cto.com/kf/ ...

  9. ZOJ1610 Count the Colors —— 线段树 区间染色

    题目链接:https://vjudge.net/problem/ZOJ-1610 Painting some colored segments on a line, some previously p ...

随机推荐

  1. 【Vim编辑器】基本命令

    前言 工作中免不了会使用到vim编辑文档,总会觉得不好上手,遂从网上找到一篇说明文档整理如下,共勉. 原文地址: https://www.cnblogs.com/shiyanlou/archive/2 ...

  2. windows系统的安装时间怎么查看

    方法一:利用命令符窗口查询 直接按下Windows+R组合键  出现运行对话框(或 点击开始—运行),输入cmd,进入命令符窗口 然后,在该界面下输入”systeminfo”,然后回车,等待系统自动运 ...

  3. excel 中相乘函数

    excel  中相乘函数   “PRODUCT”并且是公式的框框,格式要是 常规,不能是文本

  4. CodeForces - 1038D (线性DP)

    题目:https://codeforces.com/problemset/problem/1038/D 题意:给你n个数字,每个数字可以吃左右两边的数,然后吃完后自己变成 a[i]-a[i+1]或者a ...

  5. 经典JS 判断上传文件大小和JS即时同步电脑时间

    我也是新手,还是一个JS笨,有一些网站要实现的功能要自己写么? 答案是不会,去问同事大佬吧,闲简单.就在晚上看了一些其他大佬们写的JS效果, 代码很少.占用网站CPU也小的多.可以一用, 废话少扯.代 ...

  6. exception中return方法

    public class ExceptionPitfall { public static void main(String[] args) { try { throw new RuntimeExce ...

  7. 返回闭包不能引用循环变量,请改写count()函数,让它正确返回能计算1x1、2x2、3x3的函数。

    错误写法: 正确写法:

  8. upc组队赛16 WTMGB【模拟】

    WTMGB 题目链接 题目描述 YellowStar is very happy that the FZU Code Carnival is about to begin except that he ...

  9. Learning OSG programing---osgScribe

    Learning OSG programing---osgScribe Scribe可以翻译为素描,抄写等.本例通过在模型表面添加一层素描,来显示模型的骨架. 关键代码: osg::ref_ptr&l ...

  10. Neo4j查询节点间最短路径

    Neo4j最短路径问题 1.指定某一结点 无向边: MATCH (p1:Person {name:"aaaaaaa"}),(p2:Person{name:"bbbbbb& ...