Problem:
問題 ４
平面上の n 個の点 P
1
, ..., P
n
が与えられたとき，
距離が最小の２点を求めたい．
入力ファイルの１行目には整数 n が書いてある．
２行目から n+1 行目のそれぞれには，
２つの正整数 x, y が１つの半角空白文字を区切りとして書いてある．
i+1 行目の x, y はそれぞれ P
i
の x 座標，
P
i
の y 座標である．
これら n 点の中から最も近い２点を選んだとき，
この２点間の距離の２乗を出力せよ．
ただし，
2≤n≤500000 かつ -10000≤x≤10000, -10000≤y≤10000 とし，
５つの入力ファイルのうち３つでは n≤100 である．
また，どの入力ファイルにおいても，全ての座標は異なるものとする．
出力ファイルにおいては，
出力の最後の行にも改行コードを入れること．
入出力例
入力例１
2
0 0
1 1
出力例１
2
入力例２
3
5 5
0 0
-3 -4
出力例２
25
問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

void Solve() {
    int N;
    cin >> N;
    vector<pair<ll,ll>> P(N);
    rep(i,0,N) {
        ll X, Y;
        cin >> X >> Y;
        P[i] = {X,Y};
    }
    sort(ALL(P));
    auto Dist = [&](int i, int j) -> ll {
        return (P[i].first - P[j].first) * (P[i].first - P[j].first) + (P[i].second - P[j].second) * (P[i].second - P[j].second);
    };
    auto Calc = [&](auto self, int L, int R) -> ll {
        if (R - L == 1) return INF;
        if (R - L == 2) return Dist(L,L+1);
        int MID = (L+R) / 2;
        ll Ret = min(self(self,L,MID), self(self,MID,R));
        vector<int> ord(R-L);
        iota(ALL(ord),L);
        sort(ALL(ord),[&](int i, int j){return P[i].second < P[j].second;});
        ll MX = P[MID].first;
        vector<int> V;
        for (int i : ord) {
            if ((P[i].first - MX) * (P[i].first - MX) > Ret) continue;
            rrep(j,0,SZ(V)) {
                if ((P[i].second - P[V[j]].second) * (P[i].second - P[V[j]].second) > Ret) break;
                chmin(Ret, Dist(i,V[j]));
            }
            V.push_back(i);
        }
        return Ret;
    };
    ll ANS = Calc(Calc,0,N);
    cout << ANS << endl;
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
int _ = 1;
//cin >> _;
while(_--) {
    Solve();
}
}
accuracy: 1
time_ms: 330
memory_kb: 13932
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
bool cmp(pair<int,int> u1,pair<int,int> u2){
	if (u1.first==u2.first){
		return (u1.second<u2.second);
	}
	return (u1.first<u2.first);
}
bool cmp1(pair<int,int> u1,pair<int,int> u2){
	if (u1.second==u2.second){
		return (u1.first<u2.first);
	}
	return (u1.second<u2.second);
}
int main(){
	int n;cin>>n;pair<int,int>  a[n+1];
	for (int i=1;i<=n;i++) cin>>a[i].first>>a[i].second;
	sort(a+1,a+n+1,cmp);
	int mi=1e9;
	for (int i=2;i<=n;i++){
		int x=a[i-1].first,x1=a[i-1].second,x2=a[i].first,x3=a[i].second;
		mi=min(mi,(x-x2)*(x-x2)+(x1-x3)*(x1-x3));
	}
	sort(a+1,a+n+1,cmp1);
//	for (int i=1;i<=n;i++) cout<<a[i].first<<" "<<a[i].second<<endl;
	for (int i=2;i<=n;i++){
		int x=a[i-1].first,x1=a[i-1].second,x2=a[i].first,x3=a[i].second;
		mi=min(mi,(x-x2)*(x-x2)+(x1-x3)*(x1-x3));
	}
	cout<<mi<<endl;
}
accuracy: 1
time_ms: 210
memory_kb: 7196
final_rank: 1

code_snippet: #define _GLGBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

struct Point {
	int x;
	int y;
    int idx;
};

constexpr int INF = 1000000000.0;

int n;
vector<bool> q_or_r, s_or_not;

int calc_euclidean_distance(Point p_1, Point p_2) {
	return (
		(p_2.x - p_1.x) * (p_2.x - p_1.x) + 
		(p_2.y - p_1.y) * (p_2.y - p_1.y)
	);
}

bool compare_by_x(const Point &a, const Point &b) {
    return a.x < b.x;
}

bool compare_by_y(const Point &a, const Point &b) {
    return a.y < b.y;
}

pair<Point, Point> closest_pair_rec(
    vector<Point> P_x, 
    vector<Point> P_y
) {
    if (static_cast<int>(P_x.size()) <= 3) {
        int min_d = INF;
        pair<Point, Point> closest_pair;

        for (int i = 0; i < static_cast<int>(P_x.size()) - 1; i++) {
            for (int j = i + 1; j < static_cast<int>(P_x.size()); j++) {
                int euclidean_distance = calc_euclidean_distance(P_x.at(i), P_x.at(j));

                if (min_d > euclidean_distance) {
                    min_d = euclidean_distance;
                    closest_pair = make_pair(P_x.at(i), P_x.at(j));
                }
            }
        }

        return closest_pair;
    } else {
        vector<Point> Q_x, Q_y, R_x, R_y, S_y;

        for (int k = 0; k < static_cast<int>(P_x.size()); k++) {
            if (k < (static_cast<int>(P_x.size()) + 1) / 2) {
                Q_x.push_back(P_x.at(k));
                q_or_r.at(P_x.at(k).idx) = true;
            } else {
                R_x.push_back(P_x.at(k));
                q_or_r.at(P_x.at(k).idx) = false;
            }
        }

        for (auto point: P_y) {
            if (q_or_r.at(point.idx))
                Q_y.push_back(point);
            else
                R_y.push_back(point);
        }

        auto [q_1, q_2] = closest_pair_rec(Q_x, Q_y);
        auto [r_1, r_2] = closest_pair_rec(R_x, R_y);

        int delta = min(calc_euclidean_distance(q_1, q_2), calc_euclidean_distance(r_1, r_2)),
        L = Q_x.at(static_cast<int>(Q_x.size()) - 1).x;

        for (auto point: P_x) {
            if (abs(point.x - L) <= delta)
                s_or_not.at(point.idx) = true;
            else
                s_or_not.at(point.idx) = false;
        }

        for (auto point : P_y) {
            if (s_or_not.at(point.idx))
                S_y.push_back(point);
        }

        int min_d = INF;
        pair<Point, Point> closest_pair;

        for (int p = 0; p < static_cast<int>(S_y.size()); p++) {
            for (int q = 1; q <= 15; q++) {
                if (p + q < static_cast<int>(S_y.size())) {
                    int euclidean_distance = calc_euclidean_distance(S_y.at(p), S_y.at(p + q));
                    if (min_d > euclidean_distance) {
                        min_d = euclidean_distance;
                        closest_pair = make_pair(S_y.at(p), S_y.at(p + q));
                    }
                }
            }
        }

        if (min_d < delta) {
            return closest_pair;
        } else if (
            calc_euclidean_distance(q_1, q_2) <
            calc_euclidean_distance(r_1, r_2)
        ) return make_pair(q_1, q_2);
        else
            return make_pair(r_1, r_2);
    }
}

pair<Point, Point> closest_pair(vector<Point> P) {
    vector<Point> P_x = P, P_y = P;

    sort(P_x.begin(), P_x.end(), compare_by_x);
    sort(P_y.begin(), P_y.end(), compare_by_y);

	return closest_pair_rec(P_x, P_y);
}

int main() {
    cin >> n;

    vector<Point> points(n);

    for (int i = 0; i < n; i++) {
        cin >> points.at(i).x >> points.at(i).y;
        points.at(i).idx = i;
    }

    q_or_r.resize(n);
    s_or_not.resize(n);

    for (int j = 0; j < n; j++) {
        q_or_r.at(j) = false;
        s_or_not.at(j) = false;
    }

    auto [p_1, p_2] = closest_pair(points);

    cout << fixed << setprecision(12) << 
        calc_euclidean_distance(p_1, p_2)
    << '\n';

    return 0;
}
accuracy: 1
time_ms: 600
memory_kb: 73912
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll =long long;
using Point=array<ll,2>;
ll d(Point A,Point B){
    ll dx=A[0]-B[0];
    ll dy=A[1]-B[1];
    return dx*dx+dy*dy;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll N;
    cin>>N;
    vector<array<ll,2>> P(N);
    for(int i=0;i<N;i++)cin>>P[i][0]>>P[i][1];
    sort(P.begin(),P.end());
    ll an=d(P[0],P[1]);
    for(int i=2;i<N;i++){
        for(int j=i-1;j>=0;j--){
            ll dx=P[i][0]-P[j][0];
            if(dx*dx>=an)break;
            an=min(an,d(P[j],P[i]));
        }
    }
    cout<<an<<"\n";

}
accuracy: 1
time_ms: 230
memory_kb: 11244
final_rank: 8

code_snippet: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define rep(i, m, n) for (ll i = (ll)m; i < (ll)n; i++)
#define drep(i, m, n) for (ll i = m - 1; i >= n; i--)
#define Endl endl
#define all(a) a.begin(), a.end()
#define pr(i, j) make_pair(i, j)
#define isin(x, l, r) (l <= x && x < r)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define srt(ar) sort(ar.begin(), ar.end())
#define rev(ar) reverse(ar.begin(), ar.end())
#define jge(f, s, t) cout << (f ? s : t) << endl
#define printar(ar)             \
    do                          \
    {                           \
        for (auto dbg : ar)     \
        {                       \
            cout << dbg << " "; \
        }                       \
        cout << endl;           \
    } while (0)
const ll inf = 1e18;
const ld pi = 3.14159265358979;
const ld eps = 1e-9;
template <class T, ll n, ll idx = 0>
auto make_vec(const ll (&d)[n], const T &init) noexcept
{
    if constexpr (idx < n)
        return std::vector(d[idx], make_vec<T, n, idx + 1>(d, init));
    else
        return init;
}

template <class T, ll n>
auto make_vec(const ll (&d)[n]) noexcept
{
    return make_vec(d, T{});
}

//////////////// 以下を貼る ////////////////
template <class T>
size_t HashCombine(const size_t seed, const T &v)
{
    return seed ^ (std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
}
/* pair用 */
template <class T, class S>
struct std::hash<std::pair<T, S>>
{
    size_t operator()(const std::pair<T, S> &keyval) const noexcept
    {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};
////////////////////////////////////////////
ll n;
unordered_map<pair<ll, ll>, vector<ll>> mp;
vector<pair<ld, ld>> ar;
ld d(ll i, ll j)
{
    ld x = ar[i].first - ar[j].first;
    ld y = ar[i].second - ar[j].second;
    ld ret = x * x + y * y;
    return sqrt(ret);
}
void remake(ld D)
{
    unordered_map<pair<ll, ll>, vector<ll>> cp;
    rep(i, 0, n)
    {
        ll X = floor(ar[i].first / D);
        ll Y = floor(ar[i].second / D);
        cp[pr(X, Y)].push_back(i);
    }
    mp = cp;
}
// AOJ CGL_5_A
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    ar.resize(n);
    rep(i, 0, n)
    {
        cin >> ar[i].first >> ar[i].second;
    }
    ld ans = 100000.0;
    remake(ans);
    while (true)
    {
        bool f = true;
        rep(i, 0, n)
        {
            ll X = floor(ar[i].first / ans);
            ll Y = floor(ar[i].second / ans);
            rep(j, 0, 2)
            {
                rep(k, 0, 2)
                {
                    rep(l, 0, mp[pr(X + j, Y + k)].size())
                    {
                        ll idx = mp[pr(X + j, Y + k)][l];
                        if (idx == i)
                        {
                            continue;
                        }
                        if (d(i, idx) < ans)
                        {
                            chmin(ans, d(i, idx));
                            f = false;
                            remake(ans);
                            // cout << ans << Endl;
                            goto next;
                        }
                    }
                }
            }
            rep(l, 0, mp[pr(X - 1, Y + 1)].size())
            {
                ll idx = mp[pr(X - 1, Y + 1)][l];
                if (idx == i)
                {
                    continue;
                }
                if (d(i, idx) < ans)
                {
                    chmin(ans, d(i, idx));
                    f = false;
                    remake(ans);
                    // cout << ans << Endl;
                    goto next;
                }
            }
        }
    next:
        if (f)
        {
            ll A = ans * ans + eps;
            cout << A << endl;
            break;
        }
    }
}
accuracy: 1
time_ms: 5100
memory_kb: 130456
final_rank: 19

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <map>
using namespace std;


bool cmpX(pair<int, int> &a, pair<int, int> &b) {
    return a.first < b.first;
}

bool cmpY(pair<int, int> &a, pair<int, int> &b) {
    return a.second < b.second;
}

long long dist(pair<int, int> &a, pair<int, int> &b) {
    long long dx = a.first - b.first;
    long long dy = a.second - b.second;
    return dx*dx + dy*dy;
}


long long closet_pair(vector<pair<int, int> > &p, int left, int right) {
    if(right - left <= 3) {
        long long minDist = 1e19;
        for(int i = left; i < right; i++) {
            for(int j = i+1; j < right; j++) {
                minDist = min(minDist, dist(p[i], p[j]));
            }
        }
        return minDist;
    }

    int mid = (left + right) / 2;
    long long d = min(closet_pair(p, left, mid), closet_pair(p, mid, right));

    vector<pair<int, int>> b;
    for(int i = left; i < right; i++) {
        if(abs(p[i].first - p[mid].first) * 1LL * abs(p[i].first - p[mid].first) < d) {
            b.push_back(p[i]);
        }
    }

    sort(b.begin(), b.end(), cmpY);

    for(int i = 0; i < b.size(); i++) {
        for(int j = i+1; j < b.size() && (abs(b[i].second - b[j].second) * 1LL * abs(b[i].second - b[j].second)) < d; j++) {
            d = min(d, dist(b[i], b[j]));
        }
    }
    return d;
}




int main(void) {

    int n;
    cin >> n;
    vector<pair<int, int>> p(n);
    for(int i = 0; i < n; i++) {
        cin >> p[i].first >> p[i].second; //first:x座標, second:y座標
    }

    sort(p.begin(), p.end(), cmpX);
    cout << closet_pair(p, 0, n) << endl;


    return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 6920
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  int n;

  cin >> n;

  vector<pair<int, int> > p(n);

  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
  }

  sort(p.begin(), p.end());
  
  long long min_dist = 1000000000;
 
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      long long x_square = (p[j].first - p[i].first) * (p[j].first - p[i].first);
      if (x_square > min_dist) break;
      long long y_square = (p[j].second - p[i].second) * (p[j].second - p[i].second);
      if (x_square + y_square < min_dist) {
        min_dist = x_square + y_square;
      }
    }
  }

  cout << min_dist << endl;

  return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 6920
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
template<typename T>
long double dist(T x1,T y1,T x2,T y2){
    return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
template<typename T>
long double dist(pair<T,T>p1,pair<T,T>p2){
    return dist(p1.first,p1.second,p2.first,p2.second);
}
//assume sorted by x coordinate
long double closest_pair(vector<pair<long double,long double>>::iterator begin,vector<pair<long double,long double>>::iterator end){
    int size=end-begin;
    assert(size>1);
    if(size==2)return dist(*begin,*(begin+1));
    if(size==3)return min({dist(*begin,*(begin+1)),dist(*begin,*(begin+2)),dist(*(begin+1),*(begin+2))});
    auto cen=begin+size/2;
    auto d=min(closest_pair(begin,cen),closest_pair(cen,end));
    vector<pair<long double,long double>>strip;
    for(auto it=begin;it<end;it++){
        if(abs((*it).first-(*cen).first)<d)strip.emplace_back(*it);
    }
    sort(strip.begin(),strip.end(),[](pair<long double,long double>&p,pair<long double,long double>&q){return p.second<q.second;});
    for(int i=0;i<strip.size();i++){
        for(int j=i-1;j>=0&&strip[j].second>strip[i].second-d;j--){
            d=min(d,dist(strip[i],strip[j]));
        }
        for(int j=i+1;j<strip.size()&&strip[j].second<strip[i].second+d;j++){
            d=min(d,dist(strip[i],strip[j]));
        }
    }
    return d;
}
int main(){
    int N;cin>>N;
    vector<pair<long double,long double>>P(N);
    for(int i=0;i<N;i++){
        cin>>P[i].first>>P[i].second;
    }
    sort(P.begin(),P.end());
    long double d=closest_pair(P.begin(),P.end());
    cout<<(long long)round(d*d)<<endl;
    return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 18820
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

void fastIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}


struct pt{
    long long x, y;
};

pt operator -(pt a, pt b){
    return {a.x - b.x, a.y - b. y};
}


long long sq(pt p) {return p.x*p.x + p.y*p.y;}
long double abs(pt p) {return sqrt(sq(p));}


const long long oo = 1e18;

int main() {
    fastIO();

    int n; cin >> n;

    vector<pt> v(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i].x >> v[i].y;
    }

    sort(v.begin(), v.end(), [&](pt & a, pt & b)->bool{
        return a.x < b.x;
    });

    set<pair<long long, long long>> sorted_y_window;

    long long ans = oo;
    long double best = oo;
    sorted_y_window.insert({v[0].y, v[0].x});

    int last_l = 0;
    for (int i = 1; i < n; ++i) {

        //Deleting Further than best
        while(last_l < n && v[i].x - v[last_l].x > best){
            sorted_y_window.erase({v[last_l].y, v[last_l].x});
            last_l++;
        }

        auto st = sorted_y_window.lower_bound({v[i].y - best, -oo});
        auto en = sorted_y_window.lower_bound({v[i].y + best, -oo});
        
        //iterating over points
        while(st != en){
            pt cur = {st->second, st->first};
            if(sq(v[i] - cur) < ans){
                ans = sq(v[i] - cur);
                best = abs(v[i] - cur);
            }
            st++;
        }
        //handeling if last point was not checked
        if(st != sorted_y_window.end()){
            pt cur = {st->second, st->first};
            if(sq(v[i] - cur) < ans){
                ans = sq(v[i] - cur);
                best = abs(v[i] - cur);
            }
        }
        sorted_y_window.insert({v[i].y, v[i].x});
    }

    cout << ans << endl;


}
accuracy: 1
time_ms: 170
memory_kb: 10992
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

void fastIO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}


#define all(a)   a.begin(),a.end()

struct pt{
    long long x, y;
};

pt operator -(pt a, pt b){
    return {a.x - b.x, a.y - b. y};
}


long long sq(pt p) {return p.x*p.x + p.y*p.y;}
long double abs(pt p) {return sqrt(sq(p));}


const long long oo = 1e18;

int main() {
    fastIO();

    int n; cin >> n;

    vector<pt> v(n);
    for (long long i = 0; i < n; ++i) {
        cin >> v[i].x >> v[i].y;
    }

    sort(all(v), [&](pt & a, pt & b)->bool{
        return a.x < b.x;
    });

    set<pair<long long, long long>> sorted_y_window;

    long long x_line = v[0].x, ans = LLONG_MAX;
    long double best = 1e18;
    sorted_y_window.insert({v[0].y, v[0].x});

    long long last_l = 0;
    for (long long i = 1; i < n; ++i) {

        //Deleting Further than best
        while(last_l < n && v[i].x - v[last_l].x > best){
            sorted_y_window.erase({v[last_l].y, v[last_l].x});
            last_l++;
        }

        //caculating distances
        auto st = sorted_y_window.lower_bound({v[i].y - best, -oo});
        auto en = sorted_y_window.lower_bound({v[i].y + best, -oo});
        while(st != en){
            pt cur = {st->second, st->first};
            if(sq(v[i] - cur) < ans){
                ans = sq(v[i] - cur);
                best = abs(v[i] - cur);
            }
            st++;
        }
        if(st != sorted_y_window.end()){
            pt cur = {st->second, st->first};
            if(sq(v[i] - cur) < ans){
                ans = sq(v[i] - cur);
                best = abs(v[i] - cur);
            }
        }
        sorted_y_window.insert({v[i].y, v[i].x});
    }

    cout << ans << endl;


}
accuracy: 1
time_ms: 180
memory_kb: 10588
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair

typedef long long ll;
//(x座標,y座標)
typedef pair<int, int> P;

bool compare_y(P a, P b)
{
    return a.second < b.second;
}

// イテレータを渡す
template <class T>
int closest_pair(T a, int n)
{
    if (n <= 1)
        return INT32_MAX;
    int m = n / 2;
    int x = (*(a + m)).first;
    int d = min(closest_pair(a, m), closest_pair(a + m, n - m));
    inplace_merge(a, a + m, a + n, compare_y);

    vector<P> b;
    rep(i, n)
    {
        if (abs((*(a + i)).first - x) >= d)
            continue;
        int xx = b.size();
        rep(j, xx)
        {
            int x = (*(a + i)).first - b.at(xx - 1 - j).first;
            int y = (*(a + i)).second - b.at(xx - 1 - j).second;
            if (y >= d)
                break;
            d = min(d, x * x + y * y);
        }
        b.push_back(*(a + i));
    }
    return d;
}

int main()
{
    int n;
    cin >> n;

    vector<P> A(n);
    rep(i, n)
    {
        cin >> A[i].first >> A[i].second;
    }

    cout << closest_pair(A.begin(), n) << endl;

    return 0;
}
accuracy: 1
time_ms: 350
memory_kb: 12828
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define fi first
#define se second
using namespace std;
const ll N = 5e5 + 9;
ll n, i;
pair<ll,ll>a[N];
int main()
{
#define TN ""
    if (fopen(TN ".inp", "r"))
    {
        freopen(TN ".inp", "r", stdin);
        freopen(TN ".out", "w", stdout);
    }
    cin.tie(0)->sync_with_stdio(0);

    cin >> n ;

    for( i = 1; i <= n; i ++ )
        cin >> a[i].fi >> a[i].se;

    sort( a + 1, a + n + 1 );
    ll ans = 1e18;

    for( i = 1; i < n; i ++ )
        ans = min(ans, (a[i].fi - a[i + 1].fi) * ( a[i].fi - a[i + 1].fi ) + (a[i].se - a[i + 1].se) * ( a[i].se - a[i + 1].se ));

    for( i = 1; i <= n; i ++ )
        swap(a[i].fi, a[i].se);
    sort( a + 1, a + n + 1 );
    for( i = 1; i < n; i ++ )
        ans = min(ans, (a[i].fi - a[i + 1].fi) * ( a[i].fi - a[i + 1].fi ) + (a[i].se - a[i + 1].se) * ( a[i].se - a[i + 1].se ));

        cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 11188
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits> // INT_MAXが定義されているヘッダ

using namespace std;

typedef pair<int, int> Point;

// 点のx座標に基づいてソートするための比較関数
bool compareX(const Point &a, const Point &b) {
    return a.first < b.first;
}

// 点のy座標に基づいてソートするための比較関数
bool compareY(const Point &a, const Point &b) {
    return a.second < b.second;
}

// 2つの点間の距離の2乗を計算
int distSquared(const Point &a, const Point &b) {
    int dx = a.first - b.first;
    int dy = a.second - b.second;
    return dx * dx + dy * dy;
}

// ブルートフォース法で最近点間距離を計算
int bruteForce(const vector<Point> &points, int left, int right) {
    int minDist = INT_MAX;
    for (int i = left; i < right; ++i) {
        for (int j = i + 1; j < right; ++j) {
            minDist = min(minDist, distSquared(points[i], points[j]));
        }
    }
    return minDist;
}

// stripでの最小距離を求める
int stripClosest(vector<Point> &strip, int size, int d) {
    int minDist = d;
    sort(strip.begin(), strip.begin() + size, compareY);
    for (int i = 0; i < size; ++i) {
        for (int j = i + 1; j < size && (strip[j].second - strip[i].second) * (strip[j].second - strip[i].second) < minDist; ++j) {
            minDist = min(minDist, distSquared(strip[i], strip[j]));
        }
    }
    return minDist;
}

// 分割統治法で最近点間距離を求める
int closestUtil(vector<Point> &points, int left, int right) {
    if (right - left <= 3) {
        return bruteForce(points, left, right);
    }

    int mid = left + (right - left) / 2;
    Point midPoint = points[mid];

    int dl = closestUtil(points, left, mid);
    int dr = closestUtil(points, mid, right);

    int d = min(dl, dr);

    vector<Point> strip;
    for (int i = left; i < right; ++i) {
        if ((points[i].first - midPoint.first) * (points[i].first - midPoint.first) < d) {
            strip.push_back(points[i]);
        }
    }

    return min(d, stripClosest(strip, strip.size(), d));
}

int closest(vector<Point> &points) {
    sort(points.begin(), points.end(), compareX);
    return closestUtil(points, 0, points.size());
}

int main() {
    int n;
    cin >> n;
    vector<Point> points(n);
    for (int i = 0; i < n; ++i) {
        cin >> points[i].first >> points[i].second;
    }
    cout << closest(points) << endl;
    return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 6808
final_rank: 3

code_snippet: #include <iostream>
#include <utility>
#include <climits>
#include <vector>
#include <cmath>
using namespace std;

int manhattanndis(pair<int, int> point1,pair<int, int> point2){
    return abs(point1.first - point2.first) + abs(point1.second - point2.second);
}

int eudistance(pair<int, int> point1,pair<int, int> point2){
    return (point1.first - point2.first)*(point1.first - point2.first)+(point1.second - point2.second)*(point1.second - point2.second);
}

int main(){
    int n;
    cin >> n;
    vector< pair<int, int> > points(n);
    pair< pair<int, int>, pair<int, int> > closepair;
    int manhattanndistance, mindis = INT_MAX;
    
    for (int i = 0; i < n; i++){
        cin >> points[i].first >> points[i].second;
    }
        
    for (int j = 0; j < n; j++){
        for (int k = 0; k < n; k++){
            if (j != k){
                manhattanndistance = manhattanndis(points[j],points[k]);
                if (manhattanndistance < mindis){
                    mindis = manhattanndistance;
                    closepair = {points[j], points[k]};
                }
            }
        }
    }
    mindis = eudistance(closepair.first, closepair.second);
    cout << mindis << endl;
    
}
accuracy: 0.6
time_ms: 4280
memory_kb: 3440
final_rank: 20

code_snippet: #pragma region Macros

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2")

#include <bits/extc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using namespace __gnu_pbds;

// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// using Bint = mp::cpp_int;
// using Bdouble = mp::number<mp::cpp_dec_float<256>>;

#define pb emplace_back
#define int ll
#define endl '\n'

#define sqrt __builtin_sqrtl
#define cbrt __builtin_cbrtl
#define hypot __builtin_hypotl

// #define next asdnext
// #define prev asdprev

using ll = long long;
using ld = long double;
const ld PI = acosl(-1);
const int INF = 1 << 30;
const ll INFL = 1LL << 61;
// const int MOD = 998244353;
const int MOD = 1000000007;

const ld EPS = 1e-10;
const bool equals(ld a, ld b) { return fabs((a) - (b)) < EPS; }

const vector<int> dx = {0, 1, 0, -1, 1, 1, -1, -1}; // → ↓ ← ↑ ↘ ↙ ↖ ↗
const vector<int> dy = {1, 0, -1, 0, 1, -1, -1, 1};

struct Edge {
    int from, to, cost;
    Edge() : from(-1), to(-1), cost(-1) {}
    Edge(int to, int cost) : to(to), cost(cost) {}
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};

chrono::system_clock::time_point  start, now;
__attribute__((constructor))
void constructor() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
    start = chrono::system_clock::now();
}

__int128_t POW(__int128_t x, int n) {
    __int128_t ret = 1;
    assert(n >= 0);
    if (x == 1 or n == 0) ret = 1;
    else if (x == -1 && n % 2 == 0) ret = 1; 
    else if (x == -1) ret = -1; 
    else if (n % 2 == 0) {
        assert(x < INFL);
        ret = POW(x * x, n / 2);
    } else {
        assert(x < INFL);
        ret = x * POW(x, n - 1);
    }
    return ret;
}
int per(int x, int y) { // x = qy + r (0 <= r < y) を満たすq
    assert(y != 0);
    if (x >= 0 && y > 0) return x / y;
    if (x >= 0 && y < 0) return x / y - (x % y < 0);
    if (x < 0 && y < 0) return x / y + (x % y < 0);
    return x / y - (x % y < 0); //  (x < 0 && y > 0) 
}
int mod(int x, int y) { // x = qy + r (0 <= r < y) を満たすr
    assert(y != 0);
    if (x >= 0) return x % y;
    __int128_t ret = x % y; // (x < 0)
    ret += (__int128_t)abs(y) * INFL;
    ret %= abs(y);
    return ret;
}
int floor(int x, int y) { // (ld)x / y 以下の最大の整数
    assert(y != 0);
    if (y < 0) x = -x, y = -y;
    return x >= 0 ? x / y : (x + 1) / y - 1;
}
int ceil(int x, int y) { // (ld)x / y 以上の最小の整数
    assert(y != 0);
    if (y < 0) x = -x, y = -y;
    return x > 0 ? (x - 1) / y + 1 : x / y;
}
int round(int x, int y) {
    assert(y != 0);
    return (x * 2 + y) / (y * 2);
}
int round(int x, int y, int k) { // (ld)(x/y)を10^kの位に関して四捨五入
    assert(y != 0); // TODO
    return INF;
}
int round2(int x, int y) { // 五捨五超入 // 未verify
    assert(y != 0);
    if (y < 0) y = -y, x = -x;
    int z = z / y;
    if ((z * 2 + 1) * y <= y * 2) z++;
    return z;
}
int floor(ld x, ld y) { // 誤差対策TODO
    assert(!equals(y, 0));
    return floor(x / y);
}
int ceil(ld x, ld y) { // 誤差対策TODO // ceil(p/q) = -floor(-(p/q))らしい
    assert(!equals(y, 0));
    return ceil(x / y);
}
int perl(ld x, ld y) { // x = qy + r (0 <= r < y, qは整数) を満たす q
    // 未verify. 誤差対策TODO. EPS外してもいいかも。
    assert(!equals(y, 0));
    if (x >= 0 && y > 0) return floor(x / y)+EPS;
    if (x >= 0 && y < 0) return -floor(x / fabs(y));
    if (x < 0 && y < 0) return floor(x / y) + (x - floor(x/y)*y < -EPS);
    return floor(x / y) - (x - floor(x/y)*y < -EPS); //  (x < 0 && y > 0) 
}
ld modl(ld x, ld y) { // x = qy + r (0 <= r < y, qは整数) を満たす r
    // 未verify. 誤差対策TODO. -0.0が返りうる。
    assert(!equals(y, 0));
    if (x >= 0) return x - fabs(y)*fabs(per(x, y));
    return x - fabs(y)*floor(x, fabs(y));
}
int seisuu(ld x) { return (int)x; } // 整数部分. 誤差対策TODO
int seisuu(int x, int y) {
    assert(y != 0);
    return x / y;
}
int seisuu(ld x, ld y) { // 誤差対策TODO
    assert(!equals(y, 0));
    return (int)(x / y);
}

pair<int, int> max(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.first > b.first or a.first == b.first && a.second > b.second) return a;
    return b;
}
pair<int, int> min(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.first < b.first or a.first == b.first && a.second < b.second) return a;
    return b;
}

template <class T> bool chmax(T &a, const T& b) {
    if (a < b) { a = b; return true; } return false;
}
template <class T> bool chmin(T &a, const T& b) {
    if (a > b) { a = b; return true; } return false;
}
template <class T> T mid(T a, T b, T c) {
    return a + b + c - max({a, b, c}) - min({a, b, c});
}
template <class T> void Sort(T &a, T &b, bool rev = false) {
    if (rev == false) { 
        if (a > b) swap(a, b);
    } else {
        if (b > a) swap(b, a);
    }
}
template <class T> void sort(T &a, T &b, T &c, bool rev = false) {
    if (rev == false) { 
        if (a > b) swap(a, b); if (a > c) swap(a, c); if (b > c) swap(b, c);
    } else {
        if (c > b) swap(c, b); if (c > a) swap(c, a); if (b > a) swap(b, a);
    }
}
template <class T> void sort(T &a, T &b, T &c, T &d, bool rev = false) {
    if (rev == false) { 
        if (a > b) swap(a, b); if (a > c) swap(a, c);  if (a > d) swap(a, d);
        if (b > c) swap(b, c); if (b > d) swap(b, d); if (c > d) swap(c, d);
    } else {
        if (d > c) swap(d, c); if (d > b) swap(d, b); if (d > a) swap(d, a);
        if (c > b) swap(c, b); if (c > a) swap(c, a); if (b > a) swap(b, a);
    }
}

int countl_zero(int x) { return __builtin_clzll(x); }
int countl_one(int x) {
    int ret = 0; while (x % 2) { x /= 2; ret++; } return ret;
}
int countr_zero(int x) { return __builtin_ctzll(x); }
int countr_one(int x) {
    int ret = 0, k = 63 - __builtin_clzll(x);
    while (k != -1 && (x & (1LL << k))) { k--; ret++; }
    return ret;
}
int popcount(int x) { return __builtin_popcountll(x); }
int unpopcount(int x) { return 64 - __builtin_clzll(x) - __builtin_popcountll(x); }

int top_bit(int x) { return 63 - __builtin_clzll(x);} // 2^kの位
int bot_bit(int x) { return __builtin_ctz(x);} // 2^kの位
int MSB(int x) { return 1 << (63 - __builtin_clzll(x)); } // mask
int LSB(int x) { return (x & -x); } // mask

int bit_width(int x) { return 64 - __builtin_clzll(x); } // 桁数
int ceil_log2(int x) { return 63 - __builtin_clzll(x); }
int bit_floor(int x) { return 1 << (63 - __builtin_clzll(x)); }
int floor_log2(int x) { return 64 - __builtin_clzll(x-1); }
int bit_ceil(int x) { return 1 << (64 - __builtin_clzll(x-1)) - (x==1); }

int hamming(int a, int b) { return popcount(a ^ b); }
int compcnt(int x) { return (popcount(x^(x >> 1)) + (x&1)) / 2; }

class UnionFind {
public:
	UnionFind() = default;
    UnionFind(int N) : par(N), sz(N, 1) {
        iota(par.begin(), par.end(), 0);
    }
	int root(int x) {
		if (par[x] == x) return x;
		return (par[x] = root(par[x]));
	}
	bool unite(int x, int y) {
		int rx = root(x);
		int ry = root(y);
        if (rx == ry) return false;
		if (sz[rx] < sz[ry]) swap(rx, ry);
		sz[rx] += sz[ry];
		par[ry] = rx;
        return true;
	}
	bool issame(int x, int y) { return (root(x) == root(y)); }
	int size(int x) { return sz[root(x)]; }
    vector<vector<int>> groups(int N) {
        vector<vector<int>> G(N);
        for (int x = 0; x < N; x++) {
            G[root(x)].push_back(x);
        }
		G.erase( remove_if(G.begin(), G.end(),
            [&](const vector<int>& V) { return V.empty(); }), G.end());
        return G;
    }
private:
	vector<int> par, sz;
};

template<typename T>
struct BIT {
    int N;               // 要素数
    vector<T> bit[2];  // データの格納先
    BIT(int N_, int x = 0) {
        N = N_ + 1;
        bit[0].assign(N, 0); bit[1].assign(N, 0);
        if (x != 0) {
            for (int i = 0; i < N; i++) add(i, x);
        }
    }
    BIT(vector<int> &A) {
        N = A.size() + 1;
        bit[0].assign(N, 0); bit[1].assign(N, 0);
        for (int i = 0; i < (int)A.size(); i++) add(i, A[i]);
    }
    void add_sub(int p, int i, T x) {
        while (i < N) { bit[p][i] += x; i += (i & -i); }
    }
    void add(int l, int r, T x) {
        add_sub(0, l + 1, -x * l); add_sub(0, r + 1, x * r);
        add_sub(1, l + 1, x); add_sub(1, r + 1, -x);
    }
    void add(int i, T x) { add(i, i + 1, x); }
    T sum_sub(int p, int i) {
        T ret = 0;
        while (i > 0) { ret += bit[p][i]; i -= (i & -i); }
        return ret;
    }
    T sum(int i) { return sum_sub(0, i) + sum_sub(1, i) * i; }
    T sum(int l, int r) { return sum(r) - sum(l); }
    T get(int i) { return sum(i, i + 1); }
    void set(int i, T x) { T s = get(i); add(i, -s + x); }
};

template<int mod> class Modint {
public:
    int val = 0;
    Modint(int x = 0) { while (x < 0) x += mod; val = x % mod; }
    Modint(const Modint &r) { val = r.val; }

    Modint operator -() { return Modint(-val); } // 単項
    Modint operator +(const Modint &r) { return Modint(*this) += r; }
    Modint operator +(const int &q) { Modint r(q); return Modint(*this) += r; }
    Modint operator -(const Modint &r) { return Modint(*this) -= r; }
    Modint operator -(const int &q) { Modint r(q); return Modint(*this) -= r; }
    Modint operator *(const Modint &r) { return Modint(*this) *= r; }
    Modint operator *(const int &q) { Modint r(q); return Modint(*this) *= r; }
    Modint operator /(const Modint &r) { return Modint(*this) /= r; }
    Modint operator /(const int &q) { Modint r(q); return Modint(*this) /= r; }
    
    Modint& operator ++() { val++; if (val >= mod) val -= mod; return *this; } // 前置
    Modint operator ++(signed) { ++*this; return *this; } // 後置
    Modint& operator --() { val--; if (val < 0) val += mod; return *this; }
    Modint operator --(signed) { --*this; return *this; }
    Modint &operator +=(const Modint &r) { val += r.val; if (val >= mod) val -= mod; return *this; }
    Modint &operator +=(const int &q) { Modint r(q); val += r.val; if (val >= mod) val -= mod; return *this; }
    Modint &operator -=(const Modint &r) { if (val < r.val) val += mod; val -= r.val; return *this; }
    Modint &operator -=(const int &q) { Modint r(q);  if (val < r.val) val += mod; val -= r.val; return *this; }
    Modint &operator *=(const Modint &r) { val = val * r.val % mod; return *this; }
    Modint &operator *=(const int &q) { Modint r(q); val = val * r.val % mod; return *this; }
    Modint &operator /=(const Modint &r) {
        int a = r.val, b = mod, u = 1, v = 0;
        while (b) {int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v);}
        val = val * u % mod; if (val < 0) val += mod;
        return *this;
    }
    Modint &operator /=(const int &q) {
        Modint r(q); int a = r.val, b = mod, u = 1, v = 0;
        while (b) {int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v);}
        val = val * u % mod; if (val < 0) val += mod;
        return *this;
    }
    bool operator ==(const Modint& r) { return this -> val == r.val; }
    bool operator <(const Modint& r) { return this -> val < r.val; }
    bool operator >(const Modint& r) { return this -> val > r.val; }
    bool operator !=(const Modint& r) { return this -> val != r.val; }
};

using mint = Modint<MOD>;
// using Mint = modint998244353;

istream &operator >>(istream &is, mint& x) {
    int t; is >> t; x = t; return (is);
}
ostream &operator <<(ostream &os, const mint& x) {
    return os << x.val;
}
mint modpow(const mint &x, int n) {
    if (n < 0) return (mint)1 / modpow(x, -n); // 未verify
    assert(n >= 0);
    if (n == 0) return 1;
    mint t = modpow(x, n / 2);
    t = t * t;
    if (n & 1) t = t * x;
    return t;
}

int modpow(__int128_t x, int n, int mod) {
    assert(n >= 0 && mod > 0); // TODO: n <= -1
    __int128_t ret = 1;
    while (n > 0) {
        if (n % 2 == 1) ret = ret * x % mod;
        x = x * x % mod;
        n /= 2;
    }
    return ret;
}
int modinv(__int128_t x, int mod) {
    assert(mod > 0 && x > 0);
    if (x == 1) return 1;
    return mod - modinv(mod % x, mod) * (mod / x) % mod;
}

istream &operator >>(istream &is, __int128_t& x) {
    string S; is >> S;
    __int128_t ret = 0;
    int f = 1;
    if (S[0] == '-') f = -1; 
    for (int i = 0; i < S.length(); i++)
        if ('0' <= S[i] && S[i] <= '9')
            ret = ret * 10 + S[i] - '0';
    x = ret * f;
    return (is);
}
ostream &operator <<(ostream &os, __int128_t x) {
    ostream::sentry s(os);
    if (s) {
        __uint128_t tmp = x < 0 ? -x : x;
        char buffer[128]; char *d = end(buffer);
        do {
            --d; *d = "0123456789"[tmp % 10]; tmp /= 10;
        } while (tmp != 0);
        if (x < 0) { --d; *d = '-'; }
        int len = end(buffer) - d;
        if (os.rdbuf()->sputn(d, len) != len) os.setstate(ios_base::badbit);
    }
    return os;
}

__int128_t stoll(string &S) {
    __int128_t ret = 0; int f = 1;
    if (S[0] == '-') f = -1; 
    for (int i = 0; i < S.length(); i++)
        if ('0' <= S[i] && S[i] <= '9') ret = ret * 10 + S[i] - '0';
    return ret * f;
}
__int128_t gcd(__int128_t a, __int128_t b) { return b ? gcd(b, a % b) : a; }
__int128_t lcm(__int128_t a, __int128_t b) {
    return a / gcd(a, b) * b;
    // lcmが__int128_tに収まる必要あり
}

string to_string(ld x, int k) { // xの小数第k位までをstring化する
    assert(k >= 0);
    stringstream ss;
    ss << setprecision(k + 2) << x;
    string s = ss.str();
    if (s.find('.') == string::npos) s += '.';
    int pos = s.find('.');
    for (int i = 0; k >= (int)s.size() - 1 - pos; i++) s += '0';
    s.pop_back();
    if (s.back() == '.') s.pop_back();
    return s;

    // stringstream ss; // 第k+1位を四捨五入して第k位まで返す
    // ss << setprecision(k + 1) << x;
    // string s = ss.str();
    // if (s.find('.') == string::npos) s += '.';
    // int pos = s.find('.');
    // for (int i = 0; k > (int)s.size() - 1 - pos; i++) s += '0';
    // if (s.back() == '.') s.pop_back();
    // return s;
}
string to_string(__int128_t x) {
    string ret = "";
    if (x < 0) { ret += "-"; x *= -1; }
    while (x) { ret += (char)('0' + x % 10); x /= 10; }
    reverse(ret.begin(), ret.end());
    return ret;
}
string to_string(char c) { string s = ""; s += c; return s; }

struct SXor128 {
    uint64_t x = 88172645463325252LL;
    unsigned Int() {
        x = x ^ (x << 7); return x = x ^ (x >> 9);
    }
    unsigned Int(unsigned mod) {
        x = x ^ (x << 7); x = x ^ (x >> 9); return x % mod;
    }
    unsigned Int(unsigned l, unsigned r) {
        x = x ^ (x << 7); x = x ^ (x >> 9); return x % (r - l + 1) + l;
    }
    double Double() {
        return double(Int()) / UINT_MAX;
    }
} rnd;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

template<class T> size_t HashCombine(const size_t seed,const T &v) {
    return seed^(hash<T>()(v)+0x9e3779b9+(seed<<6)+(seed>>2));
}
template<class T,class S> struct hash<pair<T,S>>{
    size_t operator()(const pair<T,S> &keyval) const noexcept {
        return HashCombine(hash<T>()(keyval.first), keyval.second);
    }
};
template<class T> struct hash<vector<T>>{
    size_t operator()(const vector<T> &keyval) const noexcept {
        size_t s=0;
        for (auto&& v: keyval) s=HashCombine(s,v);
        return s;
    }
};
template<int N> struct HashTupleCore{
    template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{
        size_t s=HashTupleCore<N-1>()(keyval);
        return HashCombine(s,get<N-1>(keyval));
    }
};
template <> struct HashTupleCore<0>{
    template<class Tuple> size_t operator()(const Tuple &keyval) const noexcept{ return 0; }
};
template<class... Args> struct hash<tuple<Args...>>{
    size_t operator()(const tuple<Args...> &keyval) const noexcept {
        return HashTupleCore<tuple_size<tuple<Args...>>::value>()(keyval);
    }
};

vector<mint> _fac, _finv, _inv;
void COMinit(int N) {
    _fac.resize(N + 1); _finv.resize(N + 1);  _inv.resize(N + 1);
    _fac[0] = _fac[1] = 1; _finv[0] = _finv[1] = 1; _inv[1] = 1;
    for (int i = 2; i <= N; i++) {
        _fac[i] = _fac[i-1] * mint(i);
        _inv[i] = -_inv[MOD % i] * mint(MOD / i);
        _finv[i] = _finv[i - 1] * _inv[i];
    }
}

mint FAC(int N) {
    if (N < 0) return 0; return _fac[N];
}
mint COM(int N, int K) {
    if (N < K) return 0; if (N < 0 or K < 0) return 0;
    return _fac[N] * _finv[K] * _finv[N - K];
}
mint PERM(int N, int K) {
    if (N < K) return 0; if (N < 0 or K < 0) return 0;
    return _fac[N] *  _finv[N - K];
}
mint NHK(int N, int K) {
    if (N == 0 && K == 0)  return 1;
    return COM(N + K - 1, K);
}

#pragma endregion

struct Point {
	double x, y; // (x, y)

	Point() {}
	Point(double x, double y) :x(x), y(y) {}

	Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }
	Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }
	Point operator*(const double& k) const { return Point(x * k, y * k); }
	Point operator/(const double& k) const { return Point(x / k, y / k); }

	friend istream& operator>>(istream& is, Point& p) {
		is >> p.x >> p.y;
		return is;
	}

	bool operator==(const Point& p) const { return (fabs(x - p.x) < EPS && fabs(y - p.y) < EPS); }
	bool operator<(const Point& p) const { return (x != p.x ? x < p.x : y < p.y); }
	// (x, y) の辞書順比較

	double norm() { return x * x + y * y; }
	double abs() { return sqrt(norm()); }
};

pair<ld, vector<Point>> cp_rec(vector<Point> &P, int lt, int sz) {
    if (sz <= 1) return make_pair(INFL, vector<Point>{P[lt]});

    int m = sz / 2;
    ld x = P[lt+m].x;
    auto [d1, q1] = cp_rec(P, lt, m);
    auto [d2, q2] = cp_rec(P, lt+m, sz-m);
    ld d = min(d1, d2);

    int s = 0, t = 0;
    vector<Point> Q;
    while (s < m && t < sz-m) {
        if (q1[s].y < q2[t].y) Q.pb(q1[s++]);
        else Q.pb(q2[t++]);
    }
    while (s < m) Q.pb(q1[s++]);
    while (t < sz-m) Q.pb(q2[t++]);

    vector<Point> b;
    for (int i = 0; i < sz; i++) {
        auto [ax, ay] = Q[i];
        if (abs(x - ax) >= d) continue;
        for (int j = (int)b.size() - 1; j >= 0; j--) {
            ld dx = ax - b[j].x;
            ld dy = ay - b[j].y;
            if (dy >= d) break;
            d = min(d, sqrt(dx*dx + dy*dy));
        }
        b.pb(Q[i]);
    }
    return make_pair(d, Q);
}

signed main() {
    int N;
    cin >> N;
    vector<Point> P(N);
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        P[i] = Point(a, b);
    }
    sort(P.begin(), P.end());

    auto ans = cp_rec(P, 0, int(P.size()));
    vector<Point> p = ans.second;
    ld d = ans.first;
    cout << (int)(d*d+EPS) << endl;
}
accuracy: 1
time_ms: 220
memory_kb: 44636
final_rank: 14

code_snippet: // ClosestPair
// https://www.acmicpc.net/problem/2261
// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0585
#include<bits/stdc++.h>

using namespace std;

using vl=vector<long long>;
using pl=pair<long long,long long>;

long long vlhashing(vl &a,long long rg){
  long long step=9223372036854775783ll%rg;
  long long res=0,ce=1;
  for(auto &nx : a){
    if(nx>=0){
      res+=((nx%rg)*ce);res%=rg;
    }
    else{
      res+=((rg-((-nx)%rg))*ce);res%=rg;
    }
    ce*=step;ce%=rg;
  }
  return res;
}

vl vlblk(vl &a,double x){
  vl res=a;
  for(auto &nx : res){
    nx=floor(((double)nx)/x);
  }
  return res;
}

vl vladd(vl &a,vl &b){
  vl res=a;
  for(long long i=0;i<a.size();i++){
    res[i]+=b[i];
  }
  return res;
}

// https://ir5.hatenablog.com/entry/20131221/1387557630
// if you give d(p1, p2) = (return d^k), dpow should be k
// gridsz ... prime is good
pl ClosestPair(vector<vl> &p,function<long long(vl&,vl&)> d,double dpow,long long gridsz){
  dpow = 1.0/dpow;
  long long n=p.size();
  long long dim=p[0].size();

  // cout << n << " " << dim << "\n";

  vector<vl> adj={{}};
  for(long long i=0;i<dim;i++){
    long long sz=0;
    vector<vl> nadj;
    for(auto &nx : adj){
      for(long long j=-1;j<=1;j++){
        nadj.push_back(nx);
        nadj[sz].push_back(j);
        sz++;
      }
    }
    adj=nadj;
  }

  random_device seed_gen;
  mt19937_64 engine(seed_gen());
  vector<int> perm(n);
  for(int i=0;i<n;i++){perm[i]=i;}
  shuffle(perm.begin(),perm.end(),engine);

  long long ri=perm[0],rj=perm[1];
  long long mind=d(p[ri],p[rj]);
  double mindb=pow((double)mind,dpow);
  vector<vector<long long>> grid(gridsz);
  vector<long long> his;

  if(mind==0){return {ri,rj};}

  {
    vl bl0=vlblk(p[ri],mindb);
    long long h0=vlhashing(bl0,gridsz);
    grid[h0].push_back(ri);
    his.push_back(h0);
  }

  {
    vl bl0=vlblk(p[rj],mindb);
    long long h0=vlhashing(bl0,gridsz);
    grid[h0].push_back(rj);
    his.push_back(h0);
  }

  for(long long pt=2;pt<n;pt++){
    long long i=perm[pt];
    vl bl=vlblk(p[i],mindb);
    bool recon=false;
    for(auto &nx : adj){
      vl abl=vladd(bl,nx);
      long long ah=vlhashing(abl,gridsz);
      for(auto &ny : grid[ah]){
        long long cd=d(p[i],p[ny]);
        if(mind > cd){
          mind=cd;
          ri=i;rj=ny;
          recon=true;
        }
      }
    }
    if(recon){
      if(mind==0){return {ri,rj};}

      for(auto &nx : his){
        grid[nx].clear();
      }
      his.clear();

      mindb=pow((double)mind,dpow);

      for(long long pj=0;pj<=pt;pj++){
        long long j=perm[pj];
        vl cbl=vlblk(p[j],mindb);
        long long ch=vlhashing(cbl,gridsz);
        grid[ch].push_back(j);
        his.push_back(ch);
      }
    }
    else{
      long long h=vlhashing(bl,gridsz);
      grid[h].push_back(i);
      his.push_back(h);
    }
  }
  return {ri,rj};
}

long long d(vl &a,vl &b){
  long long res=0;
  for(long long i=0;i<a.size();i++){
    res+=(a[i]-b[i])*(a[i]-b[i]);
  }
  return res;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  long long n;
  cin >> n;
  long long dim=2;
  vector<vl> p(n);
  for(auto &nx : p){
    nx.resize(dim);
    for(auto &ny : nx){cin >> ny;}
  }
  pl res=ClosestPair(p,d,2.0,1000003);
  cout << d(p[res.first],p[res.second]) << "\n";
  return 0;
}
accuracy: 1
time_ms: 940
memory_kb: 73668
final_rank: 17

code_snippet: #include<bits/stdc++.h>

using namespace std;

using vl=vector<long long>;
using pl=pair<long long,long long>;

long long vlhashing(vl &a,long long rg){
  long long step=9223372036854775783ll%rg;
  long long res=0,ce=1;
  for(auto &nx : a){
    if(nx>=0){
      res+=((nx%rg)*ce);res%=rg;
    }
    else{
      res+=((rg-((-nx)%rg))*ce);res%=rg;
    }
    ce*=step;ce%=rg;
  }
  return res;
}

vl vlblk(vl &a,double x){
  vl res=a;
  for(auto &nx : res){
    nx=floor(((double)nx)/x);
  }
  return res;
}

vl vladd(vl &a,vl &b){
  vl res=a;
  for(long long i=0;i<a.size();i++){
    res[i]+=b[i];
  }
  return res;
}

// https://ir5.hatenablog.com/entry/20131221/1387557630
// if you give d(p1, p2) = (return d^k), dpow should be k
// gridsz ... prime is good
pl ClosestPair(vector<vl> &p,function<long long(vl&,vl&)> d,double dpow,long long gridsz){
  dpow = 1.0/dpow;
  long long n=p.size();
  long long dim=p[0].size();

  // cout << n << " " << dim << "\n";

  vector<vl> adj={{}};
  for(long long i=0;i<dim;i++){
    long long sz=0;
    vector<vl> nadj;
    for(auto &nx : adj){
      for(long long j=-1;j<=1;j++){
        nadj.push_back(nx);
        nadj[sz].push_back(j);
        sz++;
      }
    }
    adj=nadj;
  }

  random_device seed_gen;
  mt19937_64 engine(seed_gen());
  vector<int> perm(n);
  for(int i=0;i<n;i++){perm[i]=i;}
  shuffle(perm.begin(),perm.end(),engine);

  long long ri=perm[0],rj=perm[1];
  long long mind=d(p[ri],p[rj]);
  double mindb=pow((double)mind,dpow);
  vector<vector<long long>> grid(gridsz);
  vector<long long> his;

  if(mind==0){return {ri,rj};}

  {
    vl bl0=vlblk(p[ri],mindb);
    long long h0=vlhashing(bl0,gridsz);
    grid[h0].push_back(ri);
    his.push_back(h0);
  }

  {
    vl bl0=vlblk(p[rj],mindb);
    long long h0=vlhashing(bl0,gridsz);
    grid[h0].push_back(rj);
    his.push_back(h0);
  }

  for(long long pt=2;pt<n;pt++){
    long long i=perm[pt];
    vl bl=vlblk(p[i],mindb);
    bool recon=false;
    for(auto &nx : adj){
      vl abl=vladd(bl,nx);
      long long ah=vlhashing(abl,gridsz);
      for(auto &ny : grid[ah]){
        long long cd=d(p[i],p[ny]);
        if(mind > cd){
          mind=cd;
          ri=i;rj=ny;
          recon=true;
        }
      }
    }
    if(recon){
      if(mind==0){return {ri,rj};}

      for(auto &nx : his){
        grid[nx].clear();
      }
      his.clear();

      mindb=pow((double)mind,dpow);

      for(long long pj=0;pj<pt;pj++){
        long long j=perm[pj];
        vl cbl=vlblk(p[j],mindb);
        long long ch=vlhashing(cbl,gridsz);
        grid[ch].push_back(j);
        his.push_back(ch);
      }
    }
    else{
      long long h=vlhashing(bl,gridsz);
      grid[h].push_back(i);
      his.push_back(h);
    }
  }
  return {ri,rj};
}

long long d(vl &a,vl &b){
  long long res=0;
  for(long long i=0;i<a.size();i++){
    res+=(a[i]-b[i])*(a[i]-b[i]);
  }
  return res;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  long long n;
  cin >> n;
  long long dim=2;
  vector<vl> p(n);
  for(auto &nx : p){
    nx.resize(dim);
    for(auto &ny : nx){cin >> ny;}
  }
  pl res=ClosestPair(p,d,2.0,1000003);
  cout << d(p[res.first],p[res.second]) << "\n";
  return 0;
}
accuracy: 1
time_ms: 880
memory_kb: 72832
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef struct pos {
    int X;
    int Y;
    double D;
} pos;

bool compare(pos P1, pos P2) 
{ 
    return (P1.D < P2.D); 
} 
  
int main(void){
    // Your code here!
    int N;
    cin >> N;
    vector <pos> P(N);
    int X,Y,D;
    for(int i=0;i<N;i++){
        cin >> X >> Y;
        D=sqrt(X*X+Y*Y);
        P[i].X=X;
        P[i].Y=Y;
        P[i].D=D;
    }
    
    sort(P.begin(), P.end(),compare);
    
    pos PA,PB;
    int XA,YA,DA,XB,YB,DB;
    int minD2=20000*20000*2+1;
    int D2;
    double minD=sqrt(minD2);
    
    
    for(int i=0;i<N-1;i++){
        PA=P[i];
        XA=PA.X;
        YA=PA.Y;
        DA=PA.D;
        for(int j=i+1;j<N;j++){
            PB=P[j];
            XB=PB.X;
            YB=PB.Y;
            DB=PB.D;
            if(DB-DA>minD){
                break;
            }
            /*
            if(D2A-D2B>minD2){
                break;
            }
            */
            D2=(XB-XA)*(XB-XA)+(YB-YA)*(YB-YA);
            if(D2<minD2){
                minD2=D2;
                minD=sqrt(minD2);
            }
        }
    }
    cout << minD2 <<endl;
}
accuracy: 1
time_ms: 1000
memory_kb: 10620
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef struct pos {
    int X;
    int Y;
    int d2;
} pos;

bool compare(pos P1, pos P2) 
{ 
    return (P1.d2 < P2.d2); 
} 
  
int main(void){
    // Your code here!
    int N;
    cin >> N;
    vector <pos> P(N);
    int X,Y,D2;
    for(int i=0;i<N;i++){
        cin >> X >> Y;
        D2=X*X+Y*Y;
        P[i].X=X;
        P[i].Y=Y;
        P[i].d2=D2;
    }
    
    sort(P.begin(), P.end(),compare);
    
    pos PA,PB;
    int XA,YA,D2A,XB,YB,D2B;
    int minD2=20000*20000*2+1;
    
    for(int i=0;i<N-1;i++){
        PA=P[i];
        XA=PA.X;
        YA=PA.Y;
        D2A=PA.d2;
        for(int j=i+1;j<N;j++){
            PB=P[j];
            XB=PB.X;
            YB=PB.Y;
            D2B=PB.d2;
            if(sqrt(D2B)-sqrt(D2A)>sqrt(minD2)){
                break;
            }
            /*
            if(D2A-D2B>minD2){
                break;
            }
            */
            D2=(XB-XA)*(XB-XA)+(YB-YA)*(YB-YA);
            if(D2<minD2){
                minD2=D2;
            }
        }
    }
    cout << minD2 <<endl;
}
accuracy: 1
time_ms: 6130
memory_kb: 8580
final_rank: 18

code_snippet: //made by Nguyễn Hồng Nam
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const ll mod = 1e9 + 7,inf = 1e18,mxn = 5e5 + 5;
int n,mi = inf;
inline void minz(int &a,int b)
{
    if(a > b) a = b;
}
struct point
{
    int x,y;
};
bool cmp_x(const point &a,const point &b)
{
    return a.x < b.x;
}
bool cmp_y(const point &a,const point &b)
{
    return a.y < b.y;
}
void upd(const point &a,const point &b)
{
    int dist = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
    minz(mi,dist);
}
point a[mxn];
void solve(int l,int r)
{
    if(r <= l) return ;
    if(r == l + 1)
    {
        upd(a[l],a[r]);
        if(!cmp_y(a[l],a[r])) swap(a[l],a[r]);
        return;
    }
    int m = (l + r) / 2;
    int midx = a[m].x;
    solve(l,m);
    solve(m + 1,r);
    static point t[mxn];
    merge(a + l,a + m + 1,a + m + 1,a + r + 1,t,cmp_y);
    copy(t,t + r - l + 1,a + l);
    int tm = 0;
    for(int i = l; i <= r; i++)
    {
        if(abs(a[i].x - midx) < mi)
        {
            for(int j = tm - 1; j >= 0 && t[j].y > a[i].y - mi; j--)
            {
                upd(a[i],t[j]);
            }
            t[tm++] = a[i];
        }
    }
}
int32_t main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i].x >> a[i].y;
    sort(a,a + n,cmp_x);
    solve(0,n - 1);
    cout << mi << '\n';
    return 0;
}
accuracy: 1
time_ms: 1060
memory_kb: 18952
final_rank: 13