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

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
// #define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;
const int INF = 1e9 + 7;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    vector<PP> d; // start, len, id;
    rep(i, 0, n) {
      char c;
      int id, len;
      cin >> c >> id;
      if (c == 'W') {
        cin >> len;
        rep(j, 0, d.size()) {
          if (len == 0)
            break;
          if (d[j].second.second == -1) {
            int sz = d[j].second.first;
            int st = d[j].first;
            if (len < sz) {
              d.push_back(PP(st + len, P(sz - len, -1)));
              d[j].second.first = len;
              d[j].second.second = id;
              len = 0;
              break;
            } else {
              len -= sz;
              d[j].second.second = id;
            }
          }
        }
        int right = 0;
        if (len > 0) {
          if (d.size() == 0) {
            right = 0;
          } else {
            right = d[d.size() - 1].first + d[d.size() - 1].second.first;
          }
          d.push_back(PP(right, P(len, id)));
        }
      } else if (c == 'D') {
        rep(j, 0, d.size()) {
          if (d[j].second.second == id) {
            d[j].second.second = -1;
          }
        }
      } else if (c == 'R') {
        int idx = upper_bound(all(d), PP(id, P(INF, INF))) - d.begin();
        // cout << id << ' ' << idx << endl;
        if (idx == d.size()) {
          if (d.size() == 0)
            cout << -1 << endl;
          else {
            int r = d[d.size() - 1].first + d[d.size() - 1].second.first;
            if (id < r) {
              cout << d[d.size() - 1].second.second << endl;
            } else {
              cout << -1 << endl;
            }
          }
        } else {
          idx--;
          assert(0 <= idx && idx < d.size());
          cout << d[idx].second.second << endl;
        }
      }
      sort(all(d));
    }
    cout << endl;
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
// #define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;
const int INF = 1e9 + 7;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    vector<PP> d; // start, len, id;
    rep(i, 0, n) {
      char c;
      int id, len;
      cin >> c >> id;
      if (c == 'W') {
        cin >> len;
        rep(j, 0, d.size()) {
          if (d[j].second.second == -1) {
            int sz = d[j].second.first;
            int st = d[j].first;
            if (len < sz) {
              d.push_back(PP(st + len, P(sz - len, -1)));
              d[j].second.first = len;
              d[j].second.second = id;
            } else {
              len -= sz;
              d[j].second.second = id;
            }
          }
          if (len == 0)
            break;
        }
        int right = 0;
        if (len > 0) {
          if (d.size() == 0) {
            right = 0;
          } else {
            right = d[d.size() - 1].first + d[d.size() - 1].second.first;
          }
          d.push_back(PP(right, P(len, id)));
        }
      } else if (c == 'D') {
        rep(j, 0, d.size()) {
          if (d[j].second.second == id) {
            d[j].second.second = -1;
          }
        }
      } else if (c == 'R') {
        int idx = upper_bound(all(d), PP(id, P(INF, INF))) - d.begin();
        // cout << id << ' ' << idx << endl;
        if (idx == d.size()) {
          if (d.size() == 0)
            cout << -1 << endl;
          else {
            int r = d[d.size() - 1].first + d[d.size() - 1].second.first;
            if (id < r) {
              cout << d[d.size() - 1].second.second << endl;
            } else {
              cout << -1 << endl;
            }
          }
        } else {
          assert(0 <= idx && idx < d.size());
          cout << d[idx].second.second << endl;
        }
      }
      sort(all(d));
    }
    cout << endl;
  }
}