poj2528 Mayor's Posters

来源:互联网 发布:python line.strip 编辑:程序博客网 时间:2024/06/06 03:34

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


大致思路:

         完全不觉得这是一个线段树的水题啊....题目大意是按顺序每次贴一个区间的不同的海报,到最后问能看见几张海报。而区间有10^7这么大,开小会RE,开大了会爆内存。

         因为最后考虑的是能看见几张海报,只考虑相对覆盖区域的大小,不考虑绝对的覆盖区域,因此可以离散化。倘若样例中[1,4],[2,6],[8,10],[3,4],[7,10]的区间对应离散化之后为[1,4],[2,5],[7,8],[3,4],[6,8] 。但是这样的离散化会有这样的问题[1,10],[1,4],[6,10]的区间会被离散化成[1,4],[1,2],[3,4]。也就是说原本在5的地方的空间被离散化后吞掉了。看了大家的解法普遍是在相邻区间大于1的情况下加上一个数来表示空白区间,比如上例在1,4,6,10变成1,2,4,5,6,7,10.。然而错误的离散化能过poj,正确的却过不了...

         从后向前贴海报如果发现当前区间已经有海报(新的这张将会被盖在下面)就continue,否则贴上并计数。有海报记为1,没有记为0,线段树维护区间最小值


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define INF 0x3f3f3f3f#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1 using namespace std;const int maxn=2e4+5;int add[maxn<<2],sum[maxn<<2],x[maxn<<2],y[maxn<<2],b[maxn<<2];void PushUP(int rt)  {      sum[rt]=min(sum[rt<<1],sum[rt<<1|1]);  }    void PushDown(int rt,int m)  {      if(add[rt])      {          add[rt<<1]=add[rt];          add[rt<<1|1]=add[rt];          sum[rt<<1]=add[rt];          sum[rt<<1|1]=add[rt];          add[rt]=0;      }  }    void Build(int l,int r,int rt)  {      if(l==r)      {          sum[rt]=0;        return;      }      int m=(l+r)>>1;      Build(lson);      Build(rson);      PushUP(rt);  }    void Update(int L,int R,int c,int l,int r,int rt)  {      if(L<=l&&R>=r)      {          add[rt]=c;          sum[rt]=c;          return;      }      PushDown(rt,r-l+1);      int m=(l+r)>>1;      if(L<=m)          Update(L,R,c,lson);      if(R>m)          Update(L,R,c,rson);      PushUP(rt);  }    int Query(int L,int R,int l,int r,int rt)  {      if(L<=l&&R>=r)          return sum[rt];      PushDown(rt,r-l+1);      int m=(l+r)>>1;      int ret=INF;      if(L<=m)   ret=min(ret,Query(L,R,lson));      if(R>m)    ret=min(ret,Query(L,R,rson));      return ret;  }  int main(){int t;cin>>t;while(t--){memset(b,0,sizeof(b));memset(add,0,sizeof(add));memset(sum,0,sizeof(sum));int n=0,m;cin>>m;for(int i=0;i<m;i++){cin>>x[i]>>y[i];b[n++]=x[i];b[n++]=y[i];}sort(b,b+n);n=unique(b,b+n)-b;int ans=0;//for(int i=n-1;i>=1;i--) //if(b[i]!=b[i-1]+1) b[n++]=b[i-1]+1;//sort(b,b+n);//使空闲空间不被离散化后吞掉,加一个数进去代替原来中间空闲空间,加了会WA..... //for(int i=0;i<n;i++) cout<<b[i]<<' ';cout<<endl;  Build(1,n,1);for(int i=m-1;i>=0;i--){int l=lower_bound(b,b+n,x[i])-b+1;int r=lower_bound(b,b+n,y[i])-b+1;//cout<<l<<' '<<r<<endl;if(Query(l,r,1,n,1)) continue;Update(l,r,1,1,n,1);ans++;}cout<<ans<<endl;}return 0;} 



0 0
原创粉丝点击