結果

問題 No.2436 Min Diff Distance
ユーザー 👑 potato167potato167
提出日時 2023-08-15 23:17:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 476 ms / 2,000 ms
コード長 3,463 bytes
コンパイル時間 2,369 ms
コンパイル使用メモリ 210,688 KB
実行使用メモリ 10,920 KB
最終ジャッジ日時 2024-05-03 09:43:12
合計ジャッジ時間 10,309 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 462 ms
10,792 KB
testcase_04 AC 443 ms
10,788 KB
testcase_05 AC 476 ms
10,920 KB
testcase_06 AC 448 ms
10,792 KB
testcase_07 AC 457 ms
10,796 KB
testcase_08 AC 462 ms
10,792 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 257 ms
10,916 KB
testcase_12 AC 207 ms
10,796 KB
testcase_13 AC 288 ms
9,048 KB
testcase_14 AC 40 ms
5,376 KB
testcase_15 AC 450 ms
10,624 KB
testcase_16 AC 187 ms
6,912 KB
testcase_17 AC 372 ms
10,112 KB
testcase_18 AC 337 ms
9,480 KB
testcase_19 AC 384 ms
10,112 KB
testcase_20 AC 249 ms
7,808 KB
testcase_21 AC 101 ms
5,376 KB
testcase_22 AC 41 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(p) p.begin(),p.end()
#define rep(i,a,b) for(int i=(int)a;i<(int)b;i++)
const int mod=998244353;

namespace po167{
template <class T,T (*op)(T,T),T(*e)()>
struct segment_tree{
	int _n,size;
	std::vector<T> seg;
	int ceil_pow2(int a){
		int b=1;
		while(a>b){
			b<<=1;
		}
		return b;
	}
	void update(int k){seg[k]=op(seg[k*2],seg[k*2+1]);};
	segment_tree(int n) :_n(n){
		size=ceil_pow2(n);
		seg=std::vector<T>(size*2,e());
	}
	segment_tree(std::vector<T> &p) :_n((int) p.size()){
		size=ceil_pow2(_n);
		seg=std::vector<T>(size*2,e());
		for(int i=0;i<_n;i++) seg[i+size]=p[i];
		for(int i=size-1;i>0;i--) update(i);
	}
	void set(int ind,T val){
		assert(0<=ind&&ind<_n);
		ind+=size;
		seg[ind]=val;
		while(ind!=1){
			ind>>=1;
			update(ind);
		}
	}
    void addl(int ind,T val){
        set(ind,op(get(ind),val));
    }
    void addr(int ind,T val){
        set(ind,op(val,get(ind)));
    }
	T get(int ind){
		assert(0<=ind&&ind<_n);
		return seg[ind+size];
	}
	T query(int l,int r){
		assert(0<=l&&l<=r&&r<=_n);
		T l_val=e();
		T r_val=e();
		l+=size,r+=size;
		while(l<r){
			if(l&1) l_val=op(l_val,seg[l]),l+=1;
			if(r&1) r-=1,r_val=op(seg[r],r_val);
			r>>=1;
			l>>=1;
		}
		return op(l_val,r_val);
	}
	template <bool (*f)(T)> int max_right(int l) {
        return max_right(l, [](T x) { return f(x); });
    }
	template <class F> int max_right(int l, F f) {
		assert(0<=l&&l<=_n);
		assert(f(e()));
		if(f(query(l,_n))) return _n;
		T val=e();
		l+=size;
		while(true){
			while(l%2==0) l>>=1;
			if(!f(op(val,seg[l]))){
				while(l<size){
					l*=2;
					if(f(op(val,seg[l]))){
						val=op(val,seg[l]);
						l++;
					}
				}
				return l-size;
			}
			val=op(val,seg[l]);
			l++;
		}
	}
	template <bool (*f)(T)> int min_left(int r) {
        return min_left(r, [](T x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        T sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(seg[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(seg[r], sm))) {
                        sm = op(seg[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(seg[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

};
}
using po167::segment_tree;


using F= int;
F op(F a,F b){return max(a,b);}
F e(){return -100000000;}

int main(){
	int N;
	cin>>N;
	vector<int> X(N),Y(N);
	rep(i,0,N) cin>>X[i]>>Y[i];
	vector<int> A(N),B(N),P(N),Q(N,N*2+10);
	rep(i,0,N) A[i]=X[i]+Y[i],B[i]=X[i]-Y[i];
	sort(all(A)),sort(all(B));
	rep(i,0,N){
		int a=X[i]+Y[i];
		int b=X[i]-Y[i];
		P[i]=max(max(A[N-1]-a,a-A[0]),max(B[N-1]-b,b-B[0]));
	}
	rep(k,0,4){
		rep(i,0,N) X[i]=N+1-X[i];
		if(k&1) rep(i,0,N) Y[i]=N+1-Y[i];
		vector<int> order(N);
		rep(i,0,N) order[i]=i;
		sort(all(order),[&](int l,int r){
			if(X[l]==X[r]) return Y[l]<Y[r];
			return X[l]<X[r];
		});
		segment_tree<F,op,e> seg(N+3);
		rep(i,0,N){
			int a=order[i];
			int val=X[a]+Y[a];
			Q[a]=min(Q[a],val-seg.query(0,Y[a]+1));
			seg.set(Y[a],val);
		}
	}
	int ans=N*2;
	//rep(i,0,N) cout<<P[i]<<" "<<Q[i]<<"\n";
	rep(i,0,N) ans=min(ans,P[i]-Q[i]);
	cout<<ans<<"\n";
}
0