Problem p03040 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
using namespace std;
typedef long long ll;
const int MAXN = 25 + 5;
const ll INF = 1e18;
const ll MOD = 998244353;
const double pi = 3.1415926;
ll n, sum[2][(MAXN * 3) << 2], lazy[2][(MAXN * 3) << 2], rec[MAXN][3],
    bb[MAXN * 3], cnt;
multiset<ll> ms;
multiset<ll>::iterator midit;
ll midpos;
void init() {
  ms.clear();
  memset(sum, 0, sizeof(sum));
  cnt = 0;
  memset(lazy, 0, sizeof(lazy));
}
ll Hash(ll x) { return lower_bound(bb + 1, bb + 1 + cnt, x) - bb; }
void pushdown(ll op, ll l, ll r, ll rt) {
  if (lazy[op][rt]) {
    lazy[op][rt << 1] += lazy[op][rt];
    lazy[op][rt << 1 | 1] += lazy[op][rt];
    ll m = (l + r) / 2;
    sum[op][rt << 1] += lazy[op][rt] * (m - l + 1);
    sum[op][rt << 1 | 1] += lazy[op][rt] * (r - (m + 1) + 1);
    lazy[op][rt] = 0;
  }
}
void pushup(ll op, ll rt) {
  sum[op][rt] = sum[op][rt << 1] + sum[op][rt << 1 | 1];
}
void update(ll op, ll L, ll R, ll c, ll l, ll r, ll rt) {
  if (L <= l && r <= R) {
    lazy[op][rt] += c; //***注意是+=不是=
    sum[op][rt] += (r - l + 1) * c;
    return;
  }
  ll m = (l + r) / 2;
  pushdown(op, l, r, rt);
  if (L <= m) {
    update(op, L, R, c, lson);
  }
  if (R > m) {
    update(op, L, R, c, rson);
  }
  pushup(op, rt);
}
ll query(ll op, ll p, ll l, ll r, ll rt) {
  if (l == r) {
    return sum[op][rt];
  }
  ll m = (l + r) / 2;

  pushdown(op, l, r, rt);
  if (p <= m) {
    return query(op, p, lson);
  } else {
    return query(op, p, rson);
  }
}
void updmid(ll x) {
  if (ms.empty()) {
    ms.insert(x);
    midpos = 1;
    midit = ms.begin();
  } else {
    ms.insert(x);
    if (x < *midit) {
      midpos++;
    }
    ll newpos = (ms.size() + 1) / 2;
    if (newpos > midpos) {
      midit++;
    }
    if (newpos < midpos) {
      midit--;
    }
    midpos = newpos;
  }

  update(0, 1, Hash(x - 1), x, 1, cnt, 1);
  update(1, 1, Hash(x - 1), -1, 1, cnt, 1);
  update(0, Hash(x + 1), cnt, -x, 1, cnt, 1);
  update(1, Hash(x + 1), cnt, 1, 1, cnt, 1);
}
ll getmid() { return *midit; }
ll getans(ll x) {
  ll p = Hash(x);

  ll k = query(1, p, 1, cnt, 1);

  ll a = query(0, p, 1, cnt, 1);

  return k * x + a;
}
void solve() {
  sort(bb + 1, bb + 1 + cnt);
  cnt = unique(bb + 1, bb + 1 + cnt) - bb - 1;
  ll sum = 0;
  for (ll i = 1; i <= n; i++) {
    ll op = rec[i][0];
    if (op == 1) {
      ll a = rec[i][1], b = rec[i][2];
      sum += b;
      updmid(a);
    } else {
      ll p = getmid();
      ll ans1 = getans(p);
      printf("%lld %lld\n", p, ans1 + sum);
    }
  }
}
int main() {
  //    freopen("c://duipai//data.txt","r",stdin);
  //    freopen("c://duipai//wa.txt","w",stdout);
  //   printf("%d",1^2^3^4^5^6^7^8^9^10^11^12^13^14^15);
  while (~scanf("%lld", &n)) {
    //        string str;
    //        cin>>str;
    //        cout<<str<<endl;
    init();
    for (ll i = 1; i <= n; i++) {
      ll op;
      scanf("%lld", &op);
      rec[i][0] = op;
      if (op == 1) {
        ll a, b;
        scanf("%lld %lld", &a, &b);
        rec[i][1] = a;
        rec[i][2] = b;
        bb[++cnt] = a;
        bb[++cnt] = a - 1;
        bb[++cnt] = a + 1;
      }
    }
    solve();
  }
}
/*
3
第-205
1 -3 3
1 0 8
2
*/


Fixed submission:
#include <bits/stdc++.h>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
const ll INF = 1e18;
const ll MOD = 998244353;
const double pi = 3.1415926;
ll n, sum[2][(MAXN * 3) << 2], lazy[2][(MAXN * 3) << 2], rec[MAXN][3],
    bb[MAXN * 3], cnt;
multiset<ll> ms;
multiset<ll>::iterator midit;
ll midpos;
void init() {
  ms.clear();
  memset(sum, 0, sizeof(sum));
  cnt = 0;
  memset(lazy, 0, sizeof(lazy));
}
ll Hash(ll x) { return lower_bound(bb + 1, bb + 1 + cnt, x) - bb; }
void pushdown(ll op, ll l, ll r, ll rt) {
  if (lazy[op][rt]) {
    lazy[op][rt << 1] += lazy[op][rt];
    lazy[op][rt << 1 | 1] += lazy[op][rt];
    ll m = (l + r) / 2;
    sum[op][rt << 1] += lazy[op][rt] * (m - l + 1);
    sum[op][rt << 1 | 1] += lazy[op][rt] * (r - (m + 1) + 1);
    lazy[op][rt] = 0;
  }
}
void pushup(ll op, ll rt) {
  sum[op][rt] = sum[op][rt << 1] + sum[op][rt << 1 | 1];
}
void update(ll op, ll L, ll R, ll c, ll l, ll r, ll rt) {
  if (L <= l && r <= R) {
    lazy[op][rt] += c; //***注意是+=不是=
    sum[op][rt] += (r - l + 1) * c;
    return;
  }
  ll m = (l + r) / 2;
  pushdown(op, l, r, rt);
  if (L <= m) {
    update(op, L, R, c, lson);
  }
  if (R > m) {
    update(op, L, R, c, rson);
  }
  pushup(op, rt);
}
ll query(ll op, ll p, ll l, ll r, ll rt) {
  if (l == r) {
    return sum[op][rt];
  }
  ll m = (l + r) / 2;

  pushdown(op, l, r, rt);
  if (p <= m) {
    return query(op, p, lson);
  } else {
    return query(op, p, rson);
  }
}
void updmid(ll x) {
  if (ms.empty()) {
    ms.insert(x);
    midpos = 1;
    midit = ms.begin();
  } else {
    ms.insert(x);
    if (x < *midit) {
      midpos++;
    }
    ll newpos = (ms.size() + 1) / 2;
    if (newpos > midpos) {
      midit++;
    }
    if (newpos < midpos) {
      midit--;
    }
    midpos = newpos;
  }

  update(0, 1, Hash(x - 1), x, 1, cnt, 1);
  update(1, 1, Hash(x - 1), -1, 1, cnt, 1);
  update(0, Hash(x + 1), cnt, -x, 1, cnt, 1);
  update(1, Hash(x + 1), cnt, 1, 1, cnt, 1);
}
ll getmid() { return *midit; }
ll getans(ll x) {
  ll p = Hash(x);

  ll k = query(1, p, 1, cnt, 1);

  ll a = query(0, p, 1, cnt, 1);

  return k * x + a;
}
void solve() {
  sort(bb + 1, bb + 1 + cnt);
  cnt = unique(bb + 1, bb + 1 + cnt) - bb - 1;
  ll sum = 0;
  for (ll i = 1; i <= n; i++) {
    ll op = rec[i][0];
    if (op == 1) {
      ll a = rec[i][1], b = rec[i][2];
      sum += b;
      updmid(a);
    } else {
      ll p = getmid();
      ll ans1 = getans(p);
      printf("%lld %lld\n", p, ans1 + sum);
    }
  }
}
int main() {
  //    freopen("c://duipai//data.txt","r",stdin);
  //    freopen("c://duipai//wa.txt","w",stdout);
  //   printf("%d",1^2^3^4^5^6^7^8^9^10^11^12^13^14^15);
  while (~scanf("%lld", &n)) {
    //        string str;
    //        cin>>str;
    //        cout<<str<<endl;
    init();
    for (ll i = 1; i <= n; i++) {
      ll op;
      scanf("%lld", &op);
      rec[i][0] = op;
      if (op == 1) {
        ll a, b;
        scanf("%lld %lld", &a, &b);
        rec[i][1] = a;
        rec[i][2] = b;
        bb[++cnt] = a;
        bb[++cnt] = a - 1;
        bb[++cnt] = a + 1;
      }
    }
    solve();
  }
}
/*
3
第-205
1 -3 3
1 0 8
2
*/