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");  /注意不要忘了最后还有一个换行

	}

}

F - Count the Colors(线段树)的更多相关文章

  1. Count the Colors(线段树,找颜色段条数)

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

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

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

  3. [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)

    题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=1610 题意:给一个长8000的绳子,向上染色.一共有n段被染色,问染 ...

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

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

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

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

  6. ZOJ-1610 Count the Colors ( 线段树 )

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Description Painting some co ...

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

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

  8. Count the Colors 线段树

    题目 参考博客地址 题意: n范围[1,8000] ,  li 和 ri 的范围[0,8000].  n个操作,每个操作是把 [li , ri]内的点修改成一个颜色c. n个操作过后,按颜色从小到大 ...

  9. F - Count the Colors

    F - Count the Colors ZOJ - 1610   思路:调了一个小时,但是发现自己线段树木有写错,颜色统计出了错误.但是不明白自己颜色统计为什么错了. 求大佬指点迷津.思路很简单,就 ...

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

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

随机推荐

  1. springboot源码解析-管中窥豹系列之aware(六)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...

  2. 【C++】《C++ Primer 》第十八章

    第十八章 用于大型程序的工具 大规模应用程序的特殊要求包括: 在独立开发的子系统之间协同处理错误的能力. 使用各种库进行协同开发的能力. 对比较复杂的应用概念建模的能力. 一.异常处理 异常处理(ex ...

  3. 【C++】《C++ Primer 》第七章

    第七章 类 一.定义抽象数据类型 类背后的基本思想:数据抽象(data abstraction)和封装(encapsulation). 数据抽象是一种依赖于接口(interface)和实现(imple ...

  4. Educational Codeforces Round 102 (Rated for Div. 2)

    比赛地址 A(水题) 题目链接 题目: 给出一个数组\(a\)并能进行一个操作使得数组元素更改为数组任意其他两元素之和,问是否可以让数组元素全部小于等于\(d\) 解析: 排序后判断最大值是否小于等于 ...

  5. Python_列表(list)

    list()类中的提供的操作 1.索引取值 li = [11,22,33,44,55] v1 = li[3] print(li[2]) #索引取出33 print(v1) #索引取出44 print( ...

  6. vim 手动添加脚本头部信息

    vim /root/.vimrc 8,1 全部 set autoindent set tabstop=5 set shiftwidth=4 function AddTitle() call setli ...

  7. Kafka底层原理剖析(近万字建议收藏)

    Kafka 简介 Apache Kafka 是一个分布式发布-订阅消息系统.是大数据领域消息队列中唯一的王者.最初由 linkedin 公司使用 scala 语言开发,在2010年贡献给了Apache ...

  8. CTFshow萌新赛-web签到

    打开靶机 查看页面信息 可以看到有一个system函数 在Linux中可以使用":"隔离不同的语句 payload如下 https://5105c8b6-83aa-4993-91b ...

  9. PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)

    说明 文章首发于HURUWO的博客小站,本平台做同步备份发布. 如有浏览或访问异常图片加载失败或者相关疑问可前往原博客下评论浏览. 原文链接 PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二 ...

  10. Gradle使用及配置

    构建工具:Gradle(6.8) 下载地址:https://gradle.org/releases/ 下载最新版的二进制文件即可,下载"gradle-6.8.1-bin.zip文件,下载后完 ...