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

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int ll
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) begin(c), end(c)
#define range(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) range(i, 0, b)
#define rangei(i, a, b) for (ll a = a; i <= ll(b); i++)
#define repi(i, b) rangei(i, 1, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  os << "{ ";
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? " " : ", ");
  return os << "}";
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << "\\x1b[32m" << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " ["      \\
        << __LINE__ << "]\\x1b[0m" << endl)
#else
#define dump(...)
#endif
void fastios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // #define endl '\\n'
}
template <class T> size_t uniq(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  return v.size();
}
template <class T> size_t uniq(T *l, size_t n) {
  sort(l, l + n);
  return unique(l, l + n) - l;
}
#define mems(arr, val) memset(arr, val, sizeof(arr));
int const mod = 1000000007;
int const inf = numeric_limits<int>::max() / 8;

template <class T> class SqrtDecomp {
  // private:
public:
  const size_t B;
  T const initVal;
  size_t _size;
  vector<T> rangeVal, elemVal;

public:
  SqrtDecomp(const size_t size = 0, T const &initVal = numeric_limits<T>::max(),
             const size_t rangeSize = 500)
      : B(rangeSize), initVal(initVal), _size(size),
        rangeVal(size / B + 1, initVal), elemVal(size, initVal) {}

  size_t size() const { return _size; }

  T getMin(size_t l, const size_t &r) const {
    T res = numeric_limits<T>::max();
    while (l < r) {
      if (l % B == 0 && l + B <= r) {
        res = min(res, rangeVal[l / B]);
        l += B;
      } else {
        res = min(res, elemVal[l]);
        l++;
      }
    }
    return res;
  }

  void setVal(const size_t p, const T &x) {
    elemVal[p] = x;
    rangeVal[p / B] = initVal;
    size_t l = p - p % B, r = l + B, s = l / B;
    for (size_t i = l; i < min(r, size()); i++) {
      rangeVal[s] = min(rangeVal[s], elemVal[i]);
    }
  }

  vector<T> to_a(int l = -1, int r = -1) {
    if (l == -1)
      l = 0;
    if (r == -1)
      r = size();
    vector<T> res(r - l);
    for (int i = l; i < r; i++)
      res[i - l] = getMin(i, i + 1);
    return res;
  }
};

vector<int> naive(int n, int q, const vi &a, const vi &b, const vi &c) {
  vector<int> ans;
  vector<int> arr(n, (1LL << 31) - 1);
  for (int i = 0; i < q; i++) {
    if (a[i] == 0) {
      arr[b[i]] = c[i];
    } else {
      int res = (1LL << 31) - 1;
      for (int j = b[i]; j <= c[i]; j++) {
        res = min(res, arr[j]);
      }
      ans.push_back(res);
    }
  }
  return ans;
}

vector<int> solve(int n, int q, const vi &a, const vi &b, const vi &c) {
  vector<int> ans;
  SqrtDecomp<int32_t> rmq(n, (1LL << 31) - 1);
  for (int i = 0; i < q; i++) {
    if (a[i] == 0) {
      rmq.setVal(b[i], c[i]);
    } else {
      ans.push_back(rmq.getMin(b[i], c[i] + 1));
    }
  }
  return ans;
}

tuple<int, int, vi, vi, vi> generate() {
  int n = rand() % 10 + 1;
  int q = 3;
  vector<int> a(q), b(q), c(q);
  rep(i, q) {
    a[i] = rand() % 2;
    if (a[i] == 0) {
      b[i] = rand() % n;
      c[i] = rand() % 10;
    } else {
      b[i] = rand() % n;
      c[i] = rand() % n;
      if (b[i] > c[i])
        swap(b[i], c[i]);
    }
  }
  return make_tuple(n, q, a, b, c);
}

int main() {
  fastios();
  int n, q;
  cin >> n >> q;
  vector<int> a(q), b(q), c(q);
  rep(i, q) cin >> a[i] >> b[i] >> c[i];
  auto ans = solve(n, q, a, b, c);
  rep(i, ans.size()) cout << ans[i] << '\\n';
}

// int main(){
//     srand(time(0));
//     rep(i,1000000){
//         int n,q;
//         vector<int> a,b,c;
//         tie(n,q,a,b,c) = generate();
//         // a = { 0, 0, 1 };
//         // b = { 3, 3, 3 };
//         // c = { 2, 3, 6 };
//         // n = 8, q = 3;
//         vector<int> res = solve(n,q,a,b,c);
//         vector<int> exp = naive(n,q,a,b,c);
//         if(res != exp){
//             dump(res);
//             dump(exp);
//             dump(n);
//             dump(q);
//             dump(a);
//             dump(b);
//             dump(c);
//             abort();
//         }
//     }
// }

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int ll
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) begin(c), end(c)
#define range(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) range(i, 0, b)
#define rangei(i, a, b) for (ll a = a; i <= ll(b); i++)
#define repi(i, b) rangei(i, 1, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  os << "{ ";
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? " " : ", ");
  return os << "}";
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << "\\x1b[32m" << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " ["      \\
        << __LINE__ << "]\\x1b[0m" << endl)
#else
#define dump(...)
#endif
void fastios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // #define endl '\\n'
}
template <class T> size_t uniq(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  return v.size();
}
template <class T> size_t uniq(T *l, size_t n) {
  sort(l, l + n);
  return unique(l, l + n) - l;
}
#define mems(arr, val) memset(arr, val, sizeof(arr));
int const mod = 1000000007;
int const inf = numeric_limits<int>::max() / 8;

template <class T> class SqrtDecomp {
  // private:
public:
  const size_t B;
  T const initVal;
  size_t _size;
  vector<T> rangeVal, elemVal;

public:
  SqrtDecomp(const size_t size = 0, T const &initVal = numeric_limits<T>::max(),
             const size_t rangeSize = 1)
      : B(rangeSize), initVal(initVal), _size(size),
        rangeVal(size / B + 1, initVal), elemVal(size, initVal) {}

  size_t size() const { return _size; }

  T getMin(size_t l, const size_t &r) const {
    T res = numeric_limits<T>::max();
    while (l < r) {
      if (l % B == 0 && l + B <= r) {
        res = min(res, rangeVal[l / B]);
        l += B;
      } else {
        res = min(res, elemVal[l]);
        l++;
      }
    }
    return res;
  }

  void setVal(const size_t p, const T &x) {
    elemVal[p] = x;
    rangeVal[p / B] = initVal;
    size_t l = p - p % B, r = l + B, s = l / B;
    for (size_t i = l; i < min(r, size()); i++) {
      rangeVal[s] = min(rangeVal[s], elemVal[i]);
    }
  }

  vector<T> to_a(int l = -1, int r = -1) {
    if (l == -1)
      l = 0;
    if (r == -1)
      r = size();
    vector<T> res(r - l);
    for (int i = l; i < r; i++)
      res[i - l] = getMin(i, i + 1);
    return res;
  }
};

vector<int> naive(int n, int q, const vi &a, const vi &b, const vi &c) {
  vector<int> ans;
  vector<int> arr(n, (1LL << 31) - 1);
  for (int i = 0; i < q; i++) {
    if (a[i] == 0) {
      arr[b[i]] = c[i];
    } else {
      int res = (1LL << 31) - 1;
      for (int j = b[i]; j <= c[i]; j++) {
        res = min(res, arr[j]);
      }
      ans.push_back(res);
    }
  }
  return ans;
}

vector<int> solve(int n, int q, const vi &a, const vi &b, const vi &c) {
  vector<int> ans;
  SqrtDecomp<int32_t> rmq(n, (1LL << 31) - 1);
  for (int i = 0; i < q; i++) {
    if (a[i] == 0) {
      rmq.setVal(b[i], c[i]);
    } else {
      ans.push_back(rmq.getMin(b[i], c[i] + 1));
    }
  }
  return ans;
}

tuple<int, int, vi, vi, vi> generate() {
  int n = rand() % 10 + 1;
  int q = 3;
  vector<int> a(q), b(q), c(q);
  rep(i, q) {
    a[i] = rand() % 2;
    if (a[i] == 0) {
      b[i] = rand() % n;
      c[i] = rand() % 10;
    } else {
      b[i] = rand() % n;
      c[i] = rand() % n;
      if (b[i] > c[i])
        swap(b[i], c[i]);
    }
  }
  return make_tuple(n, q, a, b, c);
}

int main() {
  fastios();
  int n, q;
  cin >> n >> q;
  vector<int> a(q), b(q), c(q);
  rep(i, q) cin >> a[i] >> b[i] >> c[i];
  auto ans = solve(n, q, a, b, c);
  rep(i, ans.size()) cout << ans[i] << '\\n';
}

// int main(){
//     srand(time(0));
//     rep(i,1000000){
//         int n,q;
//         vector<int> a,b,c;
//         tie(n,q,a,b,c) = generate();
//         // a = { 0, 0, 1 };
//         // b = { 3, 3, 3 };
//         // c = { 2, 3, 6 };
//         // n = 8, q = 3;
//         vector<int> res = solve(n,q,a,b,c);
//         vector<int> exp = naive(n,q,a,b,c);
//         if(res != exp){
//             dump(res);
//             dump(exp);
//             dump(n);
//             dump(q);
//             dump(a);
//             dump(b);
//             dump(c);
//             abort();
//         }
//     }
// }