1 #include<cstdio>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #define MAXN 50010
  5 struct node
  6 {
  7     int x,y;
  8 };
  9 node a[MAXN],p[MAXN];
 10 int cmp(const void *a,const void *b)
 11 {
 12     return (*(node *)a).x-(*(node *)b).x;
 13 }
 14 int ans,tree[MAXN<<2];
 15 bool vis[MAXN];
 16 inline void PushDown(int rt)
 17 {
 18     if(tree[rt])
 19     {
 20         tree[rt<<1]=tree[rt<<1|1]=tree[rt];
 21         tree[rt]=0;
 22     }
 23 }
 24 void Update(int x,int y,int val,int L,int R,int rt)
 25 {
 26     if(x<=L&&R<=y)
 27         tree[rt]=val;
 28     else
 29     {
 30         int mid=(L+R)>>1;
 31         PushDown(rt);
 32         if(mid>=x)
 33             Update(x,y,val,L,mid,rt<<1);
 34         if(y>mid)
 35             Update(x,y,val,mid+1,R,rt<<1|1);
 36     }
 37 }
 38 void Query(int L,int R,int rt)
 39 {
 40     if(tree[rt])
 41     {
 42         if(!vis[tree[rt]])
 43         {
 44             vis[tree[rt]]=true;
 45             ans++;
 46         }
 47     }
 48     else if(L!=R)
 49     {
 50         int mid=(L+R)>>1;
 51         Query(L,mid,rt<<1);
 52         Query(mid+1,R,rt<<1|1);
 53     }
 54 }
 55 int Bin(int low,int high,int val)
 56 {
 57     int mid;
 58     while(low<high)
 59     {
 60         mid=(high+low)>>1;
 61         if(a[mid].x==val)
 62             return a[mid].y;
 63         if(a[mid].x>val)
 64             high=mid;
 65         else
 66             low=mid+1;
 67     }
 68 }
 69 int main()
 70 {
 71     int c,n,i,j,k;
 72     scanf("%d",&c);
 73     while(c--)
 74     {
 75         scanf("%d",&n);
 76         for(i=k=0;i<n;i++)
 77         {
 78             scanf("%d%d",&p[i].x,&p[i].y);
 79             a[k++].x=p[i].x;
 80             a[k++].x=p[i].y;
 81         }
 82         qsort(a,k,sizeof(a[0]),cmp);
 83         for(i=j=0;i<k;i++)
 84         {
 85             if(a[i].x!=a[j].x)
 86                 a[++j]=a[i];
 87         }
 88         k=j+1;
 89         a[0].y=j=1;
 90         for(i=1;i<k;i++)
 91         {
 92             if(a[i].x==a[i-1].x+1)
 93                 j++;
 94             else
 95                 j+=2;
 96             a[i].y=j;
 97         }
 98         memset(tree,0,sizeof(tree));
 99         memset(vis,false,sizeof(vis));
100         for(i=ans=0;i<n;i++)
101             Update(Bin(0,k,p[i].x),Bin(0,k,p[i].y),i+1,1,j,1);
102         Query(1,j,1);
103         printf("%d\n",ans);
104     }
105     return 0;
106 }
posted on 2012-05-22 01:16  DrunBee  阅读(340)  评论(0编辑  收藏  举报