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

思路:

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

    题解:分块+离散化 解题报告: 一个分块典型题呢qwq还是挺妙的毕竟是道黑题 然,然后发现忘记放链接了先放链接QAQ 有两三种解法,都港下qwq 第一个是O(n5/3)的复杂度,谢总说不够优秀没有港, ...

  2. 虚拟机中安装Ubuntu 16.04

    1.安装vmware软件(虚拟机管理软件) https://blog.csdn.net/salmon_zhang/article/details/79468918 2.安装Ubuntu https:/ ...

  3. dedecms利用addfields body在首页调用文章内容

    开源程序比较好的一点是可以进行二次开发,比如ytkah想要开发一个专家出诊表的功能模块,如下图所示,每天的专家都不一样,可以用到内容模型,但是相对比较复杂:我们可以把每天的坐诊情况写成一篇文章再通过调 ...

  4. Javascript核心对象

    JavaScript的实现包括以下3个部分: 1)核心(ECMAScript):描述了JS的语法和基本对象. 2)文档对象模型 (DOM):处理网页内容的方法和接口 3)浏览器对象模型(BOM):与浏 ...

  5. Centos 集群配置SSH免登陆脚本

    首先编写脚本生成集群服务器列表: hostsList.sh #!/bin/bash preIp="11.11.225." pwd="dyj2017" for i ...

  6. [LeetCode] 197. Rising Temperature_Easy tag: SQL

    Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...

  7. 软件包管理:rpm包管理-yum在线管理-IP地址配置和网络yum源

    只需告诉系统你想安装那个包,剩下的所有依赖问题yum都会解决. 有些情况下不能上网,但可以使用光盘. centos的yum是免费的.redhatyum付费. yum管理的其实同样是rpm包.并没有yu ...

  8. Leetcode: Reorder List && Summary: Reverse a LinkedList

    Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do th ...

  9. python直接赋值、浅拷贝和深拷贝

    # 解: # import copy # names1=['Amir','Barry','Cgakes','Dao',[11,22,33]] # names2=names1#直接赋值,指向同一个对象 ...

  10. MFC六大核心机制之三:动态创建

    MFC中很多地方都使用了动态创建技术.动态创建就是在程序运行时创建指定类的对象.例如MFC的单文档程序中,文档模板类的对象就动态创建了框架窗口对象.文档对象和视图对象.动态创建技术对于希望了解MFC底 ...