#include<iostream> #include<algorithm> #include<vector> using namespace std; struct gg { int x; int id; gg(){} gg(int xx,int idd) { x=xx; id=idd; } }; bool cmp(gg a,gg b) { return a.x<b.x; } int main() { freopen("in.txt","r",stdin); int test; cin>>test; while(test--) { int n; cin>>n; //int cnt=1; int cnt=1; vector<gg> vec; for(int i=1;i<=n;i++) { int a,b; scanf("%d%d",&a,&b); vec.push_back(gg(a,i)); vec.push_back(gg(b+1,i)); } sort(vec.begin(),vec.end(),cmp); for(int i=0;i<vec.size();i++) { if(i+1<vec.size() && vec[i].x!=vec[i+1].x) vec[i].x = cnt++; else vec[i].x = cnt; } int start[10000*3]; int end[10000*3]; memset(start,0,sizeof(start)); memset(end,0,sizeof(end)); for(int i=0;i<vec.size();i++) { if(start[vec[i].id]==0) start[vec[i].id]=vec[i].x; else end[vec[i].id]=vec[i].x; } int flag[10000*3]; bool flag2[10000*3]; memset(flag,0,sizeof(flag)); memset(flag2,0,sizeof(flag2)); for(int i=1;i<=n;i++) for(int j=start[i];j<end[i];j++) flag[j]=i; for(int i=1;i<=cnt;i++) flag2[flag[i]]=1; int ans=0; for(int i=1;i<=n;i++) if(flag2[i]) ans++; cout<<ans<<endl; } } #include<iostream> #include<vector> #include<algorithm> using namespace std; const int maxn=10005; bool flag[maxn*5]; struct gg { int x; int id; gg(){} gg(int xx,int idd) { x=xx; id=idd; } }; bool cmp(gg a,gg b) { return a.x<b.x; } struct seg_tree { int left; int right; int val; int calmid() { return (left+right)>>1; } }; seg_tree tt[100000*2*3]; void build(int left,int right,int idx) { tt[idx].left=left; tt[idx].right=right; tt[idx].val = 0; if(left == right) return; int mid = tt[idx].calmid(); build(left,mid,idx<<1); build(mid+1,right,idx<<1|1); } void inherit(int idx) { if(tt[idx].val) { tt[idx<<1].val=tt[idx<<1|1].val=tt[idx].val; tt[idx].val=0; } } void update(int left,int right,int v,int idx) { if(left == tt[idx].left && right == tt[idx].right) { tt[idx].val = v; return; } inherit(idx); int mid = tt[idx].calmid(); if(right<=mid) update(left,right,v,idx<<1); else if(left>mid) update(left,right,v,idx<<1|1); else { update(left,mid,v,idx<<1); update(mid+1,right,v,idx<<1|1); } } void query(int left,int right,int idx) { if(left == tt[idx].left && right == tt[idx].right && tt[idx].val) { flag[tt[idx].val]=1; return; } inherit(idx); int mid = tt[idx].calmid(); if(right <= mid) query(left,right,idx<<1); else if(left>mid) query(left,right,idx<<1|1); else { query(left,mid,idx<<1); query(mid+1,right,idx<<1|1); } } int main() { freopen("in.txt","r",stdin); int test; cin>>test; while(test--) { int n; vector<gg> vec; cin>>n; for(int i=1;i<=n;i++) { int a,b; scanf("%d%d",&a,&b); vec.push_back(gg(a,i)); vec.push_back(gg(b,i)); } sort(vec.begin(),vec.end(),cmp); int cnt=1; for(int i=0;i<vec.size();i++) { if(i+1<vec.size() && vec[i].x!=vec[i+1].x){ vec[i].x=cnt++;} else vec[i].x=cnt; } int st[maxn]; int end[maxn]; memset(st,-1,sizeof(st)); memset(end,0,sizeof(end)); for(int i=0;i<vec.size();i++) { if(st[vec[i].id]==-1) st[vec[i].id]=i; else end[vec[i].id]=i; } build(1,cnt,1); for(int i=1;i<=n;i++) { update(vec[st[i]].x,vec[end[i]].x,i,1); } memset(flag,0,sizeof(flag)); query(1,cnt,1); int ans=0; for(int i=1;i<=n;i++) if(flag[i]) ans++; cout<<ans<<endl; } }