結果

問題 No.2897 2集合間距離
ユーザー shkiiii_
提出日時 2024-09-21 02:56:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 3,295 bytes
コンパイル時間 2,338 ms
コンパイル使用メモリ 207,300 KB
最終ジャッジ日時 2025-02-24 10:55:23
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22 TLE * 1 -- * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


// using ld = long double;
const int inf = 100000000;
// verified https://onlinejudge.u-aizu.ac.jp/status/users/shkii/submissions/1/CGL_5_A/judge/9351284/C++17
// vはfirstについてsortされている必要がある
// vはfirstについてsortされている必要がある
// vはfirstについてsortされている必要がある
inline int calc_closest_pair(vector<pair<pair<int,int>,int>> &v,int l,int r){
  if(r-l <= 1)return inf;
  int mid = (l+r)/2;
  int mid_x = v[mid].first.first;
  int d = min(calc_closest_pair(v,l,mid),calc_closest_pair(v,mid,r));
  sort(v.begin()+l,v.begin()+r,[](pair<pair<int,int>,int> a,pair<pair<int,int>,int> b){return a.first.second < b.first.second;});
  vector<pair<pair<int,int>,int>> w;//直線mid_xから距離d未満の列挙
  for(int i = l;i < r;i++){
    if(abs(v[i].first.first-mid_x) < d){
      for(int j = (int)(w.size())-1;j >= 0;j--){//実はこのループは高々5回程度しか回らない
        int dx = v[i].first.first-w[j].first.first;
        int dy = v[i].first.second-w[j].first.second;
        if(dy >= d)break;
        if(v[i].second != w[j].second)d = min(d,abs(dx) + abs(dy));
      }
      w.emplace_back(v[i]);
    }
  }
  return d;
}
// vはfirstについてsortされている必要がある
// vはfirstについてsortされている必要がある

int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  vector<pair<pair<int,int>,int>> v;
  int n;cin >> n;
  rep(i,n){
    int a,b;cin >> a >> b;
    v.emplace_back(pair<pair<int,int>,int>{{a,b},0});
  }
  int m;cin >> m;
  rep(i,m){
    int a,b;cin >> a >> b;
    v.emplace_back(pair<pair<int,int>,int>{{a,b},1});
  }
  sort(ALL(v));
  cout << calc_closest_pair(v,0,sz(v)) << "\n";
  

  return 0;
}
0