文章目录
R e s u l t Result Result
H y p e r l i n k Hyperlink Hyperlink
https://www.ybtoj.com.cn/contest/62/problem/1
D e s c r i p t i o n Description Description
n n n个点,打掉一个点可以把它右边和它上边的所有点打掉,问打掉所有点至少需要打多少次
数据范围: n ≤ 1 0 5 n\leq 10^5 n≤105
S o l u t i o n Solution Solution
排序之后依次打即可,可以证明这是最优的
复杂度 O ( T q log q ) O(Tq\log q) O(Tqlogq)【 q q q就是上文中的 n n n】
C o d e Code Code
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;int n,m,q,T,ans;
struct node{int x,y;}p[100010];
bool bjx[100010],bjy[100010];
inline bool cmp(node x,node y){return x.x<y.x||x.x==y.x&&x.y<y.y;}
inline bool cnp(node x,node y){return x.y<y.y||x.y==y.y&&x.x<y.x;}
inline LL read()
{
char c;LL d=1,f=0;
while(c=getchar(),!isdigit(c)) if(c=='-') d=-1;f=(f<<3)+(f<<1)+c-48;
while(c=getchar(),isdigit(c)) f=(f<<3)+(f<<1)+c-48;
return d*f;
}
signed main()
{
freopen("iakioi.in","r",stdin);
freopen("iakioi.out","w",stdout);
T=read();
while(T--)
{
memset(bjx,0,sizeof(bjx));memset(bjy,0,sizeof(bjy));
n=read();m=read();q=read();
for(register int i=1;i<=q;i++) p[i].x=read(),p[i].y=read();
ans=0;
sort(p+1,p+1+q,cmp);
for(register int i=1;i<=q;i++)
{
if(bjx[p[i].x]||bjy[p[i].y]) continue;
bjx[p[i].x]=bjy[p[i].y]=true;
ans++;
}
sort(p+1,p+1+q,cnp);
for(register int i=1;i<=q;i++)
{
if(bjx[p[i].x]||bjy[p[i].y]) continue;
bjx[p[i].x]=bjy[p[i].y]=true;
ans++;
}
printf("%d\n",ans);
}
}