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

#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <utility>
#include <vector>
namespace lc {
inline unsigned int xorshift128() {
  static uint32_t x = 192479812;
  static uint32_t y = 784892731;
  static uint32_t z = 427398108;
  static uint32_t w = 48382934;
  const uint32_t t = x ^ (x << 11);
  x = y;
  y = z;
  z = w;
  w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  return w;
}
} // namespace lc
namespace lc {
template <class T> struct MinTreapListTraits {
  typedef T value_type;
  value_type operator()(const value_type &a, const value_type &b) const {
    return std::min(a, b);
  }
};
template <class Traits> class TreapList {
public:
  typedef typename Traits::value_type value_type;

private:
  struct node_type {
    value_type value;
    value_type cache;
    unsigned int priority;
    size_t size;
    node_type *children[2];
  };
  Traits m_traits;
  node_type *m_root;
  void initialize_node(node_type *p, const value_type &v) {
    p->value = p->cache = v;
    p->priority = xorshift128();
    p->size = 1;
    p->children[0] = p->children[1] = nullptr;
  }
  void destroy(node_type *p) const {
    if (p == nullptr) {
      return;
    }
    destroy(p->children[0]);
    destroy(p->children[1]);
    delete p;
  }
  size_t size(const node_type *p) const {
    if (p == nullptr) {
      return 0;
    }
    return p->size;
  }
  node_type *refresh(node_type *p) const {
    if (p == nullptr) {
      return nullptr;
    }
    p->cache = p->value;
    if (p->children[0] != nullptr) {
      p->cache = m_traits(p->children[0]->cache, p->cache);
    }
    if (p->children[1] != nullptr) {
      p->cache = m_traits(p->cache, p->children[1]->cache);
    }
    p->size = 1 + size(p->children[0]) + size(p->children[1]);
    return p;
  }
  node_type *merge(node_type *l, node_type *r) const {
    if (l == nullptr) {
      return r;
    }
    if (r == nullptr) {
      return l;
    }
    if (l->priority > r->priority) {
      l->children[1] = merge(l->children[1], r);
      return refresh(l);
    } else {
      r->children[0] = merge(l, r->children[0]);
      return refresh(r);
    }
  }
  std::pair<node_type *, node_type *> split(node_type *t, size_t k) const {
    if (t == nullptr) {
      return std::make_pair(nullptr, nullptr);
    }
    if (k <= size(t->children[0])) {
      const auto s = split(t->children[0], k);
      t->children[0] = s.second;
      return std::make_pair(s.first, refresh(t));
    } else {
      const auto s = split(t->children[1], k - size(t->children[0]) - 1);
      t->children[1] = s.first;
      return std::make_pair(refresh(t), s.second);
    }
  }

public:
  explicit TreapList(const Traits &traits = Traits())
      : m_traits(traits), m_root(nullptr) {}
  TreapList(const TreapList<Traits> &x)
      : m_traits(x.m_traits), m_root(nullptr) {
    m_root = clone(x.m_root);
  }
  TreapList(TreapList<Traits> &&x) : m_traits(x.m_traits), m_root(x.m_root) {
    x.m_root = nullptr;
  }
  ~TreapList() { destroy(m_root); }
  void insert(size_t k, const value_type &x) {
    const auto p = split(m_root, k);
    node_type *node = new node_type();
    initialize_node(node, x);
    m_root = merge(merge(p.first, node), p.second);
  }
  void erase(size_t k) {
    const auto p = split(m_root, k);
    const auto q = split(p.second, 1);
    delete q.first;
    m_root = merge(p.first, q.second);
  }
  void update(size_t k, const value_type &x) {
    erase(k);
    insert(k, x);
  }
  value_type query(size_t l, size_t r) {
    const auto p = split(m_root, l);
    const auto q = split(p.second, r - l);
    const value_type result = q.first->cache;
    m_root = merge(merge(p.first, q.first), q.second);
    return result;
  }
  void merge(TreapList<Traits> &&t) {
    m_root = merge(m_root, t.m_root);
    t.m_root = nullptr;
  }
  TreapList<Traits> split(size_t k) {
    TreapList<Traits> result(m_traits);
    const auto p = split(m_root, k);
    m_root = p.first;
    result.m_root = p.second;
    return result;
  }
};
} // namespace lc
using namespace std;
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  lc::TreapList<lc::MinTreapListTraits<int>> b;
  for (int i = 0; i < n; ++i) {
    int ai;
    scanf("%d", &ai);
    b.insert(i, ai);
  }
  while (q--) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    if (x == 0) {
      lc::TreapList<lc::MinTreapListTraits<int>> c = b.split(y);
      lc::TreapList<lc::MinTreapListTraits<int>> d = c.split(z - y);
      lc::TreapList<lc::MinTreapListTraits<int>> e = d.split(1);
      b.merge(std::move(d));
      b.merge(std::move(c));
      b.merge(std::move(e));
    } else if (x == 1) {
      printf("%d\\n", b.query(y, z + 1));
    } else {
      b.update(y, z);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <random>
#include <utility>
#include <vector>
namespace lc {
template <class T> struct MinTreapListTraits {
  typedef T value_type;
  value_type operator()(const value_type &a, const value_type &b) const {
    return std::min(a, b);
  }
};
template <class Traits> class TreapList {
public:
  typedef typename Traits::value_type value_type;

private:
  struct node_type {
    value_type value;
    value_type cache;
    unsigned int priority;
    size_t size;
    node_type *children[2];
  };
  Traits m_traits;
  std::default_random_engine m_random;
  node_type *m_root;
  void initialize_rng() {
    std::random_device seed_generator;
    m_random.seed(seed_generator());
  }
  void initialize_node(node_type *p, const value_type &v) {
    p->value = p->cache = v;
    p->priority = m_random();
    p->size = 1;
    p->children[0] = p->children[1] = nullptr;
  }
  void destroy(node_type *p) const {
    if (p == nullptr) {
      return;
    }
    destroy(p->children[0]);
    destroy(p->children[1]);
    delete p;
  }
  size_t size(const node_type *p) const {
    if (p == nullptr) {
      return 0;
    }
    return p->size;
  }
  node_type *refresh(node_type *p) const {
    if (p == nullptr) {
      return nullptr;
    }
    p->cache = p->value;
    if (p->children[0] != nullptr) {
      p->cache = m_traits(p->children[0]->cache, p->cache);
    }
    if (p->children[1] != nullptr) {
      p->cache = m_traits(p->cache, p->children[1]->cache);
    }
    p->size = 1 + size(p->children[0]) + size(p->children[1]);
    return p;
  }
  node_type *merge(node_type *l, node_type *r) const {
    if (l == nullptr) {
      return r;
    }
    if (r == nullptr) {
      return l;
    }
    if (l->priority > r->priority) {
      l->children[1] = merge(l->children[1], r);
      return refresh(l);
    } else {
      r->children[0] = merge(l, r->children[0]);
      return refresh(r);
    }
  }
  std::pair<node_type *, node_type *> split(node_type *t, size_t k) const {
    if (t == nullptr) {
      return std::make_pair(nullptr, nullptr);
    }
    if (k <= size(t->children[0])) {
      const auto s = split(t->children[0], k);
      t->children[0] = s.second;
      return std::make_pair(s.first, refresh(t));
    } else {
      const auto s = split(t->children[1], k - size(t->children[0]) - 1);
      t->children[1] = s.first;
      return std::make_pair(refresh(t), s.second);
    }
  }

public:
  explicit TreapList(const Traits &traits = Traits())
      : m_traits(traits), m_random(), m_root(nullptr) {
    initialize_rng();
  }
  TreapList(const TreapList<Traits> &x)
      : m_traits(x.m_traits), m_random(), m_root(nullptr) {
    initialize_rng();
    m_root = clone(x.m_root);
  }
  TreapList(TreapList<Traits> &&x)
      : m_traits(x.m_traits), m_random(x.m_random), m_root(x.m_root) {
    x.m_root = nullptr;
  }
  ~TreapList() { destroy(m_root); }
  void insert(size_t k, const value_type &x) {
    const auto p = split(m_root, k);
    node_type *node = (node_type *)malloc(sizeof(node_type)); // new
                                                              // node_type();
    initialize_node(node, x);
    m_root = merge(merge(p.first, node), p.second);
  }
  void erase(size_t k) {
    const auto p = split(m_root, k);
    const auto q = split(p.second, 1);
    free(q.first);
    m_root = merge(p.first, q.second);
  }
  void update(size_t k, const value_type &x) {
    erase(k);
    insert(k, x);
  }
  value_type query(size_t l, size_t r) {
    const auto p = split(m_root, l);
    const auto q = split(p.second, r - l);
    const value_type result = q.first->cache;
    m_root = merge(merge(p.first, q.first), q.second);
    return result;
  }
  void merge(TreapList<Traits> &&t) {
    m_root = merge(m_root, t.m_root);
    t.m_root = nullptr;
  }
  TreapList<Traits> split(size_t k) {
    TreapList<Traits> result(m_traits);
    const auto p = split(m_root, k);
    m_root = p.first;
    result.m_root = p.second;
    return result;
  }
};
} // namespace lc
using namespace std;
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  lc::TreapList<lc::MinTreapListTraits<int>> b;
  for (int i = 0; i < n; ++i) {
    int ai;
    scanf("%d", &ai);
    b.insert(i, ai);
  }
  while (q--) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    if (x == 0) {
      lc::TreapList<lc::MinTreapListTraits<int>> c = b.split(y);
      lc::TreapList<lc::MinTreapListTraits<int>> d = c.split(z - y);
      lc::TreapList<lc::MinTreapListTraits<int>> e = d.split(1);
      b.merge(std::move(d));
      b.merge(std::move(c));
      b.merge(std::move(e));
    } else if (x == 1) {
      printf("%d\\n", b.query(y, z + 1));
    } else {
      b.update(y, z);
    }
  }
  return 0;
}