Submission #1051867

#TimeUsernameProblemLanguageResultExecution timeMemory
1051867Alihan_8Shortcut (IOI16_shortcut)C++17
97 / 100
2050 ms147844 KiB
#include "shortcut.h"

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()

using i64 = long long;

const i64 inf = 1e15;

struct square{
	i64 x1, x2, y1, y2;
	
	square(i64 x1 = 0, i64 x2 = 0, i64 y1 = 0, i64 y2 = 0) : x1(x1), x2(x2), y1(y1), y2(y2) {}
	
	square f(square q){
		return square(max(x1, q.x1), min(x2, q.x2), max(y1, q.y1), min(y2, q.y2));
	}
	
	bool check(i64 x, i64 y){
		return x1 <= x && x2 >= x && y1 <= y && y2 >= y;
	}
};

long long find_shortcut(int n, std::vector<int> L, std::vector<int> d, int C){
	vector <i64> x(n), A(n), B(n);
	
	for ( int i = 1; i < n; i++ ){
		x[i] = x[i - 1] + L[i - 1];
	}
	
	for ( int i = 0; i < n; i++ ){
		A[i] = x[i] + d[i];
		B[i] = x[i] - d[i];
	}
	
	vector <int> pa(n), pb(n);
	
	for ( int i = 0; i < n; i++ ){
		pa[i] = pb[i] = i;
	}
	
	sort(all(pa), [&](int &u, int &v){
		return A[u] < A[v];
	});
	
	sort(all(pb), [&](int &u, int &v){
		return B[u] < B[v];
	});
	
	auto ok = [&](i64 k){
		i64 kk = k - C;
		
		vector <ar<i64,3>> pt;
		
		auto add = [&](int i, int j){
			if ( i == -1 || j == -1 || i == j ) return;
			
			if ( x[i] > x[j] ) swap(i, j);
			
			pt.pb({x[i] + x[j], x[i] - x[j], kk - (d[i] + d[j])});
		};
		
		vector <ar<i64,2>> q(4, {inf, -1}), qq(4, {inf, -1});
		
		auto upd = [&](int j){
			vector <i64> t = {A[j], -A[j], B[j], -B[j]};
			
			for ( int i = 0; i < 4; i++ ){
				if ( q[i][0] > t[i] ){
					qq[i] = q[i];
					q[i] = {t[i], j};
				} else if ( qq[i][0] > t[i] ){
					qq[i] = {t[i], j};
				}
			} 
		};
		
		int j = 0;
		
		for ( auto &i: pa ){
			while ( j < n && A[i] - k > B[pb[j]] ){
				upd(pb[j]); j++;
			}
			
			for ( auto &[vv, j]: q ) add(i, j); 
			for ( auto &[v, j]: qq ) add(i, j);
		}
		
		if ( pt.empty() ) return true;
		
		auto [X, Y, d] = pt[0];
		
		square t = square(X - d, X + d, Y - d, Y + d);
		
		for ( int i = 1; i < (int)pt.size(); i++ ){
			auto [x, y, d] = pt[i];
			
			t = t.f(square(x - d, x + d, y - d, y + d));
		}  
		
		if ( t.x1 > t.x2 || t.y1 > t.y2 ){ // area 0
			return false;
		}
		
		int l = n, r = n - 1, tl = 0, tr = -1;
		
		for ( int i = 0; i < n; i++ ){
			while ( l > 0 && t.x1 - x[i] <= x[l - 1] ) l--;
			while ( r >= 0 && t.x2 - x[i] < x[r] ) r--;
			
			while ( tr + 1 < n && x[tr + 1] <= -t.y1 + x[i] ) tr++;
			while ( tl < n && x[tl] < -t.y2 + x[i] ) tl++;
			
			int a = max(tl, l), b = min(tr, r);
			
			if ( a <= b && (b != a || b != i) ){
				return true;
			}
		}
		
		return false;
	};
	
	i64 l = 0, r = 1e15;
	
	while ( l < r ){
		i64 m = (l + r) / 2;
		
		if ( ok(m) ){
			
			r = m;
		} else l = m + 1;
	}
	
	return l;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...