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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N;
  while (cin >> N && N) {
    list<pair<pair<int, int>, int>> se;
    for (int i = 0; i < N; i++) {
      string s;
      cin >> s;
      if (s == "D") {
        int v;
        cin >> v;
        for (list<pair<pair<int, int>, int>>::iterator it = se.begin();
             it != se.end();) {
          if (it->second == v) {
            it = se.erase(it);
          } else {
            ++it;
          }
        }
      } else if (s == "W") {
        int l, sec;
        cin >> l >> sec;
        int prev = 0;
        for (list<pair<pair<int, int>, int>>::iterator it = se.begin();
             it != se.end();) {
          if (sec == 0)
            break;
          int width = it->first.first - prev;
          int use = min(sec, width);
          int tmp = it->first.second;
          if (use != 0) {
            sec -= use;
            se.insert(it, make_pair(make_pair(prev, prev + use), l));
          }
          it++;
          prev = tmp;
        }
        if (sec != 0) {
          se.push_back(make_pair(make_pair(prev, prev + sec), l));
        }
      } else {
        int pos;
        cin >> pos;
        int flag = 0;
        for (list<pair<pair<int, int>, int>>::iterator it = se.begin();
             it != se.end(); ++it) {
          if (it->first.first <= pos && pos < it->first.second) {
            cout << it->second << endl;
            flag = 1;
            break;
          }
        }
        if (!flag) {
          cout << -1 << endl;
        }
      }
    }
    cout << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N;
  while (cin >> N && N) {
    list<pair<pair<int, int>, int>> se;
    for (int i = 0; i < N; i++) {
      string s;
      cin >> s;
      if (s == "D") {
        int v;
        cin >> v;
        for (list<pair<pair<int, int>, int>>::iterator it = se.begin();
             it != se.end();) {
          if (it->second == v) {
            it = se.erase(it);
          } else {
            ++it;
          }
        }
      } else if (s == "W") {
        int l, sec;
        cin >> l >> sec;
        int prev = 0;
        for (list<pair<pair<int, int>, int>>::iterator it = se.begin();
             it != se.end();) {
          if (sec == 0)
            break;
          int width = it->first.first - prev;
          int use = min(sec, width);
          int tmp = it->first.second;
          it++;
          if (use != 0) {
            sec -= use;
            se.insert(it, make_pair(make_pair(prev, prev + use), l));
          }
          prev = tmp;
        }
        if (sec != 0) {
          se.push_back(make_pair(make_pair(prev, prev + sec), l));
        }
      } else {
        int pos;
        cin >> pos;
        int flag = 0;
        for (list<pair<pair<int, int>, int>>::iterator it = se.begin();
             it != se.end(); ++it) {
          if (it->first.first <= pos && pos < it->first.second) {
            cout << it->second << endl;
            flag = 1;
            break;
          }
        }
        if (!flag) {
          cout << -1 << endl;
        }
      }
    }
    cout << endl;
  }
}