Problem p01270 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main() {
  int n;
  while (cin >> n, n) {
    set<pii> empty({{0, INF}});
    map<int, vector<pii>> mp;
    REP(i, n) {
      char c;
      cin >> c;
      if (c == 'W') {
        int a, b;
        cin >> a >> b;
        vector<pii> v;
        vector<pii> era;
        for (auto it = empty.begin(); b > 0; it++) {
          if (it->second - it->first > b) {
            v.emplace_back(it->first, it->first + b);
            era.push_back(*it);
            empty.insert(pii(it->first + b, it->second));
            b = 0;
          } else {
            v.emplace_back(*it);
            b -= it->second - it->first;
            era.push_back(*it);
          }
        }
        REP(i, era.size()) empty.erase(era[i]);
        mp[a] = v;
      } else if (c == 'D') {
        int a;
        cin >> a;
        vector<pii> v = mp[a];
        mp.erase(a);
        REP(i, v.size()) empty.insert(v[i]);
      } else {
        int a;
        cin >> a;
        int res;
        for (auto j : empty) {
          if (a >= j.first && a < j.second) {
            res = -1;
            goto end;
          }
        }
        for (auto j : mp) {
          vector<pii> tmp = j.second;
          REP(k, tmp.size()) {
            if (a >= tmp[k].first && a < tmp[k].second) {
              res = j.first;
              break;
            }
          }
        }

      end:;
        cout << res << endl;
      }
    }
    cout << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 1145141919;
const int MOD = 1000000007;
const double EPS = 1e-10;
typedef int weight;
struct edge {
  int to;
  weight cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
typedef vector<vector<edge>> Graph;
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
int place(int x, int y, int w) { return w * x + y; }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

vector<vector<pii>> v;

int main() {
  int n;
  while (cin >> n, n) {
    v.clear();
    v.resize(1);
    v[0].push_back(pii(0, INF));
    vi index;
    index.push_back(-1);
    REP(i, n) {
      char c;
      int q;
      cin >> c >> q;
      if (c == 'W') {
        index.push_back(q);
        int t;
        cin >> t;
        vector<pii> emp;
        v.push_back(emp);
        int cnt = 0;
        for (int j = 0;; j++) {
          if (v[0][j].second - v[0][j].first >= t) {
            v.back().push_back(pii(v[0][j].first, v[0][j].first + t));
            if (v[0][j].second - v[0][j].first == t)
              cnt++;
            else
              v[0][j].first += t;
            break;
          } else {
            v.back().push_back(v[0][j]);
            t -= v[0][j].first - v[0][j].second;
            cnt++;
          }
        }
        v[0].erase(v[0].begin(), v[0].begin() + cnt);
      } else if (c == 'D') {
        int p = find(ALL(index), q) - index.begin();
        v[0].insert(v[0].end(), ALL(v[p]));
        v[p].erase(ALL(v[p]));
        sort(ALL(v[0]));
        bool up = true;
        while (up) {
          up = false;
          REP(i, v[0].size() - 1) {
            if (v[0][i].second == v[0][i + 1].first) {
              up = true;
              v[0][i] = v[0][i + 1] = pii(v[0][i].first, v[0][i + 1].second);
            }
          }
        }
        auto a = unique(ALL(v[0]));
        v[0].erase(a, v[0].end());
      } else {
        REP(i, v.size()) {
          REP(j, v[i].size()) {
            if (q >= v[i][j].first && q < v[i][j].second) {
              if (i == 0)
                puts("-1");
              else
                cout << index[i] << endl;
              goto next;
            }
          }
        }
      next:;
      }
    }
    cout << endl;
  }
  return 0;
}