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. 别人的双11 & 程序员的双11~

    双11,致敬所有的程序员欧巴! 愿代码的世界,只有爱,没有伤害!! ​ 点此参加阿里云双十一拼团活动:https://m.aliyun.com/act/team1111/ 阅读原文

  2. 从Java future 到 Guava ListenableFuture实现异步调用

    从Java future 到 Guava ListenableFuture实现异步调用 置顶 2016年04月24日 09:11:14 皮斯特劳沃 阅读数:17570 标签: java异步调用线程非阻 ...

  3. CTF | bugku | 秋名山车神

    ''' @Modify Time @Author ------------ ------- 2019/8/31 19:55 laoalo ''' import requests from lxml i ...

  4. 使用xorm将结构体转为sql文件

    操作步骤 (1)定义结构体 type User struct { Id int //表id Name string //姓名 ...}12345(2)编写代码,执行自动增量同步(mysql为例) im ...

  5. spring boot 修改banner

    在resources目录下新建一个banner.txt,里面添加要显示的内容,如: ////////////////////////////////////////////////////////// ...

  6. Houdini学习笔记——【一】散落苹果

    [案例一]散落的苹果 0.渲染 1.sop使用 - 苹果主体:curve绘制刨面曲线,revolve车削得到苹果主体,uvtexture来调整uv,convert继续转换为polygon,fuse缝合 ...

  7. <读书笔记>《Web前端开发最佳实践》

    P77 P89 CSS Reset P94 给CSS样式定义排序    排序工具:CSScomb P97 什么是CSS的权重?权重是指选择符的优先级 P100 工具:Sass Less P101 框架 ...

  8. B. pSort

    题目链接: http://codeforces.com/problemset/problem/28/B 题意: 给一个n,原本有个1到n按顺序排列的序列,给一个序列问,在给一个数组,表示这个位置的数可 ...

  9. shell cp拷贝的用法

    个人觉得这个记录的比较全 自己查阅: cp [options] <source file or directory> <target file or directory> 或 ...

  10. Java数据访问对象模式

    数据访问对象模式或DAO模式用于将低级数据访问API或操作与高级业务服务分离. 以下是数据访问对象模式的参与者. 数据访问对象接口 - 此接口定义要对模型对象执行的标准操作. 数据访问对象具体类 - ...