比赛 EYOI与SBOI开学欢乐赛10th 评测结果 EEEEEEEEEE
题目名称 耍猴游戏 最终得分 0
用户昵称 op_组撒头屯 运行时间 1.757 s
代码语言 C++ 内存使用 6.72 MiB
提交时间 2022-10-10 21:13:24
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N=100000+5;
const int T=10000+5;
const int inf=0x3f3f3f3f;
int n,m;
int x[N],y[N];
vector<int>v[T];
int ans=inf,a,b;
int getid(int x,int y){
    return ((x-1)/1000)*1000+(y-1)/1000;
}
bool check(int id){
    for (int i=0;i<v[id].size();i++){
        ans=min(ans,abs(a-x[v[id][i]])+abs(b-y[v[id][i]]));
    }
    return v[id].size()!=0;
}
bool calc(int d,int id){
    bool yes=0;
    int yy=id%1000,xx=(id-yy)/1000;
    for (int i=max(0,yy-d),p=max(xx-d,0),q=min(xx+d,99);i<=min(99,yy+d);i++){
        yes|=check(p*100+i);
        yes|=check(q*100+i);
    }
    for (int i=max(0,xx-d),p=max(yy-d,0),q=min(yy+d,99);i<=min(99,xx+d);i++){
        yes|=check(p+i*100);
        yes|=check(q+i*100);
    }
    return yes;
}
int main(){
	freopen ("monkeygame.in","r",stdin);
	freopen ("monkeygame.out","w",stdout);
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++){
	    scanf("%d%d",&x[i],&y[i]);
	    int id=getid(x[i],y[i]);
	    v[id].push_back(i);
    }
    while(m--){
        int opt;scanf("%d",&opt);
        if (opt==1){
            n++;
            scanf("%d%d",&x[n],&y[n]);
            int id=getid(x[n],y[n]);
	        v[id].push_back(n);
        }
        else{
            scanf("%d%d",&a,&b);
            int id=getid(a,b);ans=inf;
            if (check(id)){
                calc(1,id);
                printf("%d\n",ans);continue;
            }
            int d=1;
            while(!calc(d,id))d++;
            calc(d+1,id);
            printf("%d\n",ans);
        }
    }
    return 0;
}