| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 431882 | Blagojce | Rail (IOI14_rail) | C++11 | 0 ms | 0 KiB | 
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
  
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const ll inf = 1e18;
const int i_inf = 1e9;
const ll mod = 1e9+7;
 
mt19937 _rand(time(NULL));
const int mxn = 5005;
#include "grader.h"
int d0[mxn];
int dp[mxn];
/*
int getDistance(int i, int j){
	cout<<"? "<<i<<' '<<j<<endl;
	int ret;
	cin >> ret;
	
	return ret;
}
*/
vector<int> L;
vector<int> R;
void findLocation(int N, int first, int location[], int stype[]){
	location[0] = first;
	stype[0] = 1;
	if(N == 1) return;
	
	fr(i, 1, N){
		d0[i] = getDistance(0, i);
	}
	int p = 1;
	fr(i, 1, N) if(d0[i] < d0[p]) p = i;
	
	location[p] = location[0] + d0[p];
	stype[p] = 2;
	if(N == 2) return;
	
	
	dp[0] = d0[p];
	fr(i, 1, N){
		if(i == p) continue;
		dp[i] = getDistance(p, i);
	}
	
	fr(i, 1, N){
		if(i == p) continue;
		if(d0[i] == d0[p] + dp[i]){
			L.pb(i);
		}
		else{
			R.pb(i);
		}
	}
	sort(all(R), [](const int &i, const int &j){
		return d0[i] < d0[j];
	});
	
	vector<int> pivots;
	pivots.pb(p);
	
	for(auto u : R){
		bool ok = false;
		
		int piv = pivots.back();
		int w = getDistance(piv, u);
		int candpos = location[piv] - w;
		if(candpos > location[0]){			
			for(auto pi : pivots){
				if(location[pi] > candpos){
					if(getDistance(pi, u) == location[pi] - candpos){
						ok = true;
					}
					break;
				}
			}
			if(ok){
				stype[u] = 1;
				location[u] = candpos;
				continue;
			}
		}
		
		stype[u] = 2;
		location[u] = location[0] + d0[u];
		pivots.pb(u);
	}
	
	pivots.clear();
	
	for(auto u : L){
		bool ok = false;
		if(!pivots.empty()){
			int piv = pivots.back();
			int w = getDistance(piv, u);
			
			int candpos = location[piv] + w;
			if(candpos < location[p]){
				for(auto pi : pivots){
					if(location[pi] < candpos){
						if(getDistance(pi, u) == candpos - location[pi]){
							ok = true;
						}
						break;
					}
				}
				if(ok){
					stype[u] = 2;
					location[u] = candpos;
				}
			}
		}
		stype[u] = 1;
		location[u] = location[p] - dp[u];
		pivots.pb(u);
	}
	
	
	
	
	
	
	
	
	
	
	
	/*
	if(N == 1){
		return;
	}
	
	fr(i, 1, N){
		d0[i] =  getDistance(0, i);
	}
	int minD = 1e9;
	int p = 0;
	fr(i, 1, N){
		if(d0[i] < minD){
			minD = d0[i];
			p = i;
		}
	}
	
	d1[0] = d0[p];
	
	fr(i, 1, N){
		if(i == p) continue;
		d1[i] = getDistance(p, i);
	}
	
	
	
	
	fr(i, 0, N){
		if(i == p || i == 0) continue;
		
		if(d0[i] == d1[i] + d0[p]){
			L.pb(i);
		}
		else{
			R.pb(i);
		}
	}
	
	
	
	
	
	sort(all(R), [](const int &i, const int &j){
		return d0[i] < d0[j];
	});
	location[p] = location[0] + d0[p];
	
	stype[p] = 2;
	
	if(N == 2){
		return;
	}
	
	
	queue<int> P;
	P.push(p);
	
	
	for(auto u : R){
		while(!P.empty()){
			int piv = P.front();
			int w = getDistance(piv, u);
			if(d0[u] == d0[piv] + w){
				location[u] = location[piv] - w;
				stype[u] = 1;
				break;
			}
			else{
				P.pop();
			}
		}
		if(P.empty()){
			P.push(u);
			location[u] = location[0] + d0[u];
			stype[u] = 2;
		}
	}
	
	
	int p2;
	minD = 1e9;
	fr(i, 0, N){
		if(i == p) continue;
		if(d1[i] < minD){
			minD = d1[i];
			p2 = i;
		}
		
	}
	L.pb(0);
	
	sort(all(L), [](const int &i, const int &j){
		return d1[i] < d1[j];
	});
	
	
	location[p2] = location[p] - d1[p2];
	stype[p2] = 1;
	
	
	queue<int> P2;
	P2.push(p2);
	
	for(auto u : L){
		if(u == p2) continue;
		
		while(!P2.empty()){
			int piv = P2.front();
			int w = getDistance(piv, u);
			if(d1[u] == d1[piv] + w){
				location[u] = location[piv] + w;
				stype[u] = 2;
				break;
			}
			else{
				P2.pop();
			}
		}
		if(P2.empty()){
			location[u] = location[p] - d1[u];
			stype[u] = 1;
			P2.push(u);
		}
		
	}
*/
}
/*
int main(){
	int loc[mxn];
	int sty[mxn];
	findLocation(4, 3, loc, sty);
	fr(i, 0, 5){
		cout<<loc[i]<<' ';
	}
	cout<<endl;
	
	fr(i, 0, 5){
		cout<<sty[i]<<' ';
	}
	cout<<endl;
	
}
*/
/*
0 1 2 3 4 5 6 7
(   ) (   ) 
3   0 1   2
*/
/*3
5
8
9
2
5
6
5
8
1
*/
