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

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

struct state {
  int b, e, t;
  state(int _b, int _e, int _t) { b = _b, e = _e, t = _t; }
};

int main(void) {
  int n;
  while (cin >> n, n) {
    vector<state> files;
    state init(0, 1000000000, -1);
    files.pb(init);
    rep(i, n) {
      char mode;
      cin >> mode;
      if (mode == 'W') {
        int l, s;
        cin >> l >> s;
        rep(i, files.size()) {
          if (files[i].t == -1 && s > 0) {
            int fs = (files[i].e - files[i].b) + 1;
            if (s >= fs)
              files[i].t = l, s -= fs;
            else {
              state newfile(files[i].b + s, files[i].e, files[i].t);
              files[i].e = files[i].b + s - 1, files[i].t = l, s = 0;
              files.insert(files.begin() + i + 1, newfile);
            }
          }
        }
      } else if (mode == 'R') {
        int p;
        cin >> p;
        int ans = -1;
        rep(i, files.size()) if (files[i].b <= p && p <= files[i].e) ans =
            files[i].t;
        cout << ans << endl;
      } else {
        int l;
        cin >> l;
        rep(i, files.size()) if (files[i].t == l) files[i].t = -1;
      }
      // for(auto &i:files){
      //	cout << i.b << " " << i.e << " " << i.t << endl;
      // }
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll inf = 1LL << 62;

int main(void) {
  int n;
  while (cin >> n, n) {
    deque<pii> memory; // 識別子　セクタ
    rep(i, n) {
      char com;
      cin >> com;
      if (com == 'W') {
        int l, s;
        cin >> l >> s;
        int mn = memory.size();
        if (mn) {
          rep(j, mn) {
            if (memory[j].first == -1) {
              if (s > memory[j].second) {
                memory[j].first = l;
                s -= memory[j].second;
              } else {
                memory[j].second -= s;
                pii tmp;
                tmp.first = l;
                tmp.second = s;
                memory.insert(memory.begin() + j - 1, tmp);
              }
            }
          }
          if (s) {
            pii tmp;
            tmp.first = l;
            tmp.second = s;
            memory.push_back(tmp);
          }
        } else {
          pii tmp;
          tmp.first = l;
          tmp.second = s;
          memory.push_back(tmp);
        }
      } else if (com == 'D') {
        int mn = memory.size();
        int l;
        cin >> l;
        rep(j, mn) if (memory[j].first == l) memory[j].first = -1;
      } else {
        int p;
        cin >> p;
        int mn = memory.size();
        int cur = 0;
        bool out = false;
        if (mn) {
          rep(j, mn) {
            cur += memory[j].second;
            if (cur > p) {
              out = true;
              cout << memory[j].first << endl;
              break;
            }
          }
        }
        if (!out)
          cout << -1 << endl;
      }
    }
    cout << endl;
  }
  return 0;
}