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 ...
随机推荐
- MySql关联子查询
mysql有时候把子查询优化的很差,最差的情景就是 在where子句中使用in. -----<高性能mysql第二版>4.4.1
- php 中date显示时间不对与Linux文件乱码问题
php 中date显示时间不对解决办法如下1.修改/etc/php.ini文件 在里头中找到data.timezone =去掉它前面的分号';' 然后设置data.timezone = “Asia/S ...
- sublime 使用总结
不管你用什么编辑,sublime是首选编辑器,就是sublime淘汰,但已成为标准.例如:atom,几乎等同于sublime,及其他可以几乎调成到sublime操作方式. 一.常用插件 插件搜索地址: ...
- 访问GitLab的PostgreSQL数据库-(3)
1.登陆gitlab的安装服务查看配置文件 cat /var/opt/gitlab/gitlab-rails/etc/database.yml production: adapter: postgre ...
- SQL SERVER分区详解(1-5)
转自: (五)SQL Server分区自动化案例 (四)SQL Server分区管理 (三)索引分区知识详解 (二)SQL Server分区创建过程 (一)SQL Se ...
- mysql python pymysql模块 获取插入的最后一条数据的自增ID lastrowid()方法
调用游标下的lastrowid 可以获取插入之前的表里id字段存放到哪个自增id cursor.lastrowid mysql> select * from userinfo; +----+-- ...
- Python模块:配置文件解析器configparser
版权声明:本文为博主皮皮http://blog.csdn.net/pipisorry原创文章,未经博主同意不得转载. https://blog.csdn.net/pipisorry/article/d ...
- 【剑指offer】替换空格
一.题目: 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 二.思路: Python代码,先 ...
- 梯度下降法实现-python[转载]
转自:https://www.jianshu.com/p/c7e642877b0e 梯度下降法,思想及代码解读. import numpy as np # Size of the points dat ...
- Windows2008 IIS配置FTP站点
视频教程:http://vodcdn.video.taobao.com/player/ugc/tb_ugc_pieces_core_player_loader.swf?version=1.0.2015 ...