POJ2528-Mayor's posters(線段樹+離散化 or 分治+dfs)

題目鏈接

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.
http://poj.org/images/2528_1.jpg

Sample Input

1

5

1 4

2 6

8 10

3 4

7 10

Sample Output

4


題意

在一面牆上貼海報,先貼的海報會被後貼的蓋住,問最後能看到多少不同的海報。如果海報不完整,也算,但是完全被遮住了,就不算。

思路

線段樹維護一個數組,tree[i].w表示當前維護的區間的情況。

tree[i].w = -1 表示當前區間沒有海報,或者不止一個海報。

tree[i].w = n (n爲正整數)表示當前區間被一張海報覆蓋,即只有一張海報,n表示海報的編號。

query的過程,如果是w=n,說明有n這個編號的海報,vis[]數組標記一下,避免重複,然後返回1。如果w=0,且是葉子節點,則停止;如果不是葉子節點,則繼續遞歸。

初始化建樹的時候,要把w全部設爲-1,所有節點。

離散化

1000 0000太大,建不了數組,但是1 0000個海報,最多隻有2 0000個點,就是說數組中很多開了是完全沒用到的,所以建一個1-1000 0000到1-2 0000的映射,就可以開數組了。把無限空間中有限的個體映射到有限的空間中去。

具體做法: 線段樹離散化步驟

參考博客:鏈接

關於開數組大小的問題:Max=10010;tree[Max*12];12是3*4,3是算法的問題,本來應該是2,但是有坑的地方。

1
3
2 4
1 2
4 5

應該是3,但沒有處理就會是2。因爲有的位置被省略了,所以每次大於1的兩個數之間都要加上一個數。所以本來海報的兩個邊本來是Max*2,如果兩個數之間都加上一個數,就是Max*3,然後線段樹要開4倍空間的大小,所以是12。

vis[Max*3],a[Max*3],和上面3一樣。

方法2:分治+DFS

另一種方法是反向考慮,後貼的海報一定在前面,所以從後往前了話,貼的海報就一定貼上了,該位置就不能再貼別的海報。如果能貼則貼,貼完後還有位置就遞歸。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define  ll long long
using namespace std;

struct node
{
    int l,r,w;
};
const int Max=10010;
node tree[Max*12];
int vis[Max*3],init[Max][2],a[Max*3];

void build(int k,int l,int r)
{
    tree[k].l = l; tree[k].r = r; tree[k].w = -1;
    if(l==r){
        tree[k].w = -1;
        return;
    }
    int mid = (l + r)/2;
    build(k*2,l,mid);
    build(k*2+1,mid+1,r);
}

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

void update(int k,int l,int r,int v) //區間更新
{
    if(l>tree[k].r||r<tree[k].l) return;
    if(l<=tree[k].l&&tree[k].r<=r){
        tree[k].w = v;
        return;
    }
    if(tree[k].w!=-1) pushdown(k);
    update(k*2,l,r,v);
    update(k*2+1,l,r,v);
}

int query(int k)
{
    if(tree[k].w!=-1){
       if(!vis[tree[k].w]){
            vis[tree[k].w] = 1;
            return 1;
        }
        else return 0;
    }
    if(tree[k].l==tree[k].r){//如果是葉子節點,退出
        return 0;
    }
    return query(k*2) + query(k*2+1);
}

int main()
{
    int T,N,tot;
    scanf("%d",&T);
    while(T--){
        tot = 0;
        memset(vis,0,sizeof(vis));
        memset(init,0,sizeof(init));

        scanf("%d",&N);
        for(int i=0;i<N;i++){
            scanf("%d%d",&init[i][0],&init[i][1]);
            a[tot++] = init[i][0];
            a[tot++] = init[i][1];
        }
        sort(a,a+tot);
        int m = unique(a,a+tot)-a;
        for(int i=m-1;i>=1;i--){
            if(a[i]-a[i-1]>1)
                a[m++] = a[i]-1;
        }
        build(1,1,m);
        sort(a,a+m);
        for(int i=0;i<N;i++){
            int x = lower_bound(a,a+m,init[i][0])-a+1;//樹定義的下標從1開始,所以+1
            int y = lower_bound(a,a+m,init[i][1])-a+1;
            update(1,x,y,i+1);//i+1表示從1到N
        }
        printf("%d\n",query(1));
    }
}
#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

bool vis[10010];
int c,n,ans;
int st[10010],ed[10010];

inline void dfs(int l,int r,int x)
{
    if(!x) return;
    if(st[x]<=r&&l<=ed[x]){//(st[x],ed[x])和(l,r)有交集
        if(!vis[x]){
            vis[x] = true; ans++;
        }
        if(l<st[x]) dfs(l,st[x]-1,x-1);
        if(r>ed[x]) dfs(ed[x]+1,r,x-1);
    }
    else dfs(l,r,x-1);
}

int main()
{
    int t; scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d%d",&st[i],&ed[i]);
        }
        memset(vis,0,sizeof(vis));
        ans = 0;
        dfs(1,10000000,n);
        printf("%d\n",ans);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章