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

#include <bits/stdc++.h>
using namespace std;
#define LOCAL
#pragma region Macros
typedef long long ll;
#define ALL(x) (x).begin(), (x).end()
const long long MOD = 1e9 + 7;
// const long long MOD=998244353;
const int INF = 1e9;
const long long IINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const char dir[4] = {'D', 'R', 'U', 'L'};

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i) {
    os << v[i] << (i + 1 == v.size() ? "" : " ");
  }
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << '(' << p.first << ',' << p.second << ')';
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << '{';
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    os << '(' << itr->first << ',' << itr->second << ')';
    if (++itr != m.end())
      os << ',';
    --itr;
  }
  os << '}';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &s) {
  os << '{';
  for (auto itr = s.begin(); itr != s.end(); ++itr) {
    os << *itr;
    if (++itr != s.end())
      os << ',';
    --itr;
  }
  os << '}';
  return os;
}

void debug_out() { cerr << '\\n'; }
template <class Head, class... Tail>
void debug_out(Head &&head, Tail &&...tail) {
  cerr << head;
  if (sizeof...(Tail) > 0)
    cerr << ", ";
  debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...)                                                             \\
  cerr << " ";                                                                 \\
  cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]"      \\
       << '\\n';                                                                \\
  cerr << " ";                                                                 \\
  debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

template <class T1, class T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion

template <typename Monoid, typename OperatorMonoid> struct LazySegmentTree {
  typedef function<Monoid(Monoid, Monoid)> F;
  typedef function<Monoid(Monoid, OperatorMonoid)> G;
  typedef function<OperatorMonoid(OperatorMonoid, OperatorMonoid)> H;
  int n, hi;
  F f;
  G g;
  H h;
  Monoid id0;
  OperatorMonoid id1;
  vector<Monoid> dat;
  vector<OperatorMonoid> laz;
  LazySegmentTree(int n_, F f, G g, H h, Monoid id0, OperatorMonoid id1)
      : f(f), g(g), h(h), id0(id0), id1(id1) {
    init(n_);
  }
  void init(int n_) {
    n = 1, hi = 0;
    while (n < n_)
      n <<= 1, ++hi;
    dat.assign(n << 1, id0);
    laz.assign(n << 1, id1);
  }
  void build(const vector<Monoid> &v) {
    for (int i = 0; i < v.size(); ++i)
      dat[i + n] = v[i];
    for (int i = n - 1; i; --i)
      dat[i] = f(dat[i << 1 | 0], dat[i << 1 | 1]);
  }
  inline Monoid reflect(int k) {
    return laz[k] == id1 ? dat[k] : g(dat[k], laz[k]);
  }
  inline void propagate(int k) {
    if (laz[k] == id1)
      return;
    laz[k << 1 | 0] = h(laz[k << 1 | 0], laz[k]);
    laz[k << 1 | 1] = h(laz[k << 1 | 1], laz[k]);
    dat[k] = reflect(k);
    laz[k] = id1;
  }
  inline void thrust(int k) {
    for (int i = hi; i; --i)
      propagate(k >> i);
  }
  inline void recalc(int k) {
    while (k >>= 1)
      dat[k] = f(reflect(k << 1 | 0), reflect(k << 1 | 1));
  }
  void update(int a, int b, OperatorMonoid x) {
    if (a >= b)
      return;
    thrust(a += n), thrust(b += n - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        laz[l] = h(laz[l], x), ++l;
      if (r & 1)
        --r, laz[r] = h(laz[r], x);
    }
    recalc(a), recalc(b);
  }
  void set_val(int k, Monoid x) {
    thrust(k += n);
    dat[k] = x, laz[k] = id1;
    recalc(k);
  }
  Monoid query(int a, int b) {
    if (a >= b)
      return id0;
    thrust(a += n), thrust(b += n - 1);
    Monoid vl = id0, vr = id0;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        vl = f(vl, reflect(l++));
      if (r & 1)
        vr = f(reflect(--r), vr);
    }
    return f(vl, vr);
  }
  template <typename C>
  int find(int st, const C &check, Monoid &acc, int k, int l, int r) {
    if (l + 1 == r) {
      acc = f(acc, reflect(k));
      return check(acc) ? k - n : -1;
    }
    propagate(k);
    int m = (l + r) >> 1;
    if (m <= st)
      return find(st, check, acc, (k << 1) | 1, m, r);
    if (st <= l && !check(f(acc, dat[k]))) {
      acc = f(acc, dat[k]);
      return -1;
    }
    int vr = find(st, check, acc, (k << 1) | 1, m, r);
    if (~vr)
      return vr;
    return find(st, check, acc, (k << 1) | 0, l, m);
  }
  template <typename C> int find(int st, const C &check) {
    Monoid acc = id0;
    return find(st, check, acc, 1, 0, n);
  }
  Monoid operator[](int i) { return query(i, i + 1); }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W;
  cin >> H >> W;
  vector<int> A(H), B(H);
  for (int i = 0; i < H; ++i)
    cin >> A[i] >> B[i], --A[i];

  auto f = [](int a, int b) { return min(a, b); };
  auto g = [](int a, int x) { return x != -1 ? x : a; };
  auto h = [](int a, int b) { return b; };
  LazySegmentTree<int, int> seg(W + 1, f, g, h, INF, -1),
      nseg(W + 1, f, g, h, W, -1);
  vector<int> v(W + 1, 0);
  seg.build(v);
  seg.set_val(W, INF);
  iota(v.begin(), v.end(), 0);
  nseg.build(v);

  for (int i = 0; i < H; ++i) {
    int l = nseg.find(0, [&](int x) { return x < A[i]; }) + 1;
    int r = nseg.find(0, [&](int x) { return x < B[i]; });
    if (~r && B[i] < W)
      seg.set_val(B[i], min(seg[B[i]], B[i] - r));
    nseg.update(l, r + 1, B[i]);
    seg.update(A[i], B[i], INF);
    int ans = seg.query(0, W);
    cout << (ans == INF ? -1 : ans + i + 1) << '\\n';
  }
}

Error: -11
 l,r,dat[k] :[150:find]
 0, 8, 0
 l,r,dat[k] :[150:find]
 4, 8, 4
 l,r,dat[k] :[150:find]
 0, 4, 0
 l,r,dat[k] :[150:find]
 2, 4, 2
 l,r,dat[k] :[150:find]
 0, 2, 0
 l,r,dat[k] :[150:find]
 1, 2, 1
 l,r,dat[k] :[150:find]
 0, 1, 0
 l,r,dat[k] :[150:find]
 0, 8, 0
 l,r,dat[k] :[150:find]
 4, 8, 4
 l,r,dat[k] :[150:find]
 0, 4, 0
 l,r,dat[k] :[150:find]
 2, 4, 2
 l,r,dat[k] :[150:find]
 3, 4, 3
 l,r,dat[k] :[150:find]
 0, 8, 0
 l,r,dat[k] :[150:find]
 0, 8, 0
 l,r,dat[k] :[150:find]
 4, 8, 4
 l,r,dat[k] :[150:find]
 0, 4, 0
 l,r,dat[k] :[150:find]
 2, 4, 4
 l,r,dat[k] :[150:find]
 0, 2, 0
 l,r,dat[k] :[150:find]
 1, 2, 4
 l,r,dat[k] :[150:find]
 0, 1, 0
 l,r,dat[k] :[150:find]
 0, 8, 1
 l,r,dat[k] :[150:find]
 0, 8, 1
 l,r,dat[k] :[150:find]
 4, 8, 4
 l,r,dat[k] :[150:find]
 0, 4, 1
 l,r,dat[k] :[150:find]
 2, 4, 4
 l,r,dat[k] :[150:find]
 0, 2, 1
 l,r,dat[k] :[150:find]
 1, 2, 4
 l,r,dat[k] :[150:find]
 0, 1, 1
 l,r,dat[k] :[150:find]
 0, 8, 3
 l,r,dat[k] :[150:find]
 0, 8, 3
 l,r,dat[k] :[150:find]
 4, 8, 4
 l,r,dat[k] :[150:find]
 0, 4, 3
 l,r,dat[k] :[150:find]
 2, 4, 4
 l,r,dat[k] :[150:find]
 0, 2, 3
 l,r,dat[k] :[150:find]
 1, 2, 4
 l,r,dat[k] :[150:find]
 0, 1, 3


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define LOCAL
#pragma region Macros
typedef long long ll;
#define ALL(x) (x).begin(), (x).end()
const long long MOD = 1e9 + 7;
// const long long MOD=998244353;
const int INF = 1e9;
const long long IINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const char dir[4] = {'D', 'R', 'U', 'L'};

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i) {
    os << v[i] << (i + 1 == v.size() ? "" : " ");
  }
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << '(' << p.first << ',' << p.second << ')';
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << '{';
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    os << '(' << itr->first << ',' << itr->second << ')';
    if (++itr != m.end())
      os << ',';
    --itr;
  }
  os << '}';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &s) {
  os << '{';
  for (auto itr = s.begin(); itr != s.end(); ++itr) {
    os << *itr;
    if (++itr != s.end())
      os << ',';
    --itr;
  }
  os << '}';
  return os;
}

void debug_out() { cerr << '\\n'; }
template <class Head, class... Tail>
void debug_out(Head &&head, Tail &&...tail) {
  cerr << head;
  if (sizeof...(Tail) > 0)
    cerr << ", ";
  debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...)                                                             \\
  cerr << " ";                                                                 \\
  cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]"      \\
       << '\\n';                                                                \\
  cerr << " ";                                                                 \\
  debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

template <class T1, class T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion

template <typename Monoid, typename OperatorMonoid> struct LazySegmentTree {
  typedef function<Monoid(Monoid, Monoid)> F;
  typedef function<Monoid(Monoid, OperatorMonoid)> G;
  typedef function<OperatorMonoid(OperatorMonoid, OperatorMonoid)> H;
  int n, hi;
  F f;
  G g;
  H h;
  Monoid id0;
  OperatorMonoid id1;
  vector<Monoid> dat;
  vector<OperatorMonoid> laz;
  LazySegmentTree(int n_, F f, G g, H h, Monoid id0, OperatorMonoid id1)
      : f(f), g(g), h(h), id0(id0), id1(id1) {
    init(n_);
  }
  void init(int n_) {
    n = 1, hi = 0;
    while (n < n_)
      n <<= 1, ++hi;
    dat.assign(n << 1, id0);
    laz.assign(n << 1, id1);
  }
  void build(const vector<Monoid> &v) {
    for (int i = 0; i < v.size(); ++i)
      dat[i + n] = v[i];
    for (int i = n - 1; i; --i)
      dat[i] = f(dat[i << 1 | 0], dat[i << 1 | 1]);
  }
  inline Monoid reflect(int k) {
    return laz[k] == id1 ? dat[k] : g(dat[k], laz[k]);
  }
  inline void propagate(int k) {
    if (laz[k] == id1)
      return;
    laz[k << 1 | 0] = h(laz[k << 1 | 0], laz[k]);
    laz[k << 1 | 1] = h(laz[k << 1 | 1], laz[k]);
    dat[k] = reflect(k);
    laz[k] = id1;
  }
  inline void thrust(int k) {
    for (int i = hi; i; --i)
      propagate(k >> i);
  }
  inline void recalc(int k) {
    while (k >>= 1)
      dat[k] = f(reflect(k << 1 | 0), reflect(k << 1 | 1));
  }
  void update(int a, int b, OperatorMonoid x) {
    if (a >= b)
      return;
    thrust(a += n), thrust(b += n - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        laz[l] = h(laz[l], x), ++l;
      if (r & 1)
        --r, laz[r] = h(laz[r], x);
    }
    recalc(a), recalc(b);
  }
  void set_val(int k, Monoid x) {
    thrust(k += n);
    dat[k] = x, laz[k] = id1;
    recalc(k);
  }
  Monoid query(int a, int b) {
    if (a >= b)
      return id0;
    thrust(a += n), thrust(b += n - 1);
    Monoid vl = id0, vr = id0;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        vl = f(vl, reflect(l++));
      if (r & 1)
        vr = f(reflect(--r), vr);
    }
    return f(vl, vr);
  }
  // most right position that meets condition "check"
  template <typename C>
  int find(int a, int b, const C check, int k, int l, int r) {
    propagate(k);
    debug(l, r, dat[k]);
    if (!check(dat[k]) || r <= a || b <= l)
      return -1;
    if (l + 1 == r)
      return k - n;
    int vr = find(a, b, check, (k << 1) | 1, (l + r) >> 1, r);
    if (~vr)
      return vr;
    return find(a, b, check, (k << 1) | 0, l, (l + r) >> 1);
  }
  template <typename C> int find(int a, int b, const C check) {
    thrust(a + n), thrust(b + n - 1);
    return find(a, b, check, 1, 0, n);
  }
  Monoid operator[](int i) { return query(i, i + 1); }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W;
  cin >> H >> W;
  vector<int> A(H), B(H);
  for (int i = 0; i < H; ++i)
    cin >> A[i] >> B[i], --A[i];

  auto f = [](int a, int b) { return min(a, b); };
  auto g = [](int a, int x) { return x != -1 ? x : a; };
  auto h = [](int a, int b) { return b; };
  LazySegmentTree<int, int> seg(W + 1, f, g, h, INF, -1),
      nseg(W + 1, f, g, h, W, -1);
  vector<int> v(W + 1, 0);
  seg.build(v);
  seg.set_val(W, INF);
  iota(v.begin(), v.end(), 0);
  nseg.build(v);

  for (int i = 0; i < H; ++i) {
    // for (int j=0;j<W;++j) cerr << nseg[j] << (j+1==W?'\\n':' ');
    int l = nseg.find(0, W, [&](int x) { return x < A[i]; }) + 1;
    int r = nseg.find(0, W, [&](int x) { return x < B[i]; });
    if (~r && B[i] < W)
      seg.set_val(B[i], min(seg[B[i]], B[i] - r));
    nseg.update(l, r + 1, B[i]);
    seg.update(A[i], B[i], INF);
    int ans = seg.query(0, W);
    cout << (ans == INF ? -1 : ans + i + 1) << '\\n';
  }
}