这份代码在洛谷开O2能过
#include <iostream>
#include <algorithm>
#include <cmath>
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
int x1,y1,x2,y2;
int n;
typedef pair<int,int> PII;
PII a[N];
PII dis1[N],dis2[N];
int b[N];
int f(int d){
int l = 1,r = n;
while(l < r){
int mid = l + r + 1 >> 1;
if(dis1[mid].x <= d) l = mid;
else r = mid - 1;
}
return l;
}
int main(){
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d %d",&a[i].x,&a[i].y);
for(int i=1;i<=n;i++){
dis1[i].x = (x1 - a[i].x) * (x1 - a[i].x) + (y1 - a[i].y) * (y1 - a[i].y);
dis1[i].y = i;
dis2[i].x = (x2 - a[i].x) * (x2 - a[i].x) + (y2 - a[i].y) * (y2 - a[i].y);
dis2[i].y = i;
}
sort(dis1 + 1,dis1 + n + 1);
for(int i=1;i<=n;i++) b[dis1[i].y] = i;
sort(dis2 + 1,dis2 + n + 1);
int ans = 1e9;
for(int i=1;i<=n;i++){
int d = (x1 - a[i].x) * (x1 - a[i].x) + (y1 - a[i].y) * (y1 - a[i].y);
int t = f(d);
bool flag = false;
for(int j=n;j>=1;j--){
if(b[dis2[j].y] > t){
ans = min(ans,d + dis2[j].x);
flag = true;
break;
}
}
if(!flag) ans = min(ans,d);
}
printf("%d",ans);
return 0;
}