`
java-mans
  • 浏览: 11459740 次
文章分类
社区版块
存档分类
最新评论

Mayor's posters 线段树+离散化+曾经RE无数边

 
阅读更多
Mayor's posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26435 Accepted: 7624

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

Source

有一面墙,被等分为1QW份,一份的宽度为一个单位宽度。现在往墙上贴N张海报,每张海报的宽度是任意的,但是必定是单位宽度的整数倍,且<=1QW。后贴的海报若与先贴的海报有交集,后贴的海报必定会全部或局部覆盖先贴的海报。现在给出每张海报所贴的位置(左端位置和右端位置),问张贴完N张海报后,还能看见多少张海报?(PS:看见一部分也算看到。)



下面的离散化的实现比较容易理解 但是消耗空间较大

#include<stdio.h>
#include<algorithm>
using namespace std;
#define max 10005
struct haha
{
int c;
int left;
int right;
}node[max*10];//一开始一个劲的WA 就是这里错了 我乘的是4 但是注意有可能点会很多 达到2*n 即每个起点终点都不同 而不是n个 故乘以4绝对不行
int rem[max*2],used[max*2],left[max],right[max],ans;
int stl[10000000+5];
void build(int left,int right,int nd)
{
int mid;
node[nd].left=left;
node[nd].right=right;
node[nd].c=0;
if(left==right) return ;
mid=(left+right)/2;
build(left,mid,nd*2);
build(mid+1,right,nd*2+1);
}


void update(int left,int right,int per,int nd)
{
int mid;
if(node[nd].c==per) return ;
if(left==node[nd].left&&right==node[nd].right)
{
node[nd].c=per;
return ;
}
if(node[nd].c>0)
{
node[nd*2].c=node[nd].c;node[nd*2+1].c=node[nd].c;
node[nd].c=0;
}
mid=(node[nd].left+node[nd].right)/2;
if(right<=mid) update(left,right,per,nd*2);
else if(left>mid) update(left,right,per,nd*2+1);
else
{
update(left,mid,per,nd*2);
update(mid+1,right,per,nd*2+1);
}
}


int query(int idx){
int count=0;
if (node[idx].c>0){
if(!used[node[idx].c])
{
used[node[idx].c]=1;
count=1;
}
return count;
}
return count=query(idx*2)+query(idx*2+1);
}



int main()
{
int i,cas,n,cnt,num,t,j;
scanf("%d",&cas);
while(cas--)
{
cnt=0;num=0;
memset(used,0,sizeof(used));
scanf("%d",&n);
for(i=1,j=n+1;i<=n;i++,j++)
{
scanf("%d %d",&left[i],&right[i]);
rem[i]=left[i];
rem[j]=right[i];
}
sort(rem+1,rem+1+2*n);
t=1; stl[rem[1]]=t;
for(i=2;i<=2*n;i++)
{
if(rem[i]!=rem[i-1])
stl[rem[i]]=++t;//主要是这里 为离散化的关键步骤 就是把不同的rem[i]映射为新的节点
}
build(1,t,1);

for(i=1;i<=n;i++)
{
update(stl[left[i]],stl[right[i]],i,1);
}
printf("%d\n",query(1));
}
return 0;

}



此方法也很容易理解 且消耗空间少

#include<stdio.h>
#include<algorithm>
using namespace std;
struct haha
{
int c;
int left;
int right;
}node[10000*20];
int rem[10000*2+20],used[10000*2+20],left[10020],right[10000+20],ans;

void build(int left,int right,int nd)
{
int mid;
node[nd].left=left;
node[nd].right=right;
node[nd].c=-1;
if(left==right) return ;
mid=(left+right)/2;
build(left,mid,nd*2);
build(mid+1,right,nd*2+1);
}


int bsearch(int n,int s,int e)
{
int mid;
mid=(s+e)/2;
while(n!=rem[mid])
{
mid=(s+e)/2;
if(n>rem[mid]) s=mid+1;
else if(n<rem[mid]) e=mid-1;
}
return mid;

}

void update(int left,int right,int per,int nd)
{
int mid;
if(node[nd].c==per) return ;
if(left==node[nd].left&&right==node[nd].right)
{
node[nd*2].c=node[nd*2+1].c=node[nd].c;
node[nd].c=per;
return ;
}
if(node[nd].c!=-1)
{
node[nd*2].c=node[nd].c;node[nd*2+1].c=node[nd].c;
node[nd].c=-1;
}
mid=(node[nd].left+node[nd].right)/2;
if(right<=mid) update(left,right,per,nd*2);
else if(left>mid) update(left,right,per,nd*2+1);
else
{
update(left,mid,per,nd*2);
update(mid+1,right,per,nd*2+1);
}
}
void query(int nd)
{
if(used[node[nd].c]) return;
if(node[nd].c!=-1)
{
used[node[nd].c]=1;ans++;
return ;
}
query(nd*2);
query(nd*2+1);
}
int main()
{
int i,j,cas,n,cnt,r,l,num;
scanf("%d",&cas);
while(cas--)
{
cnt=0;num=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d %d",&left[i],&right[i]);
rem[cnt++]=left[i];
rem[cnt++]=right[i];
}
sort(rem,rem+cnt);
j=1;
for(i=1;i<cnt;i++)
{
if(rem[i]!=rem[i-1])
rem[j++]=rem[i];
}
cnt=j;
sort(rem,rem+cnt);
build(0,cnt-1,1);//
memset(used,0,sizeof(used));
for(i=1;i<=n;i++)
{
l=bsearch(left[i],0,cnt-1);//
r=bsearch(right[i],0,cnt-1);//
update(l,r,i,1);
}
ans=0;
query(1);
printf("%d\n",ans);
}
return 0;

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics