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

#include <bits/stdc++.h>
using namespace std;
#define SZ(a) ((int)(a).size())
typedef long long int64;
const int maxn = 200009;
const int inf = 1e9;
int v1[maxn << 2], v2[maxn << 2], lz1[maxn << 2], lz2[maxn << 2];
// lz1覆盖 lz2+v v1=v2-下标
void build(int rt, int l, int r) {
  lz1[rt] = -inf;
  if (l == r) {
    v1[rt] = -l;
    return;
  }
  int mid = (l + r) >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
  v1[rt] = min(v1[rt << 1], v1[rt << 1 | 1]);
}
void pushdownlz1(int rt, int l, int mid) {
  if (lz1[rt] != -inf) {
    lz1[rt << 1] = lz1[rt];
    lz1[rt << 1 | 1] = lz1[rt];
    lz2[rt << 1] = lz2[rt << 1 | 1] = 0;
    v1[rt << 1] = lz1[rt];
    v1[rt << 1 | 1] = lz1[rt];
    v2[rt << 1] = l + lz1[rt];
    v2[rt << 1 | 1] = mid + 1 + lz1[rt];
    lz1[rt] = -inf;
  }
}
void pushdownlz2(int rt) {
  if (lz2[rt]) {
    v1[rt << 1] += lz2[rt];
    v1[rt << 1 | 1] += lz2[rt];
    v2[rt << 1 | 1] += lz2[rt];
    v2[rt << 1] += lz2[rt];
    if (lz1[rt << 1] == -inf)
      lz2[rt << 1] += lz2[rt];
    else
      lz1[rt << 1] += lz2[rt];
    if (lz1[rt << 1 | 1] == -inf)
      lz2[rt << 1 | 1] += lz2[rt];
    else
      lz1[rt << 1 | 1] += lz2[rt];
    lz2[rt] = 0;
  }
}
void upd1(int rt, int l, int r, int ll, int rr, int v, int op) {
  if (ll > rr)
    return;
  if (ll <= l && rr >= r) {
    if (op == 1) {
      lz2[rt] = 0;
      lz1[rt] = v, v1[rt] = v, v2[rt] = v + l;
    } else {
      if (lz1[rt] == -inf)
        lz2[rt] += v, v1[rt] += v, v2[rt] += v;
      else
        lz1[rt] += v, v1[rt] += v, v2[rt] += v;
    }
    return;
  }
  int mid = (l + r) >> 1;
  if (lz1[rt] != -inf && lz2[rt]) {
    assert(0);
  }
  pushdownlz2(rt);
  pushdownlz1(rt, l, mid);

  if (ll <= mid)
    upd1(rt << 1, l, mid, ll, rr, v, op);
  if (rr > mid)
    upd1(rt << 1 | 1, mid + 1, r, ll, rr, v, op);
  v1[rt] = min(v1[rt << 1], v1[rt << 1 | 1]);
  v2[rt] = min(v2[rt << 1], v2[rt << 1 | 1]);
}
int query1(int rt, int l, int r, int ll, int rr) {
  if (ll > rr)
    return inf;
  if (ll <= l && rr >= r)
    return v1[rt];
  int mid = (l + r) >> 1, res = inf;
  if (lz1[rt] != -inf && lz2[rt]) {
    assert(0);
  }
  pushdownlz2(rt);
  pushdownlz1(rt, l, mid);
  if (ll <= mid)
    res = min(res, query1(rt << 1, l, mid, ll, rr));
  if (rr > mid)
    res = min(res, query1(rt << 1 | 1, mid + 1, r, ll, rr));
  return res;
}
int main() {
#ifdef LOCAL
  freopen(".a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int h, w;
  cin >> h >> w;
  build(1, 1, w);
  for (int i = 1; i <= h; ++i) {
    int l, r;
    cin >> l >> r;
    int res = query1(1, 1, w, 1, l - 1);
    if (res >= inf) {
      upd1(1, 1, w, l, r, inf, 1);
    } else {
      upd1(1, 1, w, l, r, res + 1, 1);
    }
    upd1(1, 1, w, 1, l - 1, 1, 2);
    upd1(1, 1, w, r + 1, w, 1, 2);
    cout << (v2[1] >= inf ? -1 : v2[1]) << '\\n';
  }

  return 0;
}
// 2020.08.31 01:00:33


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define SZ(a) ((int)(a).size())
typedef long long int64;
const int maxn = 200009;
const int inf = 1e9;
int v1[maxn << 2], v2[maxn << 2], lz1[maxn << 2], lz2[maxn << 2];
// lz1覆盖 lz2+v v1=v2-下标
void build(int rt, int l, int r) {
  lz1[rt] = -inf;
  if (l == r) {
    v1[rt] = -l;
    return;
  }
  int mid = (l + r) >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
  v1[rt] = min(v1[rt << 1], v1[rt << 1 | 1]);
}
void pushdownlz1(int rt, int l, int mid) {
  if (lz1[rt] != -inf) {
    lz1[rt << 1] = lz1[rt];
    lz1[rt << 1 | 1] = lz1[rt];
    v1[rt << 1] = lz1[rt];
    v1[rt << 1 | 1] = lz1[rt];
    v2[rt << 1] = l + lz1[rt];
    v2[rt << 1 | 1] = mid + 1 + lz1[rt];
    lz1[rt] = -inf;
  }
}
void pushdownlz2(int rt) {
  if (lz2[rt]) {
    lz2[rt << 1] += lz2[rt];
    lz2[rt << 1 | 1] += lz2[rt];
    v1[rt << 1] += lz2[rt];
    v1[rt << 1 | 1] += lz2[rt];
    v2[rt << 1 | 1] += lz2[rt];
    v2[rt << 1] += lz2[rt];
    lz2[rt] = 0;
  }
}
void upd1(int rt, int l, int r, int ll, int rr, int v, int op) {
  if (ll > rr)
    return;
  if (ll <= l && rr >= r) {
    if (op == 1) {
      lz2[rt] = 0;
      lz1[rt] = v, v1[rt] = v, v2[rt] = v + l;
    } else {
      if (lz1[rt] == -inf)
        lz2[rt] += v, v1[rt] += v, v2[rt] += v;
      else
        lz1[rt] += v, v1[rt] += v, v2[rt] += v;
    }
    return;
  }
  int mid = (l + r) >> 1;
  if (lz1[rt] != -inf && lz2[rt]) {
    assert(0);
  }
  pushdownlz2(rt);
  pushdownlz1(rt, l, mid);

  if (ll <= mid)
    upd1(rt << 1, l, mid, ll, rr, v, op);
  if (rr > mid)
    upd1(rt << 1 | 1, mid + 1, r, ll, rr, v, op);
  v1[rt] = min(v1[rt << 1], v1[rt << 1 | 1]);
  v2[rt] = min(v2[rt << 1], v2[rt << 1 | 1]);
}
int query1(int rt, int l, int r, int ll, int rr) {
  if (ll > rr)
    return inf;
  if (ll <= l && rr >= r)
    return v1[rt];
  int mid = (l + r) >> 1, res = inf;
  if (lz1[rt] != -inf && lz2[rt]) {
    assert(0);
  }
  pushdownlz2(rt);
  pushdownlz1(rt, l, mid);
  if (ll <= mid)
    res = min(res, query1(rt << 1, l, mid, ll, rr));
  if (rr > mid)
    res = min(res, query1(rt << 1 | 1, mid + 1, r, ll, rr));
  return res;
}
int main() {
#ifdef LOCAL
  freopen(".a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int h, w;
  cin >> h >> w;
  build(1, 1, w);
  for (int i = 1; i <= h; ++i) {
    int l, r;
    cin >> l >> r;
    int res = query1(1, 1, w, 1, l - 1);
    if (res >= inf) {
      upd1(1, 1, w, l, r, inf, 1);
    } else {
      upd1(1, 1, w, l, r, res + 1, 1);
    }
    upd1(1, 1, w, 1, l - 1, 1, 2);
    upd1(1, 1, w, r + 1, w, 1, 2);
    cout << (v2[1] >= inf ? -1 : v2[1]) << '\\n';
  }

  return 0;
}
// 2020.08.31 01:00:33