Problem p03040 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD =
    1000000000 + 7; // ;//1000000000 + 7 998244353 924844033 1000000000 + 9;
constexpr long long INF = numeric_limits<LL>::max();
constexpr long long DINF = 1000000000000;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourth;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<Edge>> g;
vector<edge> ed;
vector<Pll> pv;
map<LL, LL> ma;
set<LL> st;
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
LL n, m, k, p, q, w, h, ans, cnt, sum, a[310000], b[310000];
string str;
struct query {
  int flag, a, b;
};
bool f;
// char c[1100][1100];
query qu[100000];
map<LL, LL> zip;
int unzip[210000];
int compress(vector<LL> &x) {
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  for (int i = 0; i < x.size(); i++) {
    zip[x[i]] = i;
    unzip[i] = x[i];
  }
  return x.size();
}
class BIT {
public:
  vector<LL> Bit;
  LL M;
  BIT(LL M) : Bit(vector<LL>(M + 1, 0)), M(M) {}
  LL sum(LL i) {
    LL s = 0;
    while (i > 0) {
      s += Bit[i];
      i -= (i & (-i));
    }
    return s;
  }
  LL sum(LL i, LL j) { return sum(j) - sum(i - 1); }
  // 1-index
  void add(LL i, LL x) {
    while (i <= M) {
      Bit[i] += x;
      i += (i & (-i));
    }
  }
  // 初めてk以上になる、index
  int lower_bound(LL k) {
    int l = -1, r = n + 1, m;
    while (r - l > 1) {
      m = (l + r) / 2;
      if (sum(m) >= k)
        r = m;
      else
        l = m;
    }
    return (r == n + 1 ? -1 : r);
  }
  // 初めてkより大きくなる、index
  int upper_bound(LL k) {
    int l = -1, r = M + 1, m;
    while (r - l > 1) {
      m = (l + r) / 2;
      if (sum(m) > k)
        r = m;
      else
        l = m;
    }
    return (r == M + 1 ? M + 1 : r);
  }
};
BIT bi1(210000), bi2(210000);
int main() {
  cin >> n;
  vector<LL> ve;
  rep(i, n) {
    LL x, y, z;
    cin >> qu[i].flag;
    if (qu[i].flag == 1) {
      cin >> qu[i].a >> qu[i].b;
      ve.push_back(qu[i].a);
    }
  }
  ve.push_back(-INF / 1000);
  compress(ve);
  rep(i, n) {
    if (qu[i].flag == 1) {
      bi1.add(zip[qu[i].a], 1);
      bi2.add(zip[qu[i].a], qu[i].a);
      cnt++;
      sum += qu[i].b;
    } else {
      LL num = bi1.lower_bound((cnt + 1) / 2), cur = unzip[num];
      cout << cur << " "
           << (bi1.sum(num) * cur - bi2.sum(num)) +
                  (bi2.sum(num + 1, n) - bi1.sum(num + 1, n) * cur) + sum
           << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD =
    1000000000 + 7; // ;//1000000000 + 7 998244353 924844033 1000000000 + 9;
constexpr long long INF = numeric_limits<LL>::max();
constexpr long long DINF = 1000000000000;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourth;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<Edge>> g;
vector<edge> ed;
vector<Pll> pv;
map<LL, LL> ma;
set<LL> st;
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
LL n, m, k, p, q, w, h, ans, cnt, sum, a[310000], b[310000];
string str;
struct query {
  int flag, a, b;
};
bool f;
// char c[1100][1100];
query qu[210000];
map<LL, LL> zip;
int unzip[210000];
int compress(vector<LL> &x) {
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  for (int i = 0; i < x.size(); i++) {
    zip[x[i]] = i;
    unzip[i] = x[i];
  }
  return x.size();
}
class BIT {
public:
  vector<LL> Bit;
  LL M;
  BIT(LL M) : Bit(vector<LL>(M + 1, 0)), M(M) {}
  LL sum(LL i) {
    LL s = 0;
    while (i > 0) {
      s += Bit[i];
      i -= (i & (-i));
    }
    return s;
  }
  LL sum(LL i, LL j) { return sum(j) - sum(i - 1); }
  // 1-index
  void add(LL i, LL x) {
    while (i <= M) {
      Bit[i] += x;
      i += (i & (-i));
    }
  }
  // 初めてk以上になる、index
  int lower_bound(LL k) {
    int l = -1, r = n + 1, m;
    while (r - l > 1) {
      m = (l + r) / 2;
      if (sum(m) >= k)
        r = m;
      else
        l = m;
    }
    return (r == n + 1 ? -1 : r);
  }
  // 初めてkより大きくなる、index
  int upper_bound(LL k) {
    int l = -1, r = M + 1, m;
    while (r - l > 1) {
      m = (l + r) / 2;
      if (sum(m) > k)
        r = m;
      else
        l = m;
    }
    return (r == M + 1 ? M + 1 : r);
  }
};
BIT bi1(210000), bi2(210000);
int main() {
  cin >> n;
  vector<LL> ve;
  rep(i, n) {
    LL x, y, z;
    cin >> qu[i].flag;
    if (qu[i].flag == 1) {
      cin >> qu[i].a >> qu[i].b;
      ve.push_back(qu[i].a);
    }
  }
  ve.push_back(-INF / 1000);
  compress(ve);
  rep(i, n) {
    if (qu[i].flag == 1) {
      bi1.add(zip[qu[i].a], 1);
      bi2.add(zip[qu[i].a], qu[i].a);
      cnt++;
      sum += qu[i].b;
    } else {
      LL num = bi1.lower_bound((cnt + 1) / 2), cur = unzip[num];
      cout << cur << " "
           << (bi1.sum(num) * cur - bi2.sum(num)) +
                  (bi2.sum(num + 1, n) - bi1.sum(num + 1, n) * cur) + sum
           << endl;
    }
  }
  return 0;
}