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

#include <cstdio>
#include <iostream>
#include <list>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;

static const ll MAX = 1e10;

enum Data { Space, File };

struct Range {
  Data d;
  int id;
  ll start;
  ll end;
  ll size() const { return end - start; }
  bool operator<(const Range &rhs) const { return start < rhs.start; }
  void print() const {
    printf("%c %d %lld %lld\\n", (d == Space) ? 'S' : 'F', id, start, end);
  }
  Range(Data d, int id, ll start, ll end)
      : d(d), id(id), start(start), end(end) {}
};

int N;
list<Range> sector;
typedef list<Range> L;

void solve() {
  for (int i = 0; i < N; i++) {
    char cmd;
    cin >> cmd;
    switch (cmd) {
    case 'W': {
      ll id, size;
      cin >> id >> size;
      for (L::iterator it = sector.begin(); it != sector.end(); it++) {
        if (it->d == Space) {
          if (it->size() >= size) {
            Range file(File, id, it->start, it->start + size);
            sector.insert(it, file);
            it->start += size;
            break;
          } else {
            it->d = File;
            it->id = id;
            size -= it->size();
          }
        }
      }
      break;
    }
    case 'D': {
      ll id;
      cin >> id;
      for (L::iterator it = sector.begin(); it != sector.end(); it++) {
        if (it->d == File && it->id == id) {
          L::iterator prev_it = it;
          prev_it--;

          if (it != sector.begin() && prev_it->end == it->start &&
              prev_it->d == Space) {
            prev_it->end += it->size();
            it = sector.erase(it);
            goto next;
          } else {
            L::iterator next_it = it;
            next_it++;
            if (it != sector.end() && next_it->start == it->end &&
                next_it->d == Space) {
              next_it->start -= it->size();
              it = sector.erase(it);
              goto next;
            }
          }
          it->d = Space;
        next:;
        }
      }
      break;
    }
    case 'R': {
      ll sec;
      cin >> sec;
      for (L::iterator it = sector.begin(); it != sector.end(); it++) {
        if (it->start <= sec && sec < it->end) {
          if (it->d == Space) {
            cout << -1 << endl;
          } else {
            cout << it->id << endl;
          }
          break;
        }
      }
      break;
    }
    }
    /*
    cout << "debug" << endl;
    for (L::iterator it = sector.begin(); it != sector.end(); it++) {
      it->print();
    }
    cout << endl;
    */
    // cout << "debug" << endl;
    // cout << sector.size() << endl;
  }
}

void init() {
  sector.clear();
  sector.push_back(Range(Space, -1, 0, MAX));
}

int main() {
  while (true) {
    cin >> N;
    if (N == 0) {
      return 0;
    }
    init();
    solve();
    cout << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <list>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;

static const ll MAX = 1e10;

enum Data { Space, File };

struct Range {
  Data d;
  int id;
  ll start;
  ll end;
  ll size() const { return end - start; }
  bool operator<(const Range &rhs) const { return start < rhs.start; }
  void print() const {
    printf("%c %d %lld %lld\\n", (d == Space) ? 'S' : 'F', id, start, end);
  }
  Range(Data d, int id, ll start, ll end)
      : d(d), id(id), start(start), end(end) {}
};

int N;
list<Range> sector;
typedef list<Range> L;

void solve() {
  for (int i = 0; i < N; i++) {
    char cmd;
    cin >> cmd;
    switch (cmd) {
    case 'W': {
      ll id, size;
      cin >> id >> size;
      for (L::iterator it = sector.begin(); it != sector.end(); it++) {
        if (it->d == Space) {
          if (it->size() >= size) {
            Range file(File, id, it->start, it->start + size);
            sector.insert(it, file);
            it->start += size;
          } else {
            it->d = File;
            it->id = id;
            size -= it->size();
          }
        }
      }
      break;
    }
    case 'D': {
      ll id;
      cin >> id;
      for (L::iterator it = sector.begin(); it != sector.end(); it++) {
        if (it->d == File && it->id == id) {
          L::iterator prev_it = it;
          prev_it--;

          if (it != sector.begin() && prev_it->end == it->start &&
              prev_it->d == Space) {
            prev_it->end += it->size();
            it = sector.erase(it);
            goto next;
          } else {
            L::iterator next_it = it;
            next_it++;
            if (it != sector.end() && next_it->start == it->end &&
                next_it->d == Space) {
              next_it->start -= it->size();
              it = sector.erase(it);
              goto next;
            }
          }
          it->d = Space;
        next:;
        }
      }
      break;
    }
    case 'R': {
      ll sec;
      cin >> sec;
      for (L::iterator it = sector.begin(); it != sector.end(); it++) {
        if (it->start <= sec && sec < it->end) {
          if (it->d == Space) {
            cout << -1 << endl;
          } else {
            cout << it->id << endl;
          }
          break;
        }
      }
      break;
    }
    }
    /*
    cout << "debug" << endl;
    for (L::iterator it = sector.begin(); it != sector.end(); it++) {
      it->print();
    }
    cout << endl;
    */
  }
}

void init() {
  sector.clear();
  sector.push_back(Range(Space, -1, 0, MAX));
}

int main() {
  while (true) {
    cin >> N;
    if (N == 0) {
      return 0;
    }
    init();
    solve();
    cout << endl;
  }
}