POJ2528 Mayor's posters(线段树&区间更新+离散化)题解
题意:给一个区间,表示这个区间贴了一张海报,后贴的会覆盖前面的,问最后能看到几张海报。
思路:
之前就不会离散化,先讲一下离散化:这里离散化的原理是:先把每个端点值都放到一个数组中并除重+排序,我们就得到了处理后的数组,现在我们只需要用二分查找端点值在整个数组的下标,这样就达到了离散化的目的,压缩了长度。因为这里很特殊,不能用一般的离散化去做,如果区间两端只差1,那么需要给这个区间再加一个值,这个其他题解讲到了。
这里的区间更新和上一题不太一样,有一些地方要注意一下
代码:
#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#define ll long long
const int N=100005;
const int MOD=20071027;
using namespace std;
int lw[N],rw[N],x[N<<1],color[N<<2];
void push_down(int rt){
	if(color[rt]!=-1){
		color[rt<<1]=color[rt<<1|1]=color[rt];
		color[rt]=-1;
	}
}
void update(int rt,int l,int r,int L,int R,int co){ //l r为访问区间
	if(L<=l && R>=r){
		color[rt]=co;
		return;
	}
	push_down(rt);	//注意传值
	int m=(l+r)/2;
	if(L<=m){	//这里只能在L取等
		update(rt<<1,l,m,L,R,co);
	}
	if(R>m){	//这里不能取等,取等时代入m+1就错了
		update(rt<<1|1,m+1,r,L,R,co);
	}
}
set<int> col;
void query(int l,int r,int rt){
	if(color[rt]!=-1){
		col.insert(color[rt]);
		return;
	}
	if(l==r) return;
	int m=(l+r)/2;
	query(l,m,rt<<1);
	query(m+1,r,rt<<1|1);
}
int main(){
	int n,num,T;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		num=0;
		for(int i=0;i<n;i++){
			scanf("%d%d",&lw[i],&rw[i]);
			x[num++]=lw[i];
			x[num++]=rw[i];
		}
		sort(x,x+num);
		int cnt=unique(x,x+num)-x;	//去掉重复的点
		for(int i=cnt-1;i>0;i--){	//相邻两数相差大于1,中间再添一个数
			if(x[i]!=x[i-1]+1)  x[cnt++]=x[i-1]+1;
		}
		sort(x,x+cnt);
		memset(color,-1,sizeof(color));
		for(int i=0;i<n;i++){	//离散化
			int L=lower_bound(x,x+cnt,lw[i])-x;
			int R=lower_bound(x,x+cnt,rw[i])-x;
			update(1,0,cnt,L,R,i);
		}
		col.clear();
		query(0,cnt,1);
		printf("%d\n",col.size());
	}
    return 0;
}
POJ2528 Mayor's posters(线段树&区间更新+离散化)题解的更多相关文章
- POJ2528:Mayor's posters(线段树区间更新+离散化)
		Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ... 
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
		题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ... 
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
		Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 43507 Accepted: 12693 ... 
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
		POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ... 
- poj2528 Mayor's posters(线段树区间修改+特殊离散化)
		Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ... 
- poj 2528 Mayor's posters 线段树区间更新
		Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ... 
- poj2528 Mayor's posters(线段树区间覆盖)
		Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50888 Accepted: 14737 ... 
- POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
		题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ... 
- POJ-2528  Mayor's posters (线段树区间更新+离散化)
		题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ... 
- POJ-2528 Mayor's posters(线段树区间更新+离散化)
		http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ... 
随机推荐
- hive-site.xml配置
			<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-s ... 
- 便于理解mysql内幕的各种逻辑图组
			便于理解mysql内幕的各种逻辑图组 http://blog.sina.com.cn/s/blog_445e807b0101ggtl.html 以下是个人一直以来从网络等各种途径收集到的一些对理解my ... 
- 一起做RGB-D SLAM (2)
			第二讲 从图像到点云 本讲中,我们将带领读者,编写一个将图像转换为点云的程序.该程序是后期处理地图的基础.最简单的点云地图即是把不同位置的点云进行拼接得到的. 当我们使用RGB-D相机时,会从相机里读 ... 
- MongoDB复制集的工作原理介绍(二)
			复制集工作原理 1)数据复制原理 开启复制集后,主节点会在 local 库下生成一个集合叫 oplog.rs,这是一个有限集合,也就是大小是固定的.其中记录的是整个mongod实例一段时间内数据库的所 ... 
- 安卓手机上微信无法打开Https网址的完美解决方案
			1,第三方网站检测网站的SSL证书是否正确的安装 https://www.geocerts.com/ssl-checker,大概率你会看到下边的场景,一个证书链完整的警告,如果想知道我的基础配置是什么 ... 
- 3.keras实现-->高级的深度学习最佳实践
			一.不用Sequential模型的解决方案:keras函数式API 1.多输入模型 简单的问答模型 输入:问题 + 文本片段 输出:回答(一个词) from keras.models import M ... 
- Python常用函数及说明
			原文地址:博客园 CSDN 基本定制型C.__init__(self[, arg1, ...]) 构造器(带一些可选的参数)C.__new__(self[, arg1, ...]) 构造器(带一些可 ... 
- rabbitmq_坑
			一.None of the specified endpoints were reachable 这个异常在创建连接时抛出(CreateConnection()),原因一般是ConnectionF ... 
- Object-C-block
			块是对c语言的一种扩展语法 块看起来像函数,不同的是,快可以直接写在函数内部 块能够作为参数传递给函数或者方法 void sayHello(){NSLog(@"hello!");} ... 
- [WPF]WPF开发方法论
			纵观Windows GUI应用程序开发方法,从Windows API.MFC到Visual Basic再到.NET Framework,WPF的开发方法论是在.NET Framework方法论的基础上 ... 
