# poj 2528 Mayor's posters

Mayor's posters
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 56457 Accepted: 16343

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


1

3

5 6

4 5

6 8

2

3

1 6

1 3

4 6

3

1 6

1 3

5 6

2

3

//
//  main.cpp
//  POJ2528
//
//  Created by teddywang on 16/5/30.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define N 100010
int c,n;
int lazy[N<<2],num[N<<2],has[N<<2];
int li[N],ri[N];
int ans;

void pushdown(int rt)
{
if(lazy[rt]!=-1)
{
lazy[rt<<1]=lazy[rt];
lazy[rt<<1|1]=lazy[rt];
lazy[rt]=-1;
}
}

void query(int l,int r,int rt)
{
if(lazy[rt]!=-1)
{
if(has[ lazy[rt] ]==0) {
// cout<<lazy[rt]<<endl;
ans++;
has[ lazy[rt] ]=1;
}
return ;
}
if(l==r) return ;
int m=(l+r)>>1;
query(lson);
query(rson);
}

void update(int p,int l1,int r1,int l,int r,int rt)
{
if(l1<=l&&r<=r1)
{
lazy[rt]=p;
//pushdown(rt);
return ;
}
pushdown(rt);
int m=(l+r)>>1;
if(m>=l1) update(p,l1,r1,lson);
if(m<r1) update(p,l1,r1,rson);
}

int searchs(int p,int l,int r,int *x)
{

while(l<=r)
{
int m=(l+r)>>1;
if(x[m]==p) return m;
else if(x[m]>p)
r=m-1;
else l=m+1;
}
return -1;
}

int main()
{
cin>>c;
while(c--)
{
cin>>n;
int len=0,m=1;
for(int i=0;i<n;i++)
{
scanf("%d%d",&li[i],&ri[i]);
num[len++]=li[i];
num[len++]=ri[i];
}
sort(num,num+len);
for(int i=1;i<len;i++)
{
if(num[i]!=num[i-1]) num[m++]=num[i];
}
for(int i=m-1;i>0;i--)
{
if(num[i]-num[i-1]>1) num[m++]=num[i-1]+1;
}
sort(num,num+m);
memset(lazy,-1,sizeof(lazy));
for(int i=0;i<n;i++)
{
int l=searchs(li[i],0,m-1,num);
int r=searchs(ri[i],0,m-1,num);
update(i,l,r,0,m-1,1);
}
ans=0;
memset(has,0,sizeof(has));
query(0,m-1,1);
cout<<ans<<endl;
}
}


#### poj2528--Mayor's posters(线段树+离散化)

2014-08-08 19:50:02

#### POJ2528:Mayor's posters(线段树区间更新+离散化)

2013-10-31 15:52:39

#### poj - 2528 - Mayor's posters（线段树（区间更新））

2013-02-07 21:43:09

#### Mayor's posters（线段树 + 离散化）

2014-08-14 15:50:50

#### poj 2528 Mayor's poster

2012-12-06 02:10:55

#### D - Mayor's posters POJ - 2528(区间覆盖/线段树/漂浮法)

2017-03-14 20:47:49

#### POJ - 2528 - Mayor's posters

2017-11-12 14:17:12

#### POJ 2528 Mayor's posters

2013-11-05 10:09:32

#### Mayor's posters poj 2528

2011-10-06 13:49:53

#### poj 2528——Mayor's posters

2015-02-13 17:58:05