Problem p03275 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef long long ll;
// typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;
// typedef unsigned int uint;

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

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

struct BIT {
  //[0,mn]
  vector<int> node;
  int mn;

  BIT(int mn) {
    this->mn = mn;
    node.resize(mn + 1, 0);
  }

  void add(int i, int x) {
    while (i <= mn) {
      node[i] += x;
      i += (i & -i);
    }
  }

  int sum(int i) {
    int res = 0;
    while (i > 0) {
      res += node[i];
      i -= (i & -i);
    }
    return res;
  }
};

int A[100010];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int bias = 50010;

  int N;
  cin >> N;
  rep(i, 0, N) { cin >> A[i]; }

  int l = 0, r = 1 << 30;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    int sum = 0;
    int count = 0;
    BIT bit(200020);

    bit.add(bias + sum, 1);
    rep(i, 0, N) {
      if (A[i] >= mid) {
        sum++;
      } else {
        sum--;
      }
      count += bit.sum(bias + sum);
      bit.add(bias + sum, 1);
    }
    if (count >= (N * (N + 1) + 3) / 4) {
      l = mid;
    } else {
      r = mid;
    }
  }

  cout << l << endl;

  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef long long ll;
// typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;
// typedef unsigned int uint;

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

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

struct BIT {
  //[0,mn]
  vector<int> node;
  int mn;

  BIT(int mn) {
    this->mn = mn;
    node.resize(mn + 1, 0);
  }

  void add(int i, int x) {
    while (i <= mn) {
      node[i] += x;
      i += (i & -i);
    }
  }

  int sum(int i) {
    int res = 0;
    while (i > 0) {
      res += node[i];
      i -= (i & -i);
    }
    return res;
  }
};

int A[100010];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int bias = 100010;

  int N;
  cin >> N;
  rep(i, 0, N) { cin >> A[i]; }

  int l = 0, r = 1 << 30;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    int sum = 0;
    int count = 0;
    BIT bit(200020);

    bit.add(bias + sum, 1);
    rep(i, 0, N) {
      if (A[i] >= mid) {
        sum++;
      } else {
        sum--;
      }
      count += bit.sum(bias + sum);
      bit.add(bias + sum, 1);
    }
    if (count >= (N * (N + 1) + 3) / 4) {
      l = mid;
    } else {
      r = mid;
    }
  }

  cout << l << endl;

  return 0;
}