典型的双关键字排序咯。。
A,B分别排好序
从大到小遍历B,每次遍历到Bi时,确保 所有第一关键字大于Bi的 Aj都要在一个set里候选,即可保证贪心正确
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
const int N=21234;
struct node
{
int x,y;
node() {}
node(int a,int b)
{
x=a,y=b;
}
bool operator <(const node b)const
{
return y<b.y;
}
};
node A[112345];
node B[112345];
set<node > sb;
set<node > ::iterator it;
bool cmp(node a,node b)
{
if (a.x!=b.x)
return a.x<b.x;
else
return a.y<b.y;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
scanf("%d",&n);
int x,y;
for (int i=1; i<=n; i++)
{
scanf("%d%d",&x,&y);
A[i]=node(x,y);
}
for (int i=1; i<=n; i++)
{
scanf("%d%d",&x,&y);
B[i]=node(x,y);
}
sb.clear();
sort(A+1,A+1+n,cmp);
sort(B+1,B+1+n,cmp);
int j=n;
int ans=0;
for (int i=n; i>=1; i--)
{
while(A[j].x>=B[i].x&&j>0)
{
sb.insert(A[j]);
j--;
}
it=sb.lower_bound(B[i]);
if (it!=sb.end())
{
ans++;
sb.erase(it);
}
}
printf("%d\n",ans);
}
}