poj_2528Mayor's posters线段树+离散化

Mayor's posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 43519 Accepted: 12697

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]



本题是poj2777 Count color的升级版,多了一步离散化处理,剩下都是统计区间内有多少种颜色(海报),本题是在统计区间内有多少海报时,可以有两种方法:

第一种是查询到某区间有海报时,先判断是否标记过,若没有则进行标记,并计数加1,最后直接输出总计数即可,如代码所示;

第二种查询方法为查到某区间有海报时,直接对该海报标记,最后再重新统计有多少海报被标记过,即为区间内有多少海报,如代码中注释部分。

对于2777题也可以用两种查询方法,效果都一样。


思路:离散化。先把所有的始终点排序,并去掉重复的。这里用到了STL中的unique函数。然后,就相当于得到了若干个区间,而且每个区间只可能某一同一张纸。这就是离散化精妙的地方。然后再标记各区间,最后统计有多少张不同的纸就可以了。


关于离散化举例:转自優YoU http://blog.csdn.net/lyy289065406/article/details/6799170

通俗点说,离散化就是压缩区间,使原有的长区间映射到新的短区间,但是区间压缩前后的覆盖关系不变。举个例子:

有一条1到10的数轴(长度为9),给定4个区间[2,4] [3,6] [8,10] [6,9],覆盖关系就是后者覆盖前者,每个区间染色依次为 1 2 3 4。

现在我们抽取这4个区间的8个端点,2 4 3 6 8 10 6 9

然后删除相同的端点,这里相同的端点为6,则剩下2 4 3 6 8 10 9

对其升序排序,得2 3 4 6 8 9 10

然后建立映射

2     3     4     6     8     9   10

↓     ↓      ↓     ↓     ↓     ↓     ↓

1     2     3     4     5     6     7

那么新的4个区间为 [1,3] [2,4] [5,7] [4,6],覆盖关系没有被改变。新数轴为1到7,即原数轴的长度从9压缩到6,显然构造[1,7]的线段树比构造[1,10]的线段树更省空间,搜索也更快,但是求解的结果却是一致的。

离散化时有一点必须要注意的,就是必须先剔除相同端点后再排序,这样可以减少参与排序元素的个数,节省时间。



</pre><p><pre name="code" class="cpp">#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
const int maxn = 10000000;
struct node{
    int l,r,cor;
}a[maxn<<2];
int c[10000+10][2];
int d[10000*4+10],len,cnt;
bool vis[10000+10];
void build(int l,int r,int cur){
    a[cur].l = l ;
    a[cur].r = r ;
    a[cur].cor = 0;
    if(l == r )return ;
    int mid = ( l + r)>>1;
    build(l,mid,cur<<1);
    build(mid+1,r,cur<<1|1);
}
void pushdown(int cur){
    if(a[cur].cor){
        a[cur<<1].cor = a[cur].cor;
        a[cur<<1|1].cor = a[cur].cor;
        a[cur].cor = 0;
    }
}
void update(int l,int r,int v,int cur){
    if(a[cur].l ==l &&a[cur].r == r){
        a[cur].cor=v;
        return ;
    }
    pushdown(cur);
    int mid = (a[cur].l+a[cur].r )>>1;
    if(r <= mid)
        update(l,r,v,cur<<1);
    else if( l > mid)
        update(l,r,v,cur<<1|1);
    else {
        update(l,mid,v,cur<<1);
        update(mid+1,r,v,cur<<1|1);
    }
}
int find(int x){
    int l =  1,r = len;
    while(l <= r){
        int mid = (l +r)>>1;
        if(x == d[mid])
            return mid ;
        else if( x > d[mid])
            l = mid+1;
        else
            r = mid-1;
    }
    return 0;
}
void query(int l,int r,int cur){
    if(a[cur].cor > 0){
        if(vis[a[cur].cor]==false ){
            vis[a[cur].cor]=true;
            cnt++;
        }
        return ;
    }
    if(a[cur].l <  a[cur].r){
        int mid = (a[cur].l +a[cur].r )>>1;
        if( r <= mid)
            query(l,r,cur<<1);
        else if( l > mid)
            query(l,r,cur<<1|1);
        else {
            query(l,mid,cur<<1);
            query(mid+1,r,cur<<1|1);
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        int t = 1;
        for(int i = 1 ; i <= n ; i++){
            scanf("%d%d",&c[i][0],&c[i][1]);
            d[t++]=c[i][0];
            d[t++]=c[i][1];
        }
        sort(d+1,d+t);
//        for(int i =1 ; i< t ; i++)
//        printf("%d ",d[i]);puts("");
        len = unique(d+1,d+t)-(d+1);
//        printf("%d\n",len);
//        for(int i = 1 ; i <= len ; i++)
//            printf("%d ",d[i]);puts("");
        build(1,len,1);
        for(int i = 1 ;i <= n; i++){//第i号海报颜色标记为i
            update(find(c[i][0]),find(c[i][1]),i,1);
        }
        memset(vis,false,sizeof(vis));
        cnt = 0 ;//记录颜色
        query(1,len,1);
//        for(int i = 1 ; i <=10000+10;i++ )
//        if(vis[i])
//        cnt++;
        printf("%d\n",cnt);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值