| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 1247365 | khoianh | Bitaro's travel (JOI23_travel) | C++20 | 1 ms | 328 KiB | 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mn = 1e5 + 5;
ll n, q, a[mn], x, l, r, p;
void solve(){
	cin >> n;
	for(int i = 1; i <= n; ++i) cin >> a[i];
	cin >> q;
	while(q--){
		ll ans = 0;
		cin >> x;
		p = lower_bound(a + 1, a + n + 1, x) - a;
		if(p > n) l = r = n, ans += x - a[n];
		else if (p == 0) l = r = 1, ans += a[1] - x;
		else if(a[p] - x < x - a[p - 1]) l = r = p, ans += a[p] - x;
		else l = r = p - 1, ans += x - a[p - 1];
		x = l;
		while(true){
			if(l == 1){
				ans += a[n] - a[x];
				break;
			}
			if(r == n){
				ans += a[x] - a[1];
				break;
			}
//			cout << l << " " << r << "\n";
			if(a[x] - a[l - 1] <= a[r + 1] - a[x]){
				p = lower_bound(a + 1, a + n + 1, 2 * a[x] - a[r + 1]) - a;
				ans += a[x] - a[p];
				x = p;
				l = p;
			} else {
				p = upper_bound(a + 1, a + n + 1, 2 * a[x] - a[l - 1]) - a - 1;
				ans += a[p] - a[x];
				x = p;
				r = p;
			}
//			cout << l << " " << r << " " << x << " " << ans << "\n";
		}
		cout << ans << "\n";
	}
    return;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	if(fopen(".INP", "r")) {
		freopen(".INP", "r", stdin);
		freopen(".OUT", "w", stdout);
	}
	int testCase = 1;
	//cin >> testCase;
	while(testCase--) solve();
}
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
