結果

問題 No.2436 Min Diff Distance
ユーザー 👑 kmjpkmjp
提出日時 2023-08-22 00:50:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 382 ms / 2,000 ms
コード長 2,561 bytes
コンパイル時間 2,444 ms
コンパイル使用メモリ 215,208 KB
実行使用メモリ 29,004 KB
最終ジャッジ日時 2023-08-22 00:50:41
合計ジャッジ時間 8,884 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
19,388 KB
testcase_01 AC 9 ms
19,496 KB
testcase_02 AC 10 ms
19,564 KB
testcase_03 AC 375 ms
28,728 KB
testcase_04 AC 349 ms
28,780 KB
testcase_05 AC 348 ms
28,692 KB
testcase_06 AC 382 ms
28,804 KB
testcase_07 AC 352 ms
28,772 KB
testcase_08 AC 351 ms
28,752 KB
testcase_09 AC 10 ms
19,544 KB
testcase_10 AC 10 ms
19,508 KB
testcase_11 AC 199 ms
29,004 KB
testcase_12 AC 213 ms
28,632 KB
testcase_13 AC 224 ms
27,588 KB
testcase_14 AC 41 ms
20,572 KB
testcase_15 AC 338 ms
28,716 KB
testcase_16 AC 148 ms
23,868 KB
testcase_17 AC 318 ms
27,956 KB
testcase_18 AC 254 ms
28,284 KB
testcase_19 AC 304 ms
27,948 KB
testcase_20 AC 198 ms
24,696 KB
testcase_21 AC 89 ms
22,212 KB
testcase_22 AC 42 ms
20,804 KB
権限があれば一括ダウンロードができます

ソースコード

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;
int X[202020],Y[202020];
vector<int> XpY,XmY;

template<class V,int NV> class SegTree_1_ma {
public:
	vector<V> val;
	static V const def=-(1<<30);
	V comp(V l,V r){ return max(l,r);};
	
	SegTree_1_ma(){val=vector<V>(NV*2,def);};
	void reinit(){ FORR(v,val) v=-(1<<30);}
	V getval(int x,int y,int l=0,int r=NV,int k=1) { // x<=i<y
		if(r<=x || y<=l) return def;
		if(x<=l && r<=y) return val[k];
		return comp(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry]=comp(v,val[entry]); //上書きかチェック
		while(entry>1) entry>>=1, val[entry]=comp(val[entry*2],val[entry*2+1]);
	}
};
SegTree_1_ma<int,1<<20> stp,stm;
int ma[202020],mi[202020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	vector<pair<ll,int>> P;
	FOR(i,N) {
		cin>>X[i]>>Y[i];
		XpY.push_back(X[i]+Y[i]);
		XmY.push_back(X[i]-Y[i]);
		P.push_back({X[i],Y[i]});
	}
	sort(ALL(P));
	sort(ALL(XpY));
	sort(ALL(XmY));
	int ret=1<<30;
	FOR(i,N) {
		X[i]=P[i].first;
		Y[i]=P[i].second;
		ma[i]=max(ma[i],abs(X[i]+Y[i]-*XpY.begin()));
		ma[i]=max(ma[i],abs(X[i]+Y[i]-*XpY.rbegin()));
		ma[i]=max(ma[i],abs(X[i]-Y[i]-*XmY.begin()));
		ma[i]=max(ma[i],abs(X[i]-Y[i]-*XmY.rbegin()));
		
		mi[i]=1<<30;
		mi[i]=min(mi[i],abs(X[i]+Y[i]-stp.getval(0,Y[i]+1)));
		mi[i]=min(mi[i],abs(X[i]-Y[i]-stm.getval(Y[i],N+1)));
		stp.update(Y[i],Y[i]+X[i]);
		stm.update(Y[i],X[i]-Y[i]);
	}
	stp.reinit();
	stm.reinit();
	for(i=N-1;i>=0;i--) {
		mi[i]=min(mi[i],abs(X[i]+Y[i]+stp.getval(Y[i],N+1)));
		mi[i]=min(mi[i],abs(-X[i]+Y[i]-stm.getval(0,Y[i]+1)));
		stp.update(Y[i],-Y[i]-X[i]);
		stm.update(Y[i],-X[i]+Y[i]);
		//cout<<X[i]<<" "<<Y[i]<<" "<<ma[i]<<" "<<mi[i]<<endl;
		ret=min(ret,ma[i]-mi[i]);
	}
	cout<<ret<<endl;
}


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