1. 程式人生 > >poj 2528 Mayor's posters(線段樹+離散化)

poj 2528 Mayor's posters(線段樹+離散化)

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(n<=10000) 個人依次貼海報,給出每張海報所貼的範圍li,ri(1<=li<=ri<=10000000) 。求出最後還能看見多少張海報。

由於線段的資料範圍太大,所以如果這麼開資料一定會mle,我們會發現其實資料一共就10000*2個點,沒必要開這麼大,我們只需記錄線段的相對大小就可以了,因此需要用到離散化~
由於沒學離散化外加一直無心學習~~~emmm浪費了好多天

離散化見這~
https://blog.csdn.net/ling_wang/article/details/81707676

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

const int N = 10010;//由於將線段大小離散化了,所以最大值就變成有多少個點
int ha[10000010];//記錄每個點的雜湊值
int x[N<<1];//離散化用的備份,記錄所有線段的左右端點大小

struct node
{
    int l, r;//記錄左右端點
    bool flag;//記錄該線段是否被覆蓋
}tree[N<<3];//正常的線段樹不是開到4倍嗎,,,為啥這裡要開到8倍

struct post
{
    int l, r;//記錄離散化前的原始資料,即所有線段左右端點大小
}po[N];

void build(int l, int r, int rt)//建樹
{
    tree[rt].l = l; tree[rt].r = r; tree[rt].flag = 0;
    if(l == r)      return;
    int m = (l+r)>>1;
    build(l, m, rt<<1);
    build(m+1, r, rt<<1|1);
}

int made(int l, int r, int rt)
{
    if(tree[rt].flag)//如果已被覆蓋,該資料就不記錄
        return 0;
    else if(tree[rt].l == l && tree[rt].r == r){//如果該資料未被覆蓋,就記錄加一,且進行標記
        tree[rt].flag = 1;
        return 1;
    }
    int m = (tree[rt].l + tree[rt].r) >> 1;
    bool re;//記錄當所求線段與當前線段不同時,就向下遞迴的子樹的覆蓋情況
    if(m >= r)    re = made(l, r, rt<<1);//如果該線段全被位於當前線段的左子樹
    else if(m < l)   re = made(l, r, rt<<1|1);//如果該線段全位於當前線段的右子樹
    else{//如果該線段覆蓋了當前線段的兩個子樹,就都進行遍歷
        bool flag1 = made(l, m, rt<<1);
        bool flag2 = made(m+1, r, rt<<1|1);
        re = flag1 || flag2;//只要有一個子樹沒被覆蓋,當前線段就沒被覆蓋
    }
    if(tree[rt<<1].flag == 1 && tree[rt<<1|1].flag == 1)
        tree[rt].flag = 1;//如果兩個子樹都被覆蓋了,說明當前線段也被覆蓋了
    return re;
}

int main()
{
    int t, i, cnt, n;
    scanf("%d", &t);
    while(t--){
        cnt = 0;
        scanf("%d", &n);
        for(i = 0; i < n; i++){
            scanf("%d%d", &po[i].l, &po[i].r);
            x[cnt++] = po[i].l;
            x[cnt++] = po[i].r;
        }
        //離散化處理
        sort(x, x+cnt);
        cnt = unique(x, x+cnt) - x;
        for(i = 0; i < cnt; i++){
            ha[x[i]] = i;
        }
        build(0, cnt-1, 1);
        int s = 0;
        for(i = n-1; i >=0; i--){//從覆蓋在最上面的線段往下尋找
            if(made(ha[po[i].l], ha[po[i].r], 1))
                s++;
        }
        printf("%d\n", s);
    }
    return 0;
}