Problem p03277 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define MOD 1000000000 + 7
#define INF (1LL << 30)
#define LINF (1LL << 55)

int tree[500000];
int a[1000000];
int arr[1000000];
void build(int x, int l, int r) {
  if (l == r)
    tree[x] = 0;
  else {
    build(x * 2, l, (l + r) / 2);
    build(x * 2 + 1, (l + r) / 2 + 1, r);
  }
}
void upd(int x, int l, int r, int v, int val) {
  if (v > r || v < l)
    return;
  if (l == r)
    tree[x] += val;
  else {
    if (v <= (l + r) / 2)
      upd(x * 2, l, (l + r) / 2, v, val);
    else
      upd(x * 2 + 1, (l + r) / 2 + 1, r, v, val);
    tree[x] = tree[x * 2] + tree[x * 2 + 1];
  }
}
int query(int x, int l, int r, int s, int e) {
  if (s > e)
    return 0;
  if (s > r || e < l)
    return 0;
  if (l >= s && r <= e)
    return tree[x];
  return query(x * 2, l, (l + r) / 2, s, e) +
         query(x * 2 + 1, (l + r) / 2 + 1, r, s, e);
}
int n;
long long GetSmallerThan(int m) {
  int N = 2 * n;
  // build(1,0,N);
  memset(tree, 0, sizeof(tree));
  upd(1, 0, N, n, 1);
  int sum = n;
  long long ret = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] <= m)
      sum++;
    else
      sum--;
    ret += query(1, 0, N, 0, sum - 1);
    upd(1, 0, N, sum, 1);
  }
  return ret;
}

int b[1000000];
map<int, int> to;
map<int, int> from;
set<int> S;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], S.insert(a[i]);
  int tot = 0;
  for (set<int>::iterator it = S.begin(); it != S.end(); it++) {
    to[*it] = tot;
    from[tot++] = *it;
  }
  for (int i = 1; i <= n; i++) {
    a[i] = to[a[i]];
    b[i] = a[i];
  }
  sort(b + 1, b + 1 + n);
  int l = 1, r = n;
  long long len = n;
  len = len * (len + 1) / 2;
  long long need = len / 2 + 1;
  int ret = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    long long num = GetSmallerThan(b[mid]);
    if (num >= need) {
      ret = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  // cout<<GetSmallerThan(2)<<endl;
  // cout<<GetSmallerThan(2)<<endl;
  // assert(ret!=-1);
  cout << from[b[ret]] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define MOD 1000000000 + 7
#define INF (1LL << 30)
#define LINF (1LL << 55)

int tree[1000000];
int a[1000000];
int arr[1000000];
void build(int x, int l, int r) {
  if (l == r)
    tree[x] = 0;
  else {
    build(x * 2, l, (l + r) / 2);
    build(x * 2 + 1, (l + r) / 2 + 1, r);
  }
}
void upd(int x, int l, int r, int v, int val) {
  if (v > r || v < l)
    return;
  if (l == r)
    tree[x] += val;
  else {
    if (v <= (l + r) / 2)
      upd(x * 2, l, (l + r) / 2, v, val);
    else
      upd(x * 2 + 1, (l + r) / 2 + 1, r, v, val);
    tree[x] = tree[x * 2] + tree[x * 2 + 1];
  }
}
int query(int x, int l, int r, int s, int e) {
  if (s > e)
    return 0;
  if (s > r || e < l)
    return 0;
  if (l >= s && r <= e)
    return tree[x];
  return query(x * 2, l, (l + r) / 2, s, e) +
         query(x * 2 + 1, (l + r) / 2 + 1, r, s, e);
}
int n;
long long GetSmallerThan(int m) {
  int N = 2 * n;
  // build(1,0,N);
  memset(tree, 0, sizeof(tree));
  upd(1, 0, N, n, 1);
  int sum = n;
  long long ret = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] <= m)
      sum++;
    else
      sum--;
    ret += query(1, 0, N, 0, sum - 1);
    upd(1, 0, N, sum, 1);
  }
  return ret;
}

int b[1000000];
map<int, int> to;
map<int, int> from;
set<int> S;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], S.insert(a[i]);
  int tot = 0;
  for (set<int>::iterator it = S.begin(); it != S.end(); it++) {
    to[*it] = tot;
    from[tot++] = *it;
  }
  for (int i = 1; i <= n; i++) {
    a[i] = to[a[i]];
    b[i] = a[i];
  }
  sort(b + 1, b + 1 + n);
  int l = 1, r = n;
  long long len = n;
  len = len * (len + 1) / 2;
  long long need = len / 2 + 1;
  int ret = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    long long num = GetSmallerThan(b[mid]);
    if (num >= need) {
      ret = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  // cout<<GetSmallerThan(2)<<endl;
  // cout<<GetSmallerThan(2)<<endl;
  // assert(ret!=-1);
  cout << from[b[ret]] << endl;
}