Problem p03275 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

template <typename T> class BinaryIndexedTree {
public:
  vector<T> arr;
  long long size;
  T initial_value;

  BinaryIndexedTree(long long N) { setSize(N); }

  BinaryIndexedTree(long long N, T init) {
    setSize(N);
    setInitialValue(init);
    initializeArr();
  }

  void setSize(long long N) { size = N + 1; }

  void setInitialValue(T init) { initial_value = init; }

  void initializeArr() { arr = vector<T>(size, initial_value); }

  void initializeArr(T init) {
    setInitialValue(init);
    initializeArr();
  }

  T func(T x, T y);

  void update(long long idx, T value) {
    while (idx <= size) {
      arr[idx] = func(arr[idx], value);
      idx += (idx & -idx);
    }
  }

  T query(long long idx) {
    T ret = initial_value;
    while (idx > 0) {
      ret = func(ret, arr[idx]);
      idx -= (idx & -idx);
    }
    return ret;
  }
};

template <typename T> T BinaryIndexedTree<T>::func(T a, T b) { return a + b; }

long long convert_to_BIT_idx(long long idx, long long n) { return idx + n + 1; }

int main() {
  long long N;
  cin >> N;

  vector<long long> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  long long l = 1;
  long long r = 1000000001;
  while (r - l > 1) {
    long long c = (l + r) / 2;
    BinaryIndexedTree<long long> bit(2 * N, 0);
    long long current = 0;
    long long count = 0;
    bit.update(convert_to_BIT_idx(current, N), 1);
    for (int i = 0; i < N; i++) {
      if (a[i] >= c) {
        current++;
      } else {
        current--;
      }
      count += bit.query(convert_to_BIT_idx(current, N));
      bit.update(convert_to_BIT_idx(current, N), 1);
    }

    if (count * 2 >= N * (N + 1) / 2) {
      l = c;
    } else {
      r = c;
    }
  }
  cout << l << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

template <typename T> class BinaryIndexedTree {
public:
  vector<T> arr;
  long long size;
  T initial_value;

  BinaryIndexedTree(long long N) { setSize(N); }

  BinaryIndexedTree(long long N, T init) {
    setSize(N);
    setInitialValue(init);
    initializeArr();
  }

  void setSize(long long N) { size = N + 1; }

  void setInitialValue(T init) { initial_value = init; }

  void initializeArr() { arr = vector<T>(size, initial_value); }

  void initializeArr(T init) {
    setInitialValue(init);
    initializeArr();
  }

  T func(T x, T y);

  void update(long long idx, T value) {
    while (idx <= size) {
      arr[idx] = func(arr[idx], value);
      idx += (idx & -idx);
    }
  }

  T query(long long idx) {
    T ret = initial_value;
    while (idx > 0) {
      ret = func(ret, arr[idx]);
      idx -= (idx & -idx);
    }
    return ret;
  }
};

template <typename T> T BinaryIndexedTree<T>::func(T a, T b) { return a + b; }

long long convert_to_BIT_idx(long long idx, long long n) { return idx + n + 1; }

int main() {
  long long N;
  cin >> N;

  vector<long long> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  long long l = 1;
  long long r = 1000000001;
  while (r - l > 1) {
    long long c = (l + r) / 2;
    BinaryIndexedTree<long long> bit(2 * N + 1, 0);
    long long current = 0;
    long long count = 0;
    bit.update(convert_to_BIT_idx(current, N), 1);
    for (int i = 0; i < N; i++) {
      if (a[i] >= c) {
        current++;
      } else {
        current--;
      }
      count += bit.query(convert_to_BIT_idx(current, N));
      bit.update(convert_to_BIT_idx(current, N), 1);
    }

    if (count * 2 >= N * (N + 1) / 2) {
      l = c;
    } else {
      r = c;
    }
  }
  cout << l << endl;
}