Problem p01270 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i, n) for (long long i = 0, i##_len = (n); i < i##_len; i++)
#define RANGE(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i <= i##_end; i++)
#define DSRNG(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i >= i##_end; i--)
#define UNIQUE(v)                                                              \\
  {                                                                            \\
    sort((v).begin(), (v).end());                                              \\
    (v).erase(unique((v).begin(), (v).end()), (v).end());                      \\
  }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) (q ? "Yes" : "No")
#define YES(q) (q ? "YES" : "NO")
#define DUMP(q)                                                                \\
  cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q)                                                             \\
  cerr << "[DEBUG] " #q ": [";                                                 \\
  REP(dumpall_i, (q).size()) {                                                 \\
    cerr << q[dumpall_i] << (dumpall_i == (q).size() - 1 ? "" : ", ");         \\
  }                                                                            \\
  cerr << "] at " __FILE__ ":" << __LINE__ << endl

// gcc拡張マクロ
#define gcd __gcd
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using namespace std;

// モジュール

// 処理内容
int main() {

  ll n;

  while (cin >> n, n) {

    vector<tuple<ll, ll, ll>>
        files; // (l, r, i) means [l, r) is a part of the file i

    REP(i, n) {

      char cmd;
      cin >> cmd;

      if (cmd == 'R') {

        ll p;
        cin >> p;
        bool found = false;
        for (auto &f : files) {
          if (get<0>(f) <= p && p < get<1>(f)) {
            cout << get<2>(f) << endl;
            found = true;
            break;
          }
        }
        if (!found)
          cout << "-1" << endl;

      } else if (cmd == 'D') {

        ll i;
        cin >> i;
        files.erase(remove_if(ALL_OF(files),
                              [&i](auto f) -> bool { return get<2>(f) == i; }),
                    files.end());

      } else if (cmd == 'W') {

        ll i, s;
        cin >> i >> s;

        auto old_back =
            files.empty() ? make_tuple(0LL, 0LL, 0LL) : files.back();

        if (!files.empty()) {
          if (get<0>(files.front()) > 0) {
            ll gap = min(get<0>(files.front()), s);
            s -= gap;
            files.insert(files.begin(), make_tuple(0LL, gap, i));
          }
          if (s >= 0)
            REP(j, files.size() - 1) {
              ll gap = get<0>(files[j + 1]) - get<1>(files[j]);
              if (gap > 0) {
                chmin(gap, s);
                s -= gap;
                ll bk = get<1>(files[j]);
                files.emplace_back(bk, bk + gap, i);
                if (s <= 0)
                  break;
              }
            }
        }

        if (s > 0) {
          ll bk = files.empty() ? 0 : get<1>(old_back);
          files.emplace_back(bk, bk + s, i);
        }

        sort(ALL_OF(files));
      }
    }

    cout << endl;
  }
}


Error: WA

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i, n) for (long long i = 0, i##_len = (n); i < i##_len; i++)
#define RANGE(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i <= i##_end; i++)
#define DSRNG(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i >= i##_end; i--)
#define UNIQUE(v)                                                              \\
  {                                                                            \\
    sort((v).begin(), (v).end());                                              \\
    (v).erase(unique((v).begin(), (v).end()), (v).end());                      \\
  }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) (q ? "Yes" : "No")
#define YES(q) (q ? "YES" : "NO")
#define DUMP(q)                                                                \\
  cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q)                                                             \\
  cerr << "[DEBUG] " #q ": [";                                                 \\
  REP(dumpall_i, (q).size()) {                                                 \\
    cerr << q[dumpall_i] << (dumpall_i == (q).size() - 1 ? "" : ", ");         \\
  }                                                                            \\
  cerr << "] at " __FILE__ ":" << __LINE__ << endl

// gcc拡張マクロ
#define gcd __gcd
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using namespace std;

// モジュール

// 処理内容
int main() {

  ll n;

  while (cin >> n, n) {

    vector<tuple<ll, ll, ll>>
        files; // (l, r, i) means [l, r) is a part of the file i

    REP(i, n) {

      char cmd;
      cin >> cmd;

      if (cmd == 'R') {

        ll p;
        cin >> p;
        bool found = false;
        for (auto &f : files) {
          if (get<0>(f) <= p && p < get<1>(f)) {
            cout << get<2>(f) << endl;
            found = true;
            break;
          }
        }
        if (!found)
          cout << "-1" << endl;

      } else if (cmd == 'D') {

        ll i;
        cin >> i;
        files.erase(remove_if(ALL_OF(files),
                              [&i](auto f) -> bool { return get<2>(f) == i; }),
                    files.end());

      } else if (cmd == 'W') {

        ll i, s;
        cin >> i >> s;

        if (!files.empty()) {
          if (get<0>(files.front()) > 0) {
            ll gap = min(get<0>(files.front()), s);
            s -= gap;
            files.insert(files.begin(), make_tuple(0LL, gap, i));
          }
          if (s >= 0)
            REP(j, files.size() - 1) {
              ll gap = get<0>(files[j + 1]) - get<1>(files[j]);
              if (gap > 0) {
                chmin(gap, s);
                s -= gap;
                ll bk = get<1>(files[i]);
                files.emplace_back(bk, bk + gap, i);
                if (s <= 0)
                  break;
              }
            }
        }

        if (s > 0) {
          ll bk = files.empty() ? 0 : get<1>(files.back());
          files.emplace_back(bk, bk + s, i);
        }

        sort(ALL_OF(files));
      }
    }

    cout << endl;
  }
}