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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

int main() {
  int h, w;
  cin >> h >> w;
  map<int, int> dist;
  multiset<int> values;
  for (int i = 0; i < w; i++) {
    dist[i] = 0;
    values.insert(0);
  }

  for (int i = 0; i < h; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    if (auto to = dist.upper_bound(b);
        to != dist.begin() && b + 1 < w && dist.count(b + 1) == 0) {
      auto prev_to = prev(to);
      dist[b + 1] = prev_to->second + (b + 1 - prev_to->first);
      values.insert(dist[b + 1]);
    }

    if (auto to = dist.lower_bound(a);
        to != dist.begin() && dist.count(a - 1) == 0) {
      auto prev_to = prev(to);
      dist[a - 1] = prev_to->second + (a - 1 - prev_to->first);
      values.insert(dist[a - 1]);
    }

    for (auto to = dist.lower_bound(a); to != dist.end() && to->first <= b;) {
      int value = to->second;
      auto pos = values.lower_bound(value);
      values.erase(pos);
      dist.erase(to++);
    }

    /*        for (auto p : dist) {
                cout << p.first << ":" << p.second << " ";
            }
            cout << endl;
            for (auto v : values) {
                cout << v << " ";
            }
            cout << endl;*/

    if (values.empty())
      cout << -1 << endl;
    else
      cout << *values.begin() + i + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

int main() {
  int h, w;
  cin >> h >> w;
  map<int, int> dist;
  multiset<int> values;
  for (int i = 0; i < w; i++) {
    dist[i] = 0;
    values.insert(0);
  }

  for (int i = 0; i < h; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    if (auto to = dist.upper_bound(b); to != dist.begin() && b + 1 < w) {
      auto prev_to = prev(to);
      if (dist.count(b + 1)) {
        values.erase(values.lower_bound(dist[b + 1]));
      }
      dist[b + 1] = prev_to->second + (b + 1 - prev_to->first);
      values.insert(dist[b + 1]);
    }

    if (auto to = dist.lower_bound(a); to != dist.begin()) {
      auto prev_to = prev(to);
      if (dist.count(a - 1)) {
        values.erase(values.lower_bound(dist[b + 1]));
      }
      dist[a - 1] = prev_to->second + (a - 1 - prev_to->first);
      values.insert(dist[a - 1]);
    }

    for (auto to = dist.lower_bound(a); to != dist.end() && to->first <= b;) {
      int value = to->second;
      auto pos = values.lower_bound(value);
      values.erase(pos);
      dist.erase(to++);
    }

    /*        for (auto p : dist) {
                cout << p.first << ":" << p.second << " ";
            }
            cout << endl;
            for (auto v : values) {
                cout << v << " ";
            }
            cout << endl;*/

    if (values.empty())
      cout << -1 << endl;
    else
      cout << *values.begin() + i + 1 << endl;
  }
  return 0;
}