Problem p03158 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vl v(n);
  REP(i, n) cin >> v[i];
  vl e(n + 1, 0), o(n + 1, 0);
  REP(i, n) {
    e[i + 1] += e[i];
    o[i + 1] += o[i];
    if (i & 1)
      o[i + 1] += v[i];
    else
      e[i + 1] += v[i];
  }
  vl p(n + 1, 0);
  RREP(i, n) p[i] = p[i + 1] + v[i];
  REP(aa, q) {
    int x;
    cin >> x;
    if (x >= v.back()) {
      if (n & 1)
        cout << e[n] << endl;
      else
        cout << o[n] << endl;
      continue;
    }
    if (x <= v.front()) {
      cout << p[n / 2] << endl;
      continue;
    }
    int l = 0, r = INF;
    while (r - l > 1) {
      int mid = (r + l) / 2;

      int a = lower_bound(ALL(v), x - mid) - v.begin();
      int b1 = lower_bound(ALL(v), x + mid) - v.begin();
      int b2 = upper_bound(ALL(v), x + mid) - v.begin();

      if (n - b1 - (b1 - a) > 1) {
        if (n - b2 - (b2 - a) > 1) {
          l = mid;
        } else
          r = mid;
      } else {

        r = mid;
      }
    }
    int a = lower_bound(ALL(v), x - r) - v.begin();
    int b1 = lower_bound(ALL(v), x + r) - v.begin();
    int b2 = upper_bound(ALL(v), x + r) - v.begin();
    if (n - b1 - (b1 - a) > 1) {
      if (n - b2 - (b2 - a) == 1) {

        ll ans = p[b2];
        if (a == 0) {
          cout << ans << endl;
        } else {
          a--;
          if (a & 1)
            ans += e[a];
          else
            ans += o[a];
          cout << ans << endl;
        }
      } else {
        // assert(n - b2 - (b2 - a) == 0);
        ll ans = p[b2];
        if (a & 1)
          ans += e[a];
        else
          ans += o[a];
        cout << ans << endl;
      }
    } else {
      if (n - b1 - (b1 - a) == 1) {
        ll ans = p[b1];
        if (a == 0) {
          cout << ans << endl;
        } else {
          a--;
          if (a & 1)
            ans += e[a];
          else
            ans += o[a];
          cout << ans << endl;
        }

      } else {
        assert(n - b1 - (b1 - a) == 0);
        ll ans = p[b1];
        if (a & 1)
          ans += e[a];
        else
          ans += o[a];
        cout << ans << endl;
      }
    }
  }
}

Fixed submission:
#include "bits/stdc++.h"

#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vl v(n);
  REP(i, n) cin >> v[i];
  vl e(n + 1, 0), o(n + 1, 0);
  REP(i, n) {
    e[i + 1] += e[i];
    o[i + 1] += o[i];
    if (i & 1)
      o[i + 1] += v[i];
    else
      e[i + 1] += v[i];
  }
  vl p(n + 1, 0);
  RREP(i, n) p[i] = p[i + 1] + v[i];
  REP(aa, q) {
    int x;
    cin >> x;
    if (x >= v.back()) {
      if (n & 1)
        cout << e[n] << endl;
      else
        cout << o[n] << endl;
      continue;
    }
    if (x <= v.front()) {
      cout << p[n / 2] << endl;
      continue;
    }
    int l = -1, r = INF;
    while (r - l > 1) {
      int mid = (r + l) / 2;

      int a = lower_bound(ALL(v), x - mid) - v.begin();
      int b1 = lower_bound(ALL(v), x + mid) - v.begin();
      int b2 = upper_bound(ALL(v), x + mid) - v.begin();

      if (n - b1 - (b1 - a) > 1) {
        if (n - b2 - (b2 - a) > 1) {
          l = mid;
        } else
          r = mid;
      } else {

        r = mid;
      }
    }
    int a = lower_bound(ALL(v), x - r) - v.begin();
    int b1 = lower_bound(ALL(v), x + r) - v.begin();
    int b2 = upper_bound(ALL(v), x + r) - v.begin();
    if (n - b1 - (b1 - a) > 1) {
      if (n - b2 - (b2 - a) == 1) {

        ll ans = p[b2];
        if (a == 0) {
          cout << ans << endl;
        } else {
          a--;
          if (a & 1)
            ans += e[a];
          else
            ans += o[a];
          cout << ans << endl;
        }
      } else {
        // assert(n - b2 - (b2 - a) == 0);
        ll ans = p[b2];
        if (a & 1)
          ans += e[a];
        else
          ans += o[a];
        cout << ans << endl;
      }
    } else {
      if (n - b1 - (b1 - a) == 1) {
        ll ans = p[b1];
        if (a == 0) {
          cout << ans << endl;
        } else {
          a--;
          if (a & 1)
            ans += e[a];
          else
            ans += o[a];
          cout << ans << endl;
        }

      } else {
        assert(n - b1 - (b1 - a) == 0);
        ll ans = p[b1];
        if (a & 1)
          ans += e[a];
        else
          ans += o[a];
        cout << ans << endl;
      }
    }
  }
}