Problem p03954 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define dbv(v)                                                                 \
  cerr << #v << "=";                                                           \
  for (auto _x : v)                                                            \
    cerr << _x << ", ";                                                        \
  cerr << endl
#define dba(a, n)                                                              \
  cerr << #a << "=";                                                           \
  for (int _i = 0; _i < (n); ++_i)                                             \
    cerr << a[_i] << ", ";                                                     \
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> A(2 * n - 1), B(2 * n - 1);
  for (int i = 0; i < 2 * n - 1; ++i)
    scanf("%d", &A[i]);
  int lo = 1, hi = 2 * n - 1;
  while (lo < hi) {
    int m = (lo + hi + 1) >> 1;
    for (int i = 0; i < 2 * n - 1; ++i) {
      B[i] = A[i] >= m;
    }
    int st = 0;
    vector<int> d(2, INT_MAX);
    for (int i = 0; i < 2 * n - 1; ++i) {
      if (i + 1 == 2 * n - 1 || B[i] != B[i + 1]) {
        if (i - st + 1 >= 2) {
          if (st < n - 1)
            d[B[i]] = min(d[B[i]], n - 1 - st);
          else if (i > n - 1)
            d[B[i]] = min(d[B[i]], i - (n - 1));
          else
            d[B[i]] = 0;
        }
        st = i + 1;
      }
    }
    int col = min_element(d.begin(), d.end()) - d.begin();
    if (d[col] == INT_MAX)
      col = n % 2 == 0 ? !B[n - 1] : B[n - 1];
    else
      assert(d[0] != d[1]);
    if (col == 1)
      lo = m;
    else
      hi = m - 1;
  }
  printf("%d\n", lo);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define dbv(v)                                                                 \
  cerr << #v << "=";                                                           \
  for (auto _x : v)                                                            \
    cerr << _x << ", ";                                                        \
  cerr << endl
#define dba(a, n)                                                              \
  cerr << #a << "=";                                                           \
  for (int _i = 0; _i < (n); ++_i)                                             \
    cerr << a[_i] << ", ";                                                     \
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> A(2 * n - 1), B(2 * n - 1);
  for (int i = 0; i < 2 * n - 1; ++i)
    scanf("%d", &A[i]);
  int lo = 1, hi = 2 * n - 1;
  while (lo < hi) {
    int m = (lo + hi + 1) >> 1;
    for (int i = 0; i < 2 * n - 1; ++i) {
      B[i] = A[i] >= m;
    }
    int st = 0;
    vector<int> d(2, INT_MAX);
    for (int i = 0; i < 2 * n - 1; ++i) {
      if (i + 1 == 2 * n - 1 || B[i] != B[i + 1]) {
        if (i - st + 1 >= 2) {
          if (i < n - 1)
            d[B[i]] = min(d[B[i]], n - 1 - i);
          else if (st > n - 1)
            d[B[i]] = min(d[B[i]], st - (n - 1));
          else
            d[B[i]] = 0;
        }
        st = i + 1;
      }
    }
    int col = min_element(d.begin(), d.end()) - d.begin();
    if (d[col] == INT_MAX)
      col = n % 2 == 0 ? !B[n - 1] : B[n - 1];
    else
      assert(d[0] != d[1]);
    if (col == 1)
      lo = m;
    else
      hi = m - 1;
  }
  printf("%d\n", lo);
}