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

#include <algorithm>
#include <iostream>
#include <set>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp1(a, b, c) P1(a, P(b, c))

int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;

    set<P1> S1;
    set<P1> S2;
    S1.insert(mp1(0, -1, 1000000001));
    S2.insert(mp1(-1, 0, 1000000001));

    for (int i = 0; i < n; i++) {
      char c;
      cin >> c;
      // scanf("\\n%c",&c);
      if (c == 'W') {
        int l, s;
        scanf("%d%d", &l, &s);
        set<P1>::iterator itr = S2.lower_bound(mp1(-1, 0, 0));
        while (1) {
          if (itr->sc.sc > s) {
            S1.insert(mp1(itr->sc.fr, l, s));
            S1.insert(mp1(itr->sc.fr + s, itr->fr, itr->sc.sc - s));
            S1.erase(mp1(itr->sc.fr, itr->fr, itr->sc.sc));
            S2.insert(mp1(l, itr->sc.fr, s));
            S2.insert(mp1(itr->fr, itr->sc.fr + s, itr->sc.sc - s));
            S2.erase(itr);
            break;
          } else {
            S1.insert(mp1(itr->sc.fr, l, itr->sc.sc));
            S1.erase(mp1(itr->sc.fr, -1, itr->sc.sc));
            S2.insert(mp1(l, itr->sc.fr, itr->sc.sc));
            s -= itr->sc.sc;
            S2.erase(itr++);
          }
          if (s == 0)
            break;
        }
      } else if (c == 'D') {
        int l;
        scanf("%d", &l);
        set<P1>::iterator itr = S2.lower_bound(mp1(l, 0, 0));
        while (1) {
          S1.insert(mp1(itr->sc.fr, -1, itr->sc.sc));
          S1.erase(mp1(itr->sc.fr, l, itr->sc.sc));
          S2.insert(mp1(-1, itr->sc.fr, itr->sc.sc));
          S2.erase(itr++);
          if (itr->fr != l)
            break;
        }
      } else {
        int p;
        scanf("%d", &p);
        set<P1>::iterator itr = S1.upper_bound(mp1(p + 1, -1, 0));
        printf("%d%c", (--itr)->sc.fr, 10);
      }
    }
    printf("%c", 10);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp1(a, b, c) P1(a, P(b, c))

int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;

    set<P1> S1;
    set<P1> S2;
    S1.insert(mp1(0, -1, 1000000001));
    S2.insert(mp1(-1, 0, 1000000001));

    for (int i = 0; i < n; i++) {
      char c;
      cin >> c;
      // scanf("\\n%c",&c);
      if (c == 'W') {
        int l, s;
        scanf("%d%d", &l, &s);
        set<P1>::iterator itr = S2.lower_bound(mp1(-1, 0, 0));
        while (1) {
          if (itr->sc.sc > s) {
            S1.insert(mp1(itr->sc.fr, l, s));
            S1.insert(mp1(itr->sc.fr + s, itr->fr, itr->sc.sc - s));
            S1.erase(mp1(itr->sc.fr, itr->fr, itr->sc.sc));
            S2.insert(mp1(l, itr->sc.fr, s));
            S2.insert(mp1(itr->fr, itr->sc.fr + s, itr->sc.sc - s));
            S2.erase(itr);
            break;
          } else {
            S1.insert(mp1(itr->sc.fr, l, itr->sc.sc));
            S1.erase(mp1(itr->sc.fr, -1, itr->sc.sc));
            S2.insert(mp1(l, itr->sc.fr, itr->sc.sc));
            S2.erase(itr++);
            s -= itr->sc.sc;
          }
          if (s == 0)
            break;
        }
      } else if (c == 'D') {
        int l;
        scanf("%d", &l);
        set<P1>::iterator itr = S2.lower_bound(mp1(l, 0, 0));
        while (1) {
          S1.insert(mp1(itr->sc.fr, -1, itr->sc.sc));
          S1.erase(mp1(itr->sc.fr, l, itr->sc.sc));
          S2.insert(mp1(-1, itr->sc.fr, itr->sc.sc));
          S2.erase(itr++);
          if (itr->fr != l)
            break;
        }
      } else {
        int p;
        scanf("%d", &p);
        set<P1>::iterator itr = S1.upper_bound(mp1(p, -1, 0));
        printf("%d%c", (--itr)->sc.fr, 10);
      }
    }
    printf("%c", 10);
  }
}