Problem p02345 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// c++11
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

#define mp make_pair
#define mt make_tuple
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

const int INF = 1 << 29;
const double EPS = 1e-9;
const ll MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

// SegmentTreeMin
template <typename ValueType> class SegmentTreeMin {
public:
  SegmentTreeMin() {}
  SegmentTreeMin(int n, ValueType max_value) {
    this->N = 1;
    this->max_value = max_value;
    while (this->N < n)
      this->N *= 2;
    this->data.resize(2 * N - 1, this->max_value);
  }
  // init
  void init(int n, ValueType max_value) {
    this->N = 1;
    this->max_value = max_value;
    while (this->N < n)
      this->N *= 2;
    this->data.resize(2 * N - 1, this->max_value);
  }
  // update k th element
  void update(int k, ValueType val) {
    k += N - 1;
    data[k] = val;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = std::min(data[2 * k + 1], data[2 * k + 2]);
    }
  }
  // min [a, b)
  ValueType min(int a, int b) { return min(a, b, 0, 0, N); }
  ValueType min(int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return max_value;
    if (a <= l and r <= b)
      return data[k];
    int med = (l + r) / 2;
    return std::min(min(a, b, 2 * k + 1, l, med), min(a, b, 2 * k + 2, med, r));
  }

private:
  std::vector<ValueType> data;
  int N;
  ValueType max_value;
};
///////////////////////////////////////

int N, Q;
int main() {
  cin >> N >> Q;
  SegmentTreeMin<int64_t> seg;
  int64_t INF = (1LL << 31) - 1;
  seg.init(N, INF);
  for (int i = 0; i < Q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0) {
      seg.update(x, y);
    } else {
      cout << seg.min(x, y + 1) << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

/*sparse table*/
/*build O(nlogn)*/
/*query O(1)*/
#include <iostream>
#include <vector>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long, long> pll;

const int INF = 1 << 29;
const double EPS = 1e-9;
const int MOD = 100000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

#define RMQ_MAX ((1LL << 31) - 1)

template <typename T> class RmqSparseTable {
public:
  RmqSparseTable() {}
  RmqSparseTable(const vector<T> &a) {
    size = a.size();
    log_size = 1;
    for (int k = 1; k < size; k *= 2) {
      log_size++;
    }
    for (int i = 0; i < log_size; i++) {
      rmq[i].resize(size, 0);
    }
    // rmq.resize(log_size, vector<int>(size, 0));
    for (int i = 0; i < size; i++) {
      rmq[0][i] = a[i];
    }
    for (int i = 1; i < log_size; i++) {
      for (int j = 0; j + (1 << i) - 1 < size; j++) {
        int x = rmq[i - 1][j];
        int y = rmq[i - 1][j + (1 << (i - 1))];
        rmq[i][j] = min(x, y);
      }
    }
  }
  // O(NlogN)
  void build_table(const vector<T> &a) {
    size = a.size();
    log_size = 1;
    for (int k = 1; k < size; k *= 2) {
      log_size++;
    }
    for (int i = 0; i < log_size; i++) {
      rmq[i].resize(size, 0);
    }
    for (int i = 0; i < size; i++) {
      rmq[0][i] = a[i];
    }
    for (int i = 1; i < log_size; i++) {
      for (int j = 0; j + (1 << i) - 1 < size; j++) {
        int x = rmq[i - 1][j];
        int y = rmq[i - 1][j + (1 << (i - 1))];
        rmq[i][j] = min(x, y);
      }
    }
  }
  // O(N)
  void update(const int &index, const T &val) {
    rmq[0][index] = val;
    for (int i = 1; i < log_size; i++) {
      for (int j = max(index - (1 << i) - 1, 0); j <= index; j++) {
        if (j + (1 << i) - 1 >= size)
          break;
        int x = rmq[i - 1][j];
        int y = rmq[i - 1][j + (1 << (i - 1))];
        rmq[i][j] = min(x, y);
      }
    }
  }
  // range minimum query[i, j)
  // O(1)
  T query(const int &i, const int &j) {
    int log_d = 31 - __builtin_clz(j - i);
    if (rmq[log_d][i] <= rmq[log_d][j - (1 << log_d)]) {
      return rmq[log_d][i];
    } else {
      return rmq[log_d][j - (1 << log_d)];
    }
  }

  //---debug---
  void show() {
    cerr << "--show--" << endl;
    for (int i = 0; i < rmq.size(); i++) {
      for (int j = 0; j + (1 << i) - 1 < rmq[i].size(); j++) {
        cerr << rmq[i][j] << " ";
      }
      cerr << endl;
    }
  }

private:
  int size;
  int log_size;
  vector<T> rmq[32];
};
int N, Q;
vector<int> a;
int main() {
  scanf("%d %d", &N, &Q);
  a.resize(N, RMQ_MAX);
  int com, x, y;
  RmqSparseTable<int> rmq(a);
  for (int i = 0; i < Q; i++) {
    scanf("%d %d %d", &com, &x, &y);
    if (com == 0) {
      rmq.update(x, y);
    } else {
      printf("%d\\n", rmq.query(x, y + 1));
    }
  }

  return 0;
}