#include #define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++) #define rep(i, begin, end) for(int i = int(begin); i < int(end); i++) #define all(v) v.begin(), v.end() #define lint long long auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;}; auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;}; void debug_impl() { std::cerr << std::endl; } template void debug_impl(Head head, Tail... tail){ std::cerr << " " << head; debug_impl(tail...); } #define debug(...)\ std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\ debug_impl(__VA_ARGS__);\ std::cerr << std::noboolalpha; template class segment_tree { int n, N; BinaryOp op; Value id; std::vector table; void cal (int u) { table.at(u) = op(table.at(2 * u), table.at(2 * u + 1)); } public: segment_tree(int size, BinaryOp op, Value id, const std::vector& v): n (std::pow(2, int(std::log2(2 * size - 1)))), N (n * 2), op (op), id (id), table (N, id) { assert(n > 0); std::move(v.begin(), v.end(), table.begin() + n); for (int i = n - 1; i != 0; i--) cal(i); } segment_tree(int size, BinaryOp op, Value id): segment_tree(size, std::move(op), id, std::vector(size, id)) {} auto at (int i) const -> Value { return table.at(n + i); } auto collect () const -> std::vector { auto ret = std::vector(n); for (auto i = 0; i < n; i++) { ret.at(i) = at(i); } return ret; } auto query (int l, int r) const -> Value { struct state {int top, left, right;}; auto ret = id; std::stack stk; stk.push({1, 0, n}); while (!stk.empty()) { auto now = stk.top(); stk.pop(); if (l <= now.left && now.right <= r) { ret = op(ret, table.at(now.top)); continue; } int mid = (now.left + now.right) / 2; if (l < mid) stk.push({2 * now.top, now.left, mid}); if (mid < r) stk.push({2 * now.top + 1, mid, now.right}); } return ret; } void update (int u, Value val) { table.at(u += n) = val; for (u /= 2; u != 0; u /= 2) cal(u); } void add (int u, Value val) { update(u, at(u) + val); } }; template auto make_segment_tree(int size, BinaryOp op, Value vid) { return segment_tree(size, std::move(op), vid); } template auto make_segment_tree(int size, BinaryOp op, Value vid, std::vector v) { return segment_tree(size, std::move(op), vid, std::move(v)); } template std::istream& operator>> (std::istream& is, std::vector& v) { for (auto & x : v) is >> x; return is; } template std::ostream& operator<< (std::ostream& os, const std::vector& v) { auto n = v.size(); os << "{"; for (size_t i = 0; i < n; i++) {os << (i > 0 ? "," : "") << v.at(i);} return os << "}"; } template std::ostream& operator<< (std::ostream& os, const std::pair& pair){ return os << "(" << pair.first << "," << pair.second << ")"; } template std::istream& operator>> (std::iostream& is, std::pair& pair) { return is >> pair.first >> pair.second; } template constexpr OutputIterator coenumerate(InputIterator first, InputIterator last, OutputIterator result) { int cnt = 0; while (first != last) { *result = std::make_pair(*first, cnt); result++, first++, cnt++; } return result; } int main() { std::cin.tie(0); std::cin.sync_with_stdio(false); constexpr int inf = 1 << 30; int n, q; std::cin >> n >> q; std::vector a(n); std::cin >> a; for (auto & x : a) x--; std::vector> pairs(n); coenumerate(all(a), pairs.begin()); auto min_tree = make_segment_tree>( n, [](auto p, auto q) { return p.first < q.first ? p : q; }, std::pair{inf, -1}, pairs ); // debug(min_tree.collect()); loop(q) { int c, l, r; std::cin >> c >> l >> r; if (c == 1) { l--, r--; auto L = min_tree.at(l).first; auto R = min_tree.at(r).first; min_tree.update(l, std::pair{R, l}); min_tree.update(r, std::pair{L, r}); // debug(min_tree.collect()); } else { l--; auto ret = min_tree.query(l, r).second + 1; std::cout << ret << std::endl; } } return 0; }