description: You are given a permutation p of n elements. A permutation of n elements is an array of length n containing each integer from 1 to n exactly once. For example, [1, 2, 3] and [4, 3, 5, 1, 2] are permutations, but [1, 2, 4] and [4, 3, 2, 1, 2] are not permutations. You should perform q queries.
  
  There are two types of queries:
  
    * 1 x y — swap p_x and p_y. 
    * 2 i k — print the number that i will become if we assign i = p_i k times. 
  
  Input
  
  The first line contains two integers n and q (1 ≤ n, q ≤ 10^5).
  
  The second line contains n integers p_1, p_2, ..., p_n.
  
  Each of the next q lines contains three integers. The first integer is t (1 ≤ t ≤ 2) — type of query. If t = 1, then the next two integers are x and y (1 ≤ x, y ≤ n; x ≠ y) — first-type query. If t = 2, then the next two integers are i and k (1 ≤ i, k ≤ n) — second-type query.
  
  It is guaranteed that there is at least one second-type query.
  
  Output
  
  For every second-type query, print one integer in a new line — answer to this query.
  
  Examples
  
  Input
  
  
  5 4
  5 3 4 2 1
  2 3 1
  2 1 2
  1 1 3
  2 1 2
  
  
  Output
  
  
  4
  1
  2
  
  
  Input
  
  
  5 9
  2 3 5 1 4
  2 3 5
  2 5 5
  2 5 1
  2 5 3
  2 5 4
  1 5 4
  2 5 3
  2 2 5
  2 5 1
  
  
  Output
  
  
  3
  5
  4
  2
  3
  3
  3
  1
  
  Note
  
  In the first example p = \{5, 3, 4, 2, 1\}. 
  
  The first query is to print p_3. The answer is 4.
  
  The second query is to print p_{p_1}. The answer is 1.
  
  The third query is to swap p_1 and p_3. Now p = \{4, 3, 5, 2, 1\}.
  
  The fourth query is to print p_{p_1}. The answer is 2.
solution: #include <bits/stdc++.h>
  using namespace std;
  template <typename T>
  istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
  }
  template <typename T>
  ostream& operator<<(ostream& os, const vector<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
      os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
  }
  template <typename T, typename U>
  ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
  }
  template <typename T, typename U>
  ostream& operator<<(ostream& os, const map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
      os << '(' << itr->first << ',' << itr->second << ')';
      if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
  }
  template <typename T, typename U>
  ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
      os << '(' << itr->first << ',' << itr->second << ')';
      if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
  }
  template <typename T>
  ostream& operator<<(ostream& os, const set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
      os << *itr;
      if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
  }
  template <typename T>
  ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
      os << *itr;
      if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
  }
  template <typename T>
  ostream& operator<<(ostream& os, const unordered_set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
      os << *itr;
      if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
  }
  template <typename T>
  ostream& operator<<(ostream& os, const deque<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
      os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
  }
  template <typename T, size_t N>
  ostream& operator<<(ostream& os, const array<T, N>& v) {
    for (size_t i = 0; i < N; i++) {
      os << v[i] << (i + 1 == N ? "" : " ");
    }
    return os;
  }
  template <int i, typename T>
  void print_tuple(ostream&, const T&) {}
  template <int i, typename T, typename H, class... Args>
  void print_tuple(ostream& os, const T& t) {
    if (i) os << ',';
    os << get<i>(t);
    print_tuple<i + 1, T, Args...>(os, t);
  }
  template <typename... Args>
  ostream& operator<<(ostream& os, const tuple<Args...>& t) {
    os << '{';
    print_tuple<0, tuple<Args...>, Args...>(os, t);
    return os << '}';
  }
  void debug_out() { cerr << '\n'; }
  template <class Head, class... Tail>
  void debug_out(Head&& head, Tail&&... tail) {
    cerr << head;
    if (sizeof...(Tail) > 0) cerr << ", ";
    debug_out(move(tail)...);
  }
  template <typename T>
  T gcd(T x, T y) {
    return y != 0 ? gcd(y, x % y) : x;
  }
  template <typename T>
  T lcm(T x, T y) {
    return x / gcd(x, y) * y;
  }
  int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
  int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
  int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
  int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
  int popcount(signed t) { return __builtin_popcount(t); }
  int popcount(long long t) { return __builtin_popcountll(t); }
  bool ispow2(int i) { return i && (i & -i) == i; }
  long long MSK(int n) { return (1LL << n) - 1; }
  template <class T>
  T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
  }
  template <class T>
  T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
  }
  template <class T1, class T2>
  inline bool chmin(T1& a, T2 b) {
    if (a > b) {
      a = b;
      return true;
    }
    return false;
  }
  template <class T1, class T2>
  inline bool chmax(T1& a, T2 b) {
    if (a < b) {
      a = b;
      return true;
    }
    return false;
  }
  template <typename T>
  void mkuni(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
  }
  template <typename T>
  int lwb(const vector<T>& v, const T& x) {
    return lower_bound(v.begin(), v.end(), x) - v.begin();
  }
  const int INF = 1e9;
  const long long IINF = 1e18;
  const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  const char dir[4] = {'D', 'R', 'U', 'L'};
  const long long MOD = 1000000007;
  const int SQRT = 256;
  int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    for (int& x : p) cin >> x, x--;
    vector<int> block(n), inv(n);
    for (int i = 0; i < n; i++) {
      inv[p[i]] = i;
      block[i] = i;
      for (int j = 0; j < SQRT; j++) block[i] = p[block[i]];
    }
    auto update = [&](int x, int y) {
      swap(inv[p[x]], inv[p[y]]);
      swap(p[x], p[y]);
      for (int _ = 0; _ < 2; swap(x, y), _++) {
        int to = x;
        for (int i = 0; i < SQRT; i++) to = p[to];
        for (int i = 0; i < SQRT; i++) {
          block[x] = to;
          x = inv[x];
          to = inv[to];
        }
      }
    };
    auto query = [&](int i, int k) {
      for (; k >= SQRT; k -= SQRT) i = block[i];
      for (; k > 0; k--) i = p[i];
      return i + 1;
    };
    for (; q--;) {
      int t;
      cin >> t;
      if (t == 1) {
        int x, y;
        cin >> x >> y;
        update(--x, --y);
      } else {
        int i, k;
        cin >> i >> k;
        cout << query(--i, k) << '\n';
      }
    }
    return 0;
  }
  