Mayor's posters - POJ 2528 - 线段树 + 离散


Mayor’s posters - POJ 2528 - 线段树 + 离散

  前言:这道坑爹题花了我一天的时间去理解,卡在离散和更新那里,无论怎样都想不明白。最后画了张图,这才终于明白原理。

  题目:

  The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:

  ●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

  The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+1 ,… , ri.

Output

  For each input data set print the number of visible posters after all the posters are placed.

  The picture below illustrates the case of the sample input.

Sample

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

  题意:在墙上按顺序贴海报,后贴的海报可以覆盖之前已有的海报(废话),问最后可以看见几张海报。

  思路:因为每贴一张海报都会涉及到区间更新,所以很容易联想到线段树。可是如果硬搞的话会超时,因为数据范围太大了。所以需要离散化。

  可是普通的离散化并不可以,比如下面这组数据:

1
3
1 10
1 4
6 10

  会发现普通离散化后答案是错的。那么做一个小小的处理,如果相邻数字间距大于1的话,就在这两个数字间插入一个介于他们俩之间的数字。比如{1,4,6,10}加成{1,4,5,6,10}就OK。

  刚开始我自己写了一个besearch()用来搜索海报的左右端点,代码如下:

int bsearch(int l, int r, int aim) {
int mid;
while(l<=r) {
mid = (l+r)>>1;
if(x[mid]==aim) return mid;
else if(x[mid] > aim) r = mid-1;
else l = mid+1;
}
return -1;
}

  后来突然想起来还有lower_bound(),就把代码改了一下,时间从94ms变成了79ms,相信数据量再大一些的话差距会更明显一些。

  还有一个binary_search()函数有待我琢磨一下,网上给的一些解释好像是有误的?至少我按照网上的写法去用的时候会有问题。

附上AC代码:

//
// main.cpp
// L
//
// Created by LucienShui on 2017/5/28.
// Copyright © 2017年 LucienShui. All rights reserved.
//

#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#define memset(a,b) memset(a,b,sizeof(a))
#define il inline
#define ll long long
#define ull unsigned long long

using namespace std;

#define ls (u<<1)
#define rs (u<<1|1)

const int maxn = 10000+7;
int m,x[maxn<<2],node[maxn<<4],ans;
//x存储离散后的坐标,node为线段树节点
struct {
int l,r;
}post[maxn];//存储每张海报
bool book[maxn];

void pushdown(int u) {//Lazy操作
node[ls] = node[rs] = node[u];
node[u] = -1;
return ;
}

void update(int u, int l, int r, int b, int e, int num) {
//u为根,l为左区间,r为右区间
//b为搜索的begin,e为搜索的end,num为需要更新到的值
if(b <= l && r <= e) {
node[u] = num;
return ;
}
if(~node[u]) pushdown(u);
int mid = (l+r)>>1;
if(e <= mid) update(ls,l,mid,b,e,num);
else if(mid < b) update(rs,mid+1,r,b,e,num);
else {
update(ls,l,mid,b,e,num);
update(rs,mid+1,r,b,e,num);
}
}

void query(int u, int l, int r) {//区间更新
if(~node[u]) {
if(!book[node[u]]) {
ans++;
book[node[u]] = true;
}
return ;
}
if(l==r) return ;
if(~node[u]) pushdown(u);
int mid = (l+r)>>1;
query(ls,l,mid);
query(rs,mid+1,r);
}

int main ()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int t,n;
cin >> t;
while(t--) {
memset(node,-1);
memset(book,false);
int nn = 0;
scanf("%d",&n);
for(int i=1 ; i<=n ; i++) {
scanf("%d%d",&post[i].l,&post[i].r);
x[++nn] = post[i].l;
x[++nn] = post[i].r;
}
sort(x+1,x+1+nn);//开始离散化
m = 1;
for(int i=2 ; i<=nn ; i++) if(x[i] != x[i-1]) x[++m] = x[i];
for(int i=m ; i>1 ; i--) if(x[i] - x[i-1] > 1) x[++m] = x[i] - 1;
sort(x+1,x+1+m);//离散化结束
for(int i=1 ; i<=n ; i++) {
//lower_bound搜索
int l = int(lower_bound(x+1,x+m+1,post[i].l)-x);
int r = int(lower_bound(x+1,x+m+1,post[i].r)-x);
update(1,1,m,l,r,i);
}
ans = 0;
query(1,1,m);
printf("%d\n",ans);
}
return 0;
}

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号