【poj2528】【離散線段樹】【動態建線段樹】【漂浮法】Mayor's posters

Mayor’s posters

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 59730 Accepted: 17309

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 Input

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

Sample Output

4
這裏寫圖片描述

Source

Alberta Collegiate Programming Contest 2003.10.18

這道題題目大意很簡單,就是貼了一堆海報,每個海報是一段區間,問你最後看得到那些海報,然後這裏我想說3個方法


1、離散線段樹
這個應該是第一個想到的辦法了吧,就是離散線段樹,沒什麼好說的,寫起來也不是很難,就是要注意的是這裏每個海報是覆蓋一個點,如果直接離散那麼比如原來有3個海報1~30,1~10,20~30離散後變成1~4,1~2,3~4,第一張海報就被覆蓋了,所以我們在離散的時候就加一個數在兩個之間加一個就可以了


2、線段樹動態建樹
寫的時候我就在想,爲什麼不動態建樹呢?反正是一樣的複雜度,離散起來多麼麻煩,然後我就寫了個動態建樹,poj跑出來時間一樣,都是172MS,動態建樹寫起來就很簡單了,但好像大多數人都寫的數組型線段樹,這個時候就體現了指針型的好處


3、漂浮法
感覺好強orz,這個辦法好厲害,就是對於每張海報,遇到其他的海報就段成一節或兩節繼續向上漂,看能不能浮出水面,時間稍微慢一點250MS

然後3個代碼我都放在最後面,實際上一二兩種方法基本是一樣的,下面的2基本就是1去掉離散


1、離散線段樹

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))
#define clrmax(x)  memset(x,127,sizeof(x))

using namespace std;

inline int read()
{
    char c;
    int ret=0;
    while(!(c>='0'&&c<='9'))
        c=getchar();
    while(c>='0'&&c<='9')
    {
        ret=(c-'0')+(ret<<1)+(ret<<3);
        c=getchar();
    }
    return ret;
}

#define M 100005

struct tree2
{
    tree2 *lson,*rson;
    int l,r,bo;
}*root,dizhi[M<<4];

int l[M],r[M],n,x[M<<2],b[M];
int t,m,k;

void update(tree2 *&tree)
{
    if(tree->lson==NULL)tree->lson=&dizhi[t++];
    if(tree->rson==NULL)tree->rson=&dizhi[t++];
    tree->lson->bo=tree->rson->bo=tree->bo;
}

void change(tree2 *tree,int l,int r,int x,int y,int num)
{
    if(x<=l&&y>=r)
    {
        tree->bo=num;
        return ;
    }
    if(tree->bo)update(tree);
    tree->bo=0;
    int mid=(l+r)>>1;
    if(x<=mid)
    {
        if(tree->lson==NULL)tree->lson=&dizhi[t++];
        change(tree->lson,l,mid,x,y,num);
    }
    if(y>mid)
    {
        if(tree->rson==NULL)tree->rson=&dizhi[t++];
        change(tree->rson,mid+1,r,x,y,num);
    }
}

void find(tree2 *tree,int l,int r)
{
    if(tree==NULL)return ;
    if(tree->bo!=0)
    {
        b[tree->bo]=1;
        return ;
    }
    if(l==r)return ;
    int mid=(l+r)>>1;
    if(tree->lson)find(tree->lson,l,mid);
    if(tree->rson)find(tree->rson,mid+1,r);
}

int main()
{
    freopen("in.txt","r",stdin);
    freopen("out_std.txt","w",stdout);
    int q=read();
    while(q--)
    {
        t=0;m=0;k=0;
        clr(dizhi);clr(l);clr(r);
        clr(x);clr(b);
        root=&dizhi[t++];
        n=read();
        for(int i=1;i<=n;i++)
        {
            x[++k]=l[i]=read();
            x[++k]=r[i]=read();
        }
        sort(x+1,x+1+k);
        for(int i=1;i<=k;i++)
            if(x[i]!=x[i-1])x[++m]=x[i];
        for(int i=m;i>=1;i--)
            if(x[i]-x[i-1]>1)x[++m]=x[i]-1;
        sort(x+1,x+1+m);
        for(int i=1;i<=n;i++)
        {
            int p1=lower_bound(x+1,x+1+m,l[i])-x,
                p2=lower_bound(x+1,x+1+m,r[i])-x;
            change(root,1,m,p1,p2,i);
        }   
        find(root,1,m);
        int sum=0;
        for(int i=1;i<=n;i++)
            if(b[i])sum++;
        printf("%d\n",sum);
    }
}

2、線段樹動態建樹

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))
#define clrmax(x)  memset(x,127,sizeof(x))

using namespace std;

inline int read()
{
    char c;
    int ret=0;
    while(!(c>='0'&&c<='9'))
        c=getchar();
    while(c>='0'&&c<='9')
    {
        ret=(c-'0')+(ret<<1)+(ret<<3);
        c=getchar();
    }
    return ret;
}

#define M 10005

struct tree2
{
    tree2 *lson,*rson;
    int bo;
}*root,dizhi[M<<4];

int n,b[M],t;

void update(tree2 *&tree)
{
    if(tree->lson==NULL)tree->lson=&dizhi[t++];
    if(tree->rson==NULL)tree->rson=&dizhi[t++];
    tree->lson->bo=tree->rson->bo=tree->bo;
    tree->bo=0;
}

void change(tree2 *tree,int l,int r,int x,int y,int num)
{
    if((x<=l&&y>=r)||(l==r))
    {
        tree->bo=num;
        return ;
    }
    if(tree->bo)update(tree);
    int mid=(l+r)>>1;
    if(x<=mid)
    {
        if(tree->lson==NULL)tree->lson=&dizhi[t++];
        change(tree->lson,l,mid,x,y,num);
    }
    if(y>mid)
    {
        if(tree->rson==NULL)tree->rson=&dizhi[t++];
        change(tree->rson,mid+1,r,x,y,num);
    }
}

void find(tree2 *tree,int l,int r)
{
    if(tree==NULL)return ;
    if(tree->bo)
    {
        b[tree->bo]=1;
        return ;
    }
    if(l==r)return ;
    int mid=(l+r)>>1;
    if(tree->lson)find(tree->lson,l,mid);
    if(tree->rson)find(tree->rson,mid+1,r);
}

int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    int q=read();
    while(q--)
    {
        n=read();
        clr(dizhi);
        t=0;
        root=&dizhi[t++];
        for(int i=1;i<=n;i++)
        {
            int a=read(),b=read();
            change(root,1,1E7,a,b,i);
        }
        clr(b);
        find(root,1,1E7);
        int sum=0;
        for(int i=1;i<=n;i++)
            if(b[i])sum++;
        printf("%d\n",sum);
    }
}

3、漂浮法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))
#define clrmax(x)  memset(x,127,sizeof(x))

using namespace std;

inline int read()
{
    char c;
    int ret=0;
    while(!(c>='0'&&c<='9'))
        c=getchar();
    while(c>='0'&&c<='9')
    {
        ret=(c-'0')+(ret<<1)+(ret<<3);
        c=getchar();
    }
    return ret;
}

#define M 10005

int n,l[M],r[M];

bool pf(int L,int R,int x)
{
    while(x<=n&&(R<l[x]||L>r[x]))x++;
    if(x>n)return R-L>=0;
    if(L>=l[x]&&R<=r[x])return 0;
    int ret=0;
    if(L<l[x])ret=pf(L,min(l[x],R)-1,x+1);
    if(!ret&&R>r[x])ret=pf(max(r[x],L)+1,R,x+1);
    return ret;
}

int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    int q=read();
    while(q--)
    {
        n=read();
        for(int i=1;i<=n;i++)
            l[i]=read(),r[i]=read();
        int sum=0;
        for(int i=1;i<=n;i++)
            if(pf(l[i],r[i],i+1))sum++;
        printf("%d\n",sum);
    }
    return 0;
}

大概就是這個樣子,如果有什麼問題,或錯誤,請在評論區提出,謝謝。

發佈了76 篇原創文章 · 獲贊 10 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章