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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const long long INF = (1e18);

int main() {
  int N;
  while (cin >> N, N) {
    map<pair<long long, long long>, long long> F;
    map<long long, vector<pair<long long, long long>>> M;
    F[{-INF, 0}] = -1;
    for (int i = 0; i < N; ++i) {
      char c;
      cin >> c;
      if (c == 'W') {
        long long l, s, r = 0;
        cin >> l >> s;
        vector<pair<long long, long long>> V;
        for (auto itr = F.begin(); itr != F.end(); ++itr) {
          r = max(r, itr->first.second);
          auto itr_ = itr;
          ++itr_;
          if (itr_ == F.end())
            continue;
          if (itr_->first.first != itr->first.second) {
            long long n = min(s, itr_->first.first - itr->first.second);
            s -= n;
            V.emplace_back(itr->first.second, itr->first.second + n);
            if (!s)
              break;
          }
        }
        if (s) {
          V.emplace_back(r, r + s);
        }
        for (int j = 0; j < (int)V.size(); ++j) {
          F[V.at(j)] = l;
          M[l].push_back(V.at(j));
        }

        sort(M[l].begin(), M[l].end());
        V = {};
        long long x = -2, y = -1;
        for (int j = 0; j < (int)M[l].size(); ++j) {
          if (y == M[l].at(j).first) {
            F.erase(F.find({x, y}));
            F.erase(F.find(M[l].at(j)));
            y = M[l].at(j).second;
            F[{x, y}] = l;
            V.pop_back();
            V.emplace_back(x, y);
          } else {
            x = M[l].at(j).first;
            y = M[l].at(j).second;
            V.emplace_back(x, y);
          }
        }
        M[l] = V;

      } else if (c == 'D') {
        long long l;
        cin >> l;
        for (int j = 0; j < (int)M[l].size(); ++j)
          F.erase(F.find(M[l].at(j)));
        M[l] = {};
      } else {
        long long p;
        cin >> p;
        auto itr = F.upper_bound({p, INF});
        // cout << itr->first.first << " " << itr->first.second << " " <<
        // itr->second << endl;
        assert(itr != F.begin());
        --itr;
        if (itr->first.second <= p)
          cout << -1 << endl;
        else
          cout << itr->second << endl;
      }
      // for(auto itr = F.begin(); itr != F.end(); ++itr) cout << "[" <<
      // itr->first.first << ", " << itr->first.second << ") =  " << itr->second
      // << endl;
    }
    cout << endl;
  }
  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

#include <iostream>
#include <map>
#include <vector>
using namespace std;
const long long INF = (1e18);

int main() {
  int N;
  while (cin >> N, N) {
    map<pair<long long, long long>, long long> F;
    map<long long, vector<pair<long long, long long>>> M;
    F[{-1, 0}] = -1;
    for (int i = 0; i < N; ++i) {
      char c;
      cin >> c;
      if (c == 'W') {
        long long l, s, r = 0;
        cin >> l >> s;
        vector<pair<long long, long long>> V;
        for (auto itr = F.begin(); itr != F.end(); ++itr) {
          r = max(r, itr->first.second);
          if (itr == F.end())
            continue;
          auto itr_ = itr;
          ++itr_;
          if (itr_->first.first != itr->first.second) {
            long long n = min(s, itr_->first.first - itr->first.second);
            s -= n;
            V.emplace_back(itr->first.second, itr->first.second + n);
            if (!s)
              break;
          }
        }
        if (s) {
          V.emplace_back(r, r + s);
        }
        for (int j = 0; j < (int)V.size(); ++j) {
          F[V[j]] = l;
          M[l].push_back(V[j]);
        }
      } else if (c == 'D') {
        long long l;
        cin >> l;
        for (int j = 0; j < (int)M[l].size(); ++j)
          F.erase(F.find(M[l][j]));
        M[l] = {};
      } else {
        long long p;
        cin >> p;
        auto itr = F.upper_bound({p, INF});
        // cout << itr->first.first << " " << itr->first.second << " " <<
        // itr->second << endl;
        --itr;
        if (itr->first.second <= p)
          cout << -1 << endl;
        else
          cout << itr->second << endl;
      }
      // for(auto itr = F.begin(); itr != F.end(); ++itr) cout <<
      // itr->first.first << " " << itr->first.second << " " << itr->second <<
      // endl;
    }
    cout << endl;
  }
  return 0;
}