Problem p03158 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = int(n) - 1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = int(n) - 1; (i) >= (int)(m); --(i))
#define ALL(x) begin(x), end(x)
#define dump(x) cerr << #x " = " << x << endl
#define unittest_name_helper(counter) unittest_##counter
#define unittest_name(counter) unittest_name_helper(counter)
#define unittest __attribute__((constructor)) void unittest_name(__COUNTER__)()
using ll = long long;
using namespace std;
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> inline void chmax(T &a, U const &b) {
  a = max<T>(a, b);
}
template <class T, class U> inline void chmin(T &a, U const &b) {
  a = min<T>(a, b);
}
template <typename X, typename T> auto vectors(X x, T a) {
  return vector<T>(x, a);
}
template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
  auto cont = vectors(y, z, zs...);
  return vector<decltype(cont)>(x, cont);
}
template <typename T> ostream &operator<<(ostream &out, vector<T> const &xs) {
  REP(i, int(xs.size()) - 1) out << xs[i] << ' ';
  if (not xs.empty())
    out << xs.back();
  return out;
}

template <typename UnaryPredicate>
int64_t binsearch(int64_t l, int64_t r, UnaryPredicate p) {
  assert(l <= r);
  --l;
  while (r - l > 1) {
    int64_t m = l + (r - l) / 2;
    (p(m) ? r : l) = m;
  }
  return r;
}

vector<ll> solve(int n, int q, vector<ll> const &a, vector<ll> const &x) {
  vector<ll> acc(n + 1);
  partial_sum(ALL(a), acc.begin() + 1);
  vector<ll> acc_even(n + 1);
  REP(i, n) { acc_even[i + 1] = acc_even[i] + (i % 2 == 0 ? a[i] : 0); }

  vector<int> order(q);
  iota(ALL(order), 0);
  sort(ALL(order), [&](int i, int j) { return a[i] > a[j]; });

  auto get_l_r = [&](int k) {
    int l = max(0, n - 2 * k);
    int r = n - k;
    return make_pair(l, r);
  };
  auto pred = [&](ll x, int k) {
    int l, r;
    tie(l, r) = get_l_r(k);
    ll d = max(abs(a[l] - x), abs(a[r - 1] - x));
    if (l - 1 >= 0 and abs(a[l - 1] - x) <= d)
      return false;
    // if (r < n and abs(a[r] - x) < d) return false;
    return true;
  };

  vector<ll> answer(q);
  int k = 1;
  // dump(n);
  // dump(a);
  for (int i : order) {
    while (k < (n + 1) / 2 and not pred(x[i], k))
      ++k;
    int l, r;
    tie(l, r) = get_l_r(k);
    answer[i] =
        (acc[n] - acc[r]) + (n % 2 == 0 ? acc[l] - acc_even[l] : acc_even[l]);
    // cerr << endl;
    // dump(i);
    // dump(x[i]);
    // dump(k);
    // dump(l);
    // dump(r);
  }
  return answer;
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  vector<ll> x(q);
  REP(i, q) cin >> x[i];
  for (ll y_i : solve(n, q, a, x)) {
    cout << y_i << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = int(n) - 1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = int(n) - 1; (i) >= (int)(m); --(i))
#define ALL(x) begin(x), end(x)
#define dump(x) cerr << #x " = " << x << endl
#define unittest_name_helper(counter) unittest_##counter
#define unittest_name(counter) unittest_name_helper(counter)
#define unittest __attribute__((constructor)) void unittest_name(__COUNTER__)()
using ll = long long;
using namespace std;
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> inline void chmax(T &a, U const &b) {
  a = max<T>(a, b);
}
template <class T, class U> inline void chmin(T &a, U const &b) {
  a = min<T>(a, b);
}
template <typename X, typename T> auto vectors(X x, T a) {
  return vector<T>(x, a);
}
template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
  auto cont = vectors(y, z, zs...);
  return vector<decltype(cont)>(x, cont);
}
template <typename T> ostream &operator<<(ostream &out, vector<T> const &xs) {
  REP(i, int(xs.size()) - 1) out << xs[i] << ' ';
  if (not xs.empty())
    out << xs.back();
  return out;
}

template <typename UnaryPredicate>
int64_t binsearch(int64_t l, int64_t r, UnaryPredicate p) {
  assert(l <= r);
  --l;
  while (r - l > 1) {
    int64_t m = l + (r - l) / 2;
    (p(m) ? r : l) = m;
  }
  return r;
}

vector<ll> solve(int n, int q, vector<ll> const &a, vector<ll> const &x) {
  vector<ll> acc(n + 1);
  partial_sum(ALL(a), acc.begin() + 1);
  vector<ll> acc_even(n + 1);
  REP(i, n) { acc_even[i + 1] = acc_even[i] + (i % 2 == 0 ? a[i] : 0); }

  vector<int> order(q);
  iota(ALL(order), 0);
  sort(ALL(order), [&](int i, int j) { return x[i] > x[j]; });

  auto get_l_r = [&](int k) {
    int l = max(0, n - 2 * k);
    int r = n - k;
    return make_pair(l, r);
  };
  auto pred = [&](ll x, int k) {
    int l, r;
    tie(l, r) = get_l_r(k);
    ll d = max(abs(a[l] - x), abs(a[r - 1] - x));
    if (l - 1 >= 0 and abs(a[l - 1] - x) <= d)
      return false;
    // if (r < n and abs(a[r] - x) < d) return false;
    return true;
  };

  vector<ll> answer(q);
  int k = 1;
  // dump(n);
  // dump(a);
  for (int i : order) {
    while (k < (n + 1) / 2 and not pred(x[i], k))
      ++k;
    int l, r;
    tie(l, r) = get_l_r(k);
    answer[i] =
        (acc[n] - acc[r]) + (n % 2 == 0 ? acc[l] - acc_even[l] : acc_even[l]);
    // cerr << endl;
    // dump(i);
    // dump(x[i]);
    // dump(k);
    // dump(l);
    // dump(r);
  }
  return answer;
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  vector<ll> x(q);
  REP(i, q) cin >> x[i];
  for (ll y_i : solve(n, q, a, x)) {
    cout << y_i << endl;
  }
  return 0;
}