이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5;
int n, q;
int x[N];
int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x[i];
  }
  cin >> q;
  while (q--) {
    int s; cin >> s;
    int it = lower_bound(x + 1, x + n + 1, s) - x;
    int l, r;
    if (it == n + 1) {
      l = r = n;
    } else if (x[it] == s || it == 1) {
      l = r = it;
    } else if (x[it] - s < s - x[it - 1]) {
      l = r = it;
    } else {
      l = r = it - 1;
    }
    long long res = abs(s - x[l]);
    s = x[l];
    while (1) {
      if (l == 1) {
        res += abs(s - x[n]);
        break;
      }
      if (r == n) {
        res += abs(s - x[1]);
        break;
      }
      int L = x[l - 1], R = x[r + 1];
      if (s - L <= R - s) {
        int nxt = lower_bound(x + 1, x + n + 1, 2 * s - R) - x;
        res += abs(s - x[nxt]);
        s = x[nxt];
        l = nxt;
      } else {
        int nxt = upper_bound(x + 1, x + n + 1, 2 * s - L) - x - 1;
        res += abs(s - x[nxt]);
        s = x[nxt];
        r = nxt;
      }
    }
    cout << res << "\n";
  }
  return 0;
}
| # | 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... |