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

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

#define DEBUG 0

void update(int x, int y);
int find(int index, int x, int y, int l, int r);
void initRMQ();
void printTree();

const int infinit = INT_MAX;
int n, q;
const int SIZE = 1000000;
int segmenttree[SIZE * 2 + 10];

int main() {

  cin >> n >> q;

  initRMQ();

  /*
  for (int i = 0; i < n * 2 - 1; ++i)
          segmenttree[i] = infinit;
  */

#if DEBUG == 1
  printTree();
#endif

  for (int i = 0; i < q; ++i) {

    int com, x, y;

    cin >> com >> x >> y;

    switch (com) {
    case 0:
      update(x, y);
      break;

    case 1:
      cout << find(0, x, y + 1, 0, n) << endl;
      break;
    }

#if DEBUG == 1
    cout << com << " " << x << " " << y << endl;
    printTree();
#endif // DEBUG == 1
  }

  return 0;
}

void initRMQ() {

  int n_ = 2 * n - 1;
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; ++i)
    segmenttree[i] = infinit;
}

void update(int x, int y) {

  int i = x + n - 1;
  segmenttree[i] = y;

  while (i > 0) {
    i = (i - 1) / 2;
    segmenttree[i] = min(segmenttree[i * 2 + 1], segmenttree[i * 2 + 2]);
  }
}

int find(int index, int x, int y, int l, int r) {

  if (r <= x || y <= l) {
    return infinit;
  } else if (x <= l && r <= y) {
    return segmenttree[index];
  } else {
    int left = find(index * 2 + 1, x, y, l, (r + l) / 2);
    int right = find(index * 2 + 2, x, y, (r + l) / 2, r);
#if DEBUG == 1
    cout << "(r + l) / 2 = " << (r + l) / 2 << endl;
    cout << "r = " << r << endl;
    cout << "l = " << l << endl;
    cout << "index * 2 + 2 = " << index * 2 + 2 << endl;
    cout << "l = " << left << ", r = " << right << endl;
#endif
    return min(left, right);
  }
}

void printTree() {

  for (int i = 0; i < n * 2 - 1; ++i)
    cout << segmenttree[i] << " ";
  cout << endl << endl;
}


Error: 0

Fixed submission:

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

#define DEBUG 0

void update(int x, int y);
int find(int index, int x, int y, int l, int r);
void initRMQ();
void printTree();

const int infinit = INT_MAX;
int n, q;
const int SIZE = 100000;
int segmenttree[SIZE * 2 + 10];

int main() {

  cin >> n >> q;

  initRMQ();

  /*
  for (int i = 0; i < n * 2 - 1; ++i)
          segmenttree[i] = infinit;
  */

#if DEBUG == 1
  printTree();
#endif

  for (int i = 0; i < q; ++i) {

    int com, x, y;

    cin >> com >> x >> y;

    switch (com) {
    case 0:
      update(x, y);
      break;

    case 1:
      cout << find(0, x, y + 1, 0, n) << endl;
      break;
    }

#if DEBUG == 1
    cout << com << " " << x << " " << y << endl;
    printTree();
#endif // DEBUG == 1
  }

  return 0;
}

void initRMQ() {

  int n_ = 2 * n - 1;
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; ++i)
    segmenttree[i] = infinit;
}

void update(int x, int y) {

  int i = x + n - 1;
  segmenttree[i] = y;

  while (i > 0) {
    i = (i - 1) / 2;
    segmenttree[i] = min(segmenttree[i * 2 + 1], segmenttree[i * 2 + 2]);
  }
}

int find(int index, int x, int y, int l, int r) {

  if (r <= x || y <= l) {
    return infinit;
  } else if (x <= l && r <= y) {
    return segmenttree[index];
  } else {
    int left = find(index * 2 + 1, x, y, l, (r + l) / 2);
    int right = find(index * 2 + 2, x, y, (r + l) / 2, r);
#if DEBUG == 1
    cout << "(r + l) / 2 = " << (r + l) / 2 << endl;
    cout << "r = " << r << endl;
    cout << "l = " << l << endl;
    cout << "index * 2 + 2 = " << index * 2 + 2 << endl;
    cout << "l = " << left << ", r = " << right << endl;
#endif
    return min(left, right);
  }
}

void printTree() {

  for (int i = 0; i < n * 2 - 1; ++i)
    cout << segmenttree[i] << " ";
  cout << endl << endl;
}