poj 2528 (线段树+特殊离散化)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 51098 | Accepted: 14788 |
Description
- Every candidate can place exactly one poster on the wall.
- All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
- The wall is divided into segments and the width of each segment is one byte.
- Each poster must completely cover a contiguous number of wall segments.
They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.
Input
Output
The picture below illustrates the case of the sample input. 
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4 思路:
很经典的题。。线段树+离散化,难点主要在于需要用特殊的离散化方法,一般的离散化方法会出错
比如下面这串数据
3
1-10
1-4
6-10
一般离散化后就会变成 a[0] = 1;a[1] = 4;a[2] = 6;a[3]= 10;
离散化将把这些数据的下标作为值放进线段树处理后就会成为:
0 - 3 为1颜色
0 - 1 为2颜色
2 - 3 为3颜色
最后只存在两种颜色
但正确的过程应该是:
1 - 10 为1颜色
1 - 4 为2颜色
6 - 10 为3颜色
最后存在三种颜色 实现代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 2e5+;
int col[M<<],Hash[M<<],cnt,a[M],l[M],r[M];
void pushdown(int rt){
if(col[rt]!=-){
col[rt<<] = col[rt<<|] = col[rt];
col[rt] = -;
}
} void update(int L,int R,int c,int l,int r,int rt){
if(L <= l&&R >= r){
col[rt] = c;
return ;
}
pushdown(rt);
mid;
if(L <= m) update(L,R,c,lson);
if(R > m) update(L,R,c,rson);
} void query(int l,int r,int rt){
if(col[rt]!=-){
if(Hash[col[rt]]==) cnt++;
Hash[col[rt]] = ;
return ;
}
if(l == r) return;
mid;
query(lson);
query(rson);
} int bin(int key,int n,int a[]){
int l = ,r = n-;
while(l <= r){
mid;
if(a[m] == key) return m;
else if(a[m] < key) l = m+;
else r = m-;
}
return -;
}
int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
int t,n;
cin>>t;
while(t--){
cin>>n;
memset(col,-,sizeof(col));
memset(Hash,,sizeof(Hash));
int nn = ;
cnt = ;
for(int i = ;i < n;i++){
cin>>l[i]>>r[i];
a[nn++] = l[i];a[nn++] = r[i];
}
sort(a,a+nn);
int m = ;
for(int i = ;i < nn;i ++){
if(a[i]!=a[i-]) a[m++] = a[i];
}
for(int i = m-;i > ;i --){
if(a[i]!=a[i-]+) a[m++] = a[i] + ;
}
sort(a,a+m);
for(int i = ;i < n;i ++){
int li = bin(l[i],m,a);
int ri = bin(r[i],m,a);
update(li,ri,i,,m,);
}
query(,m,);
cout<<cnt<<endl;
}
}
poj 2528 (线段树+特殊离散化)的更多相关文章
- Mayor's posters POJ - 2528 线段树(离散化处理大数?)
题意:输入t组数据,输入n代表有n块广告牌,按照顺序贴上去,输入左边和右边到达的地方,问贴完以后还有多少块广告牌可以看到(因为有的被完全覆盖了). 思路:很明显就是线段树更改区间,不过这个区间的跨度有 ...
- poj 2528(线段树+离散化) 市长的海报
http://poj.org/problem?id=2528 题目大意是市长竞选要贴海报,给出墙的长度和依次张贴的海报的长度区间(参考题目给的图),问最后你能看见的海报有几张 就是有的先贴的海报可能会 ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- poj 2528 线段树+离散化
题意:在墙上贴一堆海报(只看横坐标,可以抽象成一线段),新海报可以覆盖旧海报.求最后能看到多少张海报 sol:线段树成段更新.铺第i张海报的时候更新sg[i].x~sg[i].y这一段为i. 然而坐标 ...
- POJ 2528 (线段树 离散化) Mayor's posters
离散化其实就是把所有端点放在一起,然后排序去个重就好了. 比如说去重以后的端点个数为m,那这m个点就构成m-1个小区间.然后给这m-1个小区间编号1~m-1,再用线段树来做就行了. 具体思路是,从最后 ...
- poj 2528 线段树 离散化的小技巧
题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报思路:直接搞超时+超内存,需要离散化.离散化简单的来说就是只取我们需要的值来 用,比如说区间[1000,2000],[1990,2012] ...
- Mayor's posters POJ - 2528(线段树 + 离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 74745 Accepted: 21574 ...
- Mayor's posters POJ - 2528 线段树区间覆盖
//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...
- POJ 2528 线段树
坑: 这道题的坐标轴跟普通的坐标轴是不一样的-- 此题的坐标轴 标号是在中间的-- 线段树建树的时候就不用[l,mid][mid,r]了(这样是错的) 直接[l,mid][mid+1,r]就OK了 D ...
- ACM/ICPC 之 数据结构-线段树+区间离散化(POJ2528)
这道题用线段树做更方便更新和查询,但是其数据范围很大,因此要将离散化和线段树结合起来,算是一道比较经典的线段树+离散化的例题. 线段树的离散化有很多方法,在这里,我先用一次结点离散化,间接将源左右端点 ...
随机推荐
- 经常使用命令 echo、@、call、pause、rem
经常使用命令 echo.@.call.pause.rem(小技巧:用::取代rem)是批处理文件最经常使用的几个命令,我们就从他们開始学起. 首先, @ 不是一个命令, 而是DOS 批处理的一个特殊标 ...
- 数据库连接池DBUtils使用
一.简介 DBUtils简单说python实现的线程化数据库连接(连接池),DBUtils支持所有遵循DP-API 2规范的数据库连接模块,例如:mysql.sqlserver.oracle.sqli ...
- 用Spring.Services整合 thrift0.9.2生成的wcf中间代码-复杂的架构带来简单的代码和高可维护性
最近一直在看关于thrift的相关文章,涉及到的内容的基本都是表层的.一旦具体要用到实际的项目中的时候就会遇到各种问题了! 比如说:thrift 的服务器端载体的选择.中间代码的生成options(a ...
- flask_admin 笔记四 自定义视图
定义自己的视图 对于您的要求非常具体的情况,您很难用内置的ModelView类来满足这些需求,Flask-Admin使您可以轻松地完全控制并将自己的视图添加到界面中. 1)独立视图 可以通过扩展Bas ...
- win10+anaconda3+python3.6+opencv3.1.0
最近用windows系统比较多,就想在win10下搞一下深度学习这一方面的研究,那么就需要配置好环境巴拉巴拉的一堆东西.默默记个笔记,正所谓“好记性不如烂笔头”. 1.安装Anaconda 这个是一个 ...
- c++日志记录模块
C++ 日志记录模块 该模块从实际项目中产生,通过extern声明的方式,可在代码不同模块中生成日志,日志文件名称为随机码加用户指定名称,采用随机码是为了避免日志文件可能被覆盖的问题. 愿意的话你也能 ...
- Jupyter Notebook 工作空间 / 默认路径 的设置方式
Jupyter notebook 安装后,启动后,默认的工作空间是当前用户目录.为了方便对文档进行管理,往往需要自行设置工作空间. 下面介绍两种亲试有效的工作空间设置方法. 1.修改快捷方式 对 Ju ...
- 冒泡排序算法的C++,Java和Python实现和冒泡排序算法三种语言效率的比较
冒泡排序原理: 这一篇百度经验讲得很好,我不多说了 https://jingyan.baidu.com/article/6525d4b13f920bac7d2e9484.html 他讲的是C语言,没有 ...
- 如何在百度云虚拟机中配置thinkphp5,并且url去掉index.php
第一步:将public目录下的index.php移到和public同级目录下,[或者直接在public同级目录下新建一个index.php] 第二步:那么这个新的index.php文件的内容如下: & ...
- torchvision 批量可视化图片
1.1 简介 计算机视觉中,我们需要观察我们的神经网络输出是否合理.因此就需要进行可视化的操作. orchvision是独立于pytorch的关于图像操作的一些方便工具库. torchvision的详 ...