【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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章