Mayor's posters(离散化线段树)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 54067 | Accepted: 15713 |
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
题解:
本题大意:给定一些海报,可能相互重叠,告诉你每个海报的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张?
海报最多10000张,但是墙有10000000块瓷砖长,海报不会落在瓷砖中间。
跟颜色段那道题很像,但是写了下wa,最后借助bin神的思路才写出来; ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x,y) scanf("%lf%lf",&x,&y)
#define P_ printf(" ")
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
#define V(x) tree[x]
typedef long long LL;
const int MAXN=100010;
bool tree[MAXN<<2];
int h[10000010],seg[MAXN<<1];
struct Node{
int a,b;
Node init(int c,int d){
a=c;b=d;
}
};
Node dt[MAXN];
void build(int root,int l,int r){
V(root)=false;
int mid=(l+r)>>1;
if(l==r)return;
build(lson);build(rson);
}
bool query(int root,int l,int r,int A,int B){
int mid=(l+r)>>1;
if(V(root))return false;//线段树都是从上倒下访问的,覆盖的线段是true,就返回false
bool bcover;
if(l==A&&r==B){
V(root)=true;
return true;
}
if(mid>=B)bcover=query(lson,A,B);
else if(mid<A)bcover=query(rson,A,B);
else{
int b1=query(lson,A,mid);//
int b2=query(rson,mid+1,B);//
bcover=b1||b2;
}
if(V(ll)&&V(rr))V(root)=true;
return bcover;
}
int main(){
int T,N;
SI(T);
while(T--){
SI(N);
int a,b;
int len=0,val=0;
for(int i=0;i<N;i++){
SI(a);SI(b);
dt[i].init(a,b);
seg[len++]=a;seg[len++]=b;
}
sort(seg,seg+len);
int k=unique(seg,seg+len)-seg;
for(int i=0;i<k;i++)h[seg[i]]=i;
int ans=0;
build(1,0,k-1);
for(int i=N-1;i>=0;i--){//从上往下;
if(query(1,0,k-1,h[dt[i].a],h[dt[i].b]))ans++;
}
printf("%d\n",ans);
}
return 0;
}
wa代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x,y) scanf("%lf%lf",&x,&y)
#define P_ printf(" ")
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
#define V(x) tree[x]
typedef long long LL;
const int MAXN=20010;
int color[MAXN];
int temp;
int tree[MAXN<<2];
int seg[MAXN];
struct Node{
int a,b;
Node init(int c,int d){
a=c;b=d;
}
};
Node dt[MAXN];
void pushdown(int root){
if(V(root)>0){
V(ll)=V(root);
V(rr)=V(root);
V(root)=-1;
}
}
void build(int root,int l,int r){
int mid=(l+r)>>1;
V(root)=0;
if(l==r)return;
build(lson);build(rson);
}
void update(int root,int l,int r,int A,int B,int v){
if(l>=A&&r<=B){
V(root)=v;
return;
}
int mid=(l+r)>>1;
pushdown(root);
if(mid>=A)update(lson,A,B,v);
if(mid<B)update(rson,A,B,v);
V(root)=-1;
}
void query(int root,int l,int r){
int mid=(l+r)>>1;
if(temp==V(root))return;
if(!V(root)){
temp=0;return;
}
if(V(root)!=-1){
if(temp!=V(root)){
temp=V(root);
color[temp]++;
return;
}
return;
}
if(l==r)return;
query(lson);
query(rson);
}
int main(){
int T,N;
SI(T);
while(T--){
mem(color,0);
SI(N);
int a,b;
int len=0;
for(int i=0;i<N;i++){
SI(a);SI(b);
dt[i].init(a,b);
seg[len++]=a;seg[len++]=b;
}
sort(seg,seg+len);
int k=unique(seg,seg+len)-seg;
build(1,1,k);
for(int i=0;i<N;i++){
a=lower_bound(seg,seg+k,dt[i].a)-seg;
b=lower_bound(seg,seg+k,dt[i].b)-seg;
update(1,1,k,a+1,b,i+1);
}
temp=0;
query(1,1,k);
int ans=0;
for(int i=1;i<=N;i++){
if(color[i])ans++;
}
printf("%d\n",ans);
}
return 0;
}
Mayor's posters(离散化线段树)的更多相关文章
- 【POJ】2528 Mayor's posters ——离散化+线段树
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of Bytetown, A ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- POJ 2528 Mayor's posters(线段树/区间更新 离散化)
题目链接: 传送门 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of By ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
- Mayor's posters (线段树加离散化)
个人心得:线段树也有了一定的掌握,线段树对于区间问题的高效性还是挺好的,不过当区间过大时就需要离散化了,一直不了解离散化是什么鬼,后面去看了下 离散化,把无限空间中有限的个体映射到有限的空间中去,以此 ...
- POJ-2528 Mayor's posters(线段树区间更新+离散化)
http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...
- 【POJ 2528】Mayor’s posters(线段树+离散化)
题目 给定每张海报的覆盖区间,按顺序覆盖后,最后有几张海报没有被其他海报完全覆盖.离散化处理完区间端点,排序后再给相差大于1的相邻端点之间再加一个点,再排序.线段树,tree[i]表示节点i对应区间是 ...
- POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59239 Accepted: 17157 ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
随机推荐
- libiconv_百度百科
libiconv_百度百科 由于历史原因,国际化的文字常常由于语言或者国家的原因使用不同的编码.目录 1libiconv历史简介 2libiconv编码简介 3libico ...
- poj - 1228 - Grandpa's Estate
题意:原来一个凸多边形删去一些点后剩n个点,问这个n个点能否确定原来的凸包(1 <= 测试组数t <= 10,1 <= n <= 1000). 题目链接:http://poj. ...
- docker端口映射设置
摘自 Docker学习笔记(3)-- 如何使用Dockerfile构建镜像 # 映射一个端口 EXPOSE port1 # 相应的运行容器使用的命令 docker run -p port1 image ...
- Python 操作 MySQL--(pymysql)
h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...
- Twitter 新一代流处理利器——Heron 论文笔记之Heron架构
Twitter 新一代流处理利器--Heron 论文笔记之Heron架构 标签(空格分隔): Streaming-process realtime-process Heron Architecture ...
- Error occured processing XML 'Cannot find class [springmvc.extention.BeanArgumentResolver]'.
<Description Resource Path Location Type Error occured processing XML 'Cannot find class [springm ...
- 关于在用Swift开发iOS时如何隐藏NavigationBar和TabBar
举个例子:如果我有一个页面需要进入时同时隐藏NavigationBar和TabBar,那么我就在那个页面的ViewController的代码里加上下面的代码.就可以实现了.接下来告诉大家每一块要注意的 ...
- Repeater获取某一行TextBox值
TextBox tb = (TextBox)e.Item.FindControl("TextBoxID");
- TCP/IP协议族
1.TCP(传输控制协议)/IP(网际协议)协议族是一个网络通讯模型,以及一整个网络传输协议家族,为互联网的基础通讯架构. TCP/IP四层协议的表示方法: 2.TCP/IP参考模型映射到OSI模型: ...
- Csharp多态的实现(抽象类)
1.什么是抽象类 抽象类是虚拟的类,不能创建对象,用abstract修饰,在子类中用override进行重写 抽象类中可以存放抽象方法,属性,也可以存放非抽象方法,属性(这个在下面的代码可以看出来的) ...