POJ 2528 (线段树 + 离散化)

Mayor's posters

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 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 Input

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

Sample Output

4

题目大意:给你一个无限长的板子,然后依次往上面贴n张等高的海报,问你最后能看到多少张海报。

思路分析:题目li,ri 的范围特别大(直接开大数组肯定MLE),而 n 的范围则相对较小,则需要对其进行离散化。(关于离散化,可参考我转载的博客: https://blog.csdn.net/no_o_ac/article/details/81193135)。 不过本题离散化的处理还需要改变一下。

本题普通离散化的缺陷:
例子一:1-10 1-4 5-10
例子二:1-10 1-4 6-10
普通离散化后都变成了[1,4][1,2][3,4]
线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?
例子一是完全被覆盖掉了,而例子二没有被覆盖

解决的办法则是对于距离大于1的两相邻点,中间再插入一个点

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;

const int maxn = 20000+ 20;     //每一段包含起点与终点, 10000 * 2
int l[maxn],r[maxn];
int tree[maxn << 4];            //存每一个root,,, 放的海报
int lisan[maxn * 3];
set<int> s;

void pushdown(int root){
    tree[root*2] = tree[root*2+1] = tree[root];
    tree[root] = -1;
}

void update(int l,int r,int ql,int qr,int root,int p){
    if(ql <= l && r <= qr){
        tree[root] = p;
    }else {
        if(tree[root] != -1) pushdown(root);       //如果当前位置贴了海报,就将儿子更新一下,(lazy标记)
        int mid = (l + r) / 2;
        if(ql <= mid) update(l,mid,ql,qr,root * 2,p);
        if(qr > mid) update(mid + 1,r,ql,qr,root * 2 + 1,p);
    }
}

void query(int l,int r,int root){
    if(tree[root] != -1){
        s.insert(tree[root]);                     // 将海报(可以说是编号)放入set s;
        return ;
    }
    if(l == r) return ;
    if(tree[root]!=-1) pushdown(root);
    int mid = (l + r) / 2;
    query(l,mid,root * 2);
    query(mid + 1,r,root * 2 + 1);
}

int main()
{
    int c,n; scanf("%d",&c);
    while(c--){
        s.clear();
        memset(tree,-1,sizeof(tree));
        scanf("%d",&n);
        int pl = 0;
        ///离散化
        for(int i = 0;i < n;i ++){
            scanf("%d%d",&l[i],&r[i]);
            lisan[pl ++] = l[i];
            lisan[pl ++] = r[i];
        }
        sort(lisan,lisan + pl);
        int pli = unique(lisan,lisan + pl) - lisan;       ///对排序后的序列去重(此去重只是把有重复的元素放在序列后面),并返回第一个重复元素的地址
        int plisan = pli;
        for(int i = 1;i < plisan;i ++){
            if(lisan[i] - lisan[i-1] > 1)
                lisan[pli ++] = lisan[i-1] + 1;
        }
        sort(lisan,lisan + pli);
        ///pli  即为最终的线段树 区间右边界
        for(int i = 0;i < n;i ++){
            int x = lower_bound(lisan,lisan + pli,l[i]) - lisan;    ///lower_bound(), 返回第一个不大于 l[i] 的地址
            int y = lower_bound(lisan,lisan + pli,r[i]) - lisan;
            update(0,pli - 1,x,y,1,i);
        }
        query(0,pli - 1,1);
        printf("%d\n",s.size());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_o_ac/article/details/81203863