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

#include <bits/stdc++.h>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll, ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define rep(n) for (ll i = 0; i < n; i++)
#define repi(i, n) for (ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

int main() {
  FAST /**/

      ll h,
      w;
  cin >> h >> w;

  pair<ll, ll> hold[h + 1];
  for (ll i = 0; i < h; i++)
    cin >> hold[i].ff >> hold[i].ss, hold[i].ff--, hold[i].ss--;
  hold[h] = mp(w, w);

  ll n = h, m = w;

  set<pair<ll, ll>> st;
  rep(w) st.insert(mp(i, 0ll));
  multiset<ll> dis;
  rep(w) dis.insert(0);
  ll fin[n + 1];
  rep(n) fin[i + 1] = -1;
  fin[0] = 0;
  for (ll i = 1; i <= h; i++) {
    ll l = hold[i - 1].ff;
    ll r = hold[i - 1].ss;
    auto it = st.lower_bound(mp(l - 1, inf));
    vector<pair<ll, ll>> er;
    ll diste = inf;
    while (it != st.end()) {
      if (it->ff > r)
        break;
      er.pb(*it);
      diste = min(diste, r + 1 - it->ff + it->ss);
      dis.erase(dis.find(it->ss));
      it++;
    }

    rep(er.size()) st.erase(er[i]);

    er.clear();

    it = st.find(mp(r, inf));

    if (diste != inf && r + 1 < w &&
        (it == st.end() || (it->ff > r + 1) ||
         (it->ff == r + 1 && it->ss > diste))) {
      st.insert(mp(r + 1, diste));
      it = st.find(mp(r + 1, diste));
      dis.insert(diste);
      it++;
      while (it != st.end()) {
        if (diste + it->ff - (r + 1) <= it->ss)
          er.pb(*it), dis.erase(dis.find(it->ss));
        else
          break;
        it++;
      }

      autoit(er, it) st.erase(*it);
    }

    /*autoit(st,it)
    {
        cout<<it->ff+1<<" "<<it->ss<<"\\n";
    }
    cout<<"\\n";*/

    if (dis.size() > 0)
      fin[i] = *dis.begin() + i;
  }

  rep(n) cout << fin[i + 1] << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll, ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define rep(n) for (ll i = 0; i < n; i++)
#define repi(i, n) for (ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());

int main() {
  FAST /**/

      ll h,
      w;
  cin >> h >> w;

  queue<pair<ll, ll>> q;
  queue<ll> dis;

  pair<ll, ll> hold[h + 1];
  for (ll i = 0; i < h; i++)
    cin >> hold[i].ff >> hold[i].ss, hold[i].ff--, hold[i].ss--;
  hold[h] = mp(w, w);

  vector<pair<ll, ll>> gr[h + 1][w];
  rep(h + 1) {
    repi(j, w) {
      if (j + 1 < w)
        gr[i][j].pb(mp(i, j + 1));
      if (i + 1 < h + 1 && (j < hold[i].ff || j > hold[i].ss))
        gr[i][j].pb(mp(i + 1, j));
    }
  }

  rep(w) q.push(mp(0, i)), dis.push(0);
  ll fin[h + 1][w];
  ll n = h, m = w;
  rep(n + 1) repi(j, m) if (i == 0) fin[i][j] = 0;
  else fin[i][j] = inf;

  while (q.size() > 0) {
    auto curr = q.front();
    q.pop();

    ll di = dis.front();
    dis.pop();

    ll fs = curr.ff, sc = curr.ss;
    autoit(gr[fs][sc], it) {
      ll x = it->ff;
      ll y = it->ss;
      if (fin[x][y] != inf)
        continue;
      fin[x][y] = di + 1;
      q.push(*it);
      dis.push(di + 1);
    }
  }

  for (ll i = 0; i < n + 1; i++) {
    for (ll j = 1; j < w; j++)
      fin[i][j] = min(fin[i][j], fin[i][j - 1]);
    if (fin[i][w - 1] == inf)
      fin[i][w - 1] = -1;
  }

  rep(n) cout << fin[i + 1][w - 1] << "\\n";

  return 0;
}