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

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

typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define ALL(obj) (obj).begin(), (obj).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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};

int main() {
  cout << fixed << setprecision(10);

  ll h, w;
  cin >> h >> w;
  vint a(h), b(h);
  rep(i, h) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  using P = pair<ll, ll>;
  set<P> s;
  multiset<ll> ms;
  rep(i, w) {
    s.insert(make_pair(i, i));
    ms.insert(0);
  }

  rep(i, h) {
    // cout<<s.size()<<endl;
    // if(s.size() == 0) {cout<<-1<<endl; continue;}
    // for(auto p:s) cout<<"("<<p.second<<", "<<p.first<<") "; cout<<endl;
    auto it = s.lower_bound(make_pair(a[i], 0));
    P p;
    bool f = false;
    vector<P> cand;
    while (it != s.end() and it->first <= b[i]) {
      f = true;
      p = *it;
      // s.erase(it);
      cand.push_back(p);
      ms.erase(ms.find(it->first - it->second));
      it++;
    }
    for (auto p : cand)
      s.erase(p);

    if (f) {
      if (b[i] != w - 1) {
        p.first = b[i] + 1;
        s.insert(p);
        ms.insert(p.first - p.second);
      }
    }
    if (ms.size() == 0)
      cout << -1 << endl;
    else
      cout << *ms.begin() + i + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define ALL(obj) (obj).begin(), (obj).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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};

int main() {
  cout << fixed << setprecision(10);

  ll h, w;
  cin >> h >> w;
  vint a(h), b(h);
  rep(i, h) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }

  using P = pair<ll, ll>;
  set<P> s;
  multiset<ll> ms;
  rep(i, w) s.insert(make_pair(i, i)), ms.insert(0);

  rep(i, h) {
    // cout<<ms.size()<<endl;
    // cout<<"i: ";
    // cout<<i<<endl;
    // cout<<"s: ";
    // for(auto p:s) cout<<"("<<p.first<<", "<<p.second<<") "; cout<<endl;
    if (s.size() == 0) {
      cout << -1 << endl;
      continue;
    }
    auto it = s.lower_bound(make_pair(0, a[i]));
    vector<P> cand;
    while (it != s.end() and it->second <= b[i]) {
      cand.push_back(*it);
      it++;
    }
    // cout<<"cand: ";
    // for(auto p:cand) cout<<"("<<p.first<<", "<<p.second<<") "; cout<<endl;
    if (b[i] == w - 1) {
      for (auto p : cand) {
        s.erase(p);
        ms.erase(ms.find(p.second - p.first));
      }
    } else {
      rep(j, cand.size()) {
        auto p = cand[j];
        s.erase(p);
        ms.erase(ms.find(p.second - p.first));
      }
      auto p = cand.back();
      p.second = b[i] + 1;
      s.insert(p);
      ms.insert(p.second - p.first);
    }
    // for(auto e:ms) cout<<e<<" "; cout<<endl;
    if (ms.size() == 0)
      cout << -1 << endl;
    else {
      cout << *ms.begin() + i + 1 << endl;
    }
  }

  return 0;
}