import java.util.Scanner;
public class POJ_2528 {
public static final int MAX = (int) 1e7;
static int dist[];
static int post[][];
static int maxP;
static LineTree[] LT;
static int n;
static int sumColor ;
static boolean visit[];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int test = scan.nextInt();
for(int t=0;t<test;t++){
dist = new int[MAX+1];
n = scan.nextInt();
post = new int[n][2];
int p[] = new int[2*n];
maxP = 0;
sumColor = 0;
visit = new boolean[n+1];
for(int i=0;i<n;i++){
post[i][0] = scan.nextInt();
post[i][1] = scan.nextInt();
if(dist[post[i][0]]==0){
p[maxP++] = post[i][0];
dist[post[i][0]] = 1;
}
if(dist[post[i][1]]==0){
p[maxP++] = post[i][1];
dist[post[i][1]] = 1;
}
}
java.util.Arrays.sort(p,0,maxP);
int hash = 0;
for(int i=0;i<maxP;i++){
dist[p[i]] = hash++;
}
for(int i=0;i<n;i++){
int a = dist[post[i][0]];
int b = dist[post[i][1]];
post[i][0] = a;
post[i][1] = b;
}
dist = null;
LT = new LineTree[4*maxP];
createLineTree(0,maxP-1,1);
solution();
System.out.println(sumColor);
LT = null;
}
}
public static void solution() {
for(int i=0;i<n;i++){
insert(post[i][0],post[i][1],1,i+1);
}
DFS(1);
visit = null;
}
public static void DFS(int p) {
if(LT[p].color==0)
return ;
if(LT[p].color>0){
if(!visit[LT[p].color]){
sumColor++;
visit[LT[p].color] = true;
}
return ;
}
DFS(2*p);
DFS(2*p+1);
}
public static void insert(int s, int e, int p, int color) {
if(e<LT[p].s||s>LT[p].e)
return ;
if(s<=LT[p].s&&e>=LT[p].e){
LT[p].color = color;
return ;
}
if(LT[p].color>=0){
LT[2*p].color = LT[2*p+1].color = LT[p].color;
LT[p].color = -1;
}
insert(s,e,2*p,color);
insert(s,e,2*p+1,color);
}
public static void createLineTree(int s, int e, int p) {
LT[p] = new LineTree(s,e,0);
if(s == e)
return ;
int mid = (s+e)>>1;
createLineTree(s,mid,2*p);
createLineTree(mid+1,e,2*p+1);
}
}
class LineTree{
int s;
int e;
int color;
public LineTree(int s, int e, int color) {
super();
this.s = s;
this.e = e;
this.color = color;
}
}
poj 2528 java
最新推荐文章于 2020-10-26 20:23:14 发布