POJ2528-Mayor's posters(线段树+离散化)

原题地址:点击打开链接

Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 70678   Accepted: 20384

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   //1组测试数据
5   //5张海报
1 4   //第一张覆盖1~4
2 6   //第二张覆盖2~6
8 10  //第三张覆盖8~10
3 4   //第四张覆盖3~4
7 10   //第五张覆盖7~10


思路:

因为题目给的数据范围很大,直接开数组的话是不能的,所以要用到离散化;

比如说给数据为:

5

1 4

2 8

8 1000000000000000000000

这个时候总不能开  1000000000000000000000  的数组吧,那么我们可以将这个区间离散化,具体步骤如下:

首先定义一个数组a将1 2 4 8 8  1000000000000000000000 全都存下来并排序去重;

那么此时,a[0]=1 a[1]=2 a[3]=4 a[4]=8 a[5]=1000000000000000000000

剩下的用线段树解决就行了


代码如下:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int maxn=10005;
typedef struct{
    int l,r;
    int c;
    int mid;
}TREE;
TREE tree[maxn<<4];
int l[maxn];
int r[maxn];
int a[maxn<<3];
int vis[maxn<<3];
void build(int l,int r,int rt)
{
    if(l==r)
    {
        tree[rt].l=l;
        tree[rt].r=r;
        tree[rt].mid=(tree[rt].l+tree[rt].r)/2;
        tree[rt].c=-1;
        return;
    }
    int mid=(l+r)/2;
    build(lson);
    build(rson);
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].mid=(tree[rt].l+tree[rt].r)/2;
    tree[rt].c=-1;
}
void updata(int l,int r,int rt,int c)
{
    if(tree[rt].l==l&&tree[rt].r==r)
    {
        tree[rt].c=c;
        return;
    }
    if(tree[rt].c!=-1)
    {
        tree[rt<<1].c=tree[rt].c;
        tree[rt<<1|1].c=tree[rt].c;
        tree[rt].c=-1;
    }
    int mid=(l+r)/2;
    if(l>tree[rt].mid)
    {
        updata(l,r,rt<<1|1,c);
    }
    else if(r<=tree[rt].mid)
    {
        updata(l,r,rt<<1,c);
    }
    else
    {
        mid=tree[rt].mid;
        updata(lson,c);
        updata(rson,c);
    }
}
int ans=0;
void query(int l,int r,int rt)
{
    if(tree[rt].c!=-1&&vis[tree[rt].c]!=1)
    {
        ans++;
        vis[tree[rt].c]=1;
        tree[rt].c=-1;
        return;
    }
    if(l==r) return;
    int mid=(l+r)/2;
    query(lson);
    query(rson);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        int cnt=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&l[i],&r[i]);
            a[cnt++]=l[i];
            a[cnt++]=r[i];
        }
        sort(a,a+cnt);
//        for(int i=0;i<cnt;i++)
//        {
//            printf("%d ",a[i]);
//        }
//        cout<<endl;
        int cnt2=0;
        for(int i=0;i<cnt;i++)
        {
            if(a[i]!=a[i+1])
            {
                a[cnt2++]=a[i];
            }
        }
        for(int i=cnt2;i>=0;i--)
        {
            if(a[i]-a[i-1]>1) a[cnt2++]=a[i-1]+1;
        }
        sort(a,a+cnt2);
//        for(int i=0;i<cnt;i++)
//        {
//            printf("%d ",a[i]);
//        }
//        cout<<endl;
        build(1,cnt2,1);
        for(int i=0;i<n;i++)
        {
            int x=lower_bound(a,a+cnt2,l[i])-a+1;
            int y=lower_bound(a,a+cnt2,r[i])-a+1;
//            cout<<"x="<<x<<"y="<<y<<endl;
            updata(x,y,1,i);
        }
//        cout<<"ok"<<endl;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=19;i++)
        {
            cout<<tree[i].c<<" ";
        }
        cout<<endl;
        query(1,cnt2,1);
        cout<<ans<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/acm513828825/article/details/78631042