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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

#define NUM 2147483647

int N;
int *table;

void init(int first_N) {
  while (N < first_N)
    N *= 2;
}

void update(int loc, int value) {
  loc += N - 1;

  table[loc] = value;

  if (N == 1)
    return;

  int parent = (loc - 1) / 2;

  while (true) {
    table[parent] = min(table[2 * parent + 1], table[2 * parent + 2]);

    if (parent == 0)
      break;
    else {
      parent = (parent - 1) / 2;
    }
  }
}

int query(int search_left, int search_right, int node_id, int node_left,
          int node_right) {

  if (search_right < node_left || search_left > node_right)
    return NUM;

  if (search_left <= node_left && search_right >= node_right) {
    return table[node_id];
  }

  int left_min = query(search_left, search_right, 2 * node_id + 1, node_left,
                       (node_left + node_right) / 2);
  int right_min = query(search_left, search_right, 2 * node_id + 2,
                        (node_left + node_right) / 2 + 1, node_right);

  return min(left_min, right_min);
}

int main() {

  int first_N, Q, command, left, right;
  table = new int[1000000];
  N = 1;

  scanf("%d %d", &first_N, &Q);

  init(first_N);

  for (int i = 0; i <= 2 * N - 2; i++)
    table[i] = NUM;

  for (int i = 0; i < Q; i++) {
    scanf("%d %d %d", &command, &left, &right);

    if (command == 0) {
      update(left, right);
    } else {
      printf("%d\\n", query(left, right, 0, 0, N - 1));
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

#define NUM 2147483647

int N;
int *table;

void init(int first_N) {
  while (N < first_N)
    N *= 2;
}

void update(int loc, int value) {
  loc += N - 1;

  table[loc] = value;

  if (N == 1)
    return;

  int parent = (loc - 1) / 2;

  while (true) {
    table[parent] = min(table[2 * parent + 1], table[2 * parent + 2]);

    if (parent == 0)
      break;
    else {
      parent = (parent - 1) / 2;
    }
  }
}

int query(int search_left, int search_right, int node_id, int node_left,
          int node_right) {

  if (search_right < node_left || search_left > node_right)
    return NUM;

  if (search_left <= node_left && search_right >= node_right) {
    return table[node_id];
  }

  int left_min = query(search_left, search_right, 2 * node_id + 1, node_left,
                       (node_left + node_right) / 2);
  int right_min = query(search_left, search_right, 2 * node_id + 2,
                        (node_left + node_right) / 2 + 1, node_right);

  return min(left_min, right_min);
}

int main() {

  int first_N, Q, command, left, right;
  table = new int[250000];
  N = 1;

  scanf("%d %d", &first_N, &Q);

  init(first_N);

  for (int i = 0; i <= 2 * N - 2; i++)
    table[i] = NUM;

  for (int i = 0; i < Q; i++) {
    scanf("%d %d %d", &command, &left, &right);

    if (command == 0) {
      update(left, right);
    } else {
      printf("%d\\n", query(left, right, 0, 0, N - 1));
    }
  }

  return 0;
}