POJ-2528 Mayor's posters (线段树、段覆盖+段查询、离散化)

Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 67664   Accepted: 19553

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

 
题目大意:给定n个区间,表示在该区间上贴海报,问最后可以看见几张海报。
 
解题思路:首先想到的是线段树,此题相当于是段更新+段询问。然后看见数据很大,肯定需要离散化,然后 从后往前贴 如果发现这块区域被完全覆盖了,那就返回。(覆盖为1,否则为0)
     对于离散化的理解:将大数化成小数,其相对关系不改变。如(1000,100,10000)对应的序号为(1,2,3),离散化后相当于(1000,1)(100,2)(10000,3)对应为(2,1,3)。
     方法:个人觉得用结构体+数组的方法比较好理解。结构体(元素+序号),用数组来离散化,上述例子为例,离散化后a[1] = 2,a[2] =1,a[3] = 3。简单来说,即a[(原来的序号)] = 按大小排序后的序号。
参考博客:http://www.cnblogs.com/jackge/archive/2013/04/25/3041637.html
 
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=1e4+5;
const int maxn=16*N;
struct node
{
    int x,id;
};
bool cmp1(node a, node b)
{
    return a.x<b.x;
}
bool cmp2(node a,node b)
{
    if(a.id==b.id)
        return a.x<b.x;
    return a.id>b.id;
}
node a[maxn];
int flag,n,ql,qr,sum[maxn];

void pushup(int num)
{
    sum[num] = sum[num*2]&&sum[num*2+1];//如果左右区间都被覆盖,则该区间被覆盖
}

void build(int num,int l,int r)
{
    sum[num] = 0;//建树,初始状态都没覆盖,状态为0。
    if(l==r)
        return ;
    int mid=(l+r)/2;
    build(num*2,l,mid);
    build(num*2+1,mid+1,r);
    pushup(num);
}

void query(int num,int l,int r)
{
    if(sum[num])//[l,r]区间已满
    {
        return ;
    }
    if(ql<=l&&qr>=r)//覆盖改区间,1表示覆盖。
    {
        sum[num] = 1;
        flag = 1;
        return ;
    }
    int mid=(l+r)/2;
    if(ql<=mid)
        query(num*2,l,mid);
    if(qr>mid)
        query(num*2+1,mid+1,r);
    pushup(num);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&a[i*2-1].x,&a[i*2].x);
            a[i*2-1].id = a[i*2].id = i;
        }
        
        //离散化
        sort(a+1,a+2*n+1,cmp1);
        int last=0,pre=0;
        for(int i=1;i<=2*n;i++)
        {
            if(a[i].x==pre)
            {
                a[i].x = last;
            }
            else
            {
                pre = a[i].x;
                a[i].x = ++last;
            }
        }
        //离散化后a[i].x就是更改后的值,即第几大
        build(1,1,2*n);
        sort(a+1,a+2*n+1,cmp2);//按序号排序。从左到右遍历
        int ans = 0;
        for(int i=1;i<=2*n;i+=2)
        {
            //printf("1
");
            ql = a[i].x;
            qr = a[i+1].x;
            flag = 0;
            query(1,1,2*n);
            if(flag == 1)
                ans++;
        }
        printf("%d
",ans);
    }
}
原文地址:https://www.cnblogs.com/WWkkk/p/7374913.html