Problem p00998 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int xor128() {
  static int x = 123456789;
  static int y = 362436069;
  static int z = 521288629;
  static int w = 88675123;
  int t;

  t = x ^ (x << 11);
  x = y;
  y = z;
  z = w;
  return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}

template <typename T, typename E> struct RBST {
  typedef function<T(T, T)> F;
  typedef function<T(T, E)> G;
  typedef function<E(E, E)> H;
  typedef function<E(E, size_t)> P;

  F f;
  G g;
  H h;
  P p;
  T d1;
  E d0;

  struct Node {
    Node *l, *r;
    size_t cnt;
    T val, dat;
    E laz;
    Node() : cnt(0) { l = r = nullptr; }
    Node(T val, E laz) : cnt(1), val(val), dat(val), laz(laz) {
      l = r = nullptr;
    }
  };

  const size_t LIM = 1e6;
  vector<Node> pool;
  size_t ptr;

  RBST(F f, G g, H h, P p, T d1, E d0)
      : f(f), g(g), h(h), p(p), d1(d1), d0(d0), pool(LIM), ptr(0) {}

  Node *build(size_t l, size_t r, vector<T> &v) {
    if (l + 1 == r)
      return create(v[l]);
    size_t m = (l + r) >> 1;
    return merge(build(l, m, v), build(m, r, v));
  }

  Node *build(vector<T> &v) { return build(0, v.size(), v); }

  Node *create() { return &pool[ptr++]; }

  Node *create(T v) { return &(pool[ptr++] = Node(v, d0)); }

  size_t count(const Node *a) {
    if (a == nullptr)
      return 0;
    return a->cnt;
  }

  Node *insert(Node *a, size_t pos, T v) {
    Node *b = create(v);
    auto s = split(a, pos);
    return a = merge(merge(s.first, b), s.second);
  }

  Node *erase(Node *a, size_t pos) {
    auto s = split(a, pos);
    auto t = split(s.second, 1);
    return merge(s.first, t.second);
  }

  T query(const Node *a) {
    if (a == nullptr)
      return d1;
    // cout<<a<<":"<<(a->val)<<" "<<(a->dat)<<" "<<(a->laz)<<endl;
    return a->dat;
  }

  Node *update(Node *a) {
    if (a == nullptr)
      return a;
    Node *l = a->l, *r = a->r;
    if (a->laz != d0) {
      a->val = g(a->val, p(a->laz, 1));
      if (l != nullptr) {
        l->laz = h(l->laz, a->laz);
        l->val = g(l->val, p(l->laz, 1));
        l->dat = g(l->dat, p(l->laz, count(l) - 1));
        l->dat = f(l->val, l->dat);
      }
      if (r != nullptr) {
        r->laz = h(r->laz, a->laz);
        r->val = g(r->val, p(r->laz, 1));
        r->dat = g(r->dat, p(r->laz, count(r) - 1));
        r->dat = f(r->val, r->dat);
      }
      a->laz = d0;
    }
    a->cnt = count(l) + count(r) + 1;
    a->dat = f(a->val, f(query(l), query(r)));
    return a;
  }

  T query(Node *&a, size_t l, size_t r) {
    auto s = split(a, l);
    auto t = split(s.second, r - l);
    auto u = t.first;
    T res = query(u);
    a = merge(s.first, merge(t.first, t.second));
    return res;
  }

  void update(Node *&a, size_t l, size_t r, E v) {
    auto s = split(a, l);
    auto t = split(s.second, r - l);
    auto u = t.first;
    u->laz = h(u->laz, v);
    update(u);
    a = merge(merge(s.first, u), t.second);
  }

  void dump(Node *a, typename vector<T>::iterator it) {
    if (!count(a))
      return;
    update(a);
    dump(a->l, it);
    *(it + count(a->l)) = a->val;
    dump(a->r, it + count(a->l) + 1);
  }

  void dump(Node *a, vector<T> &v) {
    v.resize(count(a));
    dump(a, v.begin());
  }

  Node *merge(Node *a, Node *b) {
    if (a == nullptr)
      return b;
    if (b == nullptr)
      return a;
    if (xor128() % (count(a) + count(b)) < count(a)) {
      a = update(a);
      a->r = merge(a->r, b);
      return update(a);
    }
    b = update(b);
    b->l = merge(a, b->l);
    return update(b);
  }

  pair<Node *, Node *> split(Node *a, size_t k) {
    if (a == nullptr)
      return make_pair(a, a);
    update(a);
    if (k <= count(a->l)) {
      auto s = split(a->l, k);
      a->l = s.second;
      return make_pair(s.first, update(a));
    }
    auto s = split(a->r, k - (count(a->l) + 1));
    a->r = s.first;
    return make_pair(update(a), s.second);
  }
};

struct FastIO {
  FastIO() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fastio_beet;

// INSERT ABOVE HERE
signed main() {
  int n, q;
  cin >> n >> q;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

  RBST<int, int>::F f = [](int a, int b) { return min(a, b); };
  RBST<int, int>::G g = [](int a, int b) { return b < 0 ? a : b; };
  RBST<int, int>::H h = [](int a, int b) { return b < 0 ? a : b; };
  RBST<int, int>::P p = [](int a, size_t b) {
    ++b;
    return a;
  };
  const int INF = 1e9;
  RBST<int, int> rbst(f, g, h, p, INF, -1);

  auto root = rbst.build(v);

  for (int i = 0; i < q; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (x == 0) {
      int v = rbst.query(root, z, z + 1);
      root = rbst.erase(root, z);
      root = rbst.insert(root, y, v);
    }
    if (x == 1) {
      cout << rbst.query(root, y, z + 1) << endl;
    }
    if (x == 2) {
      rbst.update(root, y, y + 1, z);
    }
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int xor128() {
  static int x = 123456789;
  static int y = 362436069;
  static int z = 521288629;
  static int w = 88675123;
  int t;

  t = x ^ (x << 11);
  x = y;
  y = z;
  z = w;
  return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}

template <typename T, typename E> struct RBST {
  typedef function<T(T, T)> F;
  typedef function<T(T, E)> G;
  typedef function<E(E, E)> H;
  typedef function<E(E, size_t)> P;

  F f;
  G g;
  H h;
  P p;
  T d1;
  E d0;

  struct Node {
    Node *l, *r;
    size_t cnt;
    T val, dat;
    E laz;
    Node() : cnt(0) { l = r = nullptr; }
    Node(T val, E laz) : cnt(1), val(val), dat(val), laz(laz) {
      l = r = nullptr;
    }
  };

  const size_t LIM = 1e7;
  vector<Node> pool;
  size_t ptr;

  RBST(F f, G g, H h, P p, T d1, E d0)
      : f(f), g(g), h(h), p(p), d1(d1), d0(d0), pool(LIM), ptr(0) {}

  Node *build(size_t l, size_t r, vector<T> &v) {
    if (l + 1 == r)
      return create(v[l]);
    size_t m = (l + r) >> 1;
    return merge(build(l, m, v), build(m, r, v));
  }

  Node *build(vector<T> &v) { return build(0, v.size(), v); }

  Node *create() { return &pool[ptr++]; }

  Node *create(T v) { return &(pool[ptr++] = Node(v, d0)); }

  size_t count(const Node *a) {
    if (a == nullptr)
      return 0;
    return a->cnt;
  }

  Node *insert(Node *a, size_t pos, T v) {
    Node *b = create(v);
    auto s = split(a, pos);
    return a = merge(merge(s.first, b), s.second);
  }

  Node *erase(Node *a, size_t pos) {
    auto s = split(a, pos);
    auto t = split(s.second, 1);
    return merge(s.first, t.second);
  }

  T query(const Node *a) {
    if (a == nullptr)
      return d1;
    // cout<<a<<":"<<(a->val)<<" "<<(a->dat)<<" "<<(a->laz)<<endl;
    return a->dat;
  }

  Node *update(Node *a) {
    if (a == nullptr)
      return a;
    Node *l = a->l, *r = a->r;
    if (a->laz != d0) {
      a->val = g(a->val, p(a->laz, 1));
      if (l != nullptr) {
        l->laz = h(l->laz, a->laz);
        l->val = g(l->val, p(l->laz, 1));
        l->dat = g(l->dat, p(l->laz, count(l) - 1));
        l->dat = f(l->val, l->dat);
      }
      if (r != nullptr) {
        r->laz = h(r->laz, a->laz);
        r->val = g(r->val, p(r->laz, 1));
        r->dat = g(r->dat, p(r->laz, count(r) - 1));
        r->dat = f(r->val, r->dat);
      }
      a->laz = d0;
    }
    a->cnt = count(l) + count(r) + 1;
    a->dat = f(a->val, f(query(l), query(r)));
    return a;
  }

  T query(Node *&a, size_t l, size_t r) {
    auto s = split(a, l);
    auto t = split(s.second, r - l);
    auto u = t.first;
    T res = query(u);
    a = merge(s.first, merge(t.first, t.second));
    return res;
  }

  void update(Node *&a, size_t l, size_t r, E v) {
    auto s = split(a, l);
    auto t = split(s.second, r - l);
    auto u = t.first;
    u->laz = h(u->laz, v);
    a = merge(merge(s.first, u), t.second);
  }

  void dump(Node *a, typename vector<T>::iterator it) {
    if (!count(a))
      return;
    update(a);
    dump(a->l, it);
    *(it + count(a->l)) = a->val;
    dump(a->r, it + count(a->l) + 1);
  }

  void dump(Node *a, vector<T> &v) {
    v.resize(count(a));
    dump(a, v.begin());
  }

  Node *merge(Node *a, Node *b) {
    if (a == nullptr)
      return b;
    if (b == nullptr)
      return a;
    if (xor128() % (count(a) + count(b)) < count(a)) {
      a = update(a);
      a->r = merge(a->r, b);
      return update(a);
    }
    b = update(b);
    b->l = merge(a, b->l);
    return update(b);
  }

  pair<Node *, Node *> split(Node *a, size_t k) {
    if (a == nullptr)
      return make_pair(a, a);
    update(a);
    if (k <= count(a->l)) {
      auto s = split(a->l, k);
      a->l = s.second;
      return make_pair(s.first, update(a));
    }
    auto s = split(a->r, k - (count(a->l) + 1));
    a->r = s.first;
    return make_pair(update(a), s.second);
  }
};

struct FastIO {
  FastIO() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fastio_beet;

// INSERT ABOVE HERE
signed main() {
  int n, q;
  cin >> n >> q;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

  RBST<int, int>::F f = [](int a, int b) { return min(a, b); };
  RBST<int, int>::G g = [](int a, int b) { return b < 0 ? a : b; };
  RBST<int, int>::H h = [](int a, int b) { return b < 0 ? a : b; };
  RBST<int, int>::P p = [](int a, size_t b) {
    ++b;
    return a;
  };
  const int INF = 1e9;
  RBST<int, int> rbst(f, g, h, p, INF, -1);

  auto root = rbst.build(v);

  if (0) {
    // for(int i=0;i<n;i++) cout<<rbst.query(root,i,i+1)<<" ";
    cout << endl;
    for (int i = 0; i < n; i++) {
      cout << i << ":" << endl;
      int v = rbst.query(root, i, i + 1);
      cout << "v=" << v << endl << endl;
    }
    cout << endl;
  }

  for (int i = 0; i < q; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (x == 0) {
      int v = rbst.query(root, z, z + 1);
      root = rbst.erase(root, z);
      root = rbst.insert(root, y, v);
    }
    if (x == 1) {
      cout << rbst.query(root, y, z + 1) << endl;
    }
    if (x == 2) {
      rbst.update(root, y, y + 1, z);
    }

    if (0) {
      v.clear();
      rbst.dump(root, v);
      for (int x : v)
        cout << x << " ";
      cout << endl;
    }
  }

  return 0;
}