国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > POJ 2528 Mayor's posters(离散化线段树)

POJ 2528 Mayor's posters(离散化线段树)

来源:程序员人生   发布时间:2015-02-27 08:34:12 阅读次数:3728次

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

由于点的个数到达了1e7,、而更新却1e4所以斟酌离散化。

然后想到了map结果果断超时,就只能换种方法。

普遍做法是2维数组mp[maxn<<2][2]放(l,r)区间。

#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<bitset> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int maxn=100100; int col[maxn<<2],vis[maxn<<2]; int mp[maxn<<2][2]; struct node{ int val;//值 int num;//节点编号 }e[maxn<<2]; int t,m,ans; void pushdown(int rs) { if(col[rs]) { col[rs<<1]=col[rs<<1|1]=col[rs]; col[rs]=0; } } void build(int rs,int l,int r) { col[rs]=0; if(l==r) return ; int mid=(l+r)>>1; build(rs<<1,l,mid); build(rs<<1|1,mid+1,r); } void update(int x,int y,int c,int l,int r,int rs) { if(l>=x&&r<=y) { col[rs]=c; return ; } pushdown(rs); int mid=(l+r)>>1; if(x<=mid) update(x,y,c,l,mid,rs<<1); if(y>mid) update(x,y,c,mid+1,r,rs<<1|1); } void query(int l,int r,int rs) { if(col[rs]) { if(!vis[col[rs]]) ans++; vis[col[rs]]=1; return ; } if(l==r) return ; int mid=(l+r)>>1; query(l,mid,rs<<1); query(mid+1,r,rs<<1|1); } int cmp(node l1,node l2) { return l1.val<l2.val; } int main() { scanf("%d",&t); while(t--) { scanf("%d",&m); int l=1; REP(i,m) { scanf("%d%d",&mp[i][0],&mp[i][1]); e[i<<1].val=mp[i][0]; e[i<<1].num=-(i+1);//左端点置为负区分右端点,num可恢复原来的店编号 e[i<<1|1].val=mp[i][1]; e[i<<1|1].num=i+1; } sort(e,e+2*m,cmp); int tmp=e[0].val,len=1; REP(i,2*m) { if(e[i].val!=tmp) { len++; tmp=e[i].val; } if(e[i].num<0) mp[-e[i].num⑴][0]=len; else mp[e[i].num⑴][1]=len; } build(1,1,len); CLEAR(vis,0); REP(i,m) update(mp[i][0],mp[i][1],i+1,1,len,1);//正常的更新 ans=0; query(1,len,1); printf("%d ",ans); } return 0; }


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生