//#define NDEBUG #include #include #include #include namespace n91 { using i8 = std::int_fast8_t; using i32 = std::int_fast32_t; using i64 = std::int_fast64_t; using u8 = std::uint_fast8_t; using u32 = std::uint_fast32_t; using u64 = std::uint_fast64_t; using isize = std::ptrdiff_t; using usize = std::size_t; constexpr usize operator"" _z(unsigned long long x) noexcept { return static_cast(x); } class rep { const usize f, l; public: class itr { friend rep; usize i; constexpr itr(const usize x) noexcept : i(x) {} public: void operator++() noexcept { ++i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; constexpr rep(const usize first, const usize last) noexcept : f(first), l(last) {} constexpr itr begin() const noexcept { return itr(f); } constexpr itr end() const noexcept { return itr(l); } }; class revrep { const usize f, l; public: class itr { friend revrep; usize i; constexpr itr(usize x) noexcept : i(x) {} public: void operator++() noexcept { --i; } constexpr usize operator*() const noexcept { return i; } constexpr bool operator!=(const itr x) const noexcept { return i != x.i; } }; constexpr revrep(usize first, usize last) noexcept : f(--first), l(--last) {} constexpr itr begin() const noexcept { return itr(l); } constexpr itr end() const noexcept { return itr(f); } }; template using vec_alias = std::vector; template auto md_vec(const usize n, const T& value) { return std::vector(n, value); } template auto md_vec(const usize n, Args... args) { return std::vector(n, md_vec(args...)); } template constexpr T difference(const T& a, const T& b) { return a < b ? b - a : a - b; } template T scan() { T ret; std::cin >> ret; return ret; } } // namespace n91 #include #include #include #include template class segment_tree { public: using value_structure = Monoid; using value_type = typename value_structure::value_type; using container_type = std::vector; using const_reference = typename container_type::const_reference; using size_type = typename container_type::size_type; protected: static size_type getsize(const size_type size) { size_type ret = 1; while (ret < size) ret <<= 1; return ret; } size_type size_; container_type tree; size_type base_size() const { return tree.size() >> 1; } void recalc(const size_type index) { tree[index] = value_structure::operation(tree[index << 1], tree[index << 1 | 1]); } public: segment_tree() : size_(0), tree() {} explicit segment_tree(const size_type size) : size_(size), tree(getsize(size) << 1, value_structure::identity()) {} template segment_tree(InputIterator first, InputIterator last) : size_(::std::distance(first, last)), tree() { const size_type cap = getsize(size_); tree.reserve(cap << 1); tree.resize(cap, value_structure::identity()); tree.insert(tree.end(), first, last); tree.resize(cap << 1, value_structure::identity()); for (size_type i = cap - 1; i; --i) recalc(i); } bool empty() const { return !size_; } size_type size() const { return size_; } const_reference operator[](const size_type index) const { assert(index < size()); return tree[index + base_size()]; } value_type fold(size_type first, size_type last) const { assert(first <= last); assert(first <= size()); assert(last <= size()); value_type ret_l = value_structure::identity(), ret_r = value_structure::identity(); for (first += base_size(), last += base_size(); first < last; first >>= 1, last >>= 1) { if (first & 1) ret_l = value_structure::operation(::std::move(ret_l), tree[first++]); if (last & 1) ret_r = value_structure::operation(tree[last - 1], ::std::move(ret_r)); } return value_structure::operation(::std::move(ret_l), ::std::move(ret_r)); } template size_type search(const F& f) const { if (f(value_structure::identity())) return 0; if (!f(tree[1])) return size() + 1; value_type acc = value_structure::identity(); size_type i = 1; while (i < base_size()) if (!f(value_structure::operation(acc, tree[i <<= 1]))) acc = value_structure::operation(::std::move(acc), tree[i++]); return i - base_size() + 1; } template void update(size_type index, const F& f) { assert(index < size()); index += base_size(); tree[index] = f(::std::move(tree[index])); while (index >>= 1) recalc(index); } }; #include #include #include #include template class Container> class lazy_segment_tree { public: using value_structure = ValueMonoid; using value_type = typename value_structure::value_type; using const_reference = const value_type &; using operator_structure = OperatorMonoid; using operator_type = typename operator_structure::value_type; using modifier = Modifier; using container_type = Container<::std::pair>; using size_type = typename container_type::size_type; private: size_type size_, height; container_type tree; static size_type getheight(const size_type size) noexcept { size_type ret = 0; while (static_cast(1) << ret < size) ++ret; return ret; } static value_type reflect(typename container_type::const_reference element) { return modifier::operation(element.first, element.second); } void recalc(const size_type index) { tree[index].first = value_structure::operation( reflect(tree[index << 1]), reflect(tree[index << 1 | 1])); } static void assign(operator_type& element, const operator_type& data) { element = operator_structure::operation(element, data); } void push(const size_type index) { assign(tree[index << 1].second, tree[index].second); assign(tree[index << 1 | 1].second, tree[index].second); tree[index].second = operator_structure::identity(); } void propagate(const size_type index) { for (size_type i = height; i; --i) push(index >> i); } void thrust(const size_type index) { tree[index].first = reflect(tree[index]); push(index); } void evaluate(const size_type index) { for (size_type i = height; i; --i) thrust(index >> i); } void build(size_type index) { while (index >>= 1) recalc(index); } size_type base_size() const { return static_cast(1) << height; } public: lazy_segment_tree() : size_(0), height(0), tree() {} explicit lazy_segment_tree(const size_type size) : size_(size), height(getheight(size_)), tree(static_cast(1) << (height + 1), { value_structure::identity(), operator_structure::identity() }) {} template explicit lazy_segment_tree(InputIterator first, InputIterator last) : size_(::std::distance(first, last)), height(getheight(size_)), tree() { const size_type cap = static_cast(1) << height; tree.reserve(cap << 1); tree.resize(cap, { value_structure::identity(), operator_structure::identity() }); for (; first != last; ++first) tree.emplace_back(*first, operator_structure::identity()); tree.resize(cap << 1, { value_structure::identity(), operator_structure::identity() }); for (size_type i = cap - 1; i; --i) recalc(i); } bool empty() const { return !size_; } size_type size() const { return size_; } const_reference operator[](size_type index) { assert(index < size()); index += base_size(); evaluate(index); tree[index].first = reflect(tree[index]); tree[index].second = operator_structure::identity(); return tree[index].first; } const_reference at(size_type index) { if (index < size()) { throw ::std::out_of_range("index out of range"); } else { index += base_size(); evaluate(index); tree[index].first = reflect(tree[index]); tree[index].second = operator_structure::identity(); return tree[index].first; } } value_type fold(size_type first, size_type last) { assert(first <= last); assert(first <= size()); assert(last <= size()); first += base_size(); last += base_size(); evaluate(first); evaluate(last - 1); value_type ret_l = value_structure::identity(), ret_r = value_structure::identity(); for (; first < last; first >>= 1, last >>= 1) { if (first & 1) ret_l = value_structure::operation(ret_l, reflect(tree[first++])); if (last & 1) ret_r = value_structure::operation(reflect(tree[last - 1]), ret_r); } return value_structure::operation(ret_l, ret_r); } template size_type search(const F& f) { if (f(value_structure::identity())) return static_cast(0); if (!f(reflect(tree[1]))) return size() + 1; value_type acc = value_structure::identity(); size_type i = 1; while (i < base_size()) { thrust(i); if (!f(value_structure::operation(acc, reflect(tree[i <<= 1])))) acc = value_structure::operation(acc, reflect(tree[i++])); } return i - base_size() + 1; } template void update(size_type index, const F& f) { assert(index < size()); index += base_size(); propagate(index); tree[index].first = f(reflect(tree[index])); tree[index].second = operator_structure::identity(); build(index); } void update(size_type first, size_type last, const operator_type& data) { assert(first <= last); assert(first <= size()); assert(last <= size()); first += base_size(); last += base_size(); propagate(first); propagate(last - 1); for (size_type left = first, right = last; left < right; left >>= 1, right >>= 1) { if (left & 1) assign(tree[left++].second, data); if (right & 1) assign(tree[right - 1].second, data); } build(first); build(last - 1); } }; template using vec_alias = std::vector; #include #include #include #include namespace n91 { template class plus_monoid { public: using value_type = T; static value_type operation(const value_type& x, const value_type& y) { return x + y; } static value_type identity() { return value_type(0); } static value_type reverse(const value_type& x) { return x; } }; void main_() { const usize n = scan(); const usize q = scan(); segment_tree> seg(n); std::vector a(n); for (auto& e : a) { std::cin >> e; } struct query_type { usize l, r, ans; }; std::vector query(q); for (auto& e : query) { scan(); e.l = scan() - 1; e.r = scan(); } auto idx = md_vec(n, 0, usize()); for (const auto i : rep(0_z, q)) { idx[query[i].l].push_back(i); } std::vector st; for (const auto i : revrep(0_z, n)) { while (!st.empty() && a[st.back()] < a[i]) { seg.update(st.back(), [](auto) { return 0_z; }); st.pop_back(); } st.push_back(i); seg.update(i, [](auto) { return 1_z; }); for (const auto k : idx[i]) { query[k].ans = seg.fold(query[k].l, query[k].r); } } for (const auto& e : query) { std::cout << e.ans << std::endl; } } } // namespace n91 int main() { n91::main_(); return 0; }