Problem p03277 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

#define X first
#define Y second

// #include <boost/unordered_map.hpp>
// using namespace boost;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> rbtree; rbtree T;
*/

namespace io {
const int L = (1 << 20) + 1;

char buf[L], *S, *T, c;

char getchar() {
  if (__builtin_expect(S == T, 0)) {
    T = (S = buf) + fread(buf, 1, L, stdin);
    return (S == T ? EOF : *S++);
  }
  return *S++;
}

int inp() {
  int x = 0, f = 1;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x * f;
}

unsigned inpu() {
  unsigned x = 0;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    ;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x;
}

ll inp_ll() {
  ll x = 0;
  int f = 1;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x * f;
}

char B[25], *outs = B + 20, *outr = B + 20;
template <class T> inline void print(register T a, register char x = 0) {
  if (x)
    *--outs = x, x = 0;

  if (!a)
    *--outs = '0';
  else
    while (a)
      *--outs = (a % 10) + 48, a /= 10;

  if (x)
    *--outs = x;

  fwrite(outs, outr - outs, 1, stdout);
  outs = outr;
}
}; // namespace io

using io ::inp;
using io ::inp_ll;
using io ::inpu;
using io ::print;

ll power(ll a, ll b, ll p) {
  if (!b)
    return 1;
  ll t = power(a, b / 2, p);
  t = t * t % p;
  if (b & 1)
    t = t * a % p;
  return t;
}

const int MAXN = 5010;
const int MOD = 1000000007;
const int INF = 1000000000;

int n;
int a[MAXN], b[MAXN];
int v[MAXN];

void add(int x, int p) {
  for (; x <= 2 * n + 1; x += x & -x)
    v[x] += p;
}

int get(int x) {
  int p = 0;
  for (; x; x -= x & -x)
    p += v[x];
  return p;
}

bool check(int m) {
  for (int i = 1; i <= 2 * n + 1; ++i)
    v[i] = 0;
  ll cnt = 0;
  add(0 + n + 1, 1);
  for (int i = 1; i <= n; ++i) {
    b[i] = (a[i] >= m) ? 1 : -1;
    b[i] += b[i - 1];
    cnt += get(b[i] + n + 1);
    add(b[i] + n + 1, 1);
  }
  return cnt >= ((ll)n * (n + 1) / 2 + 1) / 2;
}

int main() {

  n = inp();
  for (int i = 1; i <= n; ++i)
    a[i] = inp();
  int L = 1, R = 1000000000;
  int ans = 0;
  while (L <= R) {
    int m = (L + R) / 2;
    if (check(m)) {
      ans = m;
      L = m + 1;
    } else
      R = m - 1;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

#define X first
#define Y second

// #include <boost/unordered_map.hpp>
// using namespace boost;

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> rbtree; rbtree T;
*/

namespace io {
const int L = (1 << 20) + 1;

char buf[L], *S, *T, c;

char getchar() {
  if (__builtin_expect(S == T, 0)) {
    T = (S = buf) + fread(buf, 1, L, stdin);
    return (S == T ? EOF : *S++);
  }
  return *S++;
}

int inp() {
  int x = 0, f = 1;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x * f;
}

unsigned inpu() {
  unsigned x = 0;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    ;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x;
}

ll inp_ll() {
  ll x = 0;
  int f = 1;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  return x * f;
}

char B[25], *outs = B + 20, *outr = B + 20;
template <class T> inline void print(register T a, register char x = 0) {
  if (x)
    *--outs = x, x = 0;

  if (!a)
    *--outs = '0';
  else
    while (a)
      *--outs = (a % 10) + 48, a /= 10;

  if (x)
    *--outs = x;

  fwrite(outs, outr - outs, 1, stdout);
  outs = outr;
}
}; // namespace io

using io ::inp;
using io ::inp_ll;
using io ::inpu;
using io ::print;

ll power(ll a, ll b, ll p) {
  if (!b)
    return 1;
  ll t = power(a, b / 2, p);
  t = t * t % p;
  if (b & 1)
    t = t * a % p;
  return t;
}

const int MAXN = 200010;
const int MOD = 1000000007;
const int INF = 1000000000;

int n;
int a[MAXN], b[MAXN];
int v[MAXN];

void add(int x, int p) {
  for (; x <= 2 * n + 1; x += x & -x)
    v[x] += p;
}

int get(int x) {
  int p = 0;
  for (; x; x -= x & -x)
    p += v[x];
  return p;
}

bool check(int m) {
  for (int i = 1; i <= 2 * n + 1; ++i)
    v[i] = 0;
  ll cnt = 0;
  add(0 + n + 1, 1);
  for (int i = 1; i <= n; ++i) {
    b[i] = (a[i] >= m) ? 1 : -1;
    b[i] += b[i - 1];
    cnt += get(b[i] + n + 1);
    add(b[i] + n + 1, 1);
  }
  return cnt >= ((ll)n * (n + 1) / 2 + 1) / 2;
}

int main() {

  n = inp();
  for (int i = 1; i <= n; ++i)
    a[i] = inp();
  int L = 1, R = 1000000000;
  int ans = 0;
  while (L <= R) {
    int m = (L + R) / 2;
    if (check(m)) {
      ans = m;
      L = m + 1;
    } else
      R = m - 1;
  }
  cout << ans << endl;

  return 0;
}