Problem p03040 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

template <typename T> struct BIT {
  vector<T> bit;
  int n;

  BIT() {}
  BIT(int n) : bit(vector<T>(n + 2, 0)), n(n) {}

  T sum(int i) {
    i++;
    T s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }

  void add(int i, T x) {
    i++;
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }

  T pquery(int i) { return sum(i) - sum(i - 1); }

  int lower_bound(T x) {
    int l = -1, r = bit.size();
    while (r - l > 1) {
      int m = (l + r) / 2;

      if (sum(m) >= x)
        r = m;
      else
        l = m;
    }
    if (r == bit.size())
      r = -1;

    return r;
  }
};

int Q;
int query[21235];
int A[212345], B[212345];

int main() {
  int Q;
  cin >> Q;
  REP(i, Q) {
    scanf("%d", query + i);
    if (query[i] == 1)
      scanf("%d %d", A + i, B + i);
  }

  vector<int> cx;
  REP(i, Q) if (query[i] == 1) cx.push_back(A[i]);
  sort(ALL(cx));

  BIT<ll> popcnt(Q + 10), sum(Q + 10); // 1-indexed

  ll bsum = 0;
  REP(q, Q) {
    if (query[q] == 1) {
      int idx = lower_bound(ALL(cx), A[q]) - cx.begin() + 1;
      popcnt.add(idx, 1);
      sum.add(idx, A[q]);
      bsum += B[q];
    } else {
      int size = popcnt.sum(Q);
      if (size == 0) {
        puts("0 0");
        continue;
      }
      int idx = popcnt.lower_bound((size + 1) / 2); // 1-indexed

      ll x = cx[idx - 1];
      ll ans =
          -x * (popcnt.sum(Q) - popcnt.sum(idx)) + (sum.sum(Q) - sum.sum(idx));
      ans += x * popcnt.sum(idx) - sum.sum(idx);
      ans += bsum;

      printf("%lld %lld\n", x, ans);
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

template <typename T> struct BIT {
  vector<T> bit;
  int n;

  BIT() {}
  BIT(int n) : bit(vector<T>(n + 2, 0)), n(n) {}

  T sum(int i) {
    i++;
    T s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }

  void add(int i, T x) {
    i++;
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }

  T pquery(int i) { return sum(i) - sum(i - 1); }

  int lower_bound(T x) {
    int l = -1, r = bit.size();
    while (r - l > 1) {
      int m = (l + r) / 2;

      if (sum(m) >= x)
        r = m;
      else
        l = m;
    }
    if (r == bit.size())
      r = -1;

    return r;
  }
};

int Q;
int query[212345];
int A[212345], B[212345];

int main() {
  int Q;
  cin >> Q;
  REP(i, Q) {
    scanf("%d", query + i);
    if (query[i] == 1)
      scanf("%d %d", A + i, B + i);
  }

  vector<int> cx;
  REP(i, Q) if (query[i] == 1) cx.push_back(A[i]);
  sort(ALL(cx));

  BIT<ll> popcnt(Q + 10), sum(Q + 10); // 1-indexed

  ll bsum = 0;
  REP(q, Q) {
    if (query[q] == 1) {
      int idx = lower_bound(ALL(cx), A[q]) - cx.begin() + 1;
      popcnt.add(idx, 1);
      sum.add(idx, A[q]);
      bsum += B[q];
    } else {
      int size = popcnt.sum(Q);
      if (size == 0) {
        puts("0 0");
        continue;
      }
      int idx = popcnt.lower_bound((size + 1) / 2); // 1-indexed

      ll x = cx[idx - 1];
      ll ans =
          -x * (popcnt.sum(Q) - popcnt.sum(idx)) + (sum.sum(Q) - sum.sum(idx));
      ans += x * popcnt.sum(idx) - sum.sum(idx);
      ans += bsum;

      printf("%lld %lld\n", x, ans);
    }
  }

  return 0;
}