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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_poizcy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (int i = b; i < e; i++)

using namespace std;
// using namespace __gnu_pbds;
typedef long double lldb;
typedef pair<ll, ll> ii;
typedef pair<lldb, lldb> iidb;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<ll> vll;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e9 + 7;
const lldb PI = acos(-1);
#define initseg ll new_nodo = (nodo * 2), mid = (iz + der) / 2;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define tam 2020000
#define offset 200
#define ptr nodo *
const ll MOD = 1e9 + 7;
#define lazy first
#define sum second
int vmin[tam], viz[tam], vder[tam];
int main() {
  forr(i, 0, tam) vmin[i] = INF;
  int H, W;
  cin >> H >> W;
  map<int, int> mapa;
  multiset<int> mins;
  forr(i, 1, W + 1) {
    mapa[i] = 0;
    mins.insert(0);
  }

  forr(i, 0, H) {
    cin >> viz[i] >> vder[i];
    auto iteiz = mapa.lower_bound(viz[i]);
    int minimo = INF;
    while (iteiz != mapa.end()) {
      if (iteiz->fi > vder[i])
        break;
      minimo = min(minimo, iteiz->se + (vder[i] + 1) - iteiz->fi);
      auto elim = iteiz;
      mins.erase(mins.find(elim->se));
      iteiz++;
      mapa.erase(elim);
    }
    // if (minimo==INF)continue;
    if (vder[i] + 1 <= W && minimo != INF) {
      if (mapa.find(vder[i] + 1) == mapa.end()) {
        mapa[vder[i] + 1] = minimo;
        mins.insert(minimo);
      } else {
        if (mapa[vder[i] + 1] > minimo) {
          mins.erase(mins.find(mapa[vder[i] + 1]));
          mins.insert(minimo);
          mapa[vder[i] + 1] = minimo;
        }
      }
    }

    if (!mins.empty())
      cout << (*mins.begin()) + i + 1 << endl;
    else
      cout << -1 << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_poizcy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (int i = b; i < e; i++)

using namespace std;
// using namespace __gnu_pbds;
typedef long double lldb;
typedef pair<ll, ll> ii;
typedef pair<lldb, lldb> iidb;
typedef pair<ll, ii> iii;
typedef vector<ll> vi;
typedef vector<ll> vll;
// typedef
// tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e9 + 7;
const lldb PI = acos(-1);
#define initseg ll new_nodo = (nodo * 2), mid = (iz + der) / 2;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define tam 2020000
#define offset 200
#define ptr nodo *
const ll MOD = 1e9 + 7;
#define lazy first
#define sum second
int vmin[tam], viz[tam], vder[tam];
int main() {
  forr(i, 0, tam) vmin[i] = INF;
  int H, W;
  cin >> H >> W;
  map<int, int> mapa;
  multiset<int> mins;
  forr(i, 1, W + 1) {
    mapa[i] = 0;
    mins.insert(0);
  }

  forr(i, 0, H) {
    cin >> viz[i] >> vder[i];
    auto iteiz = mapa.lower_bound(viz[i]);
    int minimo = INF;
    while (iteiz != mapa.end()) {
      if (iteiz->fi > vder[i])
        break;
      minimo = min(minimo, iteiz->se + (vder[i] + 1) - iteiz->fi);
      auto elim = iteiz;
      mins.erase(mins.find(elim->se));
      iteiz++;
      mapa.erase(elim);
    }
    if (vder[i] + 1 <= W) {
      if (mapa.find(vder[i] + 1) == mapa.end()) {
        mapa[vder[i] + 1] = minimo;
        mins.insert(minimo);
      } else {
        if (mapa[vder[i] + 1] > minimo) {
          mins.erase(mapa[vder[i] + 1]);
          mins.insert(minimo);
        }
      }
    }

    if (!mins.empty())
      cout << (*mins.begin()) + i + 1 << endl;
    else
      cout << -1 << endl;
  }
}