Problem p03040 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef pair<int, int> pii;

#define LOCAL 0
#define dbg(x) cout << #x << " is " << x << "\n"
#define gll(x) scanf("%d", &x)
#define gll2(x, y) scanf("%d%d", &x, &y)
#define gll3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define gllarr(arr, n) f(i, n) gll(arr[i]);
#define sz(x) ((int)x.size())
#define s(x) sort(x.begin(), x.end())
#define all(v) v.begin(), v.end()
#define rs(v)                                                                  \
  {                                                                            \
    s(v);                                                                      \
    r(v);                                                                      \
  }
#define r(v)                                                                   \
  { reverse(all(v)); }
#define pb push_back
#define f(i, n) for (int i = 0; i < n; i++)
#define fr(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)

const ll mod = (ll)1e9 + 7;
const ll inf = (ll)1e16;
const ld eps = 1e-12;
const ll N = (int)1e5 + 5;
const ll LOGN = 19;
const ld PI = 3.14159265358979323846;
inline ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m; }
inline ll add(ll a, ll b, ll m = mod) {
  a += b;
  if (a >= m)
    a -= m;
  if (a < 0)
    a += m;
  return a;
}
inline ll power(ll a, ll b, ll m = mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return (a % m);
  ll x = power(a, b / 2, m);
  x = mul(x, x, m);
  if (b % 2)
    x = mul(x, a, m);
  return x;
}

map<ll, ll> mapper, first, last;
vector<tuple<int, ll, ll>> queries;

/* Segtree - point update, range query
 * a - Initializer array. Building is done based on array a[].
 * Each function assumes 0-based indexing.
 */
struct node {
  ll val;
  ll dist;

  node() {
    val = 0;
    dist = 0;
  }

  void init(ll x) {
    val = x;
    dist = 1;
  }

  void merge(node &l, node &r) {
    val = l.val + r.val;
    dist = l.dist + r.dist;
  }

  void no_use() {
    // Fill up the values that do not affect the answer.
    val = 0;
    dist = 0;
  }
};

node segtree[N << 2];

// Point update - Here : change value at index = idx to val -> a[idx] = val.
// In case of tight TL, create idx and val as global variables.
void update(int num, int start, int end, int idx, int val) {
  if (start == end) {
    segtree[num].init(val);
    return;
  }
  int lc = num + num + 1, rc = lc + 1;
  int mid = (start + end) >> 1;
  if (start <= idx && idx <= mid)
    update(lc, start, mid, idx, val);
  else
    update(rc, mid + 1, end, idx, val);
  segtree[num].merge(segtree[lc], segtree[rc]);
}

// Range Query - Returns the answer of query in O(logn) time.
node query(int num, int start, int end, int qs, int qe) {
  if (start > qe || end < qs) {
    node junk;
    junk.no_use();
    return junk;
  }
  if (start >= qs && end <= qe) {
    return segtree[num];
  }
  int lc = num + num + 1, rc = lc + 1;
  int mid = (start + end) >> 1;
  node lf = query(lc, start, mid, qs, qe);
  node rg = query(rc, mid + 1, end, qs, qe);
  node ret;
  ret.merge(lf, rg);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  if (LOCAL) {
    freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\input.txt", "r",
            stdin);
    freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\output.txt", "w",
            stdout);
  }
  int q;
  cin >> q;
  ordered_set<pair<ll, int>> S;
  vector<ll> vec;
  f(i, q) {
    int ty;
    cin >> ty;
    ll a = 0, b = 0;
    if (ty == 1)
      cin >> a >> b, vec.pb(a);
    queries.pb(make_tuple(ty, a, b));
  }
  s(vec);
  int ptr = 0;
  f(i, sz(vec)) {
    int to = vec[i];
    if (first.find(to) == first.end())
      first[to] = ptr;
    last[to] = ptr;
    if (mapper.find(to) == mapper.end())
      mapper[to] = ptr;
    ptr++;
  }
  ll sum = 0, terms = 0;
  f(tt, q) {
    int ty = get<0>(queries[tt]);
    if (ty == 1) {
      ll a, b;
      a = get<1>(queries[tt]);
      b = get<2>(queries[tt]);
      sum += b;
      terms++;
      S.insert({a, terms});
      update(0, 0, sz(vec) - 1, mapper[a], a);
      mapper[a]++;
    } else {
      ll to = terms / 2;
      if (terms % 2 == 0)
        to--;
      pair<ll, int> chk = *(S.find_by_order(to));
      ll num = chk.first;
      ll ub = first[num] - 1;
      ll lb = last[num] + 1;
      node ff = query(0, 0, sz(vec) - 1, 0, ub);
      node ss = query(0, 0, sz(vec) - 1, lb, sz(vec) - 1);
      ll cans = ss.val - ss.dist * num + ff.dist * num - ff.val;
      cout << num << ' ' << cans + sum << "\n";
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef pair<int, int> pii;

#define LOCAL 0
#define dbg(x) cout << #x << " is " << x << "\n"
#define gll(x) scanf("%d", &x)
#define gll2(x, y) scanf("%d%d", &x, &y)
#define gll3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define gllarr(arr, n) f(i, n) gll(arr[i]);
#define sz(x) ((int)x.size())
#define s(x) sort(x.begin(), x.end())
#define all(v) v.begin(), v.end()
#define rs(v)                                                                  \
  {                                                                            \
    s(v);                                                                      \
    r(v);                                                                      \
  }
#define r(v)                                                                   \
  { reverse(all(v)); }
#define pb push_back
#define f(i, n) for (int i = 0; i < n; i++)
#define fr(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)

const ll mod = (ll)1e9 + 7;
const ll inf = (ll)1e16;
const ld eps = 1e-12;
const ll N = (int)2e5 + 5;
const ll LOGN = 19;
const ld PI = 3.14159265358979323846;
inline ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m; }
inline ll add(ll a, ll b, ll m = mod) {
  a += b;
  if (a >= m)
    a -= m;
  if (a < 0)
    a += m;
  return a;
}
inline ll power(ll a, ll b, ll m = mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return (a % m);
  ll x = power(a, b / 2, m);
  x = mul(x, x, m);
  if (b % 2)
    x = mul(x, a, m);
  return x;
}

map<ll, ll> mapper, first, last;
vector<tuple<int, ll, ll>> queries;

/* Segtree - point update, range query
 * a - Initializer array. Building is done based on array a[].
 * Each function assumes 0-based indexing.
 */
struct node {
  ll val;
  ll dist;

  node() {
    val = 0;
    dist = 0;
  }

  void init(ll x) {
    val = x;
    dist = 1;
  }

  void merge(node &l, node &r) {
    val = l.val + r.val;
    dist = l.dist + r.dist;
  }

  void no_use() {
    // Fill up the values that do not affect the answer.
    val = 0;
    dist = 0;
  }
};

node segtree[N << 2];

// Point update - Here : change value at index = idx to val -> a[idx] = val.
// In case of tight TL, create idx and val as global variables.
void update(int num, int start, int end, int idx, int val) {
  if (start == end) {
    segtree[num].init(val);
    return;
  }
  int lc = num + num + 1, rc = lc + 1;
  int mid = (start + end) >> 1;
  if (start <= idx && idx <= mid)
    update(lc, start, mid, idx, val);
  else
    update(rc, mid + 1, end, idx, val);
  segtree[num].merge(segtree[lc], segtree[rc]);
}

// Range Query - Returns the answer of query in O(logn) time.
node query(int num, int start, int end, int qs, int qe) {
  if (start > qe || end < qs) {
    node junk;
    junk.no_use();
    return junk;
  }
  if (start >= qs && end <= qe) {
    return segtree[num];
  }
  int lc = num + num + 1, rc = lc + 1;
  int mid = (start + end) >> 1;
  node lf = query(lc, start, mid, qs, qe);
  node rg = query(rc, mid + 1, end, qs, qe);
  node ret;
  ret.merge(lf, rg);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  if (LOCAL) {
    freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\input.txt", "r",
            stdin);
    freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\output.txt", "w",
            stdout);
  }
  int q;
  cin >> q;
  ordered_set<pair<ll, int>> S;
  vector<ll> vec;
  f(i, q) {
    int ty;
    cin >> ty;
    ll a = 0, b = 0;
    if (ty == 1)
      cin >> a >> b, vec.pb(a);
    queries.pb(make_tuple(ty, a, b));
  }
  s(vec);
  int ptr = 0;
  f(i, sz(vec)) {
    int to = vec[i];
    if (first.find(to) == first.end())
      first[to] = ptr;
    last[to] = ptr;
    if (mapper.find(to) == mapper.end())
      mapper[to] = ptr;
    ptr++;
  }
  ll sum = 0, terms = 0;
  f(tt, q) {
    int ty = get<0>(queries[tt]);
    if (ty == 1) {
      ll a, b;
      a = get<1>(queries[tt]);
      b = get<2>(queries[tt]);
      sum += b;
      terms++;
      S.insert({a, terms});
      update(0, 0, sz(vec) - 1, mapper[a], a);
      mapper[a]++;
    } else {
      ll to = terms / 2;
      if (terms % 2 == 0)
        to--;
      pair<ll, int> chk = *(S.find_by_order(to));
      ll num = chk.first;
      ll ub = first[num] - 1;
      ll lb = last[num] + 1;
      node ff = query(0, 0, sz(vec) - 1, 0, ub);
      node ss = query(0, 0, sz(vec) - 1, lb, sz(vec) - 1);
      ll cans = ss.val - ss.dist * num + ff.dist * num - ff.val;
      cout << num << ' ' << cans + sum << "\n";
    }
  }
  return 0;
}