POJ 2528 Mayor's posters (线段树成段更新+离散化)

 Mayor's posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 35515 Accepted: 10306

Description
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 li 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 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+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 Inpu t

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

Sample Output

4

Source

Alberta Collegiate Programming Contest 2003.10.18


题意:

有好多的候选市长要竞选,他们要将海报贴在一面墙上宣传自己。海报的高度都是和墙的高度一样的,宽度从1到好大好大。海报贴的左右端点的位置都是这一大段范围里的两个确切整数点。之后贴的海报可以覆盖之前贴的,问最后露出脸的海报有几张(部分露出也算)。

思路:

一开始对着样例笔画了几下,发现最后越是之后贴的海报呢露脸的机会越大,那么就有了逆着顺序做题的想法。

使用线段树的成段更新,把有覆盖过的点都被标上1,那么连续n段的话就会被标上n。如样例:(逆序)先是来了一张7--10范围的海报,更新时把7--10范围都给标记了,之后来了一张8--10范围的海报,可这8--10范围的海报在更新时什么都标记不了,就是说没办法露面了,那么这一张在正序中就是被覆盖的。

使用这种方法可以过样例,但是对于这道题,还有一道坎,就是以前说的宽度从1到好大好大。线段树遇到这好大好大的数据范围,很容易TLE+MLE的。网上搜了一下,需要使用离散化,之前听说过,但还没有学过,今天试着看了看离散化,有了点感觉,自己试着敲了一下。改了好多种敲离散的方法,都是出现RE,最后就把线段树里的初始化4倍大小的数组直接改成8倍就过了,很奇怪啊,求大神解释。

PS:给两组特殊样例:

3
1 10
1 4
5 10
(解为2)

3
1 10
1 4
6 10

(解为3)


/*************************************************************************
	> File Name: poj2528.cpp
	> Author: BSlin
	> Mail:  
	> Created Time: 2013年09月19日 星期四 11时07分00秒
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define MP make_pair
#define INF (1<<30)
#define PI acos(-1.0)
#define esp 1e-8
const int dx[4]={0,0,0,0};
using namespace std;
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)
#define LL __int64
#define LLS "%" "I" "6" "4" "d"
#else
#define LL long long
#define LLS "%" "l" "l" "d"
#endif



#define MAXN 20020

struct date {
    int l,r;
    int num,id;
    bool operator <(const date b) const {
        return num < b.num;
    }
}a[10010],ep[MAXN];

struct node {
    int L,R,sum;
    int mark;
}tree[MAXN<<3];

int flag,dis[MAXN][2];

void up(int p) {
    tree[p].sum = tree[p<<1].sum + tree[p<<1|1].sum;
}

void build(int L,int R,int p) {
    tree[p].L = L;
    tree[p].R = R;
    tree[p].sum = 0;
    if(L == R) return ;
    int mid = (L + R) >> 1;
    build(L,mid,p<<1);
    build(mid+1,R,p<<1|1);
}

void update(int L,int R,int p) {
    if(L <= tree[p].L && tree[p].R <= R) {
        if(tree[p].sum != tree[p].R - tree[p].L + 1) {
            flag = 1;
            tree[p].sum = tree[p].R - tree[p].L + 1;
        }
        return ;
    }
    if(tree[p].sum == tree[p].R - tree[p].L + 1) return ;
    int mid = (tree[p].L + tree[p].R) >> 1;
    if(R <= mid) update(L,R,p<<1);
    else if(L > mid) update(L,R,p<<1|1);
    else {
        update(L,mid,p<<1);
        update(mid+1,R,p<<1|1);
    }
    up(p);
}

int main(int argc, char** argv) {
    //read;
    int t,n,ans,temp,hash,p,max;
    scanf("%d",&t);
    while(t--) {
        scanf("%d",&n);
        memset(dis,0,sizeof(dis));
        p=0;
        for(int i=0;i<n;i++) {
            scanf("%d%d",&a[i].l,&a[i].r);
            ep[p<<1].num = a[i].l;
            ep[p<<1].id = i + 1;
            ep[p<<1|1].num = a[i].r;
            ep[p<<1|1].id = -(i + 1);
            p++;
        }
        sort(ep,ep+2*p);           //离散化,排序+映射
        hash = 0;
        temp = 0;
        for(int i=0;i<2*p;i++) {
            if(ep[i].id > 0) {
                if(temp != ep[i].num){
                    hash++;
                    if(temp + 1 != ep[i].num) //为了防止离散化中出现的小问题
                        hash++;
                }
                dis[ep[i].id - 1][0] = hash;
            }
            else {
                if(temp != ep[i].num) {
                    hash++;
                    if(temp + 1 != ep[i].num) 
                        hash++;
                }
                dis[- ep[i].id - 1][1] = hash;
            }
            temp = ep[i].num;
        }
        build(1,hash,1);
        ans = 0;
        for(int i=n-1;i>=0;i--) {
            flag = 0;
            update(dis[i][0],dis[i][1],1);
            if(flag){
                ans++;  
            } 
        }
        printf("%d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值