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

#include <cstdio>
#include <iostream>
using namespace std;

const int N = 1e5 + 10;
const int inf = 1e9 + 7;

struct Node {
  int val;
  int res;
  int l, r;
  Node() {}
  Node(int val, int res, int l, int r) : val(val), res(res), l(l), r(r) {}
};

Node nodes[N * 20];

void build_tree(int l, int r, int id) {
  if (l == r) {
    nodes[id] = Node(l, 0, l, r);
  } else {
    nodes[id] = Node(-1, 0, l, r);
    int m = (l + r) >> 1;
    build_tree(l, m, id * 2);
    build_tree(m + 1, r, id * 2 + 1);
  }
}

int get_value(int id, int x) {
  if (nodes[id].val != -1) {
    return nodes[id].val;
  }
  int m = (nodes[id].l + nodes[id].r) >> 1;
  if (x <= m)
    return get_value(id * 2, x);
  else
    return get_value(id * 2 + 1, x);
}

void push_down(int id) {
  if (nodes[id].val != -1 && nodes[id].l != nodes[id].r) {
    nodes[id * 2].val = nodes[id].val;
    nodes[id * 2].res = nodes[id].res;
    nodes[id * 2 + 1].val = nodes[id].val;
    nodes[id * 2 + 1].res = nodes[id].res + nodes[id * 2 + 1].l - nodes[id].l;
    nodes[id].val = -1;
  }
}

void push_up(int id) {
  nodes[id].res = min(nodes[id * 2].res, nodes[id * 2 + 1].res);
}

void update(int id, int l, int r, int val) {
  if (nodes[id].l == l && nodes[id].r == r) {
    nodes[id].val = val;
    nodes[id].res = (val == 0 ? inf : l - val);
    return;
  }
  push_down(id);
  int m = (nodes[id].l + nodes[id].r) >> 1;
  if (r <= m)
    update(id * 2, l, r, val);
  else if (m < l)
    update(id * 2 + 1, l, r, val);
  else {
    update(id * 2, l, m, val);
    update(id * 2 + 1, m + 1, r, val);
  }
  push_up(id);
}

int main() {
  //    freopen("in.txt", "r", stdin);
  int h, w, a, b;
  cin >> h >> w;
  build_tree(1, w, 1);
  for (int i = 0; i < h; i++) {
    cin >> a >> b;
    int left_val = (a == 1 ? 0 : get_value(1, a - 1));
    update(1, a, b, left_val);
    if (nodes[1].res == inf)
      cout << "-1" << '\\n';
    else
      cout << nodes[1].res + i + 1 << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

const int N = 1e5 + 10;
const int inf = 1e9 + 7;

struct Node {
  int val;
  int res;
  int l, r;
  Node() {}
  Node(int val, int res, int l, int r) : val(val), res(res), l(l), r(r) {}
};

Node nodes[N * 20];

void build_tree(int l, int r, int id) {
  if (l == r) {
    nodes[id] = Node(l, 1, l, r);
  } else {
    nodes[id] = Node(-1, 1, l, r);
    int m = (l + r) >> 1;
    build_tree(l, m, id * 2);
    build_tree(m + 1, r, id * 2 + 1);
  }
}

int get_value(int id, int x) {
  if (nodes[id].val != -1) {
    return nodes[id].val;
  }
  int m = (nodes[id].l + nodes[id].r) >> 1;
  if (x <= m)
    return get_value(id * 2, x);
  else
    return get_value(id * 2 + 1, x);
}

void push_down(int id) {
  if (nodes[id].val != -1 && nodes[id].l != nodes[id].r) {
    nodes[id * 2].val = nodes[id].val;
    nodes[id * 2].res = nodes[id].res;
    nodes[id * 2 + 1].val = nodes[id].val;
    nodes[id * 2 + 1].res =
        nodes[id].res + nodes[id * 2 + 1].l - nodes[id].l + 1;
  }
}

void push_up(int id) {
  nodes[id].res = min(nodes[id * 2].res, nodes[id * 2 + 1].res);
}

void update(int id, int l, int r, int val, int res) {
  //    cout << id << ' ' << l << ' ' << r << endl;
  if (nodes[id].l == l && nodes[id].r == r) {
    nodes[id].val = val;
    nodes[id].res = res;
    return;
  }
  push_down(id);
  int m = (nodes[id].l + nodes[id].r) >> 1;
  if (r <= m)
    update(id * 2, l, r, val, res);
  else if (m < l)
    update(id * 2 + 1, l, r, val, res);
  else {
    update(id * 2, l, m, val, res);
    update(id * 2 + 1, m + 1, r, val, res);
  }
  push_up(id);
}

int main() {
  //    freopen("in.txt", "r", stdin);
  int h, w, a, b;
  cin >> h >> w;
  build_tree(1, h, 1);
  for (int i = 0; i < h; i++) {
    cin >> a >> b;
    int left_val = (a == 1 ? 0 : get_value(1, a - 1));
    //        cout << left_val <<endl;
    int left_res = (left_val == 0 ? inf : a - left_val + 1);
    //        cout << left_val << ' ' << left_res <<endl;
    update(1, a, b, left_val, left_res);
    if (nodes[1].res == inf)
      cout << "-1" << '\\n';
    else
      cout << nodes[1].res + i << '\\n';
  }
  return 0;
}