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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<ll, ll>;

constexpr ll inf = 1e18;

int main() {
  int n;
  while (cin >> n, n) {
    vector<char> com(n);
    vector<ll> id(n, -1), S(n, -1), p(n, -1);
    set<pii> pool = {make_pair(0, inf)};
    for (int i = 0; i < n; ++i) {
      cin >> com[i];
      if (com[i] == 'W') {
        cin >> id[i] >> S[i];
      } else if (com[i] == 'D') {
        cin >> id[i];
      } else {
        cin >> p[i];
      }
    }
    map<pii, int> seg_to_id;
    map<int, vector<pii>> segs; // id -> seg
    for (int i = 0; i < n; ++i) {
      if (com[i] == 'W') {
        vector<pii> erased;
        for (auto s : pool) {
          if (s.second - s.first > S[i]) {
            pool.emplace(s.first + S[i], s.second);
            segs[id[i]].emplace_back(s.first, s.first + S[i]);
            seg_to_id[segs[id[i]].back()] = id[i];
            S[i] = 0;
          } else {
            S[i] -= s.second - s.first;
            segs[id[i]].push_back(s);
            seg_to_id[s] = id[i];
          }
          erased.push_back(s);
          if (S[i] == 0)
            break;
        }
        for (auto e : erased) {
          pool.erase(e);
        }
      } else if (com[i] == 'D') {
        for (auto x : segs[id[i]]) {
          seg_to_id.erase(x);
        }
        pool.insert(begin(segs[id[i]]), end(segs[id[i]]));
        segs[id[i]].clear();
      } else {
        auto it = seg_to_id.lower_bound(make_pair(p[i], -1));
        if (it != end(seg_to_id) && it->first.first == p[i]) {
          cout << it->second << endl;
        } else if (it != begin(seg_to_id)) {
          it--;
          if (it->first.first <= p[i] && p[i] < it->first.second) {
            cout << it->second << endl;
          } else {
            cout << -1 << endl;
          }
        } else {
          cout << -1 << endl;
        }
      }
      // cout << "debug seg_to_id: " << endl;
      // for(auto& p : seg_to_id) {
      //   cout << p.first.first << ' ' << p.first.second << ' ' << p.second <<
      //   endl;
      // }
      // cout << "debug pool: " << endl;
      // for(auto& s : pool) {
      //   cout << s.first << ' ' << s.second << endl;
      // }
      // cout << "segs: " << endl;
      // for(auto& p : segs) {
      //   cout << p.first << " -> " << endl;
      // }
    }
    cout << endl;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> xs;
    vector<char> com(n);
    vector<int> id(n, -1), S(n, -1), p(n, -1);
    xs.push_back(0);
    map<int, int> lens;
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      cin >> com[i];
      if (com[i] == 'W') {
        cin >> id[i] >> S[i];
        sum += S[i];
        lens[id[i]] = S[i];
        xs.push_back(sum);
      } else if (com[i] == 'D') {
        cin >> id[i];
        sum -= lens[id[i]];
        xs.push_back(sum);
      } else {
        cin >> p[i];
      }
    }
    sort(begin(xs), end(xs));
    xs.erase(unique(begin(xs), end(xs)), end(xs));

    map<int, vector<int>> ls;
    vector<int> ref_id(xs.size(), -1);
    for (int i = 0; i < n; ++i) {
      if (com[i] == 'W') {
        int idx = 0, remain = S[i];
        while (remain > 0) {
          if (ref_id[idx] == -1) {
            ref_id[idx] = id[i];
            assert(idx + 1 < xs.size());
            remain -= xs[idx + 1] - xs[idx];
            ls[id[i]].push_back(idx);
          }
          idx++;
        }
      } else if (com[i] == 'D') {
        for (auto r : ls[id[i]]) {
          ref_id[r] = -1;
        }
        ls[id[i]].clear();
      } else {
        auto pos = lower_bound(begin(xs), end(xs), p[i]) - begin(xs);
        if (pos == (int)xs.size()) {
          cout << -1 << endl;
          continue;
        }
        if (xs[pos] > p[i])
          pos--;
        cout << ref_id[pos] << endl;
      }
    }
    cout << endl;
  }
}