1. 程式人生 > >POJ 2528 (線段樹 + 離散化)

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;
}