【POJ 2528】Mayor's posters(线段树,离散化)

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

题意:

有一面很长的墙,依次按顺序往上贴海报,后面的会覆盖前面的。输入1 4即代表在单位1~4上贴满此海报。求贴完之后你能看到多少不同种类的海报并输出这个种类数量。

思路:

由于输入数据量很大,需要将数据离散化。(可以有效的减小数据量,降低复杂度)
离散化:将数据排序之后用C++中STL中的unique函数去重。
Pushup单点更新,将叶子结点的信息传递上来。
这里将更新和查询在同一函数(Query)中完成。

线段树精髓(转):

1、 线段树是二叉树,且必定是平衡二叉树,但不一定是完全二叉树。
2、 对于区间[a,b],令mid=(a+b)/2,则其左子树为[a,mid],右子树为[mid+1,b],当a==b时,该区间为线段树的叶子,无需继续往下划分。
3、 线段树虽然不是完全二叉树,但是可以用完全二叉树的方式去构造并存储它,只是最后一层可能存在某些叶子与叶子之间出现“空叶子”,这个无需理会,同样给空叶子按顺序编号,在遍历线段树时当判断到a==b时就认为到了叶子,“空叶子”永远也不会遍历到。
4、 之所以要用完全二叉树的方式去存储线段树,是为了提高在插入线段和搜索时的效率。用p*2,p*2+1的索引方式检索p的左右子树要比指针快得多。
5、线段树的精髓是,能不往下搜索,就不要往下搜索,尽可能利用子树的根的信息去获取整棵子树的信息。如果在插入线段或检索特征值时,每次都非要搜索到叶子,还不如直接建一棵普通树更来得方便。

代码:

#include<iostream>
#include<cstring> 
#include<cstdio>
#include<algorithm>
#define MAX 10005
using namespace std;
struct Tree{
    int left;
    int right;
    bool iscover;
}tree[MAX<<4];
int finalpos[MAX<<1];
bool flag=false;
int ans=0,t,n;
void Pushup(int i)
{
    tree[i].iscover=(tree[i*2].iscover&&tree[i*2+1].iscover);
}

void Built(int l,int r,int i)
{
    tree[i].left=l;
    tree[i].right=r;
    tree[i].iscover=false;
    if(l==r) return ;
    int mid=(l+r)/2;
    Built(l,mid,2*i);
    Built(mid+1,r,i*2+1);

}


void Query(int l,int r,int i)
{
    if(tree[i].iscover) return ;
    if(l>tree[i].right||r<tree[i].left) return ;
    if(tree[i].left==tree[i].right)
    {
        if(!tree[i].iscover) 
        {
            tree[i].iscover=true;
            flag=true;
        }
        return ;
    }
    int mid=(l+r)/2;
    /*
    Query(l,mid,i*2);
    Query(mid+1,r,i*2+1);
    */
    Query(l,r,i*2);
    Query(l,r,i*2+1);
    Pushup(i);
}
int tag=0;
int le[MAX*2],ri[MAX*2];
void discretization()
{
    tag=0;
    for(int i=0;i<n;i++){
        scanf("%d %d",&le[i],&ri[i]);
        finalpos[tag++]=le[i];
        finalpos[tag++]=ri[i];
    }
    sort(finalpos,finalpos+tag);
    tag=unique(finalpos,finalpos+tag)-finalpos;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        discretization();
        Built(1,MAX<<2,1);
        ans=0;
        for(int i=n-1;i>=0;i--)
        {
            flag=false;
            int a=lower_bound(finalpos,finalpos+tag,le[i])-finalpos+1;
            int b=lower_bound(finalpos,finalpos+tag,ri[i])-finalpos+1;
            Query(a,b,1);
            if(flag) ans++;
        }
        printf("%d\n",ans); 
    }   

    return 0;
}

猜你喜欢

转载自blog.csdn.net/chen_yuazzy/article/details/77427173