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

思路:

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

    一.并发引起的问题 脏读:能读取未提交的数据 不可重复读:T1事务读取数据后,T2事务执行更新操作,使T1无法再现前一次读取的结果.分三种情况: 1.值不同 2.少了记录 3.多了记录 幻象读(后两种 ...

  2. SpringBoot @Transactional声明事务无效问题

    查看系统支持的存储引擎:show engines; 查看表使用的引擎:show table status from db_name where name='table_name'; 修改表引擎方法:  ...

  3. 居然上了模板使用排行榜第一 happy一下

    这段时间在学习css和div,顺便把博客给整了一下,然后不小心就上了FFandIE模板使用排行榜第一,happy一下下.不知道这个算不算排名,还是随机刷新.感觉应该是按流量统计的,这段时间有几篇文章一 ...

  4. vue之vue-cookies

    vue之vue-cookies npm链接:https://www.npmjs.com/package/vue-cookies 安装: npm install vue-cookies --save 使 ...

  5. [py][mx]django get方法返回login页面

    get方法返回login.html users/views.py def login(request): if request.method == "POST": pass eli ...

  6. WebService之Axis2(1):用POJO实现0配置的WebService

    Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物.Axis2不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的REST WebService,同时还支持S ...

  7. 计划评审技术PERT

    概念 编辑 PERT(Program Evaluation and Review Technique)即 [2]  计划评审技术,最早是由美国海军在计划和控制北极星导弹的研制时发展起来的.PERT技术 ...

  8. jmeter Bean Shell的使用(一)

    未经作者允许,禁止转载!!! Jmeter有哪些Bean Shell 定时器: BeanShell Timer 前置处理器:BeanShell PreProcessor 采样器: BeanShell ...

  9. unp第七章补充之TCP半开连接与半闭连接

    半打开(Half-Open)连接和半关闭(Half-Close)连接.TCP是一个全双工(Full-Duplex)协议,因此这里的半连接"半"字就是相对于全双工的"全&q ...

  10. js实现轮播图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...