Problem p02345 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

struct edge {
  int /*from,*/ to, cost;
};
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
typedef vector<int> VI;
typedef vector<long long int> VL;
typedef vector<edge> VE;
static const int MOD = 1000000007;
static const int INF = 2147483647;
// static const long long INF = 9223372000000000000;
// static const long long INF = 9223372000000000000/2;
// static const int INF = 1000010000;
// int dx4[4] = {0,1,0,-1}, dy4[4] = {-1,0,1,0};
// int dx5[5] = {-1,0,0,0,1}, dy5[5] = {0,-1,0,1,0};
// int dx8[8] = {-1,0,1,1,1,0,-1,-1}, dy8[8] = {1,1,1,0,-1,-1,-1,0};
// int dx9[9] = {-1,0,1,1,1,0,-1,-1,0}, dy9[9] = {1,1,1,0,-1,-1,-1,0,0};
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define np next_permutation
#define pq priority_queue
#define UB upper_bound
#define LB lower_bound
#define SZ(a) int((a).size())
#define LEN(a) int((a).length())
#define MAX(a, b, c) max((a), max((b), (c)))
#define MIN(a, b, c) min((a), min((b), (c)))
#define SORT(c) sort((c).begin(), (c).end())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REP1(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREP1(i, x) for (int i = ((int)(x)); i > 0; i--)
// #define int ll

// Range Minimum Query????????°???????????¨??§

const int MAXN = 114514;

//?????°???????????¨???????????°??????????????????
int n, dat[2 * MAXN - 1];

//?????????
void init(int n_) {
  //?°????????????????????´???°???2???????????????
  n = 1;
  while (n < n_)
    n *= 2;

  //??¨????????????INT_MAX???
  REP(i, 2 * n - 1) dat[i] = INF;
}

// k????????????(0-indexed)???a????????´
void update(int k, int a) {
  //????????????
  k += n - 1;
  dat[k] = a;
  //?????????????????´??°
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

//[a,b)???????°????????±???????
//????????????query(a,b,0,0,n)??¨???????????¶
int query(int a, int b, int k, int l, int r) {
  //[a,b)??¨[l,r)?????????????????????????????°INF?????????
  if (r <= a || b <= l)
    return INF;

  //[a,b)???[l,r)????????¨???????????§????????°?????????????????????
  if (a <= l && r <= b)
    return dat[k];
  else {
    //????????§???????????°??????????????????????°????
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}

signed main() {
  int q;
  scanf("%d%d", &n, &q);
  init(n);

  REP(i, q) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (a)
      printf("%d\n", query(b, c + 1, 0, 0, n));
    else
      update(b, c);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

struct edge {
  int /*from,*/ to, cost;
};
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, pair<int, int>> PP;
typedef vector<int> VI;
typedef vector<long long int> VL;
typedef vector<edge> VE;
static const int MOD = 1000000007;
static const int INF = 2147483647;
// static const long long INF = 9223372000000000000;
// static const long long INF = 9223372000000000000/2;
// static const int INF = 1000010000;
// int dx4[4] = {0,1,0,-1}, dy4[4] = {-1,0,1,0};
// int dx5[5] = {-1,0,0,0,1}, dy5[5] = {0,-1,0,1,0};
// int dx8[8] = {-1,0,1,1,1,0,-1,-1}, dy8[8] = {1,1,1,0,-1,-1,-1,0};
// int dx9[9] = {-1,0,1,1,1,0,-1,-1,0}, dy9[9] = {1,1,1,0,-1,-1,-1,0,0};
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define np next_permutation
#define pq priority_queue
#define UB upper_bound
#define LB lower_bound
#define SZ(a) int((a).size())
#define LEN(a) int((a).length())
#define MAX(a, b, c) max((a), max((b), (c)))
#define MIN(a, b, c) min((a), min((b), (c)))
#define SORT(c) sort((c).begin(), (c).end())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REP1(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREP1(i, x) for (int i = ((int)(x)); i > 0; i--)
// #define int ll

// Range Minimum Query????????°???????????¨??§

const int MAXN = 1145140;

//?????°???????????¨???????????°??????????????????
int n, dat[2 * MAXN - 1];

//?????????
void init(int n_) {
  //?°????????????????????´???°???2???????????????
  n = 1;
  while (n < n_)
    n *= 2;

  //??¨????????????INT_MAX???
  REP(i, 2 * n - 1) dat[i] = INF;
}

// k????????????(0-indexed)???a????????´
void update(int k, int a) {
  //????????????
  k += n - 1;
  dat[k] = a;
  //?????????????????´??°
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

//[a,b)???????°????????±???????
//????????????query(a,b,0,0,n)??¨???????????¶
int query(int a, int b, int k, int l, int r) {
  //[a,b)??¨[l,r)?????????????????????????????°INF?????????
  if (r <= a || b <= l)
    return INF;

  //[a,b)???[l,r)????????¨???????????§????????°?????????????????????
  if (a <= l && r <= b)
    return dat[k];
  else {
    //????????§???????????°??????????????????????°????
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}

signed main() {
  int q;
  scanf("%d%d", &n, &q);
  init(n);

  REP(i, q) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (a)
      printf("%d\n", query(b, c + 1, 0, 0, n));
    else
      update(b, c);
  }
  return 0;
}