# poj2528Mayor's posters【线段树+离散化】

179人阅读 评论(0)

Language:
Mayor's posters
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52643 Accepted: 15337

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

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int maxn=100010;
int num[maxn<<1];
int sum[maxn<<2];
int vis[maxn];
struct Node{
int s,e;
}A[maxn];
int BS(int n,int k){
int l=1,r=k;
while(l<=r){
int mid=(l+r)>>1;
if(n==num[mid])return mid;
else if(n<num[mid]){
r=mid-1;
}
else {
l=mid+1;
}
}
}
void pushdown(int rt){
if(sum[rt]){
sum[rt<<1]=sum[rt<<1|1]=sum[rt];
sum[rt]=0;
}
}
void update(int left,int right,int k,int l,int r,int rt){
if(left<=l&&r<=right){
sum[rt]=k;
return;
}
int mid=(l+r)>>1;
pushdown(rt);
if(left<=mid)update(left,right,k,lson);
if(right>mid)update(left,right,k,rson);
}
int ans;
void query(int l,int r,int rt){
if(sum[rt]){
if(!vis[sum[rt]]){
vis[sum[rt]]=1;
ans++;
}
return ;
}
if(l==r)return ;
int mid=(l+r)>>1;
query(lson);
query(rson);
}
int main()
{
int t,i,j,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(vis,0,sizeof(vis));
memset(sum,0,sizeof(sum));
int m=0,k=1;
for(i=1;i<=n;++i){
scanf("%d%d",&A[i].s,&A[i].e);
num[++m]=A[i].s;
num[++m]=A[i].e;
}
sort(num,num+m+1);
for(i=2;i<=m;++i){
if(num[i]!=num[i-1]){
num[++k]=num[i];
}
}
for(i=k;i>1;--i){
if(num[i]!=num[i-1]+1)num[++k]=num[i-1]+1;
}
sort(num,num+k+1);
for(i=1;i<=n;++i){
int l=BS(A[i].s,k);
int r=BS(A[i].e,k);
update(l,r,i,1,k,1);
}
ans=0;query(1,k,1);
printf("%d\n",ans);
}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：247496次
• 积分：10322
• 等级：
• 排名：第1623名
• 原创：819篇
• 转载：5篇
• 译文：1篇
• 评论：24条
行到水穷处 ， 坐看云起时。