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

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(v)                                                               \\
  cout << #v << ":";                                                           \\
  for (auto x : v) {                                                           \\
    cout << x << ' ';                                                          \\
  }                                                                            \\
  cout << endl;
#define mod 1000000007
using ll = long long;
const int INF = 1000000000;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

struct IOSetup {
  IOSetup() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(12);
  }
} iosetup;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++)
    os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}

template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
  // Monoid: 要素  OperatorMonoid: 作用素
  // 各マージ関数
  using F = function<Monoid(Monoid, Monoid)>;
  using G = function<Monoid(Monoid, OperatorMonoid)>;
  using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;

  int sz, height;
  vector<Monoid> data;
  vector<OperatorMonoid> lazy;
  const F f;
  const G g;
  const H h;
  // 単位元
  const Monoid M1;
  const OperatorMonoid OM0;

  LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1,
                  const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    height = 0;
    while (sz < n)
      sz <<= 1, height++;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) { data[k + sz] = x; }

  // セグ木のbuild
  void build() {
    for (int k = sz - 1; k > 0; k--)
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
  }

  // 伝播させる
  inline void propagate(int k) {
    if (lazy[k] != OM0) {
      // 子に伝えた後にkの場所のデータのほうの更新
      lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      data[k] = reflect(k);
      lazy[k] = OM0;
    }
  }

  inline Monoid reflect(int k) {
    return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
  }
  inline void recalc(int k) {
    // 下から上にdataの値を計算しなおす
    while (k >>= 1)
      data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
  }
  inline void thrust(int k) {
    for (int i = height; i > 0; i--)
      propagate(k >> i);
  }

  void update(int a, int b, const OperatorMonoid &x) {
    if (a >= b)
      return;
    thrust(a += sz);
    thrust(b += sz - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        lazy[l] = h(lazy[l], x), l++;
      if (r & 1)
        --r, lazy[r] = h(lazy[r], x);
    }
    // recalcすればpが要らない
    recalc(a);
    recalc(b);
  }

  Monoid query(int a, int b) {
    thrust(a += sz);
    thrust(b += sz - 1);
    Monoid L = M1, R = M1;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        L = f(L, reflect(l++));
      if (r & 1)
        R = f(reflect(--r), R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) { return query(k, k + 1); }
};

using M = pair<int, int>;
using OM = pair<int, int>;

const M M0 = M(INF, INF);
const OM OM0 = OM(-INF, -INF);

M segf(M l, M r) { return M(min(l.first, r.first), min(l.second, r.second)); }

M segg(M x, OM y) {
  if (y == OM0)
    return x;
  return y;
}

OM segh(OM x, OM y) { return y == OM0 ? x : y; }

signed main() {
  int h, w;
  cin >> h >> w;
  LazySegmentTree<M, OM> seg(w, segf, segg, segh, M0, OM0);
  rep(i, w) seg.set(i, M(0, w - 1 - i));
  seg.build();

  rep(i, h) {
    int a, b;
    cin >> a >> b;
    a--;
    auto p = seg.query(0, b);
    int nb = p.second - (w - 1 - b);
    if (b != w) {
      if (seg[b].first > nb)
        seg.update(b, b + 1, OM(nb, nb + (w - 1) - b));
    }
    seg.update(a, b, OM(INF, INF));
    auto res = seg.query(0, w);

    cout << (res.first >= 10000000 ? -1 : res.first + i + 1) << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(v)                                                               \\
  cout << #v << ":";                                                           \\
  for (auto x : v) {                                                           \\
    cout << x << ' ';                                                          \\
  }                                                                            \\
  cout << endl;
#define mod 1000000007
using ll = long long;
const int INF = 1000000000;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

struct IOSetup {
  IOSetup() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(12);
  }
} iosetup;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++)
    os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v)
    is >> x;
  return is;
}

template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
  // Monoid: 要素  OperatorMonoid: 作用素
  // 各マージ関数
  using F = function<Monoid(Monoid, Monoid)>;
  using G = function<Monoid(Monoid, OperatorMonoid)>;
  using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;

  int sz, height;
  vector<Monoid> data;
  vector<OperatorMonoid> lazy;
  const F f;
  const G g;
  const H h;
  // 単位元
  const Monoid M1;
  const OperatorMonoid OM0;

  LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1,
                  const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    height = 0;
    while (sz < n)
      sz <<= 1, height++;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) { data[k + sz] = x; }

  // セグ木のbuild
  void build() {
    for (int k = sz - 1; k > 0; k--)
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
  }

  // 伝播させる
  inline void propagate(int k) {
    if (lazy[k] != OM0) {
      // 子に伝えた後にkの場所のデータのほうの更新
      lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      data[k] = reflect(k);
      lazy[k] = OM0;
    }
  }

  inline Monoid reflect(int k) {
    return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
  }
  inline void recalc(int k) {
    // 下から上にdataの値を計算しなおす
    while (k >>= 1)
      data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
  }
  inline void thrust(int k) {
    for (int i = height; i > 0; i--)
      propagate(k >> i);
  }

  void update(int a, int b, const OperatorMonoid &x) {
    if (a >= b)
      return;
    thrust(a += sz);
    thrust(b += sz - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        lazy[l] = h(lazy[l], x), l++;
      if (r & 1)
        --r, lazy[r] = h(lazy[r], x);
    }
    // recalcすればpが要らない
    recalc(a);
    recalc(b);
  }

  Monoid query(int a, int b) {
    thrust(a += sz);
    thrust(b += sz - 1);
    Monoid L = M1, R = M1;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        L = f(L, reflect(l++));
      if (r & 1)
        R = f(reflect(--r), R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) { return query(k, k + 1); }
};

// 区間set 区間minmax
// この時h=g
ll gen = -LINF;
ll g(ll a, ll b) {
  if (b == gen)
    return a;
  else
    return b;
}
ll f(ll a, ll b) { return max(a, b); }

signed main() {
  int h, w;
  cin >> h >> w;
  vector<int> a(h), b(h);
  rep(i, h) {
    cin >> a[i] >> b[i];
    a[i]--;
  }

  LazySegmentTree<ll, ll> seg(w, f, g, g, gen, gen);
  rep(i, w) seg.set(i, h);
  seg.build();
  for (int i = h - 1; i >= 0; i--) {
    seg.update(a[i], b[i], i);
  }

  vector<int> res(h, INF);

  int mx = seg.query(0, w);
  // cout<<mx<<endl;

  rep(i, mx) { res[i] = i + 1; }

  auto f = [&](int m, bool upd = false) {
    int p = m;
    int cost = 0;
    rep(i, h) {
      if (a[i] <= p and p < b[i]) {
        cost += b[i] - p;
        p = b[i];
      }
      if (p == w) {
        return i;
      }
      cost++;
      if (upd)
        chmin(res[i], cost);
    }
    return h;
  };

  vector<int> st;
  rep(i, w) {
    st.push_back(a[i]);
    st.push_back(b[i]);
  }

  sort(ALL(st));
  st.erase(unique(ALL(st)), end(st));

  mt19937 mt(time(0));
  shuffle(ALL(st), mt);

  int sz = min(2000, (int)st.size());
  rep(i, sz) { f(st[i], true); }
  f(0, true);
  if (a[0] != 0)
    f(a[0] - 1, true);
  rep(i, h) {
    if (res[i] == INF)
      cout << -1 << endl;
    else
      cout << res[i] << endl;
  }
  return 0;
}