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>
  struct nd {
    nd* ch[2];
    nd* p;
    int sz;
    nd() : ch{0, 0}, p(0), sz(1) {}
  };
  int size(nd* v) {
    if (!v) return 0;
    return v->sz;
  }
  void fill_size(nd* v) {
    if (!v) return;
    v->sz = 1 + size(v->ch[0]) + size(v->ch[1]);
  }
  void zig(nd* v) {
    nd* p = v->p;
    int r = p->ch[1] == v;
    p->ch[r] = v->ch[!r];
    if (p->ch[r]) p->ch[r]->p = p;
    v->ch[!r] = p;
    v->p = p->p;
    if (p->p) p->p->ch[p->p->ch[1] == p] = v;
    p->p = v;
    fill_size(p);
    fill_size(v);
  }
  void splay(nd* v) {
    while (v->p)
      if (!v->p->p)
        zig(v);
      else if ((v->p->ch[1] == v) == (v->p->p->ch[1] == v->p))
        zig(v->p), zig(v);
      else
        zig(v), zig(v);
  }
  nd* pow(nd* v, int k) {
    splay(v);
    k += size(v->ch[0]);
    k %= v->sz;
    for (int szl = size(v->ch[0]); k != szl; szl = size(v->ch[0]))
      if (k < szl)
        v = v->ch[0];
      else
        k -= szl + 1, v = v->ch[1];
    splay(v);
    return v;
  }
  void trans(nd* u, nd* v) {
    splay(u);
    splay(v);
    if (u->p && u->p->p == v) zig(u);
    if (v->ch[0] == u) {
      splay(u);
      std::swap(u, v);
    }
    bool inside = v->ch[1] == u;
    if (inside) {
      v->ch[1] = u->ch[1];
      if (v->ch[1]) v->ch[1]->p = v;
      u->p = 0;
      u->ch[1] = 0;
      fill_size(v);
      fill_size(u);
    } else {
      std::swap(u->ch[1], v->ch[1]);
      if (u->ch[1]) u->ch[1]->p = u;
      if (v->ch[1]) v->ch[1]->p = v;
      fill_size(u);
      fill_size(v);
      nd* rr = v;
      while (rr->ch[1]) rr = rr->ch[1];
      splay(rr);
      rr->ch[1] = u;
      u->p = rr;
      fill_size(rr);
    }
  }
  void proc() {
    int n, q;
    std::cin >> n >> q;
    std::vector<int> p(n);
    for (int i = 0; i < n; --p[i], i++) std::cin >> p[i];
    std::vector<nd> f(n);
    for (int i = 0; i < n; i++)
      if (!f[i].p && f[i].sz == 1)
        for (int j = i; p[j] != i; j = p[j]) {
          f[j].p = &f[p[j]];
          f[p[j]].ch[0] = &f[j];
          f[p[j]].sz += f[j].sz;
        }
    while (q--) {
      int t, i, k;
      std::cin >> t >> i >> k;
      if (t == 1)
        trans(&f[i - 1], &f[k - 1]);
      else
        std::cout << pow(&f[i - 1], k) - &f[0] + 1 << std::endl;
    }
  }
  int main() {
    std::cin.tie(0);
    std::cout.tie(0);
    std::cin.sync_with_stdio(false);
    std::cout.sync_with_stdio(false);
    int t = 1;
    while (t--) proc();
    return 0;
  }
  