#include <iostream> #include <algorithm> using namespace std; #define MAXN 20010 struct TNode { int left, right; int n; }; struct Map { int num; //表示端点坐标 bool isLeft; //表示右端点 int id; //表示输入时的原序号 bool operator<(const Map& rhs) { return num < rhs.num; } }; int temp[MAXN][2]; Map map[MAXN*2]; TNode T[MAXN*4]; int cnt; bool hash[MAXN/2]; void build(int s, int t, int step) { T[step].left = s; T[step].right = t; T[step].n = 0; if(s == t) return; int mid = (s + t) >> 1; build(s, mid, step*2); build(mid+1, t, step*2+1); } void insert(int s, int t, int step, int color) // insert [s, t] in the tree O(logn) { if(s == T[step].left && t == T[step].right) { T[step].n = color; return; } if(T[step].n != 0 && T[step].n != color) { T[step*2].n = T[step].n; T[step*2+1].n = T[step].n; T[step].n = 0; } int mid = (T[step].left + T[step].right) >> 1; if(t <= mid) insert(s, t, step*2, color); else if(s > mid) insert(s, t, step*2+1, color); else { insert(s,mid, step*2, color); insert(mid+1, t, step*2+1, color); } } void calculate(int step) // caculate target in the tree O(logn) { if(T[step].n) { if(hash[T[step].n] != true) { hash[T[step].n] = true; cnt++; } return; } calculate(step*2); calculate(step*2+1); } int main() { int N, n; scanf("%d", &N); while(N--) { //输入: scanf("%d", &n); int total = 0; for(int i = 0; i < n; ++i) { scanf("%d%d", &temp[i][0], &temp[i][1]); map[total].num = temp[i][0]; map[total].isLeft = true; map[total].id = i; total++; map[total].num = temp[i][1]; map[total].isLeft = false; map[total].id = i; total++; } //离散化过程如下: sort(map, map+n*2); int TMP = -1; int index = 0; for(int i = 0; i < 2*n; ++i) { if(map[i].num != TMP) { index++; TMP = map[i].num; } if(map[i].isLeft) temp[map[i].id][0] = index; else temp[map[i].id][1] = index; } //处理数据: build(1, index, 1); for(int i = 0; i < n; ++i) insert(temp[i][0], temp[i][1], 1, i+1); memset(hash, false, sizeof(bool)*MAXN); cnt = 0; calculate(1); printf("%d/n", cnt); } return 0; }