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

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
int n;
vector<tuple<int, int, int>> vec;
vector<int> res;
void Write(int a, int b) {
  int L = -1;
  int R = vec.size();
  for (int i = L; i < R; i++) {
    if (b == 0)
      continue;
    int SL = 0;
    if (i >= 0)
      SL = get<1>(vec[i]) + 1;
    int SR = 1999999999;
    if (i < R - 1)
      SR = get<0>(vec[i + 1]) - 1;
    int SZ = SR - SL + 1;
    if (SZ >= 1) {
      if (b < SZ) {
        vec.push_back(make_tuple(SL, SL + b - 1, a));
        b = 0;
      } else {
        vec.push_back(make_tuple(SL, SR, a));
        b -= SZ;
      }
    }
  }
  sort(vec.begin(), vec.end());
  return;
}
void Delete(int a) {
  vector<tuple<int, int, int>> vec2;
  for (int i = 0; i < vec.size(); i++) {
    if (get<2>(vec[i]) != a)
      vec2.push_back(vec[i]);
  }
  vec = vec2;
  return;
}
int Read(int a) {
  for (int i = 0; i < vec.size(); i++) {
    if (get<0>(vec[i]) <= a && a <= get<1>(vec[i])) {
      return get<2>(vec[i]);
    }
  }
  return -1;
}
int main() {
  while (true) {
    cin >> n;
    vec.clear();
    res.clear();
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      char p;
      cin >> p;
      if (p == 'W') {
        int l, s;
        cin >> l >> s;
        Write(l, s);
      }
      if (p == 'D') {
        int l;
        cin >> l;
        Delete(l);
      }
      if (p == 'R') {
        int l;
        cin >> l;
        res.push_back(Read(l));
      }
    }
    for (int i = 0; i < res.size(); i++)
      cout << res[i] << endl;
    cout << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
int n;
vector<tuple<int, int, int>> vec;
void Write(int a, int b) {
  int R = vec.size();
  int E = -1;
  if (vec.size() >= 1)
    E = get<0>(vec[0]) - 1;
  if (E >= 0) {
    if (b < E + 1) {
      vec.push_back(make_tuple(0, b - 1, a));
      sort(vec.begin(), vec.end());
      return;
    }
    if (b >= E + 1) {
      vec.push_back(make_tuple(0, E, a));
      sort(vec.begin(), vec.end());
      b -= (E + 1);
    }
  }
  for (int i = 0; i < R - 1; i++) {
    if (b == 0)
      break;
    int E = get<1>(vec[i]) + 1;
    int F = get<0>(vec[i + 1]) - 1;
    int SS = F - E + 1;
    if (b >= SS) {
      vec.push_back(make_tuple(E, F, a));
      b -= SS;
    }
    if (b < SS) {
      vec.push_back(make_tuple(E, E + b - 1, a));
      b = 0;
    }
  }
  sort(vec.begin(), vec.end());
  if (b >= 1) {
    int E = 0;
    if (vec.size() >= 1)
      E = get<1>(vec[vec.size() - 1]) + 1;
    vec.push_back(make_tuple(E, E + b - 1, a));
  }
  return;
}
void Delete(int a) {
  vector<tuple<int, int, int>> vec2;
  for (int i = 0; i < vec.size(); i++) {
    if (get<2>(vec[i]) != a)
      vec2.push_back(vec[i]);
  }
  vec = vec2;
  return;
}
int Read(int a) {
  for (int i = 0; i < vec.size(); i++) {
    if (get<0>(vec[i]) <= a && a <= get<1>(vec[i])) {
      return get<2>(vec[i]);
    }
  }
  return -1;
}
int main() {
  while (true) {
    cin >> n;
    vec.clear();
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      char p;
      cin >> p;
      if (p == 'W') {
        int l, s;
        cin >> l >> s;
        Write(l, s);
      }
      if (p == 'D') {
        int l;
        cin >> l;
        Delete(l);
      }
      if (p == 'R') {
        int l;
        cin >> l;
        cout << Read(l) << endl;
      }
    }
    cout << endl;
  }
  return 0;
}