CSES - Shared codeLink to this code:
https://cses.fi/paste/c30edc5329f98fb43a6cd0/
#include<iostream>
#include<algorithm>
#include<vector>
#define int long long
using namespace std;
const int N=2e5+30;
typedef pair<int,int> p;
p P[N];
bool cmp(int a,int b){
return P[a].second<P[b].second;
}
long long DQ(int l,int r){
if(l==r) return 8e18;
int mid=l+(r-l)/2,cur=min(DQ(l,mid),DQ(mid+1,r));
vector<int> V;
for(int i=l;i<=r;i++){
if(abs(P[i].first-P[mid].first)<cur){
V.push_back(i);
}
}
sort(V.begin(),V.end(),cmp);
for(int i=0;i<V.size();i++){
for(int j=max((long long)0,i-10);j<i;j++){
int x=(P[V[i]].first-P[V[j]].first),y=(P[V[i]].second-P[V[j]].second);
// cout<<x<<" "<<y<<"\n";
if(cur>x*x+y*y){
cur=x*x+y*y;
}
}
}
return cur;
}
signed main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>P[i].first>>P[i].second;
}
sort(P,P+n);
cout<<DQ(0,n-1)<<"\n";
}