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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

#define first start
#define second goal

using namespace std;

const int NONE = -1;
const int MAX = 1000000000;

struct sector {
  int length, id;
  sector(int id_, int length_) : id(id_), length(length_) {}
};

vector<sector> sys;
int pos;

void read(int pos) {
  int idx = 0;
  // rep(i, sys.size()) { cout << sys[i].id << "," << sys[i].length << " ";}
  // cout << endl;
  rep(i, sys.size()) {
    idx += sys[i].length;
    if (idx > pos) {
      cout << sys[i].id << endl;
      return;
    }
  }
  cout << -1 << endl;
}

void write(int id, int size) {

  if (sys.size() == 0) {
    sys.push_back(sector(id, size));
    return;
  }

  int n = sys.size();
  rep(i, n) {
    if (sys[i].id != -1)
      continue;

    vector<sector>::iterator it = sys.begin() + i;
    if (sys[i].length == size) {
      sys[i].id = id;
      return;
    } else if (sys[i].length - size > 0) {
      sys.insert(it + 1, sector(-1, sys[i].length - size));
      sys[i].id = id;
      sys[i].length = size;
      return;
    } else if (sys[i].length - size < 0) {
      sys[i].id = id;
      size -= sys[i].length;
    }
    if (size <= 0)
      return;
  }
  sys.push_back(sector(id, size));

  return;
}

void del(int id) {
  rep(i, sys.size()) {
    if (sys[i].id == id)
      sys[i].id = -1;
  }
}

int main() {
  int n, m;
  while (cin >> n, n) {

    sys.clear();

    char cmd;
    rep(i, n) {
      cin >> cmd;
      if (cmd == 'W') {
        int id, size;
        cin >> id >> size;
        write(id, size);
      } else if (cmd == 'D') {
        int id;
        cin >> id;
        del(id);
      } else if (cmd == 'R') {
        int pos;
        cin >> pos;
        read(pos);
      }
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

#define first start
#define second goal

using namespace std;

const int NONE = -1;
const int MAX = 1000000000;

struct sector {
  int length, id;
  sector(int id_, int length_) : id(id_), length(length_) {}
};

vector<sector> sys;
int pos;

void read(int pos) {
  int idx = 0;
  // rep(i, sys.size()) { cout << sys[i].id << "," << sys[i].length << " ";}
  // cout << endl;
  rep(i, sys.size()) {
    idx += sys[i].length;
    if (idx >= pos) {
      cout << sys[i].id << endl;
      return;
    }
  }
  cout << -1 << endl;
}

void write(int id, int size) {

  if (sys.size() == 0) {
    sys.push_back(sector(id, size));
    return;
  }

  int n = sys.size();
  rep(i, n) {
    if (sys[i].id != -1)
      continue;

    vector<sector>::iterator it = sys.begin() + i;
    if (sys[i].length == size) {
      sys[i].id = id;
      return;
    } else if (sys[i].length - size > 0) {
      sys.insert(it + 1, sector(-1, sys[i].length - size));
      sys[i].id = id;
      sys[i].length = size;
    } else if (sys[i].length - size < 0) {
      sys[i].id = id;
      size -= sys[i].length;
    }
    if (size <= 0)
      return;
  }
  sys.push_back(sector(id, size));

  return;
}

void del(int id) {
  rep(i, sys.size()) {
    if (sys[i].id == id)
      sys[i].id = -1;
  }
}

int main() {
  int n, m;
  while (cin >> n, n) {

    sys.clear();

    char cmd;
    rep(i, n) {
      cin >> cmd;
      if (cmd == 'W') {
        int id, size;
        cin >> id >> size;
        write(id, size);
      } else if (cmd == 'D') {
        int id;
        cin >> id;
        del(id);
      } else if (cmd == 'R') {
        int pos;
        cin >> pos;
        read(pos);
      }
    }
    cout << endl;
  }
  return 0;
}