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

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long LL;
int a[200001], Segtree[200000 * 5 * 5];

void build(int p, int L, int R) {
  if (L == R) {
    Segtree[p] = a[L];
    return;
  }
  int mid = (L + R) >> 1;
  build(p << 1, L, mid);
  build(p << 1 | 1, mid + 1, R);
  Segtree[p] = min(Segtree[p << 1], Segtree[p << 1 | 1]);
}

void update(int p, int L, int R, int idx) {
  if (L == R) {
    Segtree[p] = a[L];
    return;
  }
  int mid = (L + R) >> 1;
  if (idx <= mid)
    update(p << 1, L, mid, idx);
  else
    update(p << 1 | 1, mid + 1, R, idx);
  Segtree[p] = min(Segtree[p << 1], Segtree[p << 1 | 1]);
}

int main() {
  // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int H, W;
  scanf("%d%d", &H, &W);
  set<int> data;
  for (int i = 1; i <= W; i++) {
    data.insert(i);
  }
  build(1, 1, W);
  for (int j = 1; j <= H; j++) {
    int a2, b;
    scanf("%d%d", &a2, &b);
    if (data.empty()) {
      printf("-1\\n");
      continue;
    }
    auto i = data.lower_bound(b);
    if (i == data.end())
      i--;
    while (i != data.begin() && *i > b)
      i--;
    if (*i <= b) {
      vector<int> tmp;
      while (*i >= a2) {
        int val = *i;
        tmp.pb(val);
        if (data.count(b + 1) == 0 && b < W) {
          data.insert(b + 1);
          a[b + 1] = b + 1 - val + a[val];
          update(1, 1, W, b + 1);
        }
        if (i == data.begin())
          break;
        i--;
      }
      for (auto x : tmp)
        data.erase(x), a[x] = 1e9, update(1, 1, W, x);
    }
    if (Segtree[1] == 1e9)
      printf("-1\\n");
    else
      printf("%d\\n", Segtree[1] + j);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long LL;
int a[200001], Segtree[200000 * 5 * 5];

void build(int p, int L, int R) {
  if (L == R) {
    Segtree[p] = a[L];
    return;
  }
  int mid = (L + R) >> 1;
  build(p << 1, L, mid);
  build(p << 1 | 1, mid + 1, R);
  Segtree[p] = min(Segtree[p << 1], Segtree[p << 1 | 1]);
}

void update(int p, int L, int R, int idx) {
  if (L == R) {
    Segtree[p] = a[L];
    return;
  }
  int mid = (L + R) >> 1;
  if (idx <= mid)
    update(p << 1, L, mid, idx);
  else
    update(p << 1 | 1, mid + 1, R, idx);
  Segtree[p] = min(Segtree[p << 1], Segtree[p << 1 | 1]);
}

int main() {
  // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int H, W;
  scanf("%d%d", &H, &W);
  set<int> data;
  for (int i = 1; i <= W; i++) {
    data.insert(i);
  }
  build(1, 1, W);
  for (int j = 1; j <= H; j++) {
    int a2, b;
    scanf("%d%d", &a2, &b);
    auto i = data.lower_bound(b + 1);
    i--;
    vector<int> tmp;
    while (*i >= a2) {
      tmp.pb(*i);
      if (data.count(b + 1) == 0 && b < W) {
        data.insert(b + 1);
        a[b + 1] = b + 1 - *i + a[*i];
        update(1, 1, W, b + 1);
      }
      if (i == data.begin())
        break;
      i--;
    }
    for (auto x : tmp)
      data.erase(x), a[x] = 1e9, update(1, 1, W, x);
    if (Segtree[1] == 1e9)
      printf("-1\\n");
    else
      printf("%d\\n", Segtree[1] + j);
  }
}