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

#include <algorithm>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, init, n) for (int i = init; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define Cout(obj) cout << obj << endl
#define Size(obj) (int)(obj).size()
#define fcout cout << fixed << setprecision(10)
#define fi first
#define se second

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

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

// node[0]を参照するバグるので注意
template <typename T> class BIT {
private:
  int N;
  vector<T> node;

public:
  // BIT(int size) : node(size + 1, 0), N(size) {}
  BIT(vector<T> vec) {
    N = Size(vec);
    node.resize(N + 1);

    REP(i, N) { node[i + 1] = vec[i]; }
  }

  T get_sum(int i) {
    T sum = 0;
    while (i > 0) {
      sum += node[i];
      i -= i & -i;
    }

    return sum;
  }

  T get_sum(int l, int r) {
    T res_l = get_sum(l - 1);
    T res_r = get_sum(r);

    return res_r - res_l;
  }

  void add(int i, T x) {
    while (i <= N) {
      node[i] += x;
      i += i & -i;
    }
  }

  int lower_bound(int w) {
    if (w <= 0) {
      return 0;
    }

    int init = 1;
    while (init < N) {
      init *= 2;
    }

    int x = 0;
    for (int k = init; k > 0; k /= 2) {
      if (x + k <= N && node[x + k] < w) {
        w -= node[x + k];
        x += k;
      }
    }

    return x + 1;
  }
};

template <typename T> struct SegTreeMin {
private:
  int N = 1;
  int init = pow(2, 31) - 1;
  vector<T> node;

public:
  SegTreeMin(vector<T> vec) {
    int size = vec.size();

    while (N < size) {
      N *= 2;
    }

    node.resize(2 * N - 1, init);

    REP(i, size) { node[i + N - 1] = vec[i]; }

    for (int i = N - 2; 0 <= i; i--) {
      node[i] = min(node[2 * i + 1], node[2 * i + 2]);
    }
  }

  void update(int i, T value) {
    i += (N - 1);

    node[i] = value;

    while (i > 0) {
      i = (i - 1) / 2;

      node[i] = min(node[2 * i + 1], node[2 * i + 2]);
    }
  }

  T get_min(int a, int b, int i = 0, int l = 0, int r = -1) {
    if (r < 0) {
      r = N;
    }

    if (r <= a || b <= l) {
      return init;
    }

    if (a <= l && r <= b) {
      return node[i];
    }

    T vl = get_min(a, b, 2 * i + 1, l, (l + r) / 2);

    T vr = get_min(a, b, 2 * i + 2, (l + r) / 2, r);

    return min(vl, vr);
  }
};

template <typename T> struct SegTreeMax {
private:
  ll N = 1;
  vector<T> node;

public:
  SegTreeMax(vector<T> vec) {
    ll size = vec.size();

    while (N < size) {
      N *= 2;
    }

    node.resize(2 * N - 1, -iINF);

    REP(i, size) { node[i + N - 1] = vec[i]; }

    for (int i = N - 2; 0 <= i; i--) {
      node[i] = max(node[2 * i + 1], node[2 * i + 2]);
    }
  }

  void update(int i, T value) {
    i += (N - 1);

    node[i] = value;

    while (i > 0) {
      i = (i - 1) / 2;

      node[i] = max(node[2 * i + 1], node[2 * i + 2]);
    }
  }

  T get_max(int a, int b, int i = 0, int l = 0, int r = -1) {
    if (r < 0) {
      r = N;
    }

    if (r <= a || b <= l) {
      return -iINF;
    }

    if (a <= l && r <= b) {
      return node[i];
    }

    T vl = get_max(a, b, 2 * i + 1, l, (l + r) / 2);

    T vr = get_max(a, b, 2 * i + 2, (l + r) / 2, r);

    return max(vl, vr);
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, Q;
  cin >> N >> Q;
  vector<int> vec(N, pow(2, 31) - 1);
  SegTreeMin<int> seg(vec);

  REP(i, Q) {
    int com, x, y;
    cin >> com >> x >> y;

    if (com == 0) {
      seg.update(x, y);
    } else {
      Cout(seg.get_min(x, y + 1));
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, init, n) for (int i = init; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define Cout(obj) cout << obj << endl
#define Size(obj) (int)(obj).size()
#define fcout cout << fixed << setprecision(10)
#define fi first
#define se second

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

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

template <typename T> class BIT {
private:
  vector<T> array;
  int N;

public:
  BIT(int size) : array(size + 1, 0), N(size) {}

  T sum(int i) {
    T sum = 0;
    while (i > 0) {
      sum += array[i];
      i -= i & -i;
    }

    return sum;
  }

  T sum(int l, int r) {
    T res_l = sum(l - 1);
    T res_r = sum(r);

    return res_r - res_l;
  }

  void add(int i, T x) {
    while (i <= N) {
      array[i] += x;
      i += i & -i;
    }
  }

  int lower_bound(int w) {
    if (w <= 0) {
      return 0;
    }

    int init = 1;
    while (init < N) {
      init *= 2;
    }

    int x = 0;
    for (int k = init; k > 0; k /= 2) {
      if (x + k <= N && array[x + k] < w) {
        w -= array[x + k];
        x += k;
      }
    }

    return x + 1;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, Q;
  cin >> N >> Q;
  BIT<int> bit(N);

  REP(i, Q) {
    int com, x, y;
    cin >> com >> x >> y;

    if (com == 0) {
      bit.add(x, y);
    } else {
      Cout(bit.sum(x, y));
    }
  }

  return 0;
}