题意:给一个区间,表示这个区间贴了一张海报,后贴的会覆盖前面的,问最后能看到几张海报。

思路:

之前就不会离散化,先讲一下离散化:这里离散化的原理是:先把每个端点值都放到一个数组中并除重+排序,我们就得到了处理后的数组,现在我们只需要用二分查找端点值在整个数组的下标,这样就达到了离散化的目的,压缩了长度。因为这里很特殊,不能用一般的离散化去做,如果区间两端只差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(线段树&区间更新+离散化)题解的更多相关文章

  1. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  2. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  3. poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 43507   Accepted: 12693 ...

  4. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  5. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  6. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

  7. poj2528 Mayor's posters(线段树区间覆盖)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 50888   Accepted: 14737 ...

  8. POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)

    题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...

  9. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  10. POJ-2528 Mayor's posters(线段树区间更新+离散化)

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

随机推荐

  1. Python开发【笔记】:获取mp3文件获取信息

    import eyed3 def main(): mp3 = '8200031001_13429822982_1_00163e086216990b11e82403f00d3d9a.mp3' xx = ...

  2. Redis经验谈(转)

    原文:http://www.programmer.com.cn/14577/ 新浪作为全世界最大的Redis用户,在开发和运维方面有非常多的经验.本文作者来自新浪,希望能为业界提供一些亲身经历,让大家 ...

  3. IO流(4)重命名

    重命名功能:public boolean renameTo(File dest) * 如果路径名相同,就是改名. * 如果路径名不同,就是改名并剪切. * * 路径以盘符开始:绝对路径 c:\\a.t ...

  4. linux平台mysql密码设置

    登录mysql默认没有指定账号 查看默认账号是谁 select user(); mysql> select user();+----------------+| user() |+------- ...

  5. zabbix 3.2.5 agent端(源码包)安装部署 (二)

    一.zabbix agent 端安装部署 1.创建zabbix用户和组 groupadd zabbix useradd -g zabbix zabbix -s /sbin/nologin 2.解压za ...

  6. android 6.0之后动态获取权限

    Android 6.0 动态权限申请   1. 概述 Android 6.0 (API 23) 之前应用的权限在安装时全部授予,运行时应用不再需要询问用户.在 Android 6.0 或更高版本对权限 ...

  7. 007-ant design 对象属性赋值,双向绑定

    1.state对象属性赋值 设对象为 state={ datavalue:{ id:'', name:'', }, } 修改对象属性为: SetName=(e)=>{ let val=e.tar ...

  8. [py][mx]django自定义认证类-实现邮箱作为用户名登录

    创建自定义验证用户名密码类CustomBackend users/views.py from django.contrib.auth import authenticate, login from d ...

  9. [py][mx]django xadmin后台配置

    xadmin配置 - 安装 pip install -r https://github.com/sshwsfc/xadmin/blob/django2/requirements.txt 以下被我测试通 ...

  10. Python随机数生成方法

    假设你对在Python生成随机数与random模块中最经常使用的几个函数的关系与不懂之处.以下的文章就是对Python生成随机数与random模块中最经常使用的几个函数的关系,希望你会有所收获,以下就 ...