STL是多么重要啊。
#include<stdio.h>
#include<algorithm>
#include<set>
using namespace std;
typedef struct
{
int h,w;
}Node; Node Alice[100010],Bob[100010]; multiset<int> s;
int cmp(Node a,Node b)
{
if(a.w==b.w) return a.h<b.h;
return a.w<b.w;
}
int main()
{
int T,N,h,w,i,j,ans;
scanf("%d",&T);
while(T--)
{
s.clear();ans=0;
scanf("%d",&N);
for(i=1;i<=N;i++) scanf("%d%d",&Alice[i].h,&Alice[i].w);
for(i=1;i<=N;i++) scanf("%d%d",&Bob[i].h,&Bob[i].w);
sort(Alice+1,Alice+N+1,cmp);
sort(Bob+1,Bob+N+1,cmp);
int p=1; int k;
for(i=1;i<=N;i++)
{
while( p<=N && Bob[p].w<=Alice[i].w )
{
s.insert(Bob[p].h); p++;
}
if(s.empty()) continue;
multiset<int>::iterator it=s.lower_bound(Alice[i].h);
if( *(it) == Alice[i].h)
{
s.erase(it); ans++;
}
else
{
if(it==s.begin()) continue;
else s.erase(--it),ans++;
}
}
printf("%d\n",ans);
}
return 0;
}