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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

typedef struct S {
  int s, e, f; //(s e]
  bool operator<(const S &a) const { return s < a.s; };
} S;
int N;
vector<S> F;
int main() {
  for (; scanf("%d\\n", &N), N; puts("")) {
    F.clear();
    S t = {0, 1000000001, -1};
    F.push_back(t);
    for (; N--;) {
      int a, b;
      char c;
      scanf("%c", &c);
      if (c == 'W') {
        scanf("%d%d\\n", &a, &b);
        for (int i = 0; b > 0; i++) {
          if (F[i].f == -1) {
            if (b > F[i].e - F[i].s) {
              F[i].f = a;
              b -= F[i].e - F[i].s;
            } else {
              S ins = {F[i].s, F[i].s + b, a};
              F[i].s = F[i].s + b;
              F.push_back(ins);
              b = 0;
              sort(F.begin(), F.end());
            }
          }
        }
      } else if (c == 'R') {
        scanf("%d\\n", &a);
        for (int i = 0; i < F.size(); i++)
          if (F[i].s <= a && a < F[i].e) {
            printf("%d\\n", F[i].f);
            break;
          }
      } else {
        scanf("%d\\n", &a);
        for (int i = 0; i < F.size(); i++)
          if (F[i].f == a)
            F[i].f = -1;
      }
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <utility>
#include <vector>
using namespace std;
typedef int ll;
typedef pair<int, int> P;
#define MX 1000000001
int N, d1[10005], d2[10005], cnt;
char c[10005];

struct BIT {
  vector<ll> T;
  int n;
  BIT(int n) : n(n + 1), T(n + 1, 0) {}
  ll _sum(int p) {
    ll res = 0;
    for (; p > 0; p -= p & -p)
      res += T[p];
    return res;
  }
  ll sum(int r, int l) { return _sum(l) - _sum(r); };
  void add(int i, int x) {
    for (; i < n; i += i & -i)
      T[i] += x;
  }
};

int main() {
  for (; scanf("%d\\n", &N), N; puts("")) {
    map<int, int> T;
    T[-10000] = -1;
    map<int, vector<int>> D;
    for (int i = 0; i < N; i++) {
      scanf("%c", &c[i]);
      if (c[i] == 'W')
        scanf("%d%d\\n", &d1[i], &d2[i]);
      else {
        scanf("%d\\n", &d1[i]);
        if (c[i] == 'R')
          T[d1[i]] = -1;
      }
    }

    BIT SEQ(T.size());

    for (int i = 0; i < N; i++) {
      if (c[i] == 'R') {
        printf("%d\\n", T[d1[i]]);
      } else if (c[i] == 'D') {
        for (int j = 1; j < D[d1[i]].size(); j++)
          SEQ.add(j, -D[d1[i]][j]);
        for (map<int, int>::iterator it = T.begin(); it != T.end(); it++)
          if (it->second == d1[i])
            it->second = -1;
      } else {
        D[d1[i]].push_back(0);
        int j = 1, s = d2[i];
        for (map<int, int>::iterator it = T.begin(); it != T.end(); it++, j++) {
          int ins = 0, emp = it->first - SEQ._sum(j) + 1;
          if (emp > 0) {
            if (s >= emp)
              it->second = d1[i];
            ins = min(s, emp);
            s -= min(s, emp);
          }
          SEQ.add(j, ins);
          D[d1[i]].push_back(ins);
        }
      }
    }
  }
}