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)
这道题用线段树做更方便更新和查询,但是其数据范围很大,因此要将离散化和线段树结合起来,算是一道比较经典的线段树+离散化的例题. 线段树的离散化有很多方法,在这里,我先用一次结点离散化,间接将源左右端点 ...
随机推荐
- 如何高效的通过BP算法来训练CNN
< Neural Networks Tricks of the Trade.2nd>这本书是收录了1998-2012年在NN上面的一些技巧.原理.算法性文章,对于初学者或者是正在学习NN的 ...
- PRML5-神经网络(1)
本节来自<pattern recognition and machine learning>第5章. 五.神经网络 在本书的第3.4章分别是基于回归和分类的线性模型,都是通过将固定的基函数 ...
- 何谓BLDC电机?BLDC电机是如何旋转的?
何谓BLDC电机?BLDC电机是如何旋转的?
- C51中的关键字和ANSIC标准关键字
C51中的关键字和ANSIC标准关键字 作 者:武力戡乱 修改日期:2017-09-05 备 注: 1.总备注信息 2.联系方式 3.其它博文链接:武力戡乱博客目录总表 内 ...
- 【Python实践-7】输出100以内的所有素数
#输出100以内的所有素数,素数之间以一个空格区分(注意,最后一个数字之后不能有空格). i= l=[] : k= ,i): : k=k+ : l.append(i) i=i+ print(" ...
- CODE[VS] 1159 最大全0子矩阵
写一道CODEVS的题目 其实我还是很喜欢CODEVS的界面的 主要是系统地学习一下悬线法这个看似十分简单,实际就是十分简单的算法 对于一些详细的东西参考dalao's blog,不喜勿喷 对于悬线法 ...
- Luogu P1972 [SDOI2009]HH的项链
很清新自然凶猛的数据结构题,都是套路啊 我们可以考虑离线做,先把区间按右端点从小到大排序 首先注意到一种贝壳如果在一段中出现超过1次,那么它在前面或后面就无关紧要了 举一个例子: 对于数列1 2 3 ...
- stl源码剖析 详细学习笔记 算法(3)
//---------------------------15/03/30---------------------------- //min_element template<class Fo ...
- 2017qq红包雨最强攻略
这个只支持苹果手机,而且要有苹果电脑,只有苹果手机是不行的. QQ红包规则:只要你到达指定的位置,就可以领取附近的红包,一般也就几毛,还有几分的,当然也不排除有更高的,只不过我是没遇到... 那么既然 ...
- HTML 图像实例
61.插入图像本例演示如何在网页中显示图像.图像标签(<img>)和源属性(Src)在 HTML 中,图像由 <img> 标签定义. <img> 是空标签,意思是说 ...