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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

#define INF ((1 << 31) - 1)

class SegmentTree {
private:
  int *stree;
  int n;

public:
  SegmentTree(int N);
  ~SegmentTree();
  void update(int i, int x);
  int find(int s, int t);
  int query(int s, int t, int k, int l, int r);
};

SegmentTree::SegmentTree(int N) {
  int leading_zero = __builtin_clz(N);

  /* find nearest power of 2 for given N */
  if (__builtin_popcount(N) == 1)
    n = 1 << (sizeof(int) * 8 - leading_zero - 1);
  else
    n = 1 << (sizeof(int) * 8 - leading_zero);

  /* initializing nodes as INF */
  stree = new int[2 * n - 1];
  for (int i = 0; i < 2 * n - 1; i++) {
    stree[i] = INF;
  }
}

SegmentTree::~SegmentTree() { delete[] stree; }

void SegmentTree::update(int i, int x) {
  int j;

  /* deepest number of nodes */
  j = i + n - 1;
  stree[j] = x;

  while (j > 0) {
    /* number of upper node */
    j = (j - 1) / 2;
    stree[j] = min(stree[2 * j + 1], stree[2 * j + 2]);
  }
}

int SegmentTree::find(int s, int t) { return query(s, t + 1, 0, 0, n); }

int SegmentTree::query(int s, int t, int k, int l, int r) {
  if (r <= s || t <= l)
    return INF;
  if (s <= l && r <= t)
    return stree[k];
  else {
    int vl = query(s, t, 2 * k + 1, l, (l + r) / 2);
    int vr = query(s, t, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}

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

  SegmentTree seg(n);

  for (int i = 0; i < q; i++) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com == 0)
      seg.update(x, y);
    if (com == 1)
      cout << seg.find(x, y) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

LL find_min(LL *a, LL x, LL y) {
  LL ans = a[x];
  for (LL j = x; j <= y; j++) {
    ans = min(ans, a[j]);
  }
  return ans;
}

int main(void) {
  LL n, q, x, y, com;
  cin >> n >> q;
  LL a[n];

  for (int i = 0; i < n; i++)
    a[i] = (1 << 31) - 1;

  for (int i = 0; i < q; i++) {
    cin >> com >> x >> y;
    if (com == 0)
      a[x] = y;
    else if (com == 1) {
      cout << find_min(a, x, y) << endl;
    }
  }

  return 0;
}