POJ2528 Mayor's posters(线段树区间更新,离散化)

来源:互联网 发布:王菲现场知乎 编辑:程序博客网 时间:2024/05/26 07:30

题目:

Mayor's posters
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 66425 Accepted: 19177

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

151 42 68 103 47 10

Sample Output

4

Source

Alberta Collegiate Programming Contest 2003.10.18

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

思路:

在一面长度为很长很长的墙上贴海报,每张海报的高度一样,唯一的区别是贴海报的位置不同,问最后能看见几张海报。

这一道题直接搞肯定TLE+MLE,所以我们要用线段树+离散化来搞。

在这道题之前,我也不知道离散化是什么东西,离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了。

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

有一条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]的线段树更省空间,搜索也更快,但是求解的结果却是一致的。

------------------------------------------------但是这样有缺陷------------------

而这题的难点在于每个数字其实表示的是一个单位长度(并且一个点),这样普通的离散化会造成许多错误
给出下面两个简单的例子应该能体现普通离散化的缺陷:
1-10 1-4 5-10
1-10 1-4 6-10
为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]
如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.


线段树的功能:update成段替换,query简单hash


代码:

#include <cstdio>#include <cstring>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define N 100050#define ll long longusing namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int vis[N];int li[N],ri[N];int X[N*3];int sum[N<<4];int cnt;void pushdown(int rt){    if(sum[rt]!=-1)    {        sum[rt<<1]=sum[rt<<1|1]=sum[rt];//直接覆盖        sum[rt]=-1;    }}void update(int L,int R,int c,int l,int r,int rt){    if(L<=l&&r<=R)    {        sum[rt]=c;        return;    }    pushdown(rt);    int m=(l+r)>>1;    if(L<=m) update(L,R,c,lson);    if(m<R) update(L,R,c,rson);}void query(int l,int r,int rt){    if(sum[rt]!=-1)    {        if(!vis[sum[rt]])            cnt++;        vis[sum[rt]]=1;        return;    }    if(l==r) return;    int m=(l+r)>>1;    query(lson);    query(rson);}int bin(int k,int n,int x[])//离散化哈希函数{    int l=0,r=n-1;    while(l<=r)    {        int m=(l+r)>>1;        if(x[m]==k)            return m;        if(x[m]<k)            l=m+1;        else            r=m-1;    }    return -1;}int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        int num=0;        for(int i=0; i<n; i++)        {            scanf("%d%d",&li[i],&ri[i]);            X[num++]=li[i];            X[num++]=ri[i];//用X记录所有出现过的数        }        sort(X,X+num);        int m=1;        //去重        for(int i=1; i<num; i++)            if(X[i]!=X[i-1])                X[m++]=X[i];        //离散化的技巧,在相差大于1的数间加一个数        for(int i=m-1; i>0; i--)            if(X[i]!=X[i-1]+1)                X[m++]=X[i-1]+1;        sort(X,X+m);        mem(sum,-1);//初始化为-1        //二分寻找当前的区间在离散化中对应的区间        for(int i=0; i<n; i++)        {            int l=bin(li[i],m,X);            int r=bin(ri[i],m,X);            update(l,r,i,0,m,1);        }        cnt=0;        mem(vis,0);        //查询[0,m]区间的个数        query(0,m,1);        printf("%d\n",cnt);    }    return 0;}


经过STL优化后:

#include <cstdio>#include <cstring>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define N 100050#define ll long longusing namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int vis[N];int li[N],ri[N];int X[N*3];int sum[N<<4];int cnt;void pushdown(int rt){if(sum[rt]!=-1){sum[rt<<1]=sum[rt<<1|1]=sum[rt];sum[rt]=-1;}}void update(int L,int R,int c,int l,int r,int rt){if(L<=l&&r<=R){sum[rt]=c;return;}pushdown(rt);int m=(l+r)>>1;if(L<=m) update(L,R,c,lson);if(m<R) update(L,R,c,rson);}void query(int l,int r,int rt){if(sum[rt]!=-1){if(!vis[sum[rt]])cnt++;vis[sum[rt]]=1;return;}if(l==r) return;int m=(l+r)>>1;query(lson);query(rson);}int main(){int t,n;scanf("%d",&t);while(t--){scanf("%d",&n);int num=0;for(int i=0; i<n; i++){scanf("%d%d",&li[i],&ri[i]);X[num++]=li[i];X[num++]=ri[i];}sort(X,X+num);int len=unique(X,X+num)-X;int m=len;for(int i=m-1; i>0; i--)if(X[i]!=X[i-1]+1)X[m++]=X[i-1]+1;sort(X,X+m);mem(sum,-1);for(int i=0; i<n; i++){int l=lower_bound(X,X+m,li[i])-X;int r=lower_bound(X,X+m,ri[i])-X;update(l,r,i,0,m,1);}cnt=0;mem(vis,0);query(0,m,1);printf("%d\n",cnt);}return 0;}




原创粉丝点击