poj 2528 2010.2.22

来源:互联网 发布:安卓chroot ubuntu 编辑:程序博客网 时间:2024/05/21 22:25

poj 2528 2010.2.22

Mayor's posters

Time Limit: 1000MS  Memory Limit: 65536K

Total Submissions: 17486  Accepted: 4911

 

Description

 

The citizens of Bytetown, AB, could notstand that the candidates in the mayoral election campaign have been placingtheir electoral posters at all places at their whim. The city council hasfinally decided to build an electoral wall for placing the posters andintroduce the following rules:

 

Every candidate can place exactly oneposter on the wall.

 

All posters are of the same height equal tothe height of the wall; the width of a poster can be any integer number ofbytes (byte is the unit of length in Bytetown).

 

The wall is divided into segments and thewidth of each segment is one byte.

 

Each poster must completely cover acontiguous number of wall segments.

 

They have built a wall 10000000 bytes long(such that there is enough place for all candidates). When the electoralcampaign was restarted, the candidates were placing their posters on the walland their posters differed widely in width. Moreover, the candidates startedplacing their posters on wall segments already occupied by other posters.Everyone in Bytetown was curious whose posters will be visible (entirely or inpart) on the last day before elections.

Your task is to find the number of visibleposters 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 cgiving the number of cases that follow. The first line of data for a singlecase contains number 1 <= n <= 10000. The subsequent n lines describe theposters in the order in which they were placed. The i-th line among the n linescontains two integer numbers li and ri which are the number of the wall segmentoccupied by the left end and the right end of the i-th poster, respectively. Weknow that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. Afterthe 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 ofvisible posters after all the posters are placed.

 

The picture below illustrates the case ofthe sample input.

 

Sample Input

 

1

5

1 4

2 6

8 10

3 4

7 10

 

Sample Output

 

4

 

Source

 

Alberta Collegiate Programming Contest2003.10.18

 

3100K 94MS 


#include<stdio.h>#include<algorithm>#include <string.h>using namespace std;const int maxn=51000;int x[maxn],y[maxn],p[2*maxn],cnt[2*maxn],flag[maxn];int n,top,tot,temp;struct node{   int ls,rs,beg,end,c;};node tree[2*maxn];int inline init(int a,int b){   int cur=tot,mid;   tree[cur].beg=a;   tree[cur].end=b;   tree[cur].c=0;   if(a==b)   {   tree[cur].ls=-1;   tree[cur].rs=-1;   return cur;   }   mid=(a+b)/2;   ++tot;   tree[cur].ls=init(a,mid);   ++tot;   tree[cur].rs=init(mid+1,b);   return cur;}void inline insert(int c,int x,int y,int k)//更新一个区间{int l,r;if(k==-1)    return ;l=tree[k].beg;r=tree[k].end;if(!(x<=r&&l<=y))     return ;if(x<=l&&r<=y){tree[k].c=c;return ;}if(tree[k].c!=-1){     tree[tree[k].ls].c=tree[tree[k].rs].c=tree[k].c;     tree[k].c=-1;}insert(c,x,y,tree[k].ls);insert(c,x,y,tree[k].rs);if(tree[tree[k].ls].c!=-1&&tree[tree[k].rs].c!=-1&&tree[tree[k].ls].c==tree[tree[k].rs].c)   tree[k].c=tree[tree[k].ls].c;}void inline sum(int x,int y,int k){int l,r,mid;l=tree[k].beg;r=tree[k].end;if(k==-1)    return ;if(l<=x&&y<=r&&tree[k].c!=-1)//{flag[tree[k].c]=1;    return ;}mid=(l+r)/2; if(y<=mid)    sum(x,y,tree[k].ls);else if(x>mid)    sum(x,y,tree[k].rs);else {    sum(x,mid,tree[k].ls);    sum(mid+1,y,tree[k].rs);}}int main(){int i,j,k,T,s;scanf("%d",&T);while(T--){scanf("%d",&n);top=0;for(i=1;i<=n;++i){    scanf("%d%d",&x[i],&y[i]);    cnt[++top]=x[i];    cnt[++top]=y[i];}++top;//top为尾端。。。。sort(cnt+1,cnt+top); temp=0;p[0]=cnt[1];for(i=2;i<top;++i){    if(cnt[i]!=cnt[i-1])    {     if(cnt[i]!=cnt[i-1]+1)     {        p[++temp]=cnt[i-1]+1;           }           p[++temp]=cnt[i];       }}tot=1;memset(tree,-1,sizeof(tree));init(0,temp);//建树。。。。for(i=1;i<=n;++i){    j=upper_bound(p,p+temp+1,x[i])-p-1;    k=upper_bound(p,p+temp+1,y[i])-p-1;    insert(i,j,k,1);}memset(flag,0,sizeof(flag));sum(0,temp,1);s=0;for(i=1;i<=n;++i)    s+=flag[i];printf("%d\n",s);}return 0;}


0 0
原创粉丝点击