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

#include <cstdio>
#include <iostream>
#include <vector>

#define lson (idx << 1)
#define rson (idx << 1 | 1)
#define INF 1000000000000LL
#define MAXN 200010

using namespace std;
typedef long long ll;

template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  T sig = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      sig = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + c - '0';
  x *= sig;
}

// `lo` is the current minimum of the segment.
// `lh` is the current value of the left endpoint.
// `lazy` is the lazy tag for addition.
// `flag` is the lazy tag for assignment.
struct Node {
  int l, r;
  ll lo, lh, lazy = 0;
  bool flag = false;
} s[MAXN << 2];
int h, w;

// Push up
void calc(int idx) {
  s[idx].lo = min(s[lson].lo, s[rson].lo);
  s[idx].lh = s[lson].lh;
}

void build(int idx, int l, int r) {
  s[idx].l = l, s[idx].r = r;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  build(lson, l, mid);
  build(rson, mid + 1, r);
}

// Lazy propagation
void pushdown(int idx) {
  if (s[idx].flag) {
    ll L = s[idx].lh;
    int l = s[idx].l;
    for (int i = lson; i <= rson; ++i) {
      s[i].lo = s[i].lh = L + s[i].l - l;
      s[i].flag = true;
      s[i].lazy = 0;
    }
  } else if (s[idx].lazy) {
    for (int i = lson; i <= rson; ++i) {
      s[i].lh += s[idx].lazy;
      s[i].lo += s[idx].lazy;
      s[i].lazy += s[idx].lazy;
    }
  }
  s[idx].flag = false;
  s[idx].lazy = 0;
}

// Assign segment [l, r] according to f[l-1] = L
void update(int idx, int l, int r, ll L) {
  if (s[idx].l >= l && s[idx].r <= r) {
    s[idx].lo = s[idx].lh = L + s[idx].l - l + 1;
    s[idx].flag = true;
    s[idx].lazy = 0;
    return;
  }
  pushdown(idx);
  int mid = (s[idx].l + s[idx].r) >> 1;
  if (l <= mid)
    update(lson, l, r, L);
  if (mid + 1 <= r)
    update(rson, l, r, L);
  calc(idx);
}

// Range addition
void add(int idx, int l, int r) {
  if (s[idx].l >= l && s[idx].r <= r) {
    s[idx].lh++;
    s[idx].lo++;
    s[idx].lazy++;
    return;
  }
  pushdown(idx);
  int mid = (s[idx].l + s[idx].r) >> 1;
  if (l <= mid)
    add(lson, l, r);
  if (mid + 1 <= r)
    add(rson, l, r);
  calc(idx);
}

// Range minimum query
ll query(int idx, int l, int r) {
  if (r < 1 || l > w)
    return INF;
  if (s[idx].l >= l && s[idx].r <= r)
    return s[idx].lo;
  pushdown(idx);
  ll ans = INF;
  int mid = (s[idx].l + s[idx].r) >> 1;
  if (l <= mid)
    ans = min(ans, query(lson, l, r));
  if (mid + 1 <= r)
    ans = min(ans, query(rson, l, r));
  return ans;
}

class Solution {
public:
  void solve() {
    read(h), read(w);
    vector<int> l(h), r(h);
    for (int i = 0; i < h; ++i)
      read(l[i]), read(r[i]);
    vector<ll> ans(h, INF);
    build(1, 1, w);
    for (int i = 0; i < h; ++i) {
      if (l[i] > 1)
        add(1, 1, l[i] - 1);
      if (r[i] < w)
        add(1, r[i] + 1, w);
      ll L = query(1, l[i] - 1, l[i] - 1);
      update(1, l[i], r[i], L);
      ans[i] = min(ans[i], query(1, 1, w));
      if (ans[i] == INF)
        break;
    }
    for (ll i : ans)
      cout << (i == INF ? -1 : i) << endl;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  Solution solution = Solution();
  solution.solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

void debug_out() { cerr << endl; }
template <class T> ostream &prnt(ostream &out, T v) {
  out << v.size() << '\\n';
  for (auto e : v)
    out << e << ' ';
  return out;
}
template <class T> ostream &operator<<(ostream &out, vector<T> v) {
  return prnt(out, v);
}
template <class T> ostream &operator<<(ostream &out, set<T> v) {
  return prnt(out, v);
}
template <class T1, class T2> ostream &operator<<(ostream &out, map<T1, T2> v) {
  return prnt(out, v);
}
template <class T1, class T2>
ostream &operator<<(ostream &out, pair<T1, T2> p) {
  return out << '(' << p.st << ' ' << p.nd << ')';
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << H;
  debug_out(T...);
}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n)                                                            \\
  do {                                                                         \\
    cerr << #x "[]: ";                                                         \\
    for (int _ = 0; _ < n; ++_)                                                \\
      cerr << x[_] << " ";                                                     \\
    cerr << '\\n';                                                              \\
  } while (0)
#define dbg_ok cerr << "OK!\\n"

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef cc_hash_table<int, int, hash<int>> ht;

#define int long long
#define st first
#define nd second

const int M = 1e8;
const int maxn = 2e5 + 5;
const int inf = 1e13;

// 1 base indexing build(1,1,n);
struct Data {
  // Use required attributes
  // Default Values
  int lo;
  int lazy_inc;
  int lazy_ass;
  Data() {
    lazy_inc = lazy_ass = 0;
    lo = inf;
  }
};

#define lnode 2 * node
#define rnode 2 * node + 1

struct SegTree {
  int N;
  vector<Data> st;

  void init(int n) {
    N = n;
    st.resize(4 * N + 5);
  }

  // Write reqd merge functions
  void merge1(Data &cur, Data &l, Data &r) { cur.lo = min(l.lo, r.lo); }

  // Handle lazy propagation appriopriately
  void propagate(int node, int L, int R) {
    if (st[node].lazy_ass) {
      st[lnode].lazy_ass = st[node].lazy_ass;
      st[rnode].lazy_ass = st[node].lazy_ass;
      st[node].lo = st[node].lazy_ass + L + 1;
      st[node].lazy_ass = 0;
      st[lnode].lazy_inc = 0;
      st[rnode].lazy_inc = 0;

    } else if (st[node].lazy_inc) {
      st[lnode].lazy_inc += st[node].lazy_inc;
      st[rnode].lazy_inc += st[node].lazy_inc;
      st[node].lo += st[node].lazy_inc;

      st[node].lazy_inc = 0;
    }
  }

  void build(int node, int L, int R) {
    if (L == R) {
      st[node].lo = L - 1;
      return;
    }
    int M = (L + R) / 2;
    build(lnode, L, M);
    build(rnode, M + 1, R);
    merge1(st[node], st[lnode], st[rnode]);
  }

  Data Query(int node, int L, int R, int i, int j) {
    propagate(node, L, R);
    if (j < L || i > R) {
      return Data();
    }
    if (i <= L && R <= j)
      return st[node];
    int M = (L + R) / 2;
    Data left = Query(lnode, L, M, i, j);
    Data right = Query(rnode, M + 1, R, i, j);
    Data cur;
    merge1(cur, left, right);
    return cur;
  }

  Data pQuery(int node, int L, int R, int pos) {
    return Query(node, L, R, pos, pos);
  }

  void Update(int node, int L, int R, int i, int j,
              int val) /// assignment of L-i
  {

    propagate(node, L, R);
    if (j < L || i > R)
      return;
    if (i <= L && R <= j) {
      st[node].lazy_ass = val;
      propagate(node, L, R);
      return;
    }
    int M = (L + R) / 2;
    Update(lnode, L, M, i, j, val);
    Update(rnode, M + 1, R, i, j, val);
    merge1(st[node], st[lnode], st[rnode]);
  }
  void Update2(int node, int L, int R, int i, int j, int val) {
    propagate(node, L, R);
    if (j < L || i > R)
      return;
    if (i <= L && R <= j) {
      st[node].lazy_inc += val;
      propagate(node, L, R);
      return;
    }
    int M = (L + R) / 2;
    Update2(lnode, L, M, i, j, val);
    Update2(rnode, M + 1, R, i, j, val);
    merge1(st[node], st[lnode], st[rnode]);
  }
  void pUpdate(int node, int L, int R, int pos, int val) {
    Update(node, L, R, pos, pos, val);
  }

  Data query(int pos) { return pQuery(1, 1, N, pos); }

  Data query(int l, int r) { return Query(1, 1, N, l, r); }

  void update(int pos, int val) { pUpdate(1, 1, N, pos, val); }

  void update(int l, int r, int val) { Update(1, 1, N, l, r, val); }
  void update2(int l, int r, int val) { Update2(1, 1, N, l, r, 1); }
};

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int h, w;
  cin >> h >> w;
  SegTree t;
  t.init(w);
  t.build(1, 1, w);
  for (int i = 0; i < h; i++) {
    int l, r;
    cin >> l >> r;
    if (l == 1 and r == w) {
      t.update(l, r, inf);
    } else if (l == 1) {
      t.update(l, r, inf);
      t.update2(r + 1, w, 1);
    } else {

      t.update2(1, l - 1, 1);
      if (r != w) {
        t.update2(r + 1, w, 1);
      }
      int n = (t.query(l - 1)).lo;
      t.update(l, r, n - l);
    }

    int ans = t.query(1, w).lo;
    if (ans < 1e10) {
      cout << ans;
    } else {
      cout << -1;
    }
    cout << "\\n";
  }
}