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:
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- 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.
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 l i 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 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+1 ,... , ri.
OutputFor 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 InputThe picture below illustrates the case of the sample input.
1 5 1 4 2 6 8 10 3 4 7 10Sample Output
4
题意:给你一个无限长的板子,然后依次往上面贴n张等高的海报,问你最后能看到多少张海报。
因为数据范围很大所以要进行离散化不过普通的离散化会出现一个问题
比如 1 10 1 3 6 10 三组数据 ,显然应该是都能看到的 不过普通离散化以后变成
1 4 1 2 3 4 只能看到后面两个了 ,前面被覆盖了,我们只要在离散过程中把 左右坐标后一个数也加进来就行
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<queue>
#include<deque>
#include<math.h>
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
#define maxn 22000
const int inf=0x3f3f3f3f;
int v[maxn<<2],t,n,k,sum;
int a[11000],b[11000];
int uu[maxn<<1];
using namespace std;
void pushup(int rt)//更新
{
if(v[ls]&&v[rs])//如果左儿子和右儿子都被标记,那边说明他已经被全覆盖了
v[rt]=1;
}
void update(int L,int R,int l,int r,int rt)
{
if(v[rt]) return;//如果已经被标记
if(L<=l&&r<=R)
{
if(!v[rt]) //未被标记
{
v[rt]=1;//标记
k=1;//k为1 代表新的能看到的海边
}
return;
}
int m=(l+r)>>1;
if(L<=m&&!v[ls]) update(L,R,l,m,ls);//在范围内且未被标记
if(R>m&&!v[rs]) update(L,R,m+1,r,rs);
pushup(rt);//更新
}
int main()
{
scanf("%d",&t);
while(t--)
{
int ma=1;
scanf("%d",&n);
memset(v,0,sizeof(v));
sum=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i],&b[i]);
uu[i]=a[i];
uu[i+n]=b[i];
uu[i+2*n]=a[i]+1;//防止普通离散化的错误
uu[i+3*n]=b[i]+1;
}
sort(uu,uu+4*n);
int si=unique(uu,uu+4*n)-uu;//去重
for(int i=0;i<n;i++)
{
a[i]=upper_bound(uu,uu+si,a[i])-uu;
b[i]=upper_bound(uu,uu+si,b[i])-uu;
ma=max(ma,b[i]);
}
for(int i=n-1;i>=0;i--)
{
k=0;
update(a[i],b[i],1,ma,1);
if(k) sum++;
}
printf("%d\n",sum);
}
}