#define PROBLEM "https://judge.yosupo.jp/problem/staticrmq" #include <iostream> #include <vector> #include "Mylib/AlgebraicStructure/Monoid/bounded_min.cpp" #include "Mylib/DataStructure/SparseTable/sparse_table.cpp" #include "Mylib/IO/input_tuples.cpp" #include "Mylib/IO/input_vector.cpp" namespace hl = haar_lib; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int N, Q; std::cin >> N >> Q; auto a = hl::input_vector<int>(N); hl::sparse_table<hl::bounded_min_monoid<int>> s(a); for (auto [l, r] : hl::input_tuples<int, int>(Q)) { std::cout << s.fold(l, r).value() << "\n"; } return 0; }
#line 1 "test/yosupo-judge/staticrmq/main.sparse_table.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/staticrmq" #include <iostream> #include <vector> #line 2 "Mylib/AlgebraicStructure/Monoid/bounded_min.cpp" #include <algorithm> #include <limits> namespace haar_lib { template <typename T> struct bounded_min_monoid { using value_type = T; value_type operator()() const { return std::numeric_limits<T>::max(); } value_type operator()(const value_type &a, const value_type &b) const { return std::min(a, b); } }; } // namespace haar_lib #line 3 "Mylib/DataStructure/SparseTable/sparse_table.cpp" #include <cassert> #include <optional> #include <utility> #line 7 "Mylib/DataStructure/SparseTable/sparse_table.cpp" namespace haar_lib { template <typename Semilattice> class sparse_table { public: using value_type = typename Semilattice::value_type; private: Semilattice S_; int n_; std::vector<std::vector<value_type>> data_; std::vector<int> log_table_; public: sparse_table() {} template <typename T> sparse_table(const std::vector<T> &v) : n_(v.size()) { int logn = 0; while ((1 << logn) <= n_) ++logn; data_.assign(n_, std::vector<value_type>(logn)); for (int i = 0; i < n_; ++i) data_[i][0] = v[i]; for (int j = 1; j < logn; ++j) { for (int i = 0; i < n_; ++i) { data_[i][j] = S_(data_[i][j - 1], data_[std::min<int>(n_ - 1, i + (1 << (j - 1)))][j - 1]); } } log_table_.assign(n_ + 1, 0); for (int i = 2; i < n_ + 1; ++i) log_table_[i] = log_table_[i >> 1] + 1; } std::optional<value_type> fold(int l, int r) const { assert(0 <= l and l <= r and r <= n_); if (l == r) return std::nullopt; int k = log_table_[r - l]; return S_(data_[l][k], data_[r - (1 << k)][k]); } }; } // namespace haar_lib #line 2 "Mylib/IO/input_tuples.cpp" #include <initializer_list> #line 4 "Mylib/IO/input_tuples.cpp" #include <tuple> #line 6 "Mylib/IO/input_tuple.cpp" namespace haar_lib { template <typename T, size_t... I> static void input_tuple_helper(std::istream &s, T &val, std::index_sequence<I...>) { (void) std::initializer_list<int>{(void(s >> std::get<I>(val)), 0)...}; } template <typename T, typename U> std::istream &operator>>(std::istream &s, std::pair<T, U> &value) { s >> value.first >> value.second; return s; } template <typename... Args> std::istream &operator>>(std::istream &s, std::tuple<Args...> &value) { input_tuple_helper(s, value, std::make_index_sequence<sizeof...(Args)>()); return s; } } // namespace haar_lib #line 8 "Mylib/IO/input_tuples.cpp" namespace haar_lib { template <typename... Args> class InputTuples { struct iter { using value_type = std::tuple<Args...>; value_type value; bool fetched = false; int N, c = 0; value_type operator*() { if (not fetched) { std::cin >> value; } return value; } void operator++() { ++c; fetched = false; } bool operator!=(iter &) const { return c < N; } iter(int N) : N(N) {} }; int N; public: InputTuples(int N) : N(N) {} iter begin() const { return iter(N); } iter end() const { return iter(N); } }; template <typename... Args> auto input_tuples(int N) { return InputTuples<Args...>(N); } } // namespace haar_lib #line 4 "Mylib/IO/input_vector.cpp" namespace haar_lib { template <typename T> std::vector<T> input_vector(int N) { std::vector<T> ret(N); for (int i = 0; i < N; ++i) std::cin >> ret[i]; return ret; } template <typename T> std::vector<std::vector<T>> input_vector(int N, int M) { std::vector<std::vector<T>> ret(N); for (int i = 0; i < N; ++i) ret[i] = input_vector<T>(M); return ret; } } // namespace haar_lib #line 9 "test/yosupo-judge/staticrmq/main.sparse_table.test.cpp" namespace hl = haar_lib; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int N, Q; std::cin >> N >> Q; auto a = hl::input_vector<int>(N); hl::sparse_table<hl::bounded_min_monoid<int>> s(a); for (auto [l, r] : hl::input_tuples<int, int>(Q)) { std::cout << s.fold(l, r).value() << "\n"; } return 0; }