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

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

class Bundle {
public:
  Bundle(int t, int b) : top_(t), bottom_(b) {}
  int top() const { return top_; }
  int bottom() const { return bottom_; }
  int size() const { return bottom() - top() + 1; }
  int count(int r) const {
    if (r < top())
      return 0;
    if (bottom() <= r)
      return size();
    return r - top() + 1;
  }
  Bundle split(int i) {
    Bundle res(i, bottom());
    bottom_ = i - 1;
    return res;
  }

private:
  int top_, bottom_;
};

int main() {
  int n;
  while (cin >> n, n) {
    int m;
    cin >> m;
    int p, q, r;
    cin >> p >> q >> r;

    vector<Bundle> bundle;
    bundle.emplace_back(1, n);

    for (int k = 0; k < m; ++k) {
      int x, y;
      cin >> x >> y;
      int cnt = 0;
      for (int i = 0; i < bundle.size(); ++i) {
        cnt += bundle[i].size();
        if (cnt < x)
          continue;
        if (cnt != x)
          bundle.insert(
              begin(bundle) + i + 1,
              bundle[i].split(bundle[i].top() + bundle[i].size() - (cnt - x)));
        cnt = x;
        ++i;
        for (int j = i; j < bundle.size(); ++j) {
          cnt += bundle[j].size();
          if (cnt < y)
            continue;
          if (cnt != y)
            bundle.insert(begin(bundle) + j + 1,
                          bundle[j].split(bundle[j].top() + bundle[j].size() -
                                          (cnt - y)));
          auto A = vector<Bundle>(begin(bundle), begin(bundle) + i);
          auto B = vector<Bundle>(begin(bundle) + i, begin(bundle) + j + 1);
          auto C = vector<Bundle>(begin(bundle) + j + 1, end(bundle));
          bundle = A;
          bundle.insert(begin(bundle), begin(B), end(B));
          bundle.insert(begin(bundle), begin(C), end(C));
          break;
        }
        break;
      }
    }

    int answer = 0;
    int cnt = 0;
    for (int i = 0; i < bundle.size(); ++i) {
      cnt += bundle[i].size();
      if (cnt < p - 1)
        continue;
      if (cnt != p - 1)
        bundle.insert(begin(bundle) + i + 1,
                      bundle[i].split(bundle[i].top() + bundle[i].size() -
                                      (cnt - p + 1)));
      cnt = p - 1;
      ++i;
      for (int j = i; j < bundle.size(); ++j) {
        cnt += bundle[j].size();
        if (cnt < q)
          continue;
        if (cnt != q)
          bundle.insert(
              begin(bundle) + j + 1,
              bundle[j].split(bundle[j].top() + bundle[j].size() - (cnt - q)));
        for (int k = i; k <= j; ++k)
          answer += bundle[k].count(r);
        break;
      }
      break;
    }
    cout << answer << endl;
  }
}

Error: MLE

Fixed submission:

// Shuffle
#include <iostream>
// 平衡二分探索木
// Treap

#include <algorithm>
#include <cstdlib>
#include <memory>
#include <vector>

class Treap {
public:
  Treap(int r) : r_(r) {}

  int size() const;
  void insert(const int index, const int value);
  void erase(const int index);
  int find(const int index) const;
  void shuffle(int x, int y);
  int count(int p, int q) const;

private:
  struct Node;

  int size(const std::unique_ptr<Node> &node) const;
  int count(const std::unique_ptr<Node> &node) const;
  int count(const std::unique_ptr<Node> &node, int p, int q) const;
  int find(const std::unique_ptr<Node> &node, const int index) const;
  std::unique_ptr<Node> update(std::unique_ptr<Node> &node);
  std::unique_ptr<Node> merge(std::unique_ptr<Node> &left,
                              std::unique_ptr<Node> &right);
  std::vector<std::unique_ptr<Node>> split(std::unique_ptr<Node> &node,
                                           const int k);

  std::unique_ptr<Node> root_;
  int r_;

public:
  void print();

private:
  void print(std::unique_ptr<Node> &node);
};

// struct
struct Treap::Node {
  int value;
  int size;
  int count;
  double priority;
  std::unique_ptr<Node> left_child, right_child;
  Node(int v, int c)
      : value(v), size(1), count(c), priority(std::rand()), left_child(nullptr),
        right_child(nullptr) {}
};

// DEBUG
void Treap::print() { print(root_); }
void Treap::print(std::unique_ptr<Node> &node) {
  std::cout << node->value << "    " << node->count << std::endl;
  //     std::cout<<"left"<<std::endl;
  if (node->left_child)
    print(node->left_child);
  //     std::cout<<"right"<<std::endl;
  if (node->right_child)
    print(node->right_child);
}
// ADD
void Treap::shuffle(int x, int y) {
  auto v1 = split(root_, x);
  auto v2 = split(v1.back(), y - x);
  auto m = merge(v2.back(), v2.front());
  root_ = merge(m, v1.front());
}
int Treap::count(int p, int q) const { return count(root_, p, q); }
int Treap::count(const std::unique_ptr<Node> &node, int p, int q) const {
  if (!node)
    return 0;
  if (q < p)
    return 0;
  // std::cout<<p<<","<<q<<"    "<<(node->value)<<"
  // "<<size(node->left_child)<<std::endl;

  if (p == size(node->left_child))
    return (node->value <= r_ ? 1 : 0) + count(node->right_child, 0, q - p - 1);
  if (q <= size(node->left_child))
    return count(node->left_child, p, q);

  return count(node->left_child, p, q) + (node->value <= r_ ? 1 : 0) +
         count(node->right_child, 0, q - size(node->left_child) - 1);
}

// public
int Treap::size() const { return size(root_); }
void Treap::insert(const int index, const int value) {
  auto t = split(root_, index);
  std::unique_ptr<Node> n(new Node(value, value <= r_ ? 1 : 0));
  root_ = merge(t[0], n);
  root_ = merge(root_, t[1]);
}
void Treap::erase(const int index) {
  auto t1 = split(root_, index);
  auto t2 = split(t1[1], 1);
  root_ = merge(t1[0], t2[1]);
}
int Treap::find(const int index) const { return find(root_, index); }

// private
int Treap::size(const std::unique_ptr<Node> &node) const {
  return node ? node->size : 0;
}
int Treap::count(const std::unique_ptr<Node> &node) const {
  return node ? node->count : 0;
}
int Treap::find(const std::unique_ptr<Node> &node, const int index) const {
  if (index < size(node->left_child))
    return find(node->left_child, index);
  else if (index == size(node->left_child))
    return node->value;
  else
    return find(node->right_child, index - size(node->left_child) - 1);
}
std::unique_ptr<Treap::Node> Treap::update(std::unique_ptr<Node> &node) {
  node->size = size(node->left_child) + size(node->right_child) + 1;
  node->count = count(node->left_child) + count(node->right_child) +
                (node->value <= r_ ? 1 : 0);
  return std::move(node);
}
std::unique_ptr<Treap::Node> Treap::merge(std::unique_ptr<Node> &left,
                                          std::unique_ptr<Node> &right) {
  if (!left || !right)
    return std::move(left ? left : right);
  if (left->priority > right->priority) {
    left->right_child = merge(left->right_child, right);
    return update(left);
  } else {
    right->left_child = merge(left, right->left_child);
    return update(right);
  }
}
std::vector<std::unique_ptr<Treap::Node>>
Treap::split(std::unique_ptr<Node> &node, const int k) {
  std::vector<std::unique_ptr<Node>> result(2);
  if (!node)
    return result;
  if (k <= size(node->left_child)) {
    result = split(node->left_child, k);
    node->left_child = std::move(result[1]);
    result[1] = update(node);
  } else {
    result = split(node->right_child, k - size(node->left_child) - 1);
    node->right_child = std::move(result[0]);
    result[0] = update(node);
  }
  return result;
}

#include <iostream>
using namespace std;
int main() {
  int n, m, p, q, r;
  while (cin >> n >> m >> p >> q >> r, n) {
    Treap t(r);
    for (int i = 0; i < n; ++i)
      t.insert(i, i + 1);
    for (int i = 0; i < m; ++i) {
      int x, y;
      cin >> x >> y;
      t.shuffle(x, y);
    }
    //         t.print();
    cout << t.count(p - 1, q - 1) << endl;
  }
}