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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

template <typename Type = int> class SegmentTree {
public:
  int strnum;
  int strbeg;
  int nodemax;
  std::unique_ptr<Type[]> node;
  std::unique_ptr<std::pair<int, int>[]> noderange;

  SegmentTree(int a) {
    strnum = a;
    strbeg = 1;
    while (strbeg < strnum)
      strbeg *= 2;
    strbeg -= 1;
    nodemax = strbeg * 2 + 1;
    node.reset(new Type[nodemax]);
    noderange.reset(new std::pair<int, int>[nodemax]);
    noderange[0] = std::make_pair(0, nodemax - strbeg - 1);
    for (int i = 1; i < nodemax; i++) {
      int parent = (i - 1) / 2;
      int parentleft = noderange[parent].first;
      int parentright = noderange[parent].second;
      if (parent * 2 + 1 == i) {
        noderange[i] =
            std::make_pair(parentleft, (parentright + parentleft) / 2);
      } else {
        noderange[i] =
            std::make_pair((parentleft + parentright) / 2 + 1, parentright);
      }
    }
  }
  Type operation(Type left, Type right) { return min(left, right); }
  Type operation(Type left, Type center, Type right) {
    return min(min(left, center), right);
  }
  void init(Type in) { REP(i, nodemax) node[i] = in; }
  void showstr() {
    REP(i, nodemax) {
      long long int &n = node[i];
      printf("%lld ", n);
    }
    printf("\\n");
  }
  void showrange() {
    REP(i, nodemax) {
      std::pair<int, int> &n = noderange[i];
      printf("[%d:%d:%d] ", i, n.first, n.second);
    }
    printf("\\n");
  }
  void update(int i, Type x) {
    i += strbeg;
    node[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      node[i] = operation(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  Type find(int l, int r, int n = 0) {
    std::pair<int, int> ran = noderange[n];
    int center = (ran.first + ran.second) / 2;
    if (l == ran.first && r == ran.second)
      return node[n];
    else if (center < l)
      return find(l, r, 2 * n + 2);
    else if (center >= r)
      return find(l, r, 2 * n + 1);
    else
      return operation(find(l, center, 2 * n + 1),
                       find(center + 1, r, 2 * n + 2));
  }
};

int main() {
  int n, q;
  scanf("%d %d\\n", &n, &q);
  SegmentTree<long long int> a(n);
  a.init(((1 << 30) - 1) * 2 + 1);
  long long int b, x, y;
  REP(i, q) {
    scanf("%lld %lld %lld\\n", &b, &x, &y);
    if (b == 0) {
      a.update(x, y);
    } else {
      printf("%lld\\n", a.find(x, y));
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

template <typename Type = int> class SegmentTree {
public:
  int strnum;
  int strbeg;
  int nodemax;
  std::unique_ptr<Type[]> node;
  std::unique_ptr<std::pair<int, int>[]> noderange;

  SegmentTree(int a) {
    strnum = a;
    strbeg = 1;
    while (strbeg < strnum)
      strbeg *= 2;
    strbeg -= 1;
    nodemax = strbeg * 2 + 1;
    node.reset(new Type[nodemax]);
    noderange.reset(new std::pair<int, int>[nodemax]);
    REP(i, nodemax) {
      int k = 0;
      while (!((1 << k) - 1 <= i && i <= (1 << (k + 1)) - 2))
        k++;
      int left = (strbeg + 1) * (i + 1 - (1 << k)) / (1 << k);
      int right = left - 1 + (strbeg + 1) / (1 << k);
      noderange[i] = std::make_pair(left, right);
    }
  }
  Type operation(Type left, Type right) { return min(left, right); }
  Type operation(Type left, Type center, Type right) {
    return min(min(left, center), right);
  }
  void init(Type in) { REP(i, nodemax) node[i] = in; }
  void showstr() {
    REP(i, nodemax) {
      int &n = node[i];
      printf("%d ", n);
    }
    printf("\\n");
  }
  void update(int i, Type x) {
    i += strbeg;
    node[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      node[i] = operation(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  /*
  std::pair<int,int> range(int i){
      int k=0;
      while(!((1<<k)-1<=i&&i<=(1<<(k+1))-2))k++;
      int left=(strbeg+1)*(i+1-(1<<k))/(1<<k);
      int right=left-1+(strbeg+1)/(1<<k);
      return std::make_pair(left,right);
  }
  */
  Type find(int l, int r, int n = 0) {
    std::pair<int, int> ran = noderange[n];
    int center = (ran.first + ran.second) / 2;
    if (l == ran.first && r == ran.second)
      return node[n];
    else if (center < l)
      return find(l, r, 2 * n + 2);
    else if (center >= r)
      return find(l, r, 2 * n + 1);
    else
      return operation(find(l, center, 2 * n + 1),
                       find(center + 1, r, 2 * n + 2));
  }
};

int main() {
  int n, q;
  scanf("%d %d\\n", &n, &q);
  SegmentTree<long long int> a(n);
  a.init(((1 << 30) - 1) * 2 + 1);
  long long int b, x, y;
  REP(i, q) {
    scanf("%lld %lld %lld\\n", &b, &x, &y);
    if (b == 0) {
      a.update(x, y);
    } else {
      printf("%lld\\n", a.find(x, y));
    }
  }
  return 0;
}