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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int nextInt() {
  int a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}

void inputVector(vector<int> &v, int &n) {
  rep(i, n) { v.push_back(nextInt()); }
}
void inputVector(vector<double> &v, int &n) {
  rep(i, n) { v.push_back(nextDouble()); }
}
void inputVector(vector<string> &v, int &n) {
  rep(i, n) { v.push_back(nextString()); }
}

class RMQ {
private:
  int bottomSt, bottomEd;
  vector<int> a;
  vector<int> rs, re;
  vector<int> cand;
  int set(int n) {
    int left = 2 * n + 1;
    int right = 2 * n + 2;
    if (left <= a.size() - 1) {
      a[n] = min(set(left), set(right));
      rs[n] = rs[left];
      re[n] = re[right];
    }
    return a[n];
  }
  void search(int s, int t, int n) {
    if (s <= rs[n] && re[n] <= t) {
      cand.push_back(a[n]);
      return;
    }
    int left = 2 * n + 1;
    int right = 2 * n + 2;
    if (s <= re[left])
      search(s, t, left);
    if (rs[right] <= t)
      search(s, t, right);
  }

public:
  RMQ(vector<int> &v) {
    bottomEd = 0;
    for (int i = 1, plus = 1; plus < v.size(); i++) {
      plus *= 2;
      bottomEd += plus;
    }
    a.resize(bottomEd + 1);
    rs.resize(bottomEd + 1);
    re.resize(bottomEd + 1);

    bottomSt = bottomEd / 2;

    for (int i = bottomSt; i <= bottomEd; i++) {
      int j = i - bottomEd / 2;
      a[i] = j < v.size() ? v[j] : LLONG_MAX;
      rs[i] = j;
      re[i] = j;
    }

    set(0);
  }

  void update(int index, int x) {
    int pt = bottomSt + index;
    a[pt] = x;
    while (pt > 0) {
      pt = (pt - 1) / 2;
      a[pt] = min(a[2 * pt + 1], a[2 * pt + 2]);
    }
  }

  int find(int s, int t) {
    cand.clear();
    search(s, t, 0);
    int ret = cand[0];
    for (int i = 1; i < cand.size(); i++) {
      ret = min(ret, cand[i]);
    }
    return ret;
  }
};

signed main() {
  int n, q;
  cin >> n >> q;

  vector<int> tmp(n, 2147483647);
  RMQ rmq(tmp);

  rep(lp, q) {
    int com, x, y;
    cin >> com >> x >> y;

    if (com == 0) {
      rmq.update(x, y);
    } else {
      cout << rmq.find(x, y) << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define int long long
#define MOD7 1000000007
#define MOD9 1000000009

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()

using namespace std;

int nextInt() {
  int a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}

void inputVector(vector<int> &v, int &n) {
  rep(i, n) { v.push_back(nextInt()); }
}
void inputVector(vector<double> &v, int &n) {
  rep(i, n) { v.push_back(nextDouble()); }
}
void inputVector(vector<string> &v, int &n) {
  rep(i, n) { v.push_back(nextString()); }
}

class RMQ {
private:
  int bottomSt, bottomEd;
  vector<int> a;
  vector<int> rs, re;
  vector<int> cand;
  int set(int n) {
    int left = 2 * n + 1;
    int right = 2 * n + 2;
    if (left <= a.size() - 1) {
      a[n] = min(set(left), set(right));
      rs[n] = rs[left];
      re[n] = re[right];
    }
    return a[n];
  }
  void search(int s, int t, int n) {
    if (s <= rs[n] && re[n] <= t) {
      cand.push_back(a[n]);
      return;
    }
    int left = 2 * n + 1;
    int right = 2 * n + 2;
    if (s <= re[left])
      search(s, t, left);
    if (rs[right] <= t)
      search(s, t, right);
  }

public:
  RMQ(vector<int> v) {
    bottomEd = 0;
    for (int i = 1, plus = 2; bottomEd < v.size(); i++) {
      bottomEd += plus;
      plus *= 2;
    }
    a.resize(bottomEd + 1);
    rs.resize(bottomEd + 1);
    re.resize(bottomEd + 1);

    bottomSt = bottomEd / 2;

    for (int i = bottomSt; i <= bottomEd; i++) {
      int j = i - bottomEd / 2;
      a[i] = j < v.size() ? v[j] : LLONG_MAX;
      rs[i] = j;
      re[i] = j;
    }

    set(0);
  }

  void update(int index, int x) {
    int pt = bottomSt + index;
    a[pt] = x;
    while (true) {
      pt = (pt - 1) / 2;
      a[pt] = min(a[2 * pt + 1], a[2 * pt + 2]);

      if (pt == 0)
        break;
    }
  }

  int find(int s, int t) {
    cand.clear();
    search(s, t, 0);
    int ret = cand[0];
    for (int i = 1; i < cand.size(); i++) {
      ret = min(ret, cand[i]);
    }
    return ret;
  }
};

signed main() {
  int n, q;
  cin >> n >> q;

  vector<int> tmp(n, 2147483647);
  RMQ rmq(tmp);

  rep(lp, q) {
    int com, x, y;
    cin >> com >> x >> y;

    if (com == 0) {
      rmq.update(x, y);
    } else {
      cout << rmq.find(x, y) << endl;
    }
  }

  return 0;
}