poj2528 Mayor's posters
34772K 63MS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct post
{
int head;
int tail;
}p[10010];
struct node
{
node *left;
node *right;
int L;
int R;
bool bcover;
};
node tree[1000000];
int npoint;
int endpoint[20020];
bool cmp(int a,int b)
{
return a<b;
}
int ncount;//离散化后的不可分割区间数量
int hash[10000010];//板砖i离散化后所在的区间的编号
int nnode;
int res;
void buildtree(node *p,int L,int R)
{
if(L==R)
{
p->bcover=false;
p->L=L;
p->R=R;
return;
}
p->L=L;
p->R=R;
p->bcover=false;
int mid=(L+R)/2;
nnode++;
p->left=tree+nnode;
nnode++;
p->right=tree+nnode;
buildtree(p->left,L,mid);
buildtree(p->right,mid+1,R);
}
bool flag;
void query(node *p,int L,int R)
{
if(p->bcover==true)
{
return;
}
if(p->L==L&&p->R==R)
{
p->bcover=true;
flag=true;
return;
}
int mid=(p->L+p->R)/2;
if(R<=mid)
{
query(p->left,L,R);
}
else if(L>=mid+1)
{
query(p->right,L,R);
}
else
{
query(p->left,L,mid);
query(p->right,mid+1,R);
}
if(p->left->bcover==true&&p->right->bcover==true)
{
p->bcover=true;
}
}
int main()
{
int total;
scanf("%d",&total);
while(total--)
{
ncount=0;
int n;
scanf("%d",&n);
int i;
npoint=0;
for(i=0;i<n;i++)
{
scanf("%d %d",&p[i].head,&p[i].tail);
endpoint[npoint++]=p[i].head;
endpoint[npoint++]=p[i].tail;
}
sort(endpoint,endpoint+npoint,cmp);
npoint=unique(endpoint,endpoint+npoint)-endpoint;
//离散化
ncount=1;//离散化后的区间是从1开始的
for(i=0;i<npoint;i++)
{
hash[endpoint[i]]=ncount;
if(i==npoint-1)
{
continue;
}
if(endpoint[i+1]-endpoint[i]==1)
{
ncount++;
}
else
{
ncount+=2;
}
}
nnode=0;
buildtree(tree,1,ncount);
res=0;
for(i=n-1;i>=0;i--)
{
flag=false;
query(tree,hash[p[i].head],hash[p[i].tail]);
if(flag)
{
res++;
}
}
printf("%d\n",res);
}
return 0;
}