Problem p01057 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

namespace RBST {
// root = insert(root,k,hoge)
// ???????????????????????????????????¨????????§??????
struct T {
  T *l, *r;
  int size;
  char v;
  T() {
    l = r = NULL;
    size = 1;
  }
  T(T *a) { *this = *a; }
  T(char v) : v(v) {
    l = r = NULL;
    size = 1;
  }
};

int stack_size;
T *my_new(T *a) { return new T(a); }
T *my_new(int v) { return new T(v); }

class BST {
public:
  static int size(T *c) {
    if (!c)
      return 0;
    else
      return c->size;
  }
  static T *update(T *c) {
    if (!c)
      return 0;
    c->size = size(c->l) + size(c->r) + 1;
    return c;
  }
  static T *merge(T *a, T *b, int perm = 0) {
    if (!a)
      return b;
    if (!b)
      return a;
    if (rand() % (size(a) + size(b)) < size(a)) {
      T *R = perm ? my_new(a) : a;
      R->r = merge(a->r, b, perm);
      return update(R);
    } else {
      T *R = perm ? my_new(b) : b;
      R->l = merge(a, b->l, perm);
      return update(R);
    }
  }
  // [0,k) [k,n)
  static pair<T *, T *> split(T *c, int k, int perm = 0) {
    if (!c)
      return {0, 0};
    T *R = perm ? my_new(c) : c;
    if (k <= size(c->l)) {
      pair<T *, T *> s = split(c->l, k, perm);
      R->l = s.second;
      return make_pair(s.first, update(R));
    } else {
      pair<T *, T *> s = split(c->r, k - size(c->l) - 1, perm);
      R->r = s.first;
      return make_pair(update(R), s.second);
    }
  }
  static T *insert(T *c, int k, T *newnode, int perm = 0) {
    // cout << s.first << " " << s.second << endl;
    pair<T *, T *> s = split(c, k, perm);
    return merge(merge(s.first, newnode, perm), s.second, perm);
  }
  static T *erase(T *c, int k, int perm = 0) {
    pair<T *, T *> s1 = split(c, k, perm);
    pair<T *, T *> s2 = split(s1.second, 1, perm);
    return merge(s1.first, s2.second, perm);
  }
  // ?????????NULL
  static T *newTree() { return 0; }

  static void view(T *c) {
    if (!c)
      return;
    view(c->l);
    printf("%c", c->v);
    view(c->r);
  }
};
}; // namespace RBST

using namespace RBST;

char str[100010];
T *root[100010];
int main() {
  int N, Q;
  cin >> N >> Q;

  for (int i = 0; i < N; i++) {
    scanf("%s", str);
    int j = 0;
    while (str[j]) {
      root[i] = BST::insert(root[i], j, new T(str[j]));
      j++;
    }
  }
  for (int i = 0; i < Q; i++) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int a, b, c, d;
      scanf("%d%d%d%d", &a, &b, &c, &d);
      --a;
      --b;
      --c;
      --d;
      auto s1 = BST::split(root[a], b);
      auto s2 = BST::split(root[c], d);
      root[c] = BST::merge(s2.first, s1.second);
      root[a] = BST::merge(s1.first, s2.second);
    } else {
      int a, b;
      char c[2];
      scanf("%d%d%s", &a, &b, c);
      --a;
      --b;

      root[a] = BST::erase(root[a], b);
      // BST::view(root[a]);puts("");
      // cout << "ins" << " " << b << " " << c << endl;

      root[a] = BST::insert(root[a], b, new T(c[0]));
      // BST::view(root[a]);puts("");
    }
  }
  for (int i = 0; i < N; i++) {
    BST::view(root[i]);
    puts("");
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

namespace RBST {
// root = insert(root,k,hoge)
// ???????????????????????????????????¨????????§??????
struct T {
  T *l, *r;
  int size;
  char v;
  T() {
    l = r = NULL;
    size = 1;
  }
  T(T *a) { *this = *a; }
  T(char v) : v(v) {
    l = r = NULL;
    size = 1;
  }
};

int stack_size;
T *my_new(T *a) { return new T(a); }
T *my_new(int v) { return new T(v); }

class BST {
public:
  static int size(T *c) {
    if (!c)
      return 0;
    else
      return c->size;
  }
  static T *update(T *c) {
    if (!c)
      return 0;
    c->size = size(c->l) + size(c->r) + 1;
    return c;
  }
  static T *merge(T *a, T *b, int perm = 0) {
    if (!a)
      return b;
    if (!b)
      return a;
    if (rand() % (size(a) + size(b)) < size(a)) {
      T *R = perm ? my_new(a) : a;
      R->r = merge(a->r, b, perm);
      return update(R);
    } else {
      T *R = perm ? my_new(b) : b;
      R->l = merge(a, b->l, perm);
      return update(R);
    }
  }
  // [0,k) [k,n)
  static pair<T *, T *> split(T *c, int k, int perm = 0) {
    if (!c)
      return {0, 0};
    T *R = perm ? my_new(c) : c;
    if (k <= size(c->l)) {
      pair<T *, T *> s = split(c->l, k, perm);
      R->l = s.second;
      return make_pair(s.first, update(R));
    } else {
      pair<T *, T *> s = split(c->r, k - size(c->l) - 1, perm);
      R->r = s.first;
      return make_pair(update(R), s.second);
    }
  }
  static T *insert(T *c, int k, T *newnode, int perm = 0) {
    // cout << s.first << " " << s.second << endl;
    pair<T *, T *> s = split(c, k, perm);
    return merge(merge(s.first, newnode, perm), s.second, perm);
  }
  static T *erase(T *c, int k, int perm = 0) {
    pair<T *, T *> s1 = split(c, k, perm);
    pair<T *, T *> s2 = split(s1.second, 1, perm);
    return merge(s1.first, s2.second, perm);
  }
  // ?????????NULL
  static T *newTree() { return 0; }

  static void view(T *c) {
    if (!c)
      return;
    view(c->l);
    printf("%c", c->v);
    view(c->r);
  }
};
}; // namespace RBST

using namespace RBST;

char str[2000010];
T *root[100010];
int main() {
  int N, Q;
  cin >> N >> Q;

  for (int i = 0; i < N; i++) {
    scanf("%s", str);
    int j = 0;
    while (str[j]) {
      root[i] = BST::insert(root[i], j, new T(str[j]));
      j++;
    }
  }
  for (int i = 0; i < Q; i++) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      int a, b, c, d;
      scanf("%d%d%d%d", &a, &b, &c, &d);
      --a;
      --b;
      --c;
      --d;
      auto s1 = BST::split(root[a], b);
      auto s2 = BST::split(root[c], d);
      root[c] = BST::merge(s2.first, s1.second);
      root[a] = BST::merge(s1.first, s2.second);
    } else {
      int a, b;
      char c[2];
      scanf("%d%d%s", &a, &b, c);
      --a;
      --b;

      root[a] = BST::erase(root[a], b);
      // BST::view(root[a]);puts("");
      // cout << "ins" << " " << b << " " << c << endl;

      root[a] = BST::insert(root[a], b, new T(c[0]));
      // BST::view(root[a]);puts("");
    }
  }
  for (int i = 0; i < N; i++) {
    BST::view(root[i]);
    puts("");
  }
}