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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(), (x).end()

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> class segtree {
private:
  int n, sz, h;
  vector<T> node, lazy;
  vector<bool> lazyFlag;
  void eval(int k) {
    if (lazyFlag[k]) {
      node[k] = lazy[k];
      if (k < n) {
        lazy[k * 2] = lazy[k * 2 + 1] = lazy[k];
        lazyFlag[k * 2] = lazyFlag[k * 2 + 1] = true;
      }
      lazyFlag[k] = false;
    }
  }

public:
  segtree(const vector<T> &v) : n(1), sz((int)v.size()), h(0) {
    while (n < sz)
      n *= 2, h++;
    node.resize(2 * n, numeric_limits<T>::max());
    lazy.resize(2 * n);
    lazyFlag.resize(2 * n, false);
    for (int i = 0; i < sz; i++)
      node[i + n] = v[i];
    for (int i = n - 1; i >= 1; i--)
      node[i] = min(node[i * 2], node[i * 2 + 1]);
  }
  void range(int a, int b, T x, int k = 1, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] = x, lazyFlag[k] = true;
      eval(k);
    } else {
      range(a, b, x, 2 * k, l, (l + r) / 2);
      range(a, b, x, 2 * k + 1, (l + r) / 2, r);
      node[k] = min(node[2 * k], node[2 * k + 1]);
    }
  }
  T query(int a, int b) {
    a += n, b += n - 1;
    for (int i = h; i > 0; i--)
      eval(a >> i), eval(b >> i);
    b++;
    T res1 = numeric_limits<T>::max(), res2 = numeric_limits<T>::max();
    while (a < b) {
      if (a & 1)
        eval(a), res1 = min(res1, node[a++]);
      if (b & 1)
        eval(--b), res2 = min(res2, node[b]);
      a >>= 1, b >>= 1;
    }
    return min(res1, res2);
  }
  void print() {
    for (int i = 0; i < sz; i++)
      cout << query(i, i + 1) << " ";
    cout << endl;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> m >> n;
  vector<int> aa(n);
  vector<int> bb(n);
  rep(i, n) { bb[i] = -i; }
  segtree<int> sg(aa);
  segtree<int> sg2(bb);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    int k = sg2.query(a, b + 1);
    int nxt = k + (b + 1);
    if (b < n - 1) {
      if (sg.query(b + 1, b + 2) > nxt) {
        sg.range(b + 1, b + 2, nxt);
        sg2.range(b + 1, b + 2, nxt - (b + 1));
      }
    }
    sg2.range(a, b + 1, inf);
    sg.range(a, b + 1, inf);
    int res = sg.query(0, n);
    if (res >= inf - 1000000) {
      cout << -1 << "\\n";
    } else {
      cout << res + i + 1 << "\\n";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(), (x).end()

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> class segtree {
private:
  int n, sz, h;
  vector<T> node, lazy;
  vector<bool> lazyFlag;
  void eval(int k) {
    if (lazyFlag[k]) {
      node[k] = lazy[k];
      if (k < n) {
        lazy[k * 2] = lazy[k * 2 + 1] = lazy[k];
        lazyFlag[k * 2] = lazyFlag[k * 2 + 1] = true;
      }
      lazyFlag[k] = false;
    }
  }

public:
  segtree(const vector<T> &v) : n(1), sz((int)v.size()), h(0) {
    while (n < sz)
      n *= 2, h++;
    node.resize(2 * n, numeric_limits<T>::max());
    lazy.resize(2 * n);
    lazyFlag.resize(2 * n, false);
    for (int i = 0; i < sz; i++)
      node[i + n] = v[i];
    for (int i = n - 1; i >= 1; i--)
      node[i] = min(node[i * 2], node[i * 2 + 1]);
  }
  void range(int a, int b, T x, int k = 1, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] = x, lazyFlag[k] = true;
      eval(k);
    } else {
      range(a, b, x, 2 * k, l, (l + r) / 2);
      range(a, b, x, 2 * k + 1, (l + r) / 2, r);
      node[k] = min(node[2 * k], node[2 * k + 1]);
    }
  }
  T query(int a, int b) {
    a += n, b += n - 1;
    for (int i = h; i > 0; i--)
      eval(a >> i), eval(b >> i);
    b++;
    T res1 = numeric_limits<T>::max(), res2 = numeric_limits<T>::max();
    while (a < b) {
      if (a & 1)
        eval(a), res1 = min(res1, node[a++]);
      if (b & 1)
        eval(--b), res2 = min(res2, node[b]);
      a >>= 1, b >>= 1;
    }
    return min(res1, res2);
  }
  void print() {
    for (int i = 0; i < sz; i++)
      cout << query(i, i + 1) << " ";
    cout << endl;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> aa(n);
  vector<int> bb(n);
  rep(i, n) { bb[i] = -i; }
  segtree<int> sg(aa);
  segtree<int> sg2(bb);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    int k = sg2.query(a, b + 1);
    int nxt = k + (b + 1);
    if (b < n - 1) {
      if (sg.query(b + 1, b + 2) > nxt) {
        sg.range(b + 1, b + 2, nxt);
        sg2.range(b + 1, b + 2, nxt - (b + 1));
      }
    }
    sg2.range(a, b + 1, inf);
    sg.range(a, b + 1, inf);
    int res = sg.query(0, n);
    if (res >= inf - 1000000) {
      cout << -1 << "\\n";
    } else {
      cout << res + i + 1 << "\\n";
    }
  }
  return 0;
}