Problem p03040 (C++)

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

template <class V, int ME> class BIT {
public:
  V bit[1 << ME];
  V total(int e) {
    if (e < 0)
      return 0;
    V s = 0;
    e++;
    while (e)
      s += bit[e - 1], e -= e & -e;
    return s;
  }
  void add(int e, V val) {
    e++;
    while (e <= 1 << ME)
      bit[e - 1] += val, e += e & -e;
  }
};

const ll MA = 16;
const ll len = (1 << MA);
map<ll, ll> zip;
ll unzip[len];

typedef pair<ll, pll> plll;

ll compress(vector<ll> &x) {
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  for (ll i = 0; i < x.size(); i++) {
    zip[x[i]] = i;
    unzip[i] = x[i];
  }
  return x.size();
}

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

  ll Q;
  cin >> Q;
  vector<plll> query;
  vector<ll> A;
  REP(i, Q) {
    ll type;
    cin >> type;
    if (type == 1) {
      ll a, b;
      cin >> a >> b;
      query.push_back({1, {a, b}});
      A.push_back(a);
    } else {
      query.push_back({2, {0, 0}});
    }
  }
  BIT<ll, MA> bit1, bit2;
  REP(i, len) bit1.bit[i] = 0;
  REP(i, len) bit2.bit[i] = 0;

  compress(A);
  ll B = 0, cnt = 0;
  REP(i, Q) {
    // dump(i)
    ll type = query[i].first;
    if (type == 1) {
      ll val = query[i].second.first;
      B += query[i].second.second;
      cnt++;
      bit1.add(zip[val], val);
      bit2.add(zip[val], 1);
    } else {
      ll lb = -1, ub = len;
      while (ub - lb > 1) {
        ll m = (ub + lb) / 2;
        if (bit2.total(m) >= (cnt + 1) / 2)
          ub = m;
        else
          lb = m;
      }
      ll med = ub;
      ll med_val = unzip[med];
      ll P = (bit1.total(len - 1) - bit1.total(med));
      ll Q = med_val * (bit2.total(len - 1) - bit2.total(med));
      ll R = (bit1.total(med - 1));
      ll S = med_val * (bit2.total(med - 1));
      /*
      dump(B)
      dump(P)
      dump(Q)
      dump(R)
      dump(S)
      */
      ll res = B + P + S - Q - R;
      cout << med_val << " " << res << endl;
    }
  }

  return 0;
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

template <class V, int ME> class BIT {
public:
  V bit[1 << ME];
  V total(int e) {
    if (e < 0)
      return 0;
    V s = 0;
    e++;
    while (e)
      s += bit[e - 1], e -= e & -e;
    return s;
  }
  void add(int e, V val) {
    e++;
    while (e <= 1 << ME)
      bit[e - 1] += val, e += e & -e;
  }
};

const ll MA = 18;
const ll len = (1 << MA);
map<ll, ll> zip;
ll unzip[len];

typedef pair<ll, pll> plll;

ll compress(vector<ll> &x) {
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  for (ll i = 0; i < x.size(); i++) {
    zip[x[i]] = i;
    unzip[i] = x[i];
  }
  return x.size();
}

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

  ll Q;
  cin >> Q;
  vector<plll> query;
  vector<ll> A;
  REP(i, Q) {
    ll type;
    cin >> type;
    if (type == 1) {
      ll a, b;
      cin >> a >> b;
      query.push_back({1, {a, b}});
      A.push_back(a);
    } else {
      query.push_back({2, {0, 0}});
    }
  }
  BIT<ll, MA> bit1, bit2;
  REP(i, len) bit1.bit[i] = 0;
  REP(i, len) bit2.bit[i] = 0;

  compress(A);
  ll B = 0, cnt = 0;
  REP(i, Q) {
    // dump(i)
    ll type = query[i].first;
    if (type == 1) {
      ll val = query[i].second.first;
      B += query[i].second.second;
      cnt++;
      bit1.add(zip[val], val);
      bit2.add(zip[val], 1);
    } else {
      ll lb = -1, ub = len;
      while (ub - lb > 1) {
        ll m = (ub + lb) / 2;
        if (bit2.total(m) >= (cnt + 1) / 2)
          ub = m;
        else
          lb = m;
      }
      ll med = ub;
      ll med_val = unzip[med];
      ll P = (bit1.total(len - 1) - bit1.total(med));
      ll Q = med_val * (bit2.total(len - 1) - bit2.total(med));
      ll R = (bit1.total(med - 1));
      ll S = med_val * (bit2.total(med - 1));
      /*
      dump(B)
      dump(P)
      dump(Q)
      dump(R)
      dump(S)
      */
      ll res = B + P + S - Q - R;
      cout << med_val << " " << res << endl;
    }
  }

  return 0;
}