結果

問題 No.2909 Imaginary Summer
ユーザー 👑 kmjpkmjp
提出日時 2024-10-03 00:24:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,963 bytes
コンパイル時間 2,493 ms
コンパイル使用メモリ 214,384 KB
実行使用メモリ 16,848 KB
最終ジャッジ日時 2024-10-03 00:24:29
合計ジャッジ時間 12,015 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,M,K;

ll X[101010],Y[101010];
ll A[101010],B[101010];

struct node {
	pair<ll,ll> P;
	int left,right;
	int axis;
	ll L,R,U,D;
};

struct KDTree_2D {
	vector<node> nodes;
	static ll dist(pair<ll,ll> a, pair<ll,ll> b) {
		a.first -= b.first;
		a.second -= b.second;
		return a.first*a.first+a.second*a.second;
	}
	static ll possible(ll x,ll y,ll L,ll R,ll U,ll D) {
		ll dx=(x<L)?L-x:((x<R)?0:x-R);
		ll dy=(y<U)?U-y:((y<D)?0:y-D);
		return dx*dx+dy*dy;
	}
	int build(vector<pair<ll,ll>> vs) {
		if(vs.empty()) return -1;
		nodes.push_back({});
		int cur=nodes.size()-1;
		node n;
		n.L=n.U=1LL<<60;
		n.R=n.D=-1LL<<60;
		FORR2(a,b,vs) {
			n.L=min(n.L,a);
			n.R=max(n.R,a);
			n.U=min(n.U,b);
			n.D=max(n.D,b);
		}
		int axis = n.axis = (n.R-n.L)<=(n.D-n.U);
		if(axis) FORR2(a,b,vs) swap(a,b);
		sort(ALL(vs));
		if(axis) FORR2(a,b,vs) swap(a,b);
		int mid=vs.size()/2;
		n.P=vs[mid];
		int i;
		vector<pair<ll,ll>> VL,VR;
		FOR(i,mid) VL.push_back(vs[i]);
		for(i=mid+1;i<vs.size();i++) VR.push_back(vs[i]);
		
		n.left = build(VL);
		n.right = build(VR);
		nodes[cur]=n;
		return cur;
	}
	
	pair<ll,ll> search(pair<ll,ll> p,int cur=0) {
		if(cur==-1||cur>=nodes.size()) {
			return {-1LL<<30,-1LL<<30};
		}
		node& n=nodes[cur];
		pair<ll,ll> dp=n.P;
		ll d=dist(n.P,p);
		
		int fi,se;
		if((n.axis==0&&p.first<n.P.first)||(n.axis==1&&p.second<n.P.second)) {
			fi=n.left;
			se=n.right;
		}
		else {
			se=n.left;
			fi=n.right;
		}
		auto v1=search(p,fi);
		if(dist(v1,p)<d) {
			d=dist(v1,p);
			dp=v1;
		}
		if(possible(p.first,p.second,n.L,n.R,n.U,n.D)<d) {
			v1=search(p,se);
			if(dist(v1,p)<d) dp=v1;
		}
		return dp;
		
	}
};



void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M>>K;
	cin>>A[0]>>B[0];
	
	vector<pair<ll,ll>> V;
	FOR(i,N) {
		cin>>X[i]>>Y[i];
		V.push_back({X[i],Y[i]});
	}
	
	KDTree_2D kd;
	kd.build(V);
	
	double ret=0;
	pair<ll,ll> p=kd.search({A[0],B[0]});
	
	FOR(i,K) {
		cin>>A[i+1]>>B[i+1];
		pair<ll,ll> q=kd.search({A[i+1],B[i+1]});
		double a=2*hypot(A[0]-A[i+1],B[0]-B[i+1]);
		double b=2*(hypot(A[0]-p.first,B[0]-p.second)+hypot(A[i+1]-q.first,B[i+1]-q.second));
		ret+=min(a,b);
	}
	_P("%.12lf\n",ret);
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0