Problem p03040 (C++)

Buggy submission (status: Runtime Error):
/*input
4
1 4 2
2
1 1 -8
2
*/
#pragma GCC optimize("Ofast")
#pragma GCC target(                                                            \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 gen;
struct node;
using pnode = node *;
int sz(pnode a);
ll dd(pnode a);
struct node {
  node *l = NULL, *r = NULL;
  int w;
  int val;
  int dyd = 1;
  ll dyd1;
  node(int val) : val(val) {
    w = gen();
    dyd1 = val;
  }
  void fixS() {
    dyd = 1 + sz(l) + sz(r);
    dyd1 = val + dd(l) + dd(r);
  }
};
int sz(pnode a) {
  if (a)
    return a->dyd;
  return 0;
}
ll dd(pnode a) {
  if (a)
    return a->dyd1;
  return 0;
}
pnode merge(pnode a, pnode b) {
  if (a == NULL)
    return b;
  if (b == NULL)
    return a;
  if (a->w < b->w) {
    a->r = merge(a->r, b);
    a->fixS();
    return a;
  } else {
    b->l = merge(a, b->l);
    b->fixS();
    return b;
  }
}
pnode merge(pnode a, pnode b, pnode c) { return merge(merge(a, b), c); }
void split(pnode a, int v, pnode &x, pnode &y) { // (x <= v), (y > v)
  if (a == NULL) {
    x = NULL;
    y = NULL;
    return;
  }
  if (a->val <= v) {
    split(a->r, v, a->r, y);
    x = a;
  } else {
    split(a->l, v, x, a->l);
    y = a;
  }
  a->fixS();
}
void split_(pnode a, int k, pnode &x, pnode &y) { // (x->sz = min(a->sz, k))
  if (a == NULL) {
    x = NULL;
    y = NULL;
  } else {
    if (k <= 0) {
      x = NULL;
      y = a;
    } else if (a->dyd < k) {
      x = a;
      y = NULL;
    } else {
      if (sz(a->l) + 1 <= k) {
        split_(a->r, k - 1 - sz(a->l), a->r, y);
        x = a;
      } else {
        split_(a->l, k, x, a->l);
        y = a;
      }
      a->fixS();
    }
  }
}
void print(pnode a, char g = '\n') {
  if (a == NULL) {
    if (g != ' ')
      cout << g;
    return;
  }
  print(a->l, ' ');
  cout << a->val << ' ';
  print(a->r, g);
}

int main() {
  ios_base::sync_with_stdio(false);
  ll sumb = 0;
  pnode x = NULL;
  int Q;
  cin >> Q;
  int kiek = 0;
  while (Q--) {
    int t;
    cin >> t;
    if (t == 1) {
      kiek++;
      ll a, b;
      cin >> a >> b;
      sumb += b;
      pnode l, r;
      split(x, a, l, r);
      x = merge(l, new node(a), r);
    } else {
      pnode a, b, c;
      ll k = (kiek - 1) / 2;
      split_(x, k, a, b);
      split_(b, 1, b, c);
      ll X = b->val;
      ll Y = sumb;
      Y += dd(c);
      Y -= dd(a);
      Y += X * (sz(a) - sz(c));
      cout << X << " " << Y << "\n";
      x = merge(a, b, c);
    }
    // cout << "x: ";
    // print(x);
  }
}

Fixed submission:
/*input
4
1 4 2
2
1 1 -8
2
*/
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 gen;
struct node;
using pnode = node *;
int sz(pnode a);
ll dd(pnode a);
struct node {
  node *l = NULL, *r = NULL;
  int w;
  int val;
  int dyd = 1;
  ll dyd1;
  node(int val) : val(val) {
    w = gen();
    dyd1 = val;
  }
  void fixS() {
    dyd = 1 + sz(l) + sz(r);
    dyd1 = val + dd(l) + dd(r);
  }
};
int sz(pnode a) {
  if (a)
    return a->dyd;
  return 0;
}
ll dd(pnode a) {
  if (a)
    return a->dyd1;
  return 0;
}
pnode merge(pnode a, pnode b) {
  if (a == NULL)
    return b;
  if (b == NULL)
    return a;
  if (a->w < b->w) {
    a->r = merge(a->r, b);
    a->fixS();
    return a;
  } else {
    b->l = merge(a, b->l);
    b->fixS();
    return b;
  }
}
pnode merge(pnode a, pnode b, pnode c) { return merge(merge(a, b), c); }
void split(pnode a, int v, pnode &x, pnode &y) { // (x <= v), (y > v)
  if (a == NULL) {
    x = NULL;
    y = NULL;
    return;
  }
  if (a->val <= v) {
    split(a->r, v, a->r, y);
    x = a;
  } else {
    split(a->l, v, x, a->l);
    y = a;
  }
  a->fixS();
}
void split_(pnode a, int k, pnode &x, pnode &y) { // (x->sz = min(a->sz, k))
  if (a == NULL) {
    x = NULL;
    y = NULL;
  } else {
    if (k <= 0) {
      x = NULL;
      y = a;
    } else if (a->dyd < k) {
      x = a;
      y = NULL;
    } else {
      if (sz(a->l) + 1 <= k) {
        split_(a->r, k - 1 - sz(a->l), a->r, y);
        x = a;
      } else {
        split_(a->l, k, x, a->l);
        y = a;
      }
      a->fixS();
    }
  }
}
void print(pnode a, char g = '\n') {
  if (a == NULL) {
    if (g != ' ')
      cout << g;
    return;
  }
  print(a->l, ' ');
  cout << a->val << ' ';
  print(a->r, g);
}

int main() {
  ios_base::sync_with_stdio(false);
  ll sumb = 0;
  pnode x = NULL;
  int Q;
  cin >> Q;
  int kiek = 0;
  while (Q--) {
    int t;
    cin >> t;
    if (t == 1) {
      kiek++;
      ll a, b;
      cin >> a >> b;
      sumb += b;
      pnode l, r;
      split(x, a, l, r);
      x = merge(l, new node(a), r);
    } else {
      pnode a, b, c;
      ll k = (kiek - 1) / 2;
      split_(x, k, a, b);
      split_(b, 1, b, c);
      ll X = b->val;
      ll Y = sumb;
      Y += dd(c);
      Y -= dd(a);
      Y += X * (sz(a) - sz(c));
      cout << X << " " << Y << "\n";
      x = merge(a, b, c);
    }
    // cout << "x: ";
    // print(x);
  }
}