#line 2 "Library/src/data-structure/segtree.hpp" #include #include namespace kyopro { template class segtree { int lg, sz, n; std::vector dat; public: segtree() = default; segtree(int n) : segtree(std::vector(n, e())) {} segtree(const std::vector& vec) : n((int)vec.size()) { sz = 1, lg = 0; while (sz <= n) { sz <<= 1; lg++; } dat = std::vector(sz << 1, e()); for (int i = 0; i < n; i++) { set(i, vec[i]); } build(); } void set(int p, const S& v) { assert(0 <= p && p < sz); dat[sz + p] = v; } void build() { for (int i = sz - 1; i > 0; i--) { dat[i] = op(dat[i << 1 | 0], dat[i << 1 | 1]); } } S operator[](int p) const { return dat[sz + p]; } void update(int p, const S& v) { assert(0 <= p && p < sz); p += sz; dat[p] = v; while (p >>= 1) { dat[p] = op(dat[(p << 1) | 0], dat[(p << 1) | 1]); } } S fold(int l, int r) const { assert(0 <= l && l <= r && r <= sz); if (l == 0 && r == n) { return dat[1]; } l += sz, r += sz; S sml = e(), smr = e(); while (l != r) { if (l & 1) sml = op(sml, dat[l++]); if (r & 1) smr = op(dat[--r], smr); l >>= 1, r >>= 1; } return op(sml, smr); } void apply(int p, const S& v) { assert(0 <= p && p < sz); update(p, op(dat[sz + p], v)); } }; }; // namespace kyopro /** * @brief Segment Tree */ #line 1 "Library/src/debug.hpp" #ifdef ONLINE_JUDGE #define debug(x) void(0) #else #define _GLIBCXX_DEBUG #define debug(x) std::cerr << __LINE__ << " : " << #x << " = " << (x) << std::endl #endif #line 2 "Library/src/stream.hpp" #include #include #include #line 2 "Library/src/internal/type_traits.hpp" #include #include #include #include #include namespace kyopro { namespace internal { template struct first_enabled {}; template struct first_enabled, Args...> { using type = T; }; template struct first_enabled, Args...> : first_enabled {}; template struct first_enabled { using type = T; }; template using first_enabled_t = typename first_enabled::type; template * = nullptr> struct int_least { using type = first_enabled_t, std::enable_if, std::enable_if, std::enable_if, std::enable_if>; }; template * = nullptr> struct uint_least { using type = first_enabled_t, std::enable_if, std::enable_if, std::enable_if, std::enable_if>; }; template using int_least_t = typename int_least::type; template using uint_least_t = typename uint_least::type; template using double_size_uint_t = uint_least_t<2 * std::numeric_limits::digits>; template using double_size_int_t = int_least_t<2 * std::numeric_limits::digits>; struct modint_base {}; template using is_modint = std::is_base_of; template using is_modint_t = std::enable_if_t::value>; // is_integral template using is_integral_t = std::enable_if_t || std::is_same_v || std::is_same_v>; }; // namespace internal }; // namespace kyopro /* * @ref https://qiita.com/kazatsuyu/items/f8c3b304e7f8b35263d8 */ #line 6 "Library/src/stream.hpp" namespace kyopro { inline void single_read(char& c) { c = getchar_unlocked(); while (isspace(c)) c = getchar_unlocked(); } template * = nullptr> inline void single_read(T& a) { a = 0; bool is_negative = false; char c = getchar_unlocked(); while (isspace(c)) { c = getchar_unlocked(); } if (c == '-') is_negative = true, c = getchar_unlocked(); while (isdigit(c)) { a = 10 * a + (c - '0'); c = getchar_unlocked(); } if (is_negative) a *= -1; } template * = nullptr> inline void single_read(T& a) { long long x; single_read(x); a = T(x); } inline void single_read(std::string& str) noexcept { char c = getchar_unlocked(); while (isspace(c)) c = getchar_unlocked(); while (!isspace(c)) { str += c; c = getchar_unlocked(); } } template inline void read(T& x) noexcept {single_read(x);} template inline void read(Head& head, Tail&... tail) noexcept { single_read(head), read(tail...); } inline void single_write(char c) noexcept { putchar_unlocked(c); } template * = nullptr> inline void single_write(T a) noexcept { if (!a) { putchar_unlocked('0'); return; } if constexpr (std::is_signed_v) { if (a < 0) putchar_unlocked('-'), a *= -1; } constexpr int d = std::numeric_limits::digits10; char s[d + 1]; int now = d + 1; while (a) { s[--now] = (char)'0' + a % 10; a /= 10; } while (now <= d) putchar_unlocked(s[now++]); } template * = nullptr> inline void single_write(T a) noexcept { single_write(a.val()); } inline void single_write(const std::string& str) noexcept { for (auto c : str) { putchar_unlocked(c); } } template inline void write(T x) noexcept { single_write(x); } template inline void write(Head head, Tail... tail) noexcept { single_write(head); putchar_unlocked(' '); write(tail...); } template inline void put(Args... x) noexcept { write(x...); putchar_unlocked('\n'); } }; // namespace kyopro /** * @brief 高速入出力 */ #line 2 "Library/src/template.hpp" #include #define rep(i, n) for (int i = 0; i < (n); i++) #define all(x) std::begin(x), std::end(x) #define popcount(x) __builtin_popcountll(x) using i128 = __int128_t; using ll = long long; using ld = long double; using graph = std::vector>; using P = std::pair; constexpr int inf = std::numeric_limits::max() / 2; constexpr ll infl = std::numeric_limits::max() / 2; const long double pi = acosl(-1); constexpr uint64_t MOD = 1e9 + 7; constexpr uint64_t MOD2 = 998244353; constexpr int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; constexpr int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0}; template constexpr inline bool chmax(T1& a, T2 b) { return a < b && (a = b, true); } template constexpr inline bool chmin(T1& a, T2 b) { return a > b && (a = b, true); } #line 5 "a.cpp" using namespace std; using namespace kyopro; constexpr int op(int x, int y) { return min(x, y); } constexpr int e() { return inf; } int main() { int n, q; read(n, q); vector a(n); rep(i, n) read(a[i]); vector doubling(n, array()); segtree sg(n + 1); for (int i = n - 1; i >= 0; --i) { doubling[i][0] = sg.fold(a[i], n + 1); sg.update(a[i], i); } rep(j, 29) rep(i, n) { doubling[i][j + 1] = (doubling[i][j] != e() ? doubling[doubling[i][j]][j] : e()); } // rep(i, n) put(doubling[i][0], doubling[i][1]); // put("---"); while (q--) { int x, l, r; read(x, l, r); --l; ll ans = 0; for (int j = 29; j >= 0; --j) { if (doubling[l][j] < r) { ans += (1 << j); l = doubling[l][j]; } } put(ans + 1); } }