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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct sector {
  int begin, end, label;
  sector(int begin, int end, int label)
      : begin(begin), end(end), label(label) {}
};

int main() {
  int n;
  while (cin >> n, n) {
    list<sector> sectors;
    REP(i, n) {
      char command;
      cin >> command;
      int l, s;
      switch (command) {
      case 'W': {
        cin >> l >> s;
        int left = 0;
        int total = 0;
        if (sectors.size() == 0) {
          sectors.push_back(sector(0, s - 1, l));
        } else {
          for (list<sector>::iterator it = sectors.begin(); it != sectors.end();
               it++) {
            if (left < it->begin) {
              if (total + it->begin - left >= s) {
                it = sectors.insert(it, sector(left, s - total + left - 1, l));
                total += s - total + left;
                break;
              } else {
                int right = it->begin - 1;
                it = sectors.insert(it, sector(left, right, l));
                total += right - left + 1;
                left = it->end + 1;
              }
            } else {
              if (it->end >= left) {
                left = it->end + 1;
              }
            }
          }
          if (total < s) {
            sectors.push_back(sector(left, left + s - total - 1, l));
          }
        }
      } break;
      case 'D': {
        cin >> l;
        for (list<sector>::iterator it = sectors.begin();
             it != sectors.end();) {
          if (it->label == l) {
            it = sectors.erase(it);
            if (it == sectors.end())
              break;
          } else {
            it++;
          }
        }
      } break;
      case 'R': {
        cin >> l;
        int label = -1;
        for (list<sector>::iterator it = sectors.begin(); it != sectors.end();
             it++) {
          if (it->begin <= l && l <= it->end) {
            label = it->label;
            break;
          }
        }
        cout << label << endl;
      } break;
      }
    }
    cout << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-10;
const double PI = acos(-1.0);

struct sector {
  int begin, end, label;
  sector(int begin, int end, int label)
      : begin(begin), end(end), label(label) {}
};
int main() {
  int n;
  while (cin >> n, n) {
    list<sector> sectors;
    REP(i, n) {
      char command;
      cin >> command;
      int l, s;
      switch (command) {
      case 'W': {
        cin >> l >> s;
        int left = 0;
        int total = 0;
        if (sectors.size() == 0) {
          sectors.push_back(sector(0, s, l));
        } else {
          for (list<sector>::iterator it = sectors.begin(); it != sectors.end();
               it++) {
            if (left < it->begin) {
              if (total + it->begin - left >= s) {
                it = sectors.insert(it, sector(left, s - total + left - 1, l));
                total += s - total + left;
                break;
              } else {
                int right = it->begin - 1;
                it = sectors.insert(it, sector(left, right, l));
                total += right - left + 1;
                left = it->end;
              }
            } else {
              if (it->end > left) {
                left = it->end + 1;
              }
            }
          }
          if (total < s) {
            sectors.push_back(sector(left, left + s - total - 1, l));
          }
        }
      } break;
      case 'D': {
        cin >> l;
        for (list<sector>::iterator it = sectors.begin(); it != sectors.end();
             it++) {
          if (it->label == l) {
            it = sectors.erase(it);
          }
        }
      } break;
      case 'R': {
        cin >> l;
        int label = -1;
        for (list<sector>::iterator it = sectors.begin(); it != sectors.end();
             it++) {
          if (it->begin <= l && l <= it->end) {
            label = it->label;
            break;
          }
        }
        cout << label << endl;
      } break;
      }
    }
    cout << endl;
  }
}