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

/**
 *   @FileName	f.cpp
 *   @Author	kanpurin
 *   @Created	2020.08.30 16:54:15
 **/

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template <typename Monoid> struct LazySegmentTree {
private:
  using Func = std::function<Monoid(Monoid, Monoid)>;
  Func F = [](Monoid a, Monoid b) { return min(a, b); };
  Monoid UNITY = numeric_limits<Monoid>::max();
  int n;
  std::vector<int> prop;

  void eval(int k, int l, int r) {
    if (prop[k] != 0) {
      node[k] = lazy[k] + (prop[k] == 1 ? node[k] : 0);
      if (r - l > 1) {
        lazy[2 * k + 1] = lazy[k] + (prop[k] == 1 ? lazy[2 * k + 1] : 0);
        lazy[2 * k + 2] = lazy[k] + (prop[k] == 1 ? lazy[2 * k + 2] : 0);
        prop[2 * k + 1] = prop[2 * k + 2] = prop[k];
      }
      lazy[k] = 0;
      prop[k] = 0;
    }
  }
  Monoid _get_query(int a, int b, int &idx, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (r <= a || b <= l) {
      idx = -1;
      return UNITY;
    }
    eval(k, l, r);
    if (a <= l && r <= b) {
      idx = index[k];
      return node[k];
    }
    int l_idx, r_idx;
    Monoid vl = _get_query(a, b, l_idx, 2 * k + 1, l, (r - l) / 2 + l);
    Monoid vr = _get_query(a, b, r_idx, 2 * k + 2, (r - l) / 2 + l, r);
    idx = F(vl, vr) == vl && l_idx != -1 ? l_idx : r_idx;

    return F(vl, vr);
  }

public:
  std::vector<int> index;
  std::vector<Monoid> node, lazy;
  LazySegmentTree(int m, Monoid val) {
    n = 1;
    while (n < m)
      n <<= 1;
    node.resize(n * 2 - 1, UNITY);
    lazy.resize(n * 2 - 1, 0);
    prop.resize(n * 2 - 1, 0);
    index.resize(n * 2 - 1, -1);
    for (int i = 0; i < m; i++)
      index[n + i - 1] = i;
    if (val != LazySegmentTree::UNITY) {
      for (int i = 0; i < m; i++)
        node[n + i - 1] = val;
      build();
    }
  }
  LazySegmentTree(const std::vector<Monoid> &v) {
    int sz = v.size();
    n = 1;
    while (n < sz)
      n <<= 1;
    node.resize(n * 2 - 1, UNITY);
    lazy.resize(n * 2 - 1, 0);
    prop.resize(n * 2 - 1, 0);
    index.resize(n * 2 - 1, 0);
    for (int i = 0; i < sz; i++)
      index[n + i - 1] = i;
    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    build();
  }

  void set(int k, const Monoid &x) { node[n + k - 1] = x; }
  void build() {
    for (int i = n - 2; i >= 0; i--)
      node[i] = F(node[2 * i + 1], node[2 * i + 2]);
    for (int i = n - 2; i >= 0; i--)
      index[i] = node[2 * i + 1] == F(node[2 * i + 1], node[2 * i + 2])
                     ? index[2 * i + 1]
                     : index[2 * i + 2];
  }

  void add_query(int a, int b, const Monoid &x, int k = 0, int l = 0,
                 int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);

    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      prop[k] = 1;
      lazy[k] += x;
      eval(k, l, r);
    } else {
      add_query(a, b, x, 2 * k + 1, l, (r - l) / 2 + l);
      add_query(a, b, x, 2 * k + 2, (r - l) / 2 + l, r);
      index[k] = F(node[2 * k + 1], node[2 * k + 2]) == node[2 * k + 1]
                     ? index[2 * k + 1]
                     : index[2 * k + 2];

      node[k] = F(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  void update_query(int a, int b, const Monoid &x, int k = 0, int l = 0,
                    int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);

    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      prop[k] = 2;
      lazy[k] = x;
      index[k] = l;

      eval(k, l, r);
    } else {
      update_query(a, b, x, 2 * k + 1, l, (r - l) / 2 + l);
      update_query(a, b, x, 2 * k + 2, (r - l) / 2 + l, r);
      index[k] = F(node[2 * k + 1], node[2 * k + 2]) == node[2 * k + 1]
                     ? index[2 * k + 1]
                     : index[2 * k + 2];

      node[k] = F(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  Monoid get_query(int a, int b) {
    int tmp;
    return _get_query(a, b, tmp);
  }

  int get_index_query(int a, int b) {
    int idx;
    _get_query(a, b, idx);
    return idx;
  }
  Monoid operator[](int x) { return get_query(x, x + 1); }
  void print() {
    for (int i = 0; i < n; i++) {
      std::cout << i << "\\t: " << get_query(i, i + 1) << std::endl;
    }
  }
};
int main() {
  int h, w;
  cin >> h >> w;
  LazySegmentTree<ll> seg(w, 0);
  for (int i = 0; i < h; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    while (true) {
      int idx = seg.get_index_query(a, b + 1);
      ll val = seg.get_query(idx, idx + 1);

      if (val == numeric_limits<ll>::max())
        break;
      ll hor = b + 1 - idx;
      if (seg.get_query(b + 1, b + 2) > val + hor)
        seg.update_query(b + 1, b + 2, val + hor);
      seg.update_query(idx, idx + 1, numeric_limits<ll>::max());
    }
    ll minhor = seg.get_query(0, w);
    cout << (minhor == numeric_limits<ll>::max() ? -1 : minhor + i + 1) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

/**
 *   @FileName	f.cpp
 *   @Author	kanpurin
 *   @Created	2020.08.30 15:24:52
 **/

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

int main() {
  int h, w;
  cin >> h >> w;
  set<pair<int, int>> st1, st2;
  for (int i = 0; i < w; i++) {
    st1.insert({i, i});
    st2.insert({0, i});
  }
  for (int i = 0; i < h; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    auto itr = st1.lower_bound({b + 1, w});
    if (st1.begin() != itr)
      itr--;

    if (b + 1 == w) {
      while (itr->first >= a) {
        st2.erase({itr->first - itr->second, itr->second});
        if (st1.begin() != itr)
          st1.erase(itr--);
        else {
          st1.erase(itr);
          break;
        }
      }
    } else {
      bool fir = true;
      while (itr->first >= a) {
        if (fir) {
          st1.insert({b + 1, itr->second});
          st2.insert({b + 1 - itr->second, itr->second});
          fir = false;
        }
        st2.erase({itr->first - itr->second, itr->second});
        if (st1.begin() != itr)
          st1.erase(itr--);
        else {
          st1.erase(itr);
          break;
        }
      }
    }

    if (st2.size() == 0)
      cout << -1 << endl;
    else
      cout << st2.begin()->first + i + 1 << endl;
  }
  return 0;
}