本文主要是介绍POJ2528 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:
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 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 long
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int 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;elser=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 long
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int 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;
}
--------------------------------------------------------------------------------------
2018年02月10日13:31:55更新:
因为墙的长度长,直接线段树可能超时,所以要进行离散化处理,将离散后的坐标映射到线段树中,并不影响最后所求的海报长度.在这道题中,因为海报是按照段
来算的,而不是点
,所以可能会出现离散化之后有缝隙的问题,比如:[1,10]、[1,3]、[6,10]
离散化之后是[1,4]、[1,2]、[3,4]
这样会导致2和3之间的缝隙丢失导致错误,所以再去重完毕后,进行一个处理,如果相邻数字间距大于1的话,在其中加上任意一个数字。这样会把原来的缝隙留出来.
线段树的作用是利用lazy
标记,当需要覆盖的时候,就把当前这一段区间的lazy
变成当前第几张海报的标号,查询的时候只需要利用一个数组标记一下当前点是否访问过且有lazy标记,有的话就记录一下数量
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
const int N=10000+50;
int lazy[N<<4];
int vis[N],li[N],ri[N],X[N<<2];
int cnt;
void pushdown(int rt)//下放lazy
{if(~lazy[rt]){lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];lazy[rt]=-1;}
}
void update(int L,int R,int c,int l,int r,int rt)
{if(L<=l&&r<=R){lazy[rt]=c;return;}pushdown(rt);int m=(l+r)>>1;if(L<=m) update(L,R,c,lson);if(R>m) update(L,R,c,rson);
}
void query(int l,int r,int rt)
{if(~lazy[rt]){if(!vis[lazy[rt]])cnt++;vis[lazy[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 m=unique(X,X+num)-X;//离散化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(lazy,-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);//查询[0,m]区间个数query(0,m,1);printf("%d\n",cnt);}return 0;
}
这篇关于POJ2528 Mayor's posters(线段树区间更新,离散化)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!