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

#include <bits/stdc++.h>
using namespace std;
int const nax = 2e5 + 10;
const int INF = 1e8;
int n, m;
int l[nax];
int r[nax];
int sg[nax * 4];
int tag[nax * 4];

void push(int v, int l, int r) {
  if (!tag[v])
    return;
  if (l != r) {
    tag[v << 1] = tag[v];
    sg[v << 1] = tag[v << 1];
    tag[v << 1 | 1] = tag[v] + (l + r >> 1) - l + 1;
    sg[v << 1 | 1] = tag[v << 1 | 1];
  }
  tag[v] = 0;
}

void update(int v, int x, int y, int l, int r, int val) {
  push(v, x, y);
  if (l == x && r == y) {
    sg[v] = val;
    tag[v] = val;
    return push(v, x, y);
  }

  int mid = x + y >> 1;
  if (r <= mid) {
    update(v << 1, x, mid, l, r, val);
  } else if (l > mid) {
    update(v << 1 | 1, mid + 1, y, l, r, val);
  } else {
    update(v << 1, x, mid, l, mid, val);
    update(v << 1 | 1, mid + 1, y, mid + 1, r, val + mid - l + 1);
  }
  sg[v] = min(sg[v << 1], sg[v << 1 | 1]);
}

int get(int v, int x, int y, int l, int r) {
  push(v, x, y);
  if (l == x && r == y) {
    return sg[v];
  }
  int mid = x + y >> 1;
  if (r <= mid)
    return get(v << 1, x, mid, l, r);
  else if (l > mid)
    return get(v << 1 | 1, mid + 1, y, l, r);
  return min(get(v << 1, x, mid, l, mid),
             get(v << 1 | 1, mid + 1, y, mid + 1, r));
}

void print_sg(int v, int x, int y) {
  if (x == y) {
    printf("i %d %d\\n", x, sg[v]);
  } else {
    int mid = x + y >> 1;
    print_sg(v << 1, x, mid);
    print_sg(v << 1 | 1, mid + 1, y);
  }
}

int main() {
  scanf("%d %d", &n, &m);

  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", l + i, r + i);
    int x = l[i] == 1 ? INF : get(1, 1, m, l[i] - 1, l[i] - 1);
    update(1, 1, m, l[i], r[i], x + 1);
    // print_sg(1, 1, m);
    if (sg[1] + i >= INF) {
      puts("-1");
    } else
      printf("%d\\n", i + sg[1]);
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int const nax = 2e5 + 10;
const int INF = 1e8;
int n, m;
int l[nax];
int r[nax];
int sg[nax];
int tag[nax];
/// arithmetic progression on range segment tree

void push(int v, int l, int r) {
  if (!tag[v])
    return;
  if (l != r) {
    tag[v << 1] = tag[v];
    sg[v << 1] = tag[v << 1];
    tag[v << 1 | 1] = tag[v] + (l + r >> 1) - l + 1;
    sg[v << 1 | 1] = tag[v << 1 | 1];
  }
  tag[v] = 0;
}

void update(int v, int x, int y, int l, int r, int val) {
  push(v, x, y);
  if (l == x && r == y) {
    sg[v] = val;
    tag[v] = val;
    return push(v, x, y);
  }

  int mid = x + y >> 1;
  if (r <= mid) {
    update(v << 1, x, mid, l, r, val);
  } else if (l > mid) {
    update(v << 1 | 1, mid + 1, y, l, r, val);
  } else {
    update(v << 1, x, mid, l, mid, val);
    update(v << 1 | 1, mid + 1, y, mid + 1, r, val + mid - l + 1);
  }
  sg[v] = min(sg[v << 1], sg[v << 1 | 1]);
}

int get(int v, int x, int y, int l, int r) {
  push(v, x, y);
  if (l == x && r == y) {
    return sg[v];
  }
  int mid = x + y >> 1;
  if (r <= mid)
    return get(v << 1, x, mid, l, r);
  else if (l > mid)
    return get(v << 1 | 1, mid + 1, y, l, r);
  return min(get(v << 1, x, mid, l, mid),
             get(v << 1 | 1, mid + 1, y, mid + 1, r));
}

void print_sg(int v, int x, int y) {
  if (x == y) {
    printf("i %d %d\\n", x, sg[v]);
  } else {
    int mid = x + y >> 1;
    print_sg(v << 1, x, mid);
    print_sg(v << 1 | 1, mid + 1, y);
  }
}

int main() {
  scanf("%d %d", &n, &m);

  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", l + i, r + i);
    int x = l[i] == 1 ? INF : get(1, 1, m, 1, l[i] - 1);
    update(1, 1, m, l[i], r[i], x + 1);
    // print_sg(1, 1, m);
    if (sg[1] + i >= INF) {
      puts("-1");
    } else
      printf("%d\\n", i + sg[1]);
  }
}