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.

输入:

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.

输出:

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

题意:这个题目和D - Mayor’s posters这个题目很相似,只是这个不需要映射,而那个题目需要

要注意的就是:我们的线段树只能存点,我们不能把一个[1,4]区间用四个点去存放,因为实际上区间长度为3,如果我们用[1,4]内所有点去存就表示长度为三

解决方法:

我们只需要再给出的区间在其左边界加一,或是在右边界减一从而使其区间中的点变得和区间长度一样

上代码;

#include<cstdio>

#include<cmath>

#include<algorithm>

#include<cstring>

using namespace std;

const int maxn=8005;

struct node

{

	int l,r,sum;

}a[maxn<<2];

int x1[maxn],x2[maxn],c[maxn];

int mark[maxn];

int sum[maxn];

int lenn; 

void build(int o,int l,int r)

{

	a[o].l=l,a[o].r=r,a[o].sum=-1;

	int ls=o<<1,rs=o<<1|1,mid=(l+r>>1);

	if(l==r)

	{

		a[o].sum=-1;

		return ;

	}

	build(ls,l,mid);

	build(rs,mid+1,r);

}

void pushdown(int o)

{

	   if(a[o].sum!=-1)

	   {

	   		a[o<<1].sum=a[o].sum;

	   		a[o<<1|1].sum=a[o].sum;

	   		a[o].sum=-1;

	   }

}

void update(int o,int l,int r,int c)

{

	int ls=o<<1,rs=o<<1|1,mid=(a[o].l+a[o].r)>>1;

	if(l<=a[o].l&&a[o].r<=r)

	{

		a[o].sum=c;

		return ;

	}

	pushdown(o);

	if(l<=mid) update(ls,l,r,c);

	if(r>mid) update(rs,l,r,c);

}

void query(int o,int l,int r)

{

	int ls=o<<1,rs=o<<1|1,mid=(a[o].l+a[o].r)>>1;

	if(a[o].l==a[o].r)

	{

		mark[lenn++]=a[o].sum;

		return ;

	}

	pushdown(o);

	if(l<=mid) query(ls,l,r);

	if(r>mid) query(rs,l,r);

}

int main()

{

	int n;

	while(~scanf("%d",&n))

	{

		int maxx=0;

		for(int i=0;i<n;i++)

		{

			scanf("%d%d%d",&x1[i],&x2[i],&c[i]);

			int x=max(x1[i]+1,x2[i]);

			maxx=max(maxx,x);

		}

		memset(mark,-1,sizeof(mark));

		build(1,1,maxx);

		for(int i=0;i<n;i++)	update(1,x1[i]+1,x2[i],c[i]);

		lenn=0;

		query(1,1,maxx);

		int x;

		memset(sum,0,sizeof(sum));

		for(int i=0;i<lenn;)

		{

			if(mark[i]==-1)	

			{

				i++;

				continue;

			}

			x=mark[i];

			while(x==mark[++i]&&i<lenn) ;

			sum[x]++;

		}

		for(int i=0;i<=maxx+10;i++)

		{

			if(sum[i]!=0)

				printf("%d %d\n",i,sum[i]);

		}

		printf("\n");  /注意不要忘了最后还有一个换行

	}

}

最新文章

  1. Android 获取meta-data中的数据
  2. java画图程序_图片用字母画出来_源码发布
  3. Android之Fragment学习总结(1)
  4. 利用并查集求最大生成树和最小生成树(nlogn)
  5. 使用SeaJS实现模块化JavaScript开发(新)
  6. HTML+CSS 整站 步骤
  7. CentOS7 yum lamp 虚拟主机配置 lamp各组件简单影响性能的参数调整--for 一定的环境需求
  8. Protel99se轻松入门:一些高级设置和常用技巧
  9. linux下Python网络编程框架-Twisted安装
  10. 对 响应数据写在config文件的再次优化
  11. 分享一个.NET加密工具NetEncryptor v2.1.6(破解版)
  12. [代码审计]青云客Cms前台有条件注入至getshell,后台xss至getshell、至弹你一脸计算器
  13. hiho一下 第168周
  14. Linux 进程管理 ps、top、pstree命令
  15. Overture 5入门之如何设置延音线
  16. C语言--第六周作业评分和总结(5班)
  17. 转载:C++函数中new一块内存,作为返回值
  18. Bat 批处理杀死进程 重新启动程序
  19. 第一章 Java加解密简介
  20. easyui toopTip,鼠标划过悬浮,显示一个小提示框的方法

热门文章

  1. python学习笔记 | PyCharm创建文件时自动添加头文件
  2. python面向对象基础-属性/方法
  3. 【EXP】Oracle多表导出问题
  4. LeetCode590. N叉树的后序遍历
  5. 5V充8.4V,5V升压8.4V给电池充电的芯片电路
  6. 电脑微信电脑PC 多开/防撤回 补丁
  7. RestTemplate post请求
  8. (009)每日SQL学习:Oracle各个键说明(转)
  9. Java多线程--两种实现方式
  10. SpringMVC听课笔记(四:映射请求参数 &amp; 请求头)