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 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.

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

 C++版本一

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

const int N = 20000+5;
int tree[N<<2], lazy[N<<2];
int a[N];
bool vis[N];
int ans, n, t;
pair<int,int> P[N];

void Pushdown(int rt)
{
    if(lazy[rt])
    {
        lazy[rt<<1|1] = lazy[rt<<1] = lazy[rt];
        lazy[rt] = 0;
    }
}
void Revise(int L, int R, int  C ,int l, int r, int rt)
{
    if(L <= l && r <= R)
    {
        lazy[rt] = C;
        return ;
    }
    Pushdown(rt);
    int m = l+r >> 1;
    if(L <= m) Revise(L,R,C,l,m,rt<<1);
    if(m < R)  Revise(L,R,C,m+1,r,rt<<1|1);
}
void build(int l, int r, int rt)
{
    if(lazy[rt])
    {
        if(!vis[lazy[rt]])
        {
            vis[lazy[rt]] = 1;
            ans++;
        }
        return ;
    }
    int m = l+r >> 1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
}
int main()
{

    cin >> t;
    while(t--)
    {
        ans = 0;
        cin >> n;
        for(int i = 1; i <= 2*n; i++)
        {
            cin >> a[i];
            P[i].first = a[i], P[i].second = i;
        }
        sort(P+1,P+2*n+1);
        int last = 0, cnt = 0;
        for(int i = 1; i <= 2*n; i++) //离散化操作
        {
            if(P[i].first == last)
                a[P[i].second] = cnt;
            else a[P[i].second] = ++cnt, last = P[i].first;
        }
        memset(lazy, 0, sizeof(lazy));
        for(int i = 1; i <= 2*n; i+=2)
            Revise(a[i],a[i+1],(i+1)/2,1,cnt,1);//由于每次的海报不同所以直接给海报一个编号
        memset(vis, 0, sizeof(vis));
        build(1,cnt,1);
        cout << ans << endl;
    }
    return 0;
}

C++版本二 

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
const int N = 20000+10;
using namespace std;
int c,n,q;
int lazy[N<<2];
int a[10010];
int ans=0;
int x[N];
pair<int,int> P[N];

void PushDown(int rt){
    if(lazy[rt]){
        lazy[rt*2] = lazy[rt];
        lazy[rt*2+1] = lazy[rt];
        lazy[rt] = 0;
    }
}
void Build(int l,int r,int rt){
    if(l==r){
        a[lazy[rt]]=1;
        return ;
    }
    int m=(l+r)/2;
    PushDown(rt);
    Build(l,m,rt*2);//左边有一部分需要查询的区域。
    Build(m+1,r,rt*2+1);//右边有一部分。

}

void build(int l, int r, int rt)
{
    if(lazy[rt])
    {
        if(!a[lazy[rt]])
        {
            a[lazy[rt]] = 1;
            ans++;
        }
        return ;
    }
    int m = l+r >> 1;
    build(l,m,rt*2);//左边有一部分需要查询的区域。
    build(m+1,r,rt*2+1);//右边有一部分。
}

void Updata(int l,int r,int rt,int L,int R,int C){// l,r,rt 与前面的定义一样, L代表的是要更新区间的位置,C代表的是修改后的值
    if(L <= l && r <= R){// 这里不能写成 if(l == L) 因为有可能左端点恰好是要更新的位置, 但是还有右端点, 我们直接更新的只有区间 [L,L]。
        lazy[rt] = C;

        return ;
    }
    int m=(l+r)/2;
    PushDown(rt);
    if(L<=m) Updata(l,m,rt*2,L,R,C);//要更新的区间在左边部分,所以往左边走,更新左边
    if(m < R) Updata(m+1,r,rt*2+1,L,R,C);//要更新的区间在右边部分, 往右边走,更新右边
}
void init(){
    memset(lazy,0,sizeof(lazy));
    memset(a,0,sizeof(a));
    memset(x,0,sizeof(x));
}
int main()
{
    scanf("%d",&c);
    for(int j = 1; j <= c; j++){
        init();
        scanf("%d",&n);
        for(int i = 1; i <= 2*n; i++){
            scanf("%d",&x[i]);
            P[i].first = x[i], P[i].second = i;

        }
        sort(P+1,P+2*n+1);
        int last = 0, cnt = 0;
        for(int i = 1; i <= 2*n; i++){ //离散化操作

                if(P[i].first == last)
                    x[P[i].second] = cnt;
                else
                    x[P[i].second] = ++cnt, last = P[i].first;
        }
        for(int i = 1; i <= 2*n; i+=2)
            Updata(1,cnt,1,x[i],x[i+1],(i+1)/2);//由于每次的海报不同所以直接给海报一个编号


        ans=0;
        build(1,cnt,1);
        cout << ans << endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本三

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 10005;
#define dd puts("haha");
int li[N],ri[N];
int x[N<<3];
int col[N<<4];
bool Hash[N];
void PushDown(int rt){
    col[rt<<1] = col[rt];
    col[rt<<1|1] = col[rt];
    col[rt] = -1;
    return;
}
void Update(int L, int R, int l, int r, int c, int rt)
{
    if( L <= l && R >= r ) {
        col[rt] = c;
        return;
    }
    if(col[rt]!=-1) PushDown(rt);
    int m = (l+r)>>1;
    if(L <= m ) Update(L,R,l,m,c,rt<<1);
    if(R > m) Update(L,R,m+1,r,c,rt<<1|1);
}
int ans;
void query(int l, int r, int rt)
{
    if(l==r||~col[rt]){//~col[rt]相当于col[rt]!=-1;因为~是按位取反的意思
        if(!Hash[col[rt]]&&(col[rt]!=-1)){
            ans++;
            Hash[col[rt]] = 1;
        }
        return;
    }
    if(~col[rt])PushDown(rt);
    int m = (l+r)>>1;
    query(l,m,rt<<1);
    query(m+1,r,rt<<1|1);
}
int BSearch(int l, int r, int c)
{
    int m;
    while(l<=r){
        m =(l+r)>>1;
        if(x[m]==c) return m;
        else if(x[m]<c) l = m+1;
        else if(x[m]>c) r = m-1;
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(col,-1,sizeof(col));
        memset(Hash,0,sizeof(Hash));
        int n;
        scanf("%d",&n);
        int cnt = 1;
        for(int i = 1; i <=n; i++)
        {
            scanf("%d %d",&li[i],&ri[i]);
            x[cnt++] = li[i];
            x[cnt++] = ri[i];
            x[cnt++] = ri[i]+1;
        }
        sort(x+1,x+cnt+1);
        int m = unique(x+1,x+cnt+1)-x-1;
        for(int i = 1; i <= n; i++){
            int ll = BSearch(1,m,li[i]);
            int rr = BSearch(1,m,ri[i]);
            //printf("(%d %d)\n",ll,rr);
            Update(ll,rr,1,m,i,1);
        }
        //dd;
        ans = 0;
        query(1,m,1);
        printf("%d\n",ans);
    }
    return 0;
}