#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#define MAX 20001
using namespace std;
int c,n,ls[MAX];
struct node
{
int l,r;
int c;
} tr[MAX*4];
struct ln
{
int li,num;//num表示第幾張海報
} line[MAX];
int set[MAX][2];
bool visit[MAX];
int ans;
bool cmp(struct ln a,struct ln b)
{
return a.li<b.li;
}
void build(int pos,int ll,int rr)
{
tr[pos].l = ll;
tr[pos].r = rr;
tr[pos].c = 0;
if(ll!=rr)
{
int mid = (ll+rr)>>1;
build(pos*2,ll,mid);
build(pos*2+1,mid+1,rr);
}
}
void Insert(int pos,int ll,int rr,int color)
{
if(tr[pos].l == ll && tr[pos].r == rr)
{
tr[pos].c = color;
return;
}
if(tr[pos].c > 0 && tr[pos].c != color)
{
tr[pos*2].c = tr[pos].c;
tr[pos*2+1].c = tr[pos].c;
tr[pos].c = 0;
}
int mid = (tr[pos].l + tr[pos].r)>>1;
if(rr<=mid)
{
Insert(pos*2,ll,rr,color);
}
else if(ll>mid)
{
Insert(pos*2+1,ll,rr,color);
}
else
{
Insert(pos*2,ll,mid,color);
Insert(pos*2+1,mid+1,rr,color);
}
}
void Search(int pos)
{
if(tr[pos].c!=0)
{
if(!visit[tr[pos].c]) //tr[pos].c
{
visit[tr[pos].c] = true;
ans++;
}
return ;
}
Search(2*pos);
Search(2*pos+1);
}
int main()
{
int i;
while(scanf("%d",&c)!=EOF)
{
while(c--)
{
scanf("%d",&n);
for(i = 0; i < n; ++i) //離散化
{
scanf("%d%d",&set[i][0],&set[i][1]);
line[2*i].li = set[i][0];
line[2*i].num = -(i+1);//用負數表示 線段起點
line[2*i+1].li = set[i][1];
line[2*i+1].num = i+1;
}
sort(line,line+2*n,cmp);
int temp = line[0].li,tp = 1;
for(i = 0; i < 2*n; ++i)
{
if(line[i].li != temp)
{
tp++;
temp = line[i].li;
}
if(line[i].num < 0)
{
set[-line[i].num-1][0] = tp;
}
else
{
set[line[i].num-1][1] = tp;
}
}//離散化
build(1,1,tp);
for(i = 0; i < n; ++i)
{
Insert(1,set[i][0],set[i][1],i+1);
}
memset(visit,0,sizeof(visit));
ans = 0;
Search(1);
printf("%d\n",ans);
}
}
return 0;
}
node
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAX 20001
struct node
{
int l;
int r;
int c;
} tr[4*MAX];
struct p
{
int num;
int val;
} qq[2*MAX];
int set[MAX*2][2];
int vis[2*MAX];
int ans;
bool cmp(struct p a,struct p b)
{
return a.val<b.val;
}
int build(int root,int l,int r)
{
tr[root].l=l;
tr[root].r=r;
tr[root].c=0;
int mid=(l+r)/2;
if(l!=r)
{
build(root*2,l,mid);
build(root*2+1,mid+1,r);
}
}
int insert(int root,int l,int r,int val)
{
if(tr[root].l==l&&tr[root].r==r)
{
return tr[root].c=val;
}
int mid=(tr[root].l+tr[root].r)/2;
if(tr[root].c>0&&tr[root].c!=val)
{
tr[root*2].c=val;
tr[root*2+1].c=val;
tr[root].c=0;
}
if(r<=mid)
{
insert(root*2,l,r,val);
}
if(l>mid)
{
insert(root*2+1,l,r,val);
}
else
{
insert(root*2,l,mid,val);
insert(root*2+1,mid+1,r,val);
}
}
void search(int root)
{
if(tr[root].c!=0)
{
if(vis[tr[root].c]==0)
{
ans++;
vis[tr[root].c]=1;
}
return;
}
search(root*2);
search(root*2+1);
}
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d%d",&set[i][0],&set[i][1]);
qq[i*2].val=set[i][0];
qq[i*2].num=-(i+1);
qq[i*2+1].val=set[i][1];
qq[i*2+1].num=i+1;
}
sort(qq,qq+n*2,cmp);
int temp=qq[0].val,tp=1;
for(int i=0; i<2*n; i++)
{
if(temp!=qq[i].val)
{
tp++;
temp=qq[i].val;
}
if(qq[i].num<0)
{
set[-qq[i].num-1][0] = tp;
}
if(qq[i].num>0)
{
set[qq[i].num-1][1]=tp;
}
}
build(1,1,n);
for(int i=0; i<n; i++)
{
insert(1,set[i][0],set[i][1],i+1);
}
memset(vis,0,sizeof(vis));
ans=0;
search(1);
cout<<ans<<endl;
}
}
return 0;
}
ios