#include"cstdio" #include"algorithm" using namespace std; const int MAXN=10005; struct Post{ int l,r; }posters[MAXN]; int x[MAXN*2]; int hash[10000005]; struct node{ int l,r; bool beCovered; }; int cnt; node a[MAXN*8];//*4 会RE ?? void build(int rt,int l,int r) { a[rt].l=l; a[rt].r=r; if(l==r) { a[rt].beCovered=false; return ; } int mid=(l+r)>>1; build(rt<<1,l,mid); build((rt<<1)|1,mid+1,r); a[rt].beCovered=(a[rt<<1].beCovered&&a[(rt<<1)|1].beCovered); } void update(int rt,int l,int r) { if(a[rt].l==l&&a[rt].r==r) { a[rt].beCovered=true; return ; } if(a[rt].beCovered) { a[rt<<1].beCovered=true; a[(rt<<1)|1].beCovered=true; } int mid=(a[rt].l+a[rt].r)>>1; if(r<=mid) update(rt<<1,l,r); else if(mid<l) update((rt<<1)|1,l,r); else { update(rt<<1,l,mid); update((rt<<1)|1,mid+1,r); } a[rt].beCovered=(a[rt<<1].beCovered&&a[(rt<<1)|1].beCovered); } bool query(int rt,int l,int r) { if(a[rt].l==l&&a[rt].r==r) { return a[rt].beCovered; } if(a[rt].beCovered) { a[rt<<1].beCovered=true; a[(rt<<1)|1].beCovered=true; } int mid=(a[rt].l+a[rt].r)>>1; if(r<=mid) return query(rt<<1,l,r); else if(mid<l) return query((rt<<1)|1,l,r); else{ return (query(rt<<1,l,mid)&&query((rt<<1)|1,mid+1,r)); } } int main() { int t; scanf("%d",&t); while(t--) { cnt=0; int n; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d%d",&posters[i].l,&posters[i].r); x[cnt++]=posters[i].l; x[cnt++]=posters[i].r; } sort(x,x+cnt); cnt=unique(x,x+cnt)-x; for(int i=0;i<cnt;i++) { hash[x[i]]=i+1; } //区间离散化完毕 build(1,1,cnt); int res=0; for(int i=n-1;i>=0;i--) { if(!query(1,hash[posters[i].l],hash[posters[i].r])) res++; update(1,hash[posters[i].l],hash[posters[i].r]); } printf("%d\n",res); } return 0; }
时间: 2024-09-27 23:45:18