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

#include <algorithm>
#include <map>
#include <stdio.h>
using namespace std;
char str[2];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    map<pair<int, int>, int> M;
    M[make_pair(0, 1000000001)] = -1;
    for (int i = 0; i < a; i++) {
      scanf("%s", str);
      if (str[0] == 'W') {
        int b, c;
        scanf("%d%d", &b, &c);
        for (map<pair<int, int>, int>::iterator it = M.begin(); it != M.end();
             it++) {
          if (c <= 0)
            break;
          pair<pair<int, int>, int> dat = *it;
          if (dat.second == -1) {
            if (c >= dat.first.second - dat.first.first) {
              c -= dat.first.second - dat.first.first;
              M[dat.first] = b;
            } else {
              M[make_pair(dat.first.first, dat.first.first + c)] = b;
              M[make_pair(dat.first.first + c, dat.first.second)] = -1;
              M.erase(dat.first);
              c = 0;
              break;
            }
          }
        }
      }
      if (str[0] == 'D') {
        int b;
        scanf("%d", &b);
        for (map<pair<int, int>, int>::iterator it = M.begin(); it != M.end();
             it++) {
          if ((*it).second == b) {
            M[(*it).first] = -1;
          }
        }
      }
      if (str[0] == 'R') {
        int b;
        scanf("%d", &b);
        bool found = false;
        for (map<pair<int, int>, int>::iterator it = M.begin(); it != M.end();
             it++) {
          pair<int, int> q = (*it).first;
          if (q.first <= b && b < q.second) {
            printf("%d\\n", (*it).second);
            found = true;
            break;
          }
        }
      }
    }
    printf("\\n");
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <map>
#include <stdio.h>
using namespace std;
char str[2];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    map<pair<int, int>, int> M;
    for (int i = 0; i < a; i++) {
      scanf("%s", str);
      if (str[0] == 'W') {
        int b, c;
        scanf("%d%d", &b, &c);
        for (map<pair<int, int>, int>::iterator it = M.begin(); it != M.end();
             it++) {
          if (c <= 0)
            break;
          pair<pair<int, int>, int> dat = *it;
          if ((*it).second == -1) {
            if (c >= (*it).first.second - (*it).first.first) {
              c -= (*it).first.second - (*it).first.first;
              M[(*it).first] = b;
            } else {
              M[make_pair((*it).first.first, (*it).first.first + c)] = b;
              M[make_pair((*it).first.first + c, (*it).first.second)] = -1;
              M.erase((*it).first);
            }
          }
        }

        if (c > 0) {
          if (M.size() == 0)
            M[make_pair(0, c)] = b;
          else {
            int p = ((*(M.rbegin())).first.second);
            M[make_pair(p, p + c)] = b;
          }
        }
      }
      if (str[0] == 'D') {
        int b;
        scanf("%d", &b);
        for (map<pair<int, int>, int>::iterator it = M.begin(); it != M.end();
             it++) {
          if ((*it).second == b) {
            M[(*it).first] = -1;
          }
        }
      }
      if (str[0] == 'R') {
        int b;
        scanf("%d", &b);
        bool found = false;
        for (map<pair<int, int>, int>::iterator it = M.begin(); it != M.end();
             it++) {
          pair<int, int> q = (*it).first;
          if (q.first < b && b <= q.second) {
            printf("%d\\n", (*it).second);
            found = true;
            break;
          }
        }
        if (!found)
          printf("%d\\n", -1);
      }
    }
    printf("\\n");
  }
}