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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second

const long double pi = 3.141592653589793;
const int mod = 1e9 + 7;

int H, W, A, B;

int main() {

  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> H >> W;
  map<int, int> c2v;
  multiset<int> v;
  // if(c2v.lower_bound(22) == c2v.end()) cout<<"hello";
  rep(i, W) {
    c2v[i] = 0;
    v.insert(0);
  }

  rep(i, H) {
    cin >> A >> B;
    A--;
    B--;
    auto it = c2v.lower_bound(A);
    while (it != c2v.end() && it->first <= B) {
      if ((B + 1) < W && c2v.count(B + 1) == 0) {
        c2v[B + 1] = it->second + B - it->first + 1;
        v.insert(c2v[B + 1]);
      }

      if ((B + 1) < W && ((it->second + B - it->first + 1) < c2v[B + 1])) {
        v.erase(v.find(c2v[B + 1]));
        c2v[B + 1] = it->second + B - it->first + 1;
        v.insert(c2v[B + 1]);
      }
      v.erase(v.find(it->second));
      c2v.erase(it);

      it = c2v.lower_bound(A);
    }
    if (v.size() == 0)
      cout << -1 << endl;
    else
      cout << *v.begin() + i + 1 << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second

const long double pi = 3.141592653589793;
const int mod = 1e9 + 7;

int H, W, A, B;

int main() {

  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> H >> W;
  map<int, int> c2v;
  multiset<int> v;
  // if(c2v.lower_bound(22) == c2v.end()) cout<<"hello";
  rep(i, W) {
    c2v[i] = 0;
    v.insert(0);
  }

  rep(i, H) {
    cin >> A >> B;
    A--;
    B--;
    auto it = c2v.lower_bound(A);
    while (it != c2v.end() && it->first <= B) {
      if ((B + 1) < W && c2v.count(B + 1) == 0) {
        c2v[B + 1] = it->second + B - it->first + 1;
        v.insert(c2v[B + 1]);
      }

      if ((B + 1) < W && ((it->second + B - it->first + 1) < c2v[B + 1])) {
        v.erase(v.find(c2v[B + 1]));
        v.insert((it->second + B - it->first + 1));
      } else {
        v.erase(v.find(it->second));
      }
      c2v.erase(it);
      it = c2v.lower_bound(A);
    }
    if (v.size() == 0)
      cout << -1 << endl;
    else
      cout << *v.begin() + i + 1 << endl;
  }

  return 0;
}