Problem p03040 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
void print() { cout << "\n"; }
template <class T> void print(const T &x) { cout << x << "\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
template <class T> void printVector(const vector<T> &v) {
  for (const T &x : v) {
    cout << x << " ";
  }
  cout << "\n";
}
using ll = long long;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

template <typename T> struct BIT {
  int n;
  vector<T> bit;
  BIT(int _n) { init(_n); }
  void init(int _n) {
    n = _n + 1;
    bit.resize(n + 1, 0);
  }
  // [0, k)
  T sum(int k) {
    T res = 0;
    for (int i = k - 1; i >= 0; i = (i & (i + 1)) - 1) {
      res += bit[i];
    }
    return res;
  }
  // [l, r]
  T sum(int l, int r) { return (l <= r ? sum(r + 1) - sum(l) : 0); }
  // bit[k] += x
  void add(int k, T x) {
    for (int i = k; i < n; i |= i + 1) {
      bit[i] += x;
    }
  }
  // v[0] + ... + v[res] >= x
  int lower_bound(T x) {
    int res = -1;
    int k = 1;
    while (2 * k <= n) {
      k <<= 1;
    }
    for (; k > 0; k >>= 1) {
      if (res + k < n && bit[res + k] < x) {
        x -= bit[res + k];
        res += k;
      }
    }
    return res + 1;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  vector<ll> t(q), a(q), b(q);
  vector<ll> As;
  for (int i = 0; i < q; i++) {
    cin >> t[i];
    if (t[i] == 1) {
      cin >> a[i] >> b[i];
      As.emplace_back(a[i]);
    }
  }
  sort(ALL(As));
  As.erase(unique(ALL(As)), end(As));
  map<ll, int> mp;
  for (int i = 0; i < As.size(); i++) {
    mp[As[i]] = i;
  }
  BIT<ll> bcnt(As.size()), bsum(As.size());
  ll sum = 0;
  int sz = 0;
  for (int i = 0; i < q; i++) {
    if (t[i] == 1) {
      int id = mp[a[i]];
      bsum.add(id, a[i]), bcnt.add(id, 1);
      sum += b[i];
      sz++;
    } else {
      ll res = sum;
      int id = bcnt.lower_bound((sz + 1) / 2);
      res += bsum.sum(id + 1, sz - 1);
      res -= As[id] * bcnt.sum(id + 1, sz - 1);
      res += As[id] * bcnt.sum(0, id - 1);
      res -= bsum.sum(0, id - 1);
      print(As[id], res);
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
void print() { cout << "\n"; }
template <class T> void print(const T &x) { cout << x << "\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
template <class T> void printVector(const vector<T> &v) {
  for (const T &x : v) {
    cout << x << " ";
  }
  cout << "\n";
}
using ll = long long;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

template <typename T> struct BIT {
  int n;
  vector<T> bit;
  BIT(int _n) { init(_n); }
  void init(int _n) {
    n = _n + 1;
    bit.resize(n + 1, 0);
  }
  // [0, k)
  T sum(int k) {
    T res = 0;
    for (int i = k - 1; i >= 0; i = (i & (i + 1)) - 1) {
      res += bit[i];
    }
    return res;
  }
  // [l, r]
  T sum(int l, int r) { return (l <= r ? sum(r + 1) - sum(l) : 0); }
  // bit[k] += x
  void add(int k, T x) {
    for (int i = k; i < n; i |= i + 1) {
      bit[i] += x;
    }
  }
  // v[0] + ... + v[res] >= x
  int lower_bound(T x) {
    int res = -1;
    int k = 1;
    while (2 * k <= n) {
      k <<= 1;
    }
    for (; k > 0; k >>= 1) {
      if (res + k < n && bit[res + k] < x) {
        x -= bit[res + k];
        res += k;
      }
    }
    return res + 1;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  vector<ll> t(q), a(q), b(q);
  vector<ll> As;
  for (int i = 0; i < q; i++) {
    cin >> t[i];
    if (t[i] == 1) {
      cin >> a[i] >> b[i];
      As.emplace_back(a[i]);
    }
  }
  sort(ALL(As));
  As.erase(unique(ALL(As)), end(As));
  map<ll, int> mp;
  for (int i = 0; i < As.size(); i++) {
    mp[As[i]] = i;
  }
  BIT<ll> bcnt(As.size()), bsum(As.size());
  ll sum = 0;
  int sz = 0;
  for (int i = 0; i < q; i++) {
    if (t[i] == 1) {
      int id = mp[a[i]];
      bsum.add(id, a[i]), bcnt.add(id, 1);
      sum += b[i];
      sz++;
    } else {
      ll res = sum;
      int id = bcnt.lower_bound((sz + 1) / 2);
      res += bsum.sum(id + 1, (int)As.size() - 1);
      res -= As[id] * bcnt.sum(id + 1, (int)As.size() - 1);
      res += As[id] * bcnt.sum(0, id - 1);
      res -= bsum.sum(0, id - 1);
      print(As[id], res);
    }
  }
}