//#include "stdafx.h"
#include <cstdio>
#include <cassert>
#include <fstream>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <vector>
#include <list>
#include <map>
#include <ctime>

using namespace std;
typedef long long LL;

#define LSOne(S) (S & (-S))

const int N = 100002;

class BIT {
    int* ft, size;
public:
    // initialize a BIT of n elements to zero
    BIT() {
        size = N;
        ft = new int[N + 1];
        for (int i = 0; i < N + 1; ++i)
            ft[i] = 0;
    }

    ~BIT()  {
        delete[] ft;
    }

    // returns sum of the range [1...b]
    int sum(int b) {
        int sum = 0;
        for (; b; b -= LSOne(b)) sum += ft[b];
        return sum;
    }

    // returns sum of the range [a...b]
    int sum(int a, int b) {
        return sum(b) - (a == 1 ? 0 : sum(a - 1));
    }

    // update value of the k-th element by v (v can be +ve/inc or -ve/dec)
    // i.e., increment or decrement kth element by a value v

    void update(int k, int v) {
         for (; k <= size; k += LSOne(k)) ft[k] += v;
    }
};

map<int, int> M;
set<int> UU;

void run()
{
    const int BLK = 20;
    int n, m;
    cin >> n;
    const int LEN = (n + BLK - 1) / BLK;
    int A[N];
    int F[N];
    int P[2*N];
    int SUM[BLK + 1];
    memset(SUM, 0, sizeof(SUM));

    memset(F, 0, sizeof(F));
    vector<vector<int> > QUERY;
    BIT TREES[BLK + 1];

    int MX[BLK+1][2*N];
    memset(MX, 0, sizeof(MX));

    for (int i = 1; i <= n; ++i) {
        scanf("%d", &A[i]);
        UU.insert(A[i]);
    }

    cin >> m;
    for (int qr = 1; qr <= m; ++qr) {
        vector<int> v(3);
        scanf("%d %d %d", &v[0], &v[1], &v[2]);
        if (v[0] == 1)
            UU.insert(v[1]);
        QUERY.push_back(v);
    }

    vector<int> VV(UU.begin(), UU.end());
    for (int i = 0; i < VV.size(); ++i)
        M[VV[i]] = i + 1;
    map<int, int> MM;
    for (int i = 1; i <= n; ++i) {
        A[i] = M[A[i]];
        F[i] = ++MM[A[i]];
    }

    for (int qr = 0; qr < QUERY.size(); ++qr) if (QUERY[qr][0] == 1)
        QUERY[qr][1] = M[QUERY[qr][1]];

    for (int blk = 1; blk <= BLK; ++blk) for (int i = 1; i <= min(LEN * blk,n); ++i)
        MX[blk][A[i]] = F[i];

    for (int blk = 1; blk <= BLK; ++blk) {
        for (int i = 1; i <= LEN * blk && i <= n; ++i) {
            if (F[i] == MX[blk][A[i]])
                TREES[blk].update(F[i], 1);
        }
    }

    int qid = 0;

    for (int qr = 0; qr < QUERY.size(); ++qr) {
        int tp = QUERY[qr][0];
        if (tp == 1 && A[QUERY[qr][2]] != QUERY[qr][1]) {
            int id = QUERY[qr][2];
            int v = QUERY[qr][1];
            for (int blk = (id+LEN-1)/LEN; blk <= BLK; ++blk) {
                if (MX[blk][v])
                    TREES[blk].update(MX[blk][v], -1);
                ++MX[blk][v];
                TREES[blk].update(MX[blk][v], 1);
                TREES[blk].update(MX[blk][A[id]], -1);
                --MX[blk][A[id]];
                if (MX[blk][A[id]])
                    TREES[blk].update(MX[blk][A[id]], 1);
            }
            A[id] = v;
        }
        else if (tp == 2)
        {
            ++qid;
            if (QUERY[qr][1] == 1)
                printf("%d\n", VV[A[QUERY[qr][2]]-1]);
            else
            {
                int id = QUERY[qr][2];
                int r = QUERY[qr][1] & 1;
                int blk = (id - 1) / LEN;
                int fr = MX[blk][A[id]];
                for (int i = blk * LEN + 1; i <= id; ++i)
                    fr += A[i] == A[id];
                assert(fr);
                if (!r)
                    printf("%d\n", fr);
                else {
                    int sofar = blk ? TREES[blk].sum(fr, N) : 0;
                    if (n == 2000 && qid == 1003) {
                        /*for (int j = 1; j <= 50; ++j)
                            cout << A[j] << "|";
                        cout << endl;*/
                        //for (int i = 1; i <= id; ++i) if (A[i] == A[id])
                        //    ++fr;
                        //cout << QUERY[qr][1] << "|" << QUERY[qr][2] << "|" << fr << "|" << A[id] << "|" << VV[A[id]-1] << endl;

                    }

                    for (int i = blk * LEN + 1; i <= id; ++i)
                        P[A[i]] = MX[blk][A[i]];
                    for (int i = blk * LEN + 1; i <= id; ++i) {
                        //if (n == 2000 && qid == 2)
                        //    cout << P[A[i]] << " " << A[i] << " " << endl;
                        if (++P[A[i]] == fr)
                            ++sofar;
                    }
                    printf("%d\n", sofar);
                }
            }
        }
    }
}

int main() {
	run();
	return 0;
}


//#include "stdafx.h"
#include <cstdio>
#include <cassert>
#include <fstream>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <vector>
#include <list>
#include <map>
#include <ctime>

using namespace std;
typedef long long LL;

#define LSOne(S) (S & (-S))

const int N = 1002;

class BIT {
    int* ft, size;
public:
    // initialize a BIT of n elements to zero
    BIT() {
        size = N;
        ft = new int[N + 1];
        memset(ft, 0, sizeof(int) * (N + 1));
    }

    ~BIT()  {
        delete[] ft;
    }

    // returns sum of the range [1...b]
    int sum(int b) {
        int sum = 0;
        for (; b; b -= LSOne(b)) sum += ft[b];
        return sum;
    }

    // returns sum of the range [a...b]
    int sum(int a, int b) {
        return sum(b) - (a == 1 ? 0 : sum(a - 1));
    }

    // update value of the k-th element by v (v can be +ve/inc or -ve/dec)
    // i.e., increment or decrement kth element by a value v

    void update(int k, int v) {
         for (; k <= size; k += LSOne(k)) ft[k] += v;
    }
};

map<int, int> M;
set<int> UU;
void calmap() {
    vector<int> VV(UU.begin(), UU.end());
    for (int i = 0; i < VV.size(); ++i)
        M[VV[i]] = i + 1;
}

void run()
{
    const int BLK = 20;
    int n, m;
    cin >> n;
    const int LEN = (n + BLK - 1) / BLK;
    int A[N];
    int F[N];
    int P[2*N];

    memset(F, 0, sizeof(F));
    vector<vector<int> > QUERY;
    BIT TREES[BLK + 1];

    int MX[BLK+1][2*N];
    memset(MX, 0, sizeof(MX));

    for (int i = 1; i <= n; ++i) {
        scanf("%d", &A[i]);
        UU.insert(A[i]);
    }

    cin >> m;
    for (int qr = 1; qr <= m; ++qr) {
        vector<int> v(3);
        scanf("%d %d %d", &v[0], &v[1], &v[2]);
        if (v[0] == 1)
            UU.insert(v[1]);
        QUERY.push_back(v);
    }

    calmap();
    map<int, int> MM;
    for (int i = 1; i <= n; ++i) {
        A[i] = M[A[i]];
        F[i] = ++MM[A[i]];
    }

    for (int qr = 0; qr < QUERY.size(); ++qr) if (QUERY[qr][0] == 1)
        QUERY[qr][1] = M[QUERY[qr][1]];

    for (int blk = 1; blk <= BLK; ++blk) for (int i = 1; i <= min(LEN * blk,n); ++i)
        MX[blk][A[i]] = F[i];

    for (int blk = 1; blk <= BLK; ++blk) {
        for (int i = 1; i <= LEN * blk && i <= n; ++i) {
            if (F[i] == MX[blk][A[i]])
                TREES[blk].update(F[i], 1);
        }
    }

    int qid = 0;

    for (int qr = 0; qr < QUERY.size(); ++qr) {
        int tp = QUERY[qr][0];
        if (tp == 1 && A[QUERY[qr][2]] != QUERY[qr][1]) {
            int id = QUERY[qr][2];
            int v = QUERY[qr][1];
            for (int blk = (id+LEN-1)/LEN; blk <= BLK; ++blk) {
                if (MX[blk][v])
                    TREES[blk].update(MX[blk][v], -1);
                ++MX[blk][v];
                TREES[blk].update(MX[blk][v], 1);
                TREES[blk].update(MX[blk][A[id]], -1);
                --MX[blk][A[id]];
                if (MX[blk][A[id]])
                    TREES[blk].update(MX[blk][A[id]], 1);
            }
            A[id] = v;
        }
        else if (tp == 2)
        {
            ++qid;
            if (QUERY[qr][1] == 1)
                printf("%d\n", A[QUERY[qr][2]]);
            else
            {
                int id = QUERY[qr][2];
                int r = QUERY[qr][1] & 1;
                int blk = (id - 1) / LEN;
                int fr = MX[blk][A[id]];
                for (int i = blk * LEN + 1; i <= id; ++i)
                    fr += A[i] == A[id];
                if (!r)
                    printf("%d\n", fr);
                else {
                    int sofar = blk ? TREES[blk].sum(fr, n) : 0;
                    if (n == 2000 && qid == 2) {
                        cout << A[1] << " " << A[2] << endl;
                        cout << QUERY[qr][1] << " " << QUERY[qr][2] << " " << fr << " " << sofar << endl;
                    }

                    for (int i = blk * LEN + 1; i <= id; ++i)
                        P[A[i]] = MX[blk][A[i]];
                    for (int i = blk * LEN + 1; i <= id; ++i) {
                        if (++P[A[i]] == fr)
                            ++sofar;
                        printf("%d\n", sofar);
                    }
                }
            }
        }
    }
}

int main() {
	run();
	return 0;
}


//#include "stdafx.h"
#include <cstdio>
#include <cassert>
#include <fstream>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <vector>
#include <list>
#include <map>
#include <ctime>

using namespace std;
typedef long long LL;

#define LSOne(S) (S & (-S))

const int N = 1002;

class BIT {
    int* ft, size;
public:
    // initialize a BIT of n elements to zero
    BIT() {
        size = N;
        ft = new int[N + 1];
        memset(ft, 0, sizeof(int) * (N + 1));
    }

    ~BIT()  {
        delete[] ft;
    }

    // returns sum of the range [1...b]
    int sum(int b) {
        int sum = 0;
        for (; b; b -= LSOne(b)) sum += ft[b];
        return sum;
    }

    // returns sum of the range [a...b]
    int sum(int a, int b) {
        return sum(b) - (a == 1 ? 0 : sum(a - 1));
    }

    // update value of the k-th element by v (v can be +ve/inc or -ve/dec)
    // i.e., increment or decrement kth element by a value v

    void update(int k, int v) {
         for (; k <= size; k += LSOne(k)) ft[k] += v;
    }
};

map<int, int> M;
set<int> UU;
void calmap() {
    vector<int> VV(UU.begin(), UU.end());
    for (int i = 0; i < VV.size(); ++i)
        M[VV[i]] = i + 1;
}

void run()
{
    const int BLK = 20;
    int n, m;
    cin >> n;
    const int LEN = (n + BLK - 1) / BLK;
    int A[N];
    int F[N];
    int P[2*N];

    memset(F, 0, sizeof(F));
    vector<vector<int> > QUERY;
    BIT TREES[BLK + 1];

    int MX[BLK+1][2*N];
    memset(MX, 0, sizeof(MX));

    for (int i = 1; i <= n; ++i) {
        scanf("%d", &A[i]);
        UU.insert(A[i]);
    }

    cin >> m;
    for (int qr = 1; qr <= m; ++qr) {
        vector<int> v(3);
        scanf("%d %d %d", &v[0], &v[1], &v[2]);
        if (v[0] == 1)
            UU.insert(v[1]);
        QUERY.push_back(v);
    }

    calmap();
    map<int, int> MM;
    for (int i = 1; i <= n; ++i) {
        A[i] = M[A[i]];
        F[i] = ++MM[A[i]];
    }

    for (int qr = 0; qr < QUERY.size(); ++qr) if (QUERY[qr][0] == 1)
        QUERY[qr][1] = M[QUERY[qr][1]];

    for (int blk = 1; blk <= BLK; ++blk) for (int i = 1; i <= min(LEN * blk,n); ++i)
        MX[blk][A[i]] = F[i];

    for (int blk = 1; blk <= BLK; ++blk) {
        for (int i = 1; i <= LEN * blk && i <= n; ++i) {
            if (F[i] == MX[blk][A[i]])
                TREES[blk].update(F[i], 1);
        }
    }

    int qid = 0;

    for (int qr = 0; qr < QUERY.size(); ++qr) {
        int tp = QUERY[qr][0];
        if (tp == 1 && A[QUERY[qr][2]] != QUERY[qr][1]) {
            int id = QUERY[qr][2];
            int v = QUERY[qr][1];
            for (int blk = (id+LEN-1)/LEN; blk <= BLK; ++blk) {
                if (MX[blk][v])
                    TREES[blk].update(MX[blk][v], -1);
                ++MX[blk][v];
                TREES[blk].update(MX[blk][v], 1);
                TREES[blk].update(MX[blk][A[id]], -1);
                --MX[blk][A[id]];
                if (MX[blk][A[id]])
                    TREES[blk].update(MX[blk][A[id]], 1);
            }
            A[id] = v;
        }
        else if (tp == 2)
        {
            ++qid;
            if (QUERY[qr][1] == 1)
                printf("%d\n", A[QUERY[qr][2]]);
            else
            {
                int id = QUERY[qr][2];
                int r = QUERY[qr][1] & 1;
                int blk = (id - 1) / LEN;
                int fr = MX[blk][A[id]];
                for (int i = blk * LEN + 1; i <= id; ++i)
                    fr += A[i] == A[id];
                if (!r)
                    printf("%d\n", fr);
                else {
                    int sofar = blk ? TREES[blk].sum(fr, n) : 0;
                    if (n == 2000 && qid == 2) {
                        cout << A[1] << " " << A[2] << endl;
                        cout << QUERY[qr][1] << " " << QUERY[qr][2] << " " << fr << " " << sofar << " " << LEN << endl;
                    }

                    for (int i = blk * LEN + 1; i <= id; ++i)
                        P[A[i]] = MX[blk][A[i]];
                    for (int i = blk * LEN + 1; i <= id; ++i) {
                        if (++P[A[i]] == fr)
                            ++sofar;
                        printf("%d\n", sofar);
                    }
                }
            }
        }
    }
}

int main() {
	run();
	return 0;
}


//#include "stdafx.h"
#include <cstdio>
#include <cassert>
#include <fstream>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <vector>
#include <list>
#include <map>
#include <ctime>

using namespace std;
typedef long long LL;

#define LSOne(S) (S & (-S))

const int N = 1002;

class BIT {
    int* ft, size;
public:
    // initialize a BIT of n elements to zero
    BIT() {
        size = N;
        ft = new int[N + 1];
        memset(ft, 0, sizeof(int) * (N + 1));
    }

    ~BIT()  {
        delete[] ft;
    }

    // returns sum of the range [1...b]
    int sum(int b) {
        int sum = 0;
        for (; b; b -= LSOne(b)) sum += ft[b];
        return sum;
    }

    // returns sum of the range [a...b]
    int sum(int a, int b) {
        return sum(b) - (a == 1 ? 0 : sum(a - 1));
    }

    // update value of the k-th element by v (v can be +ve/inc or -ve/dec)
    // i.e., increment or decrement kth element by a value v

    void update(int k, int v) {
         for (; k <= size; k += LSOne(k)) ft[k] += v;
    }
};

map<int, int> M;
set<int> UU;
void calmap() {
    vector<int> VV(UU.begin(), UU.end());
    for (int i = 0; i < VV.size(); ++i)
        M[VV[i]] = i + 1;
}

void run()
{
    const int BLK = 20;
    int n, m;
    cin >> n;
    const int LEN = (n + BLK - 1) / BLK;
    int A[N];
    int F[N];
    int P[2*N];

    memset(F, 0, sizeof(F));
    vector<vector<int> > QUERY;
    BIT TREES[BLK + 1];

    int MX[BLK+1][2*N];
    memset(MX, 0, sizeof(MX));

    for (int i = 1; i <= n; ++i) {
        scanf("%d", &A[i]);
        UU.insert(A[i]);
    }

    cin >> m;
    for (int qr = 1; qr <= m; ++qr) {
        vector<int> v(3);
        scanf("%d %d %d", &v[0], &v[1], &v[2]);
        if (v[0] == 1)
            UU.insert(v[1]);
        QUERY.push_back(v);
    }

    calmap();
    map<int, int> MM;
    for (int i = 1; i <= n; ++i) {
        A[i] = M[A[i]];
        F[i] = ++MM[A[i]];
    }

    for (int qr = 0; qr < QUERY.size(); ++qr) if (QUERY[qr][0] == 1)
        QUERY[qr][1] = M[QUERY[qr][1]];

    for (int blk = 1; blk <= BLK; ++blk) for (int i = 1; i <= min(LEN * blk,n); ++i)
        MX[blk][A[i]] = F[i];

    for (int blk = 1; blk <= BLK; ++blk) {
        for (int i = 1; i <= LEN * blk && i <= n; ++i) {
            if (F[i] == MX[blk][A[i]])
                TREES[blk].update(F[i], 1);
        }
    }

    int qid = 0;

    for (int qr = 0; qr < QUERY.size(); ++qr) {
        int tp = QUERY[qr][0];
        if (tp == 1 && A[QUERY[qr][2]] != QUERY[qr][1]) {
            int id = QUERY[qr][2];
            int v = QUERY[qr][1];
            for (int blk = (id+LEN-1)/LEN; blk <= BLK; ++blk) {
                if (MX[blk][v])
                    TREES[blk].update(MX[blk][v], -1);
                ++MX[blk][v];
                TREES[blk].update(MX[blk][v], 1);
                TREES[blk].update(MX[blk][A[id]], -1);
                --MX[blk][A[id]];
                if (MX[blk][A[id]])
                    TREES[blk].update(MX[blk][A[id]], 1);
            }
            A[id] = v;
        }
        else if (tp == 2)
        {
            ++qid;
            if (n == 1000 && qid == 2) {
                cout << A[1] << " " << A[2] << endl;
                cout << QUERY[qr][1] << " " << QUERY[qr][2] << endl;
            }
            if (QUERY[qr][1] == 1)
                printf("%d\n", A[QUERY[qr][2]]);
            else
            {
                int id = QUERY[qr][2];
                int r = QUERY[qr][1] & 1;
                int blk = (id - 1) / LEN;
                int fr = MX[blk][A[id]];
                for (int i = blk * LEN + 1; i <= id; ++i)
                    fr += A[i] == A[id];
                if (!r)
                    printf("%d\n", fr);
                else {
                    int sofar = blk ? TREES[blk].sum(fr, n) : 0;
                    for (int i = blk * LEN + 1; i <= id; ++i)
                        P[A[i]] = MX[blk][A[i]];
                    for (int i = blk * LEN + 1; i <= id; ++i) {
                        if (++P[A[i]] == fr)
                            ++sofar;
                        printf("%d\n", sofar);
                    }
                }
            }
        }
    }
}

int main() {
	run();
	return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<vector>
#include<map>
using namespace std;
map<int,int>hs;
const int bound=1000;
int flag,x,y,f[111][201000],g[111][201000],list[210000],cnt_list,vl[210000],n,m,cnt;
int ans;
int get(int x)
{
	if(hs.count(x))
	   return hs[x];
    hs[x]=cnt++;
    list[cnt-1]=x;
    return cnt-1;
}
int main()
{
	int i,j,s,p,q,id;
	scanf("%d",&n);
	hs.clear();
	cnt=0;
	for(i=0;i<n;i++)
	{
	    scanf("%d",&vl[i]);
	    vl[i]=get(vl[i]);
	}
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	for(i=0;i<=(n-1)/bound;i++)
	{
		if(i)
		{
		   for(j=0;j<=n;j++)
		       g[i+1][j]=g[i][j];
	       for(j=0;j<cnt;j++)
	           f[i+1][j]=f[i][j];
		}
		for(j=i*bound;j<min(n,(i+1)*bound);j++)
		{
			f[i+1][vl[j]]++;
			g[i+1][f[i+1][vl[j]]]++;
		}
	}
	scanf("%d",&m);
    while(m--)
    {
    	scanf("%d%d%d",&flag,&x,&y);
    	y--;
		if(flag==1)
    	{
    		x=get(x);
	    	id=y/bound;
	    	for(i=id+1;i<=(n-1)/bound+1;i++)
	    	{
	    		g[i][f[i][vl[y]]]--;
	    		f[i][vl[y]]--;
	    	}
	    	vl[y]=x;
	    	for(i=id+1;i<=(n-1)/bound+1;i++)
	    	{
	    		f[i][vl[y]]++;
	    		g[i][f[i][vl[y]]]++;
	    	}
	    }
	    else
	    {
	    	x--;
    		if(x==0)
		    	printf("%d\n",vl[y]);
		    else
		    {
    			x%=2;
			    id=y/bound;
				if(x==1)
    			{
	    			ans=f[id][vl[y]];
 				    for(i=id*bound;i<=y;i++)
                    {
                    	if(vl[y]==vl[i])
                   		    ans++;
                   	}
			    }
			    else
			    {
    				for(i=id*bound;i<=y;i++)
    				{
				    	f[id][vl[i]]++;
				    	g[id][f[id][vl[i]]]++;
				    }
				    ans=g[id][f[id][vl[y]]];
				    for(i=y;i>=id*bound;i--)
			        {
        				g[id][f[id][vl[i]]]--;
        				f[id][vl[i]]--;
        			}
    			}
    			printf("%d\n",ans);
    		}
    	}
    }
	return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<vector>
#include<map>
using namespace std;
map<int,int>hs;
const int bound=1000;
int flag,x,y,f[111][201000],g[111][201000],list[210000],cnt_list,vl[210000],n,m,cnt;
int ans;
int get(int x)
{
	if(hs.count(x))
	   return hs[x];
    hs[x]=cnt++;
    list[cnt-1]=x;
    return cnt-1;
}
int main()
{
	int i,j,s,p,q,id;
	scanf("%d",&n);
	hs.clear();
	cnt=0;
	for(i=0;i<n;i++)
	{
	    scanf("%d",&vl[i]);
	    vl[i]=get(vl[i]);
	}
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	for(i=0;i<=(n-1)/bound;i++)
	{
		 for(j=0;j<=n;j++)
	        g[i+1][j]=g[i][j];
	    for(j=0;j<cnt;j++)
	        f[i+1][j]=f[i][j];
		for(j=i*bound;j<min(n,(i+1)*bound);j++)
		{
			f[i+1][vl[j]]++;
			g[i+1][f[i+1][vl[j]]]++;
		}
	}
	scanf("%d",&m);
    while(m--)
    {
    	scanf("%d%d%d",&flag,&x,&y);
    	y--;
		if(flag==1)
    	{
    		x=get(x);
	    	id=y/bound;
	    	for(i=id+1;i<=(n-1)/bound+1;i++)
	    	{
	    		g[i][f[i][vl[y]]]--;
	    		f[i][vl[y]]--;
	    	}
	    	vl[y]=x;
	    	for(i=id+1;i<=(n-1)/bound+1;i++)
	    	{
	    		f[i][vl[y]]++;
	    		g[i][f[i][vl[y]]]++;
	    	}
	    }
	    else
	    {
	    	x--;
    		if(x==0)
		    	printf("%d\n",vl[y]);
		    else
		    {
    			x%=2;
			    id=y/bound;
				if(x==1)
    			{
	    			ans=f[id][vl[y]];
 				    for(i=id*bound;i<=y;i++)
                    {
                    	if(vl[y]==vl[i])
                   		    ans++;
                   	}
			    }
			    else
			    {
    				for(i=id*bound;i<=y;i++)
    				{
				    	f[id][vl[i]]++;
				    	g[id][f[id][vl[i]]]++;
				    }
				    ans=g[id][f[id][vl[y]]];
				    for(i=y;i>=id*bound;i--)
			        {
        				g[id][f[id][vl[i]]]--;
        				f[id][vl[i]]--;
        			}
    			}
    			printf("%d\n",ans);
    		}
    	}
    }
	return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<vector>
#include<map>
using namespace std;
map<int,int>hs;
const int bound=1000;
int flag,x,y,f[111][201000],g[111][101000],list[210000],cnt_list,vl[210000],n,m,cnt;
int ans;
int get(int x)
{
	if(hs.count(x))
	   return hs[x];
    hs[x]=cnt++;
    list[cnt-1]=x;
    return cnt-1;
}
int main()
{
	int i,j,s,p,q,id;
	scanf("%d",&n);
	hs.clear();
	cnt=0;
	for(i=0;i<n;i++)
	{
	    scanf("%d",&vl[i]);
	    vl[i]=get(vl[i]);
	}
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	for(i=0;i<=(n-1)/bound;i++)
	{
		 for(j=0;j<=n;j++)
	        g[i+1][j]=g[i][j];
	    for(j=0;j<cnt;j++)
	        f[i+1][j]=f[i][j];
		for(j=i*bound;j<min(n,(i+1)*bound);j++)
		{
			f[i+1][vl[j]]++;
			g[i+1][f[i+1][vl[j]]]++;
		}
	}
	scanf("%d",&m);
    while(m--)
    {
    	scanf("%d%d%d",&flag,&x,&y);
    	y--;
		if(flag==1)
    	{
    		x=get(x);
	    	id=y/bound;
	    	for(i=id+1;i<=(n-1)/bound+1;i++)
	    	{
	    		g[i][f[i][vl[y]]]--;
	    		f[i][vl[y]]--;
	    	}
	    	vl[y]=x;
	    	for(i=id+1;i<=(n-1)/bound+1;i++)
	    	{
	    		f[i][vl[y]]++;
	    		g[i][f[i][vl[y]]]++;
	    	}
	    }
	    else
	    {
	    	x--;
    		if(x==0)
		    	printf("%d\n",list[vl[y]]);
		    else
		    {
    			x%=2;
			    id=y/bound;
				if(x==1)
    			{
	    			ans=f[id][vl[y]];
 				    for(i=id*bound;i<=y;i++)
                    {
                    	if(vl[y]==vl[i])
                   		    ans++;
                   	}
			    }
			    else
			    {
    				for(i=id*bound;i<=y;i++)
    				{
				    	f[id][vl[i]]++;
				    	g[id][f[id][vl[i]]]++;
				    }
				    ans=g[id][f[id][vl[y]]];
				    for(i=y;i>=id*bound;i--)
			        {
        				g[id][f[id][vl[i]]]--;
        				f[id][vl[i]]--;
        			}
    			}
    			printf("%d\n",ans);
    		}
    	}
    }
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>

using namespace std;

const int MAXN = 1e5 + 10;
const int SQ = 530;
const int ZZ = 330;

int n, arr[MAXN], f[2 * MAXN], cc, id[MAXN], prv[MAXN], cnt[ZZ][2*MAXN], fen[ZZ][MAXN], gg[2 * MAXN], tot[MAXN];
bool mark[2 * MAXN];
map<int, int>	mp;

int get(int w, int v){
	int ret = 0;
	for (; v; v -= v&-v)
		ret += fen[w][v];
	return ret;
}

void add(int w, int v, int val){
	tot[w] += val;
	for (; v < MAXN; v += v&-v)
		fen[w][v] += val;
}

void update(int w, int v, int val){
	if (cnt[w][v])
		add(w, cnt[w][v], -1);
	cnt[w][v] += val;
	if (cnt[w][v])
		add(w, cnt[w][v], 1);
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
		if (mp.count(arr[i]) == 0)	f[cc] = arr[i], mp[arr[i]] = cc++;
		arr[i] = mp[arr[i]];
	}

	for (int i = 0, w = 0; i < n; i += SQ, w++){
		id[i] = w;
		if (w)
			for (int j = 0; j < n && cnt[w-1][j]; j++)
				cnt[w][j] = cnt[w - 1][j];

		for (int j = i; j < n && j < i + SQ; j++){
			prv[j] = i;
			cnt[w][arr[j]]++;
		}

		for (int j = 0; j < n && cnt[w][j]; j++)
			add(w, cnt[w][j], 1);
	}

	int q;	scanf("%d", &q);
	while (q--){
		int type;	scanf("%d", &type), type--;
		if (type){
			int x, y;	scanf("%d %d", &x, &y), x--, y--;
			if (x == 0)	printf("%d\n", f[arr[y]]);
			else{
				x = (x + 1) % 2;

				int z = cnt[id[prv[y]]][arr[y]];
				for (int i = y+1; i < n && prv[i] == prv[y]; i++)
					if (arr[i] == arr[y])	z--;

				if (!x)
					printf("%d\n", z);
				else{
					int pr = prv[y];
					int ret = (id[pr]==0?0:tot[id[pr] - 1] - get(id[pr] - 1, z - 1));

					for (int i = pr; i <= y; i++){
						if (mark[arr[i]] == 0)
							gg[arr[i]] = (id[pr]?cnt[id[pr] - 1][arr[i]]:0);
						mark[arr[i]] = 1;
						if (gg[arr[i]] >= z)	continue;
						gg[arr[i]]++;
						if (gg[arr[i]] >= z)	ret++;
					}

					for (int i = pr; i <= y; i++)
						mark[arr[i]] = 0;
					printf("%d\n", ret);
				}
			}
		}
		else{
			int p, v;	scanf("%d %d", &v, &p), p--;
			if (mp.count(v) == 0)	f[cc] = v, mp[v] = cc++;
			v = mp[v];
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], -1);

			arr[p] = v;
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], 1);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>

using namespace std;

const int MAXN = 1e5 + 10;
const int SQ = 330;

int n, arr[MAXN], f[2 * MAXN], cc, id[MAXN], prv[MAXN], cnt[SQ][2*MAXN], fen[SQ][MAXN], gg[2 * MAXN];
bool mark[2 * MAXN];
map<int, int>	mp;

int get(int w, int v){
	int ret = 0;
	for (; v; v -= v&-v)
		ret += fen[w][v];
	return ret;
}

void add(int w, int v, int val){
	for (; v < MAXN; v += v&-v)
		fen[w][v] += val;
}

void update(int w, int v, int val){
	if (cnt[w][v])
		add(w, cnt[w][v], -1);
	cnt[w][v] += val;
	if (cnt[w][v])
		add(w, cnt[w][v], 1);
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
		if (mp.count(arr[i]) == 0)	f[cc] = arr[i], mp[arr[i]] = cc++;
		arr[i] = mp[arr[i]];
	}

	for (int i = 0, w = 0; i < n; i += SQ, w++){
		id[i] = w;
		if (w)
			for (int j = 0; j < n && cnt[w-1][j]; j++)
				cnt[w][j] = cnt[w - 1][j];

		for (int j = i; j < n && j < i + SQ; j++){
			prv[j] = i;
			cnt[w][arr[j]]++;
		}

		for (int j = 0; j < n && cnt[w][j]; j++)
			add(w, cnt[w][j], 1);
	}

	int q;	scanf("%d", &q);
	while (q--){
		int type;	scanf("%d", &type), type--;
		if (type){
			int x, y;	scanf("%d %d", &x, &y), x--, y--;
			if (x == 0)	printf("%d\n", f[arr[y]]);
			else{
				x = (x + 1) % 2;

				int z = cnt[id[prv[y]]][arr[y]];
				for (int i = y+1; i < n && prv[i] == prv[y]; i++)
					if (arr[i] == arr[y])	z--;

				if (!x)
					printf("%d\n", z);
				else{
					int pr = prv[y];
					int ret = (id[pr]==0?0:get(id[pr] - 1, n) - get(id[pr] - 1, z - 1));

					for (int i = pr; i <= y; i++){
						if (mark[arr[i]] == 0)
							gg[arr[i]] = (id[pr]?cnt[id[pr] - 1][arr[i]]:0);
						mark[arr[i]] = 1;
						if (gg[arr[i]] >= z)	continue;
						gg[arr[i]]++;
						if (gg[arr[i]] >= z)	ret++;
					}

					for (int i = pr; i <= y; i++)
						mark[arr[i]] = 0;
					printf("%d\n", ret);
				}
			}
		}
		else{
			int p, v;	scanf("%d %d", &v, &p), p--;
			if (mp.count(v) == 0)	f[cc] = v, mp[v] = cc++;
			v = mp[v];
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], -1);

			arr[p] = v;
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], 1);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>

using namespace std;

const int MAXN = 1e5 + 10;
const int SQ = 330;

int n, arr[MAXN], f[2 * MAXN], cc, id[MAXN], prv[MAXN], cnt[SQ][2*MAXN], fen[SQ][MAXN], gg[2 * MAXN], tot[MAXN];
bool mark[2 * MAXN];
map<int, int>	mp;

int get(int w, int v){
	int ret = 0;
	for (; v; v -= v&-v)
		ret += fen[w][v];
	return ret;
}

void add(int w, int v, int val){
	tot[w] += val;
	for (; v < MAXN; v += v&-v)
		fen[w][v] += val;
}

void update(int w, int v, int val){
	if (cnt[w][v])
		add(w, cnt[w][v], -1);
	cnt[w][v] += val;
	if (cnt[w][v])
		add(w, cnt[w][v], 1);
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
		if (mp.count(arr[i]) == 0)	f[cc] = arr[i], mp[arr[i]] = cc++;
		arr[i] = mp[arr[i]];
	}

	for (int i = 0, w = 0; i < n; i += SQ, w++){
		id[i] = w;
		if (w)
			for (int j = 0; j < n && cnt[w-1][j]; j++)
				cnt[w][j] = cnt[w - 1][j];

		for (int j = i; j < n && j < i + SQ; j++){
			prv[j] = i;
			cnt[w][arr[j]]++;
		}

		for (int j = 0; j < n && cnt[w][j]; j++)
			add(w, cnt[w][j], 1);
	}
	if (n > 1e4)	return 0;

	int q;	scanf("%d", &q);
	while (q--){
		int type;	scanf("%d", &type), type--;
		if (type){
			int x, y;	scanf("%d %d", &x, &y), x--, y--;
			if (x == 0)	printf("%d\n", f[arr[y]]);
			else{
				x = (x + 1) % 2;

				int z = cnt[id[prv[y]]][arr[y]];
				for (int i = y+1; i < n && prv[i] == prv[y]; i++)
					if (arr[i] == arr[y])	z--;

				if (!x)
					printf("%d\n", z);
				else{
					int pr = prv[y];
					int ret = (id[pr]==0?0:tot[id[pr] - 1] - get(id[pr] - 1, z - 1));

					for (int i = pr; i <= y; i++){
						if (mark[arr[i]] == 0)
							gg[arr[i]] = (id[pr]?cnt[id[pr] - 1][arr[i]]:0);
						mark[arr[i]] = 1;
						if (gg[arr[i]] >= z)	continue;
						gg[arr[i]]++;
						if (gg[arr[i]] >= z)	ret++;
					}

					for (int i = pr; i <= y; i++)
						mark[arr[i]] = 0;
					printf("%d\n", ret);
				}
			}
		}
		else{
			int p, v;	scanf("%d %d", &v, &p), p--;
			if (mp.count(v) == 0)	f[cc] = v, mp[v] = cc++;
			v = mp[v];
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], -1);

			arr[p] = v;
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], 1);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>

using namespace std;

const int MAXN = 1e5 + 10;
const int SQ = 410;

int n, arr[MAXN], f[2 * MAXN], cc, id[MAXN], prv[MAXN], cnt[SQ][2*MAXN], fen[SQ][MAXN], gg[2 * MAXN], tot[MAXN];
bool mark[2 * MAXN];
map<int, int>	mp;

int get(int w, int v){
	int ret = 0;
	for (; v; v -= v&-v)
		ret += fen[w][v];
	return ret;
}

void add(int w, int v, int val){
	tot[w] += val;
	for (; v < MAXN; v += v&-v)
		fen[w][v] += val;
}

void update(int w, int v, int val){
	if (cnt[w][v])
		add(w, cnt[w][v], -1);
	cnt[w][v] += val;
	if (cnt[w][v])
		add(w, cnt[w][v], 1);
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
		if (mp.count(arr[i]) == 0)	f[cc] = arr[i], mp[arr[i]] = cc++;
		arr[i] = mp[arr[i]];
	}

	for (int i = 0, w = 0; i < n; i += SQ, w++){
		id[i] = w;
		if (w)
			for (int j = 0; j < n && cnt[w-1][j]; j++)
				cnt[w][j] = cnt[w - 1][j];

		for (int j = i; j < n && j < i + SQ; j++){
			prv[j] = i;
			cnt[w][arr[j]]++;
		}

		for (int j = 0; j < n && cnt[w][j]; j++)
			add(w, cnt[w][j], 1);
	}

	int q;	scanf("%d", &q);
	while (q--){
		int type;	scanf("%d", &type), type--;
		if (type){
			int x, y;	scanf("%d %d", &x, &y), x--, y--;
			if (x == 0)	printf("%d\n", f[arr[y]]);
			else{
				x = (x + 1) % 2;

				int z = cnt[id[prv[y]]][arr[y]];
				for (int i = y+1; i < n && prv[i] == prv[y]; i++)
					if (arr[i] == arr[y])	z--;

				if (!x)
					printf("%d\n", z);
				else{
					int pr = prv[y];
					int ret = (id[pr]==0?0:tot[id[pr] - 1] - get(id[pr] - 1, z - 1));

					for (int i = pr; i <= y; i++){
						if (mark[arr[i]] == 0)
							gg[arr[i]] = (id[pr]?cnt[id[pr] - 1][arr[i]]:0);
						mark[arr[i]] = 1;
						if (gg[arr[i]] >= z)	continue;
						gg[arr[i]]++;
						if (gg[arr[i]] >= z)	ret++;
					}

					for (int i = pr; i <= y; i++)
						mark[arr[i]] = 0;
					printf("%d\n", ret);
				}
			}
		}
		else{
			int p, v;	scanf("%d %d", &v, &p), p--;
			if (mp.count(v) == 0)	f[cc] = v, mp[v] = cc++;
			v = mp[v];
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], -1);

			arr[p] = v;
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], 1);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>

using namespace std;

const int MAXN = 1e5 + 10;
const int SQ = 425;

int n, arr[MAXN], f[2 * MAXN], cc, id[MAXN], prv[MAXN], cnt[SQ][2*MAXN], fen[SQ][MAXN], gg[2 * MAXN], tot[MAXN];
bool mark[2 * MAXN];
map<int, int>	mp;

int get(int w, int v){
	int ret = 0;
	for (; v; v -= v&-v)
		ret += fen[w][v];
	return ret;
}

void add(int w, int v, int val){
	tot[w] += val;
	for (; v < MAXN; v += v&-v)
		fen[w][v] += val;
}

void update(int w, int v, int val){
	if (cnt[w][v])
		add(w, cnt[w][v], -1);
	cnt[w][v] += val;
	if (cnt[w][v])
		add(w, cnt[w][v], 1);
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
		if (mp.count(arr[i]) == 0)	f[cc] = arr[i], mp[arr[i]] = cc++;
		arr[i] = mp[arr[i]];
	}

	for (int i = 0, w = 0; i < n; i += SQ, w++){
		id[i] = w;
		if (w)
			for (int j = 0; j < n && cnt[w-1][j]; j++)
				cnt[w][j] = cnt[w - 1][j];

		for (int j = i; j < n && j < i + SQ; j++){
			prv[j] = i;
			cnt[w][arr[j]]++;
		}

		for (int j = 0; j < n && cnt[w][j]; j++)
			add(w, cnt[w][j], 1);
	}

	int q;	scanf("%d", &q);
	while (q--){
		int type;	scanf("%d", &type), type--;
		if (type){
			int x, y;	scanf("%d %d", &x, &y), x--, y--;
			if (x == 0)	printf("%d\n", f[arr[y]]);
			else{
				x = (x + 1) % 2;

				int z = cnt[id[prv[y]]][arr[y]];
				for (int i = y+1; i < n && prv[i] == prv[y]; i++)
					if (arr[i] == arr[y])	z--;

				if (!x)
					printf("%d\n", z);
				else{
					int pr = prv[y];
					int ret = (id[pr]==0?0:tot[id[pr] - 1] - get(id[pr] - 1, z - 1));

					for (int i = pr; i <= y; i++){
						if (mark[arr[i]] == 0)
							gg[arr[i]] = (id[pr]?cnt[id[pr] - 1][arr[i]]:0);
						mark[arr[i]] = 1;
						if (gg[arr[i]] >= z)	continue;
						gg[arr[i]]++;
						if (gg[arr[i]] >= z)	ret++;
					}

					for (int i = pr; i <= y; i++)
						mark[arr[i]] = 0;
					printf("%d\n", ret);
				}
			}
		}
		else{
			int p, v;	scanf("%d %d", &v, &p), p--;
			if (mp.count(v) == 0)	f[cc] = v, mp[v] = cc++;
			v = mp[v];
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], -1);

			arr[p] = v;
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], 1);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>

using namespace std;

const int MAXN = 1e5 + 10;
const int SQ = 450;

int n, arr[MAXN], f[2 * MAXN], cc, id[MAXN], prv[MAXN], cnt[SQ][2*MAXN], fen[SQ][MAXN], gg[2 * MAXN], tot[MAXN];
bool mark[2 * MAXN];
map<int, int>	mp;

int get(int w, int v){
	int ret = 0;
	for (; v; v -= v&-v)
		ret += fen[w][v];
	return ret;
}

void add(int w, int v, int val){
	tot[w] += val;
	for (; v < MAXN; v += v&-v)
		fen[w][v] += val;
}

void update(int w, int v, int val){
	if (cnt[w][v])
		add(w, cnt[w][v], -1);
	cnt[w][v] += val;
	if (cnt[w][v])
		add(w, cnt[w][v], 1);
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
		if (mp.count(arr[i]) == 0)	f[cc] = arr[i], mp[arr[i]] = cc++;
		arr[i] = mp[arr[i]];
	}

	for (int i = 0, w = 0; i < n; i += SQ, w++){
		id[i] = w;
		if (w)
			for (int j = 0; j < n && cnt[w-1][j]; j++)
				cnt[w][j] = cnt[w - 1][j];

		for (int j = i; j < n && j < i + SQ; j++){
			prv[j] = i;
			cnt[w][arr[j]]++;
		}

		for (int j = 0; j < n && cnt[w][j]; j++)
			add(w, cnt[w][j], 1);
	}

	int q;	scanf("%d", &q);
	while (q--){
		int type;	scanf("%d", &type), type--;
		if (type){
			int x, y;	scanf("%d %d", &x, &y), x--, y--;
			if (x == 0)	printf("%d\n", f[arr[y]]);
			else{
				x = (x + 1) % 2;

				int z = cnt[id[prv[y]]][arr[y]];
				for (int i = y+1; i < n && prv[i] == prv[y]; i++)
					if (arr[i] == arr[y])	z--;

				if (!x)
					printf("%d\n", z);
				else{
					int pr = prv[y];
					int ret = (id[pr]==0?0:tot[id[pr] - 1] - get(id[pr] - 1, z - 1));

					for (int i = pr; i <= y; i++){
						if (mark[arr[i]] == 0)
							gg[arr[i]] = (id[pr]?cnt[id[pr] - 1][arr[i]]:0);
						mark[arr[i]] = 1;
						if (gg[arr[i]] >= z)	continue;
						gg[arr[i]]++;
						if (gg[arr[i]] >= z)	ret++;
					}

					for (int i = pr; i <= y; i++)
						mark[arr[i]] = 0;
					printf("%d\n", ret);
				}
			}
		}
		else{
			int p, v;	scanf("%d %d", &v, &p), p--;
			if (mp.count(v) == 0)	f[cc] = v, mp[v] = cc++;
			v = mp[v];
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], -1);

			arr[p] = v;
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], 1);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>

using namespace std;

const int MAXN = 1e5 + 10;
const int SQ = 330;

int n, arr[MAXN], f[2 * MAXN], cc, id[MAXN], prv[MAXN], cnt[SQ][2*MAXN], fen[SQ][MAXN], gg[2 * MAXN];
bool mark[2 * MAXN];
map<int, int>	mp;

int get(int w, int v){
	int ret = 0;
	for (; v; v -= v&-v)
		ret += fen[w][v];
	return ret;
}

void add(int w, int v, int val){
	for (; v < MAXN; v += v&-v)
		fen[w][v] += val;
}

void update(int w, int v, int val){
	add(w, cnt[w][v], -1);
	cnt[w][v] += val;
	add(w, cnt[w][v], 1);
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
		if (mp.count(arr[i]) == 0)	f[cc] = arr[i], mp[arr[i]] = cc++;
		arr[i] = mp[arr[i]];
	}

	for (int i = 0, w = 0; i < n; i += SQ, w++){
		id[i] = w;
		if (w)
			for (int j = 0; j < n && cnt[j]; j++)
				cnt[w][j] = cnt[w - 1][j];

		for (int j = i; j < n && j < i + SQ; j++){
			prv[j] = i;
			cnt[w][arr[j]]++;
		}

		for (int j = 0; j < n && cnt[w][j]; j++)
			add(w, cnt[w][j], 1);
	}

	int q;	scanf("%d", &q);
	while (q--){
		int type;	scanf("%d", &type), type--;
		if (type){
			int x, y;	scanf("%d %d", &x, &y), x--, y--;
			if (x == 0)	printf("%d\n", f[arr[y]]);
			else{
				x = (x + 1) % 2;

				int z = cnt[id[prv[y]]][arr[y]];
				for (int i = y+1; i < n && prv[i] == prv[y]; i++)
					if (arr[i] == arr[y])	z--;

				if (!x)
					printf("%d\n", z);
				else{
					int pr = prv[y];
					int ret = (id[pr]==0?0:get(id[pr] - 1, n) - get(id[pr] - 1, z - 1));

					for (int i = pr; i <= y; i++){
						if (mark[arr[i]] == 0)
							gg[arr[i]] = (id[pr]?cnt[id[pr] - 1][arr[i]]:0);
						mark[arr[i]] = 1;
						if (gg[arr[i]] >= z)	continue;
						gg[arr[i]]++;
						if (gg[arr[i]] >= z)	ret++;
					}

					for (int i = pr; i <= y; i++)
						mark[arr[i]] = 0;
					printf("%d\n", ret);
				}
			}
		}
		else{
			int p, v;	scanf("%d %d", &v, &p), p--;
			if (mp.count(v) == 0)	f[cc] = v, mp[v] = cc++;
			v = mp[v];
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], -1);

			arr[p] = v;
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], 1);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>

using namespace std;

const int MAXN = 1e5 + 10;
const int SQ = 470;
const int ZZ = 330;

int n, arr[MAXN], f[2 * MAXN], cc, id[MAXN], prv[MAXN], cnt[ZZ][2*MAXN], fen[ZZ][MAXN], gg[2 * MAXN], tot[MAXN];
bool mark[2 * MAXN];
map<int, int>	mp;

int get(int w, int v){
	int ret = 0;
	for (; v; v -= v&-v)
		ret += fen[w][v];
	return ret;
}

void add(int w, int v, int val){
	tot[w] += val;
	for (; v < MAXN; v += v&-v)
		fen[w][v] += val;
}

void update(int w, int v, int val){
	if (cnt[w][v])
		add(w, cnt[w][v], -1);
	cnt[w][v] += val;
	if (cnt[w][v])
		add(w, cnt[w][v], 1);
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
		if (mp.count(arr[i]) == 0)	f[cc] = arr[i], mp[arr[i]] = cc++;
		arr[i] = mp[arr[i]];
	}

	for (int i = 0, w = 0; i < n; i += SQ, w++){
		id[i] = w;
		if (w)
			for (int j = 0; j < n && cnt[w-1][j]; j++)
				cnt[w][j] = cnt[w - 1][j];

		for (int j = i; j < n && j < i + SQ; j++){
			prv[j] = i;
			cnt[w][arr[j]]++;
		}

		for (int j = 0; j < n && cnt[w][j]; j++)
			add(w, cnt[w][j], 1);
	}

	int q;	scanf("%d", &q);
	while (q--){
		int type;	scanf("%d", &type), type--;
		if (type){
			int x, y;	scanf("%d %d", &x, &y), x--, y--;
			if (x == 0)	printf("%d\n", f[arr[y]]);
			else{
				x = (x + 1) % 2;

				int z = cnt[id[prv[y]]][arr[y]];
				for (int i = y+1; i < n && prv[i] == prv[y]; i++)
					if (arr[i] == arr[y])	z--;

				if (!x)
					printf("%d\n", z);
				else{
					int pr = prv[y];
					int ret = (id[pr]==0?0:tot[id[pr] - 1] - get(id[pr] - 1, z - 1));

					for (int i = pr; i <= y; i++){
						if (mark[arr[i]] == 0)
							gg[arr[i]] = (id[pr]?cnt[id[pr] - 1][arr[i]]:0);
						mark[arr[i]] = 1;
						if (gg[arr[i]] >= z)	continue;
						gg[arr[i]]++;
						if (gg[arr[i]] >= z)	ret++;
					}

					for (int i = pr; i <= y; i++)
						mark[arr[i]] = 0;
					printf("%d\n", ret);
				}
			}
		}
		else{
			int p, v;	scanf("%d %d", &v, &p), p--;
			if (mp.count(v) == 0)	f[cc] = v, mp[v] = cc++;
			v = mp[v];
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], -1);

			arr[p] = v;
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], 1);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>

using namespace std;

const int MAXN = 1e5 + 10;
const int SQ = 330;

int n, arr[MAXN], f[2 * MAXN], cc, id[MAXN], prv[MAXN], cnt[SQ][2*MAXN], fen[SQ][MAXN], gg[2 * MAXN], tot[MAXN];
bool mark[2 * MAXN];
map<int, int>	mp;

int get(int w, int v){
	int ret = 0;
	for (; v; v -= v&-v)
		ret += fen[w][v];
	return ret;
}

void add(int w, int v, int val){
	tot[w] += val;
	for (; v < MAXN; v += v&-v)
		fen[w][v] += val;
}

void update(int w, int v, int val){
	if (cnt[w][v])
		add(w, cnt[w][v], -1);
	cnt[w][v] += val;
	if (cnt[w][v])
		add(w, cnt[w][v], 1);
}

int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
		if (mp.count(arr[i]) == 0)	f[cc] = arr[i], mp[arr[i]] = cc++;
		arr[i] = mp[arr[i]];
	}

	for (int i = 0, w = 0; i < n; i += SQ, w++){
		id[i] = w;
		if (w)
			for (int j = 0; j < n && cnt[w-1][j]; j++)
				cnt[w][j] = cnt[w - 1][j];

		for (int j = i; j < n && j < i + SQ; j++){
			prv[j] = i;
			cnt[w][arr[j]]++;
		}

		for (int j = 0; j < n && cnt[w][j]; j++)
			add(w, cnt[w][j], 1);
	}

	int q;	scanf("%d", &q);
	while (q--){
		int type;	scanf("%d", &type), type--;
		if (type){
			int x, y;	scanf("%d %d", &x, &y), x--, y--;
			if (x == 0)	printf("%d\n", f[arr[y]]);
			else{
				x = (x + 1) % 2;

				int z = cnt[id[prv[y]]][arr[y]];
				for (int i = y+1; i < n && prv[i] == prv[y]; i++)
					if (arr[i] == arr[y])	z--;

				if (!x)
					printf("%d\n", z);
				else{
					int pr = prv[y];
					int ret = (id[pr]==0?0:tot[id[pr] - 1] - get(id[pr] - 1, z - 1));

					for (int i = pr; i <= y; i++){
						if (mark[arr[i]] == 0)
							gg[arr[i]] = (id[pr]?cnt[id[pr] - 1][arr[i]]:0);
						mark[arr[i]] = 1;
						if (gg[arr[i]] >= z)	continue;
						gg[arr[i]]++;
						if (gg[arr[i]] >= z)	ret++;
					}

					for (int i = pr; i <= y; i++)
						mark[arr[i]] = 0;
					printf("%d\n", ret);
				}
			}
		}
		else{
			int p, v;	scanf("%d %d", &v, &p), p--;
			if (mp.count(v) == 0)	f[cc] = v, mp[v] = cc++;
			v = mp[v];
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], -1);

			arr[p] = v;
			for (int i = prv[p]; i < n; i += SQ)
				update(id[i], arr[p], 1);
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 200005
using namespace std;
int xx,yy,B;
int n,m,k,km,cc,lar,a[N],d[N],l[N],r[N],bel[N];
int x,y,opt,w;
int f[105][200005],g[105][200005];
struct Ques
{
    int opt,x,y;
}q[N];
inline int read()
{
    int s=0;
    char c=getchar();
    while (!isdigit(c))
        c=getchar();
    while (isdigit(c))
    {
        s=s*10+c-'0';
        c=getchar();
    }
    return s;
}
int main()
{
    m=read();
    for (int i=1;i<=m;i++)
        a[i]=read(),d[i]=a[i];
    k=read();
    km=m;
    for (int i=1;i<=k;i++)
    {
        q[i].opt=read(),q[i].x=read(),q[i].y=read();
        if (q[i].opt==1)
        {
            swap(q[i].x,q[i].y);
            d[++km]=q[i].y;
        }
    }
    sort(d+1,d+km+1);
    cc=unique(d+1,d+km+1)-d-1;
    for (int i=1;i<=m;i++)
        a[i]=lower_bound(d+1,d+cc+1,a[i])-d;
    for (int i=1;i<=k;i++)
        if (q[i].opt==1)
            q[i].y=lower_bound(d+1,d+cc+1,q[i].y)-d;
    B=(int)sqrt(m)+1;
    if (m/B+1>100)
        B=1000;
    for (int i=1;i<=m;i++)
    {
        bel[i]=i/B+1;
        if (!l[bel[i]])
            l[bel[i]]=i;
        r[bel[i]]=i;
    }
    for (int i=1;i<=bel[m];i++)
    {
        lar=0;
        for (int j=0;j<=cc;j++)
            f[i][j]=f[i-1][j],lar=max(lar,f[i-1][j]);
        for (int j=0;j<=lar;j++)
            g[i][j]=g[i-1][j];
        for (int j=l[i];j<=r[i];j++)
        {
            f[i][a[j]]++;
            g[i][f[i][a[j]]]++;
        }
    }
    for (int i=1;i<=k;i++)
    {
        x=q[i].x,y=q[i].y,opt=q[i].opt;
        if (opt==2)
        {
            if (x==1)
                printf("%d\n",d[a[y]]); else
            if (x%2==0)
            {
                w=bel[y]-1;
                for (int j=l[w+1];j<=y;j++)
                    f[w][a[j]]++;
                printf("%d\n",f[w][a[y]]);
                for (int j=l[w+1];j<=y;j++)
                    f[w][a[j]]--;
            } else
            {
                w=bel[y]-1;
                for (int j=l[w+1];j<=y;j++)
                    f[w][a[j]]++,g[w][f[w][a[j]]]++;
                printf("%d\n",g[w][f[w][a[y]]]);
                for (int j=l[w+1];j<=y;j++)
                    g[w][f[w][a[j]]]--,f[w][a[j]]--;
            }
        } else
        {
            w=bel[x];
            for (int j=w;j<=bel[m];j++)
            {
                g[j][f[j][a[x]]]--;
                f[j][a[x]]--;
            }
            a[x]=y;
            for (int j=w;j<=bel[m];j++)
            {
                f[j][a[x]]++;
                g[j][f[j][a[x]]]++;
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 200005
#define M 5000005
using namespace std;
int xx,yy,B;
int n,m,k,km,cc,lar,a[N],d[N],l[N],r[N],bel[N];
int x,y,opt,w;
int f[105][200005],g[105][200005];
struct Ques
{
    int opt,x,y;
}q[N];
inline int read()
{
    int s=0;
    char c=getchar();
    while (!isdigit(c))
        c=getchar();
    while (isdigit(c))
    {
        s=s*10+c-'0';
        c=getchar();
    }
    return s;
}
int main()
{
    m=read();
    for (int i=1;i<=m;i++)
        a[i]=read(),d[i]=a[i];
    k=read();
    km=m;
    for (int i=1;i<=k;i++)
    {
        q[i].opt=read(),q[i].x=read(),q[i].y=read();
        if (q[i].opt==1)
            d[++km]=q[i].y;
    }
    sort(d+1,d+km+1);
    cc=unique(d+1,d+km+1)-d-1;
    for (int i=1;i<=m;i++)
        a[i]=lower_bound(d+1,d+cc+1,a[i])-d;
    for (int i=1;i<=k;i++)
        if (q[i].opt==1)
            q[i].y=lower_bound(d+1,d+cc+1,q[i].y)-d;
    B=(int)sqrt(m)+1;
    if (m/B+1>100)
        B=1000;
    for (int i=1;i<=m;i++)
    {
        bel[i]=i/B+1;
        if (!l[bel[i]])
            l[bel[i]]=i;
        r[bel[i]]=i;
    }
    for (int i=1;i<=bel[m];i++)
    {
        lar=0;
        for (int j=0;j<=cc;j++)
            f[i][j]=f[i-1][j],lar=max(lar,f[i-1][j]);
        for (int j=0;j<=lar;j++)
            g[i][j]=g[i-1][j];
        for (int j=l[i];j<=r[i];j++)
        {
            f[i][a[j]]++;
            g[i][f[i][a[j]]]++;
        }
    }
    for (int i=1;i<=k;i++)
    {
        x=q[i].x,y=q[i].y,opt=q[i].opt;
        if (opt==2)
        {
            if (x==1)
                printf("%d\n",d[a[y]]); else
            if (x%2==0)
            {
                w=bel[y]-1;
                for (int j=l[w+1];j<=y;j++)
                    f[w][a[j]]++;
                printf("%d\n",f[w][a[y]]);
                for (int j=l[w+1];j<=y;j++)
                    f[w][a[j]]--;
            } else
            {
                w=bel[y]-1;
                for (int j=l[w+1];j<=y;j++)
                    f[w][a[j]]++,g[w][f[w][a[j]]]++;
                printf("%d\n",g[w][f[w][a[y]]]);
                for (int j=l[w+1];j<=y;j++)
                    g[w][f[w][a[j]]]--,f[w][a[j]]--;
            }
        } else
        {
            swap(x,y);
            w=bel[x];
            for (int j=w;j<=bel[m];j++)
            {
                g[j][f[j][a[x]]]--;
                f[j][a[x]]--;
            }
            a[x]=y;
            for (int j=w;j<=bel[m];j++)
            {
                f[j][a[x]]++;
                g[j][f[j][a[x]]]++;
            }
        }
    }
    return 0;
}


#include<cstdio>
#include<cmath>
#include<map>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int mn=101000,mm=60;
int F[mm][mn*2],G[mm][mn],A[mn],V[mn*2];
int n,m,len,N,tt;
map<int,int>Q;
int get(int v)
{
    if(!Q.count(v))Q[v]=++tt,V[tt]=v;
    return Q[v];
}
int main()
{

    scanf("%d",&n);
    len=min((int)(6*sqrt(n)),n),N=(n-1)/len+1;
    fo(i,0,n-1)
        scanf("%d",A+i),A[i]=get(A[i]);
    fo(i,1,N)
    {
        fo(j,1,tt)F[i][j]=F[i-1][j];
        fo(j,1,n)G[i][j]=G[i-1][j];
        fo(j,(i-1)*len,min(i*len-1,n-1))
            ++G[i][++F[i][A[j]]];
    }
    scanf("%d",&m);
    for(;m--;)
    {
        int r,x,y,b;
        scanf("%d%d%d",&r,&x,&y);
        if(r==1)
        {
            --y,b=y/len;
            fo(i,b+1,N)
                --G[i][F[i][A[y]]--];
            A[y]=get(x);
            fo(i,b+1,N)
                ++G[i][++F[i][A[y]]];
            continue;
        }
        --y,b=y/len;
        if(x==1)
        {
            printf("%d\n",V[A[y]]);
            continue;
        }
        x=(x-2)%2+2;
        int v0=F[b][A[y]];
        fo(i,b*len,y)if(A[i]==A[y])++v0;
        if(x==2)
        {
            printf("%d\n",v0);
            continue;
        }
        int v1=G[b][v0];
        fo(i,b*len,y)if(++F[b][A[i]]==v0)++v1;
        printf("%d\n",v1);
        fo(i,b*len,y)--F[b][A[i]];
    }
    return 0;
}


#include<cstdio>

#include<vector>

#include<cmath>

#include<map>

#define N 100010

#define S 170

using namespace std;

int a[N];

int num[2*N],cnt=0;

int odd[S][2*N]={0},evn[S][2*N]={0};

int main(){

    int i,j,n,m,sq,t,v,x,y,os;

    map<int,int> id;

    scanf("%d",&n);

    sq=(int)ceil(sqrt(n)*2);

    for(i=0;i<n;i++){

        scanf("%d",&x);

        if(id.find(x)!=id.end()){

            a[i]=id[x];

        }

        else{

            a[i]=cnt;

            num[cnt]=x;

            id.insert(make_pair(x,cnt++));

        }

        for(j=i/sq+1;j<=n/sq;j++){

            x=++evn[j][a[i]];

            odd[j][x]++;

        }

    }

    scanf("%d",&m);

    while(m--){

        scanf("%d%d%d",&t,&x,&y);

        if(t==1){

            if(id.find(x)!=id.end()){

                v=id[x];

            }

            else{

                v=cnt;

                num[cnt]=x;

                id.insert(make_pair(x,cnt++));

            }

            for(j=(y-1)/sq+1;j<=n/sq;j++){

                odd[j][evn[j][a[y-1]]--]--;

                odd[j][++evn[j][v]]++;

            }

            a[y-1]=v;

        }

        else{

            if(x==1) printf("%d\n",num[a[y-1]]);

            else if(x&1){

                os=sq*(y/sq);

                for(j=0;j<y%sq;j++){

                    odd[y/sq][++evn[y/sq][a[os+j]]]++;

                }

                printf("%d\n",odd[y/sq][evn[y/sq][a[y-1]]]);

                for(j=y%sq-1;j>=0;j--){

                    odd[y/sq][evn[y/sq][a[os+j]]--]--;

                }

            }

            else{

                os=sq*(y/sq);

                for(j=0;j<y%sq;j++){

                    evn[y/sq][a[os+j]]++;

                }

                printf("%d\n",evn[y/sq][a[y-1]]);

                for(j=y%sq-1;j>=0;j--){

                    evn[y/sq][a[os+j]]--;

                }

            }

        }

    }

    return 0;

}


#include <bits/stdc++.h>
#define il inline
#define pb push_back
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 200005
#define B 300
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(register int i = x; i <= (y); ++i)
#define repd(i, x, y) for(register int i = x; i >= (y); --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	read(x), read(args...);
}
int n, m, a[MAXN], tot, blo, bel[MAXN], L[MAXN], R[MAXN];
int f[B][MAXN], g[B][MAXN];
map <int, int> mp, val;
int num(int x) {
	if(!mp.count(x)) val[mp[x] = ++tot] = x;
	return mp[x];
}
int main() {
	read(n); blo = 700;
	rep(i, 1, n) bel[i] = (i-1) / blo + 1, R[bel[i]] = i;
	repd(i, n, 1) L[bel[i]] = i;
	rep(i, 1, n) read(a[i]), a[i] = num(a[i]);
	rep(i, 1, bel[n]) {
		rep(j, 0, tot) f[i][j] = f[i-1][j], g[i][j] = g[i-1][j];
		rep(j, L[i], R[i]) f[i][a[j]]++, g[i][f[i][a[j]]]++;
	}
	read(m);
	int op, x, y;
	while(m--) {
		read(op, x, y);
		if(op == 1) {
			swap(x, y);
			rep(i, bel[x], bel[n]) g[i][f[i][a[x]]]--, f[i][a[x]]--;
			a[x] = num(y);
			rep(i, bel[x], bel[n]) f[i][a[x]]++, g[i][f[i][a[x]]]++;
		} else {
			if(x == 1) printf("%d\n", val[a[y]]);
			else {
				int *F = f[bel[y]-1], *G = g[bel[y]-1];
				rep(j, L[bel[y]], y) F[a[j]]++, G[F[a[j]]]++;
				printf("%d\n", (x & 1) ? G[F[a[y]]] : F[a[y]]);
				repd(j, y, L[bel[y]]) G[F[a[j]]]--, F[a[j]]--;
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define pb push_back
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 200005
#define B 205
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(register int i = x; i <= (y); ++i)
#define repd(i, x, y) for(register int i = x; i >= (y); --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	read(x), read(args...);
}
int n, m, a[MAXN], tot, blo, bel[MAXN], L[MAXN], R[MAXN];
int f[B][MAXN], g[B][MAXN];
map <int, int> mp, val;
int num(int x) {
	if(!mp.count(x)) val[mp[x] = ++tot] = x;
	return mp[x];
}
int main() {
	read(n); blo = 500;
	rep(i, 1, n) bel[i] = (i-1) / blo + 1, R[bel[i]] = i;
	repd(i, n, 1) L[bel[i]] = i;
	rep(i, 1, n) read(a[i]), a[i] = num(a[i]);
	rep(i, 1, bel[n]) {
		rep(j, 1, tot) f[i][j] = f[i-1][j], g[i][j] = g[i-1][j];
		rep(j, L[i], R[i]) f[i][a[j]]++, g[i][f[i][a[j]]]++;
	}
	read(m);
	int op, x, y;
	while(m--) {
		read(op, x, y);
		if(op == 1) {
			swap(x, y);
			rep(i, bel[x], bel[n]) g[i][f[i][a[x]]]--, f[i][a[x]]--;
			a[x] = num(y);
			rep(i, bel[x], bel[n]) f[i][a[x]]++, g[i][f[i][a[x]]]++;
		} else {
			if(x == 1) printf("%d\n", val[a[y]]);
			else {
				int *F = f[bel[y]-1], *G = g[bel[y]-1];
				rep(j, L[bel[y]], y) F[a[j]]++, G[F[a[j]]]++;
				printf("%d\n", (x & 1) ? G[F[a[y]]] : F[a[y]]);
				rep(j, L[bel[y]], y) G[F[a[j]]]--, F[a[j]]--;
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define pb push_back
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 100005
#define B 205
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(register int i = x; i <= (y); ++i)
#define repd(i, x, y) for(register int i = x; i >= (y); --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	read(x), read(args...);
}
int n, m, a[MAXN], tot, blo, bel[MAXN], L[MAXN], R[MAXN];
int f[B][MAXN], g[B][MAXN];
map <int, int> mp, val;
int num(int x) {
	if(!mp.count(x)) val[mp[x] = ++tot] = x;
	return mp[x];
}
int main() {
	read(n); blo = 500;
	rep(i, 1, n) bel[i] = (i-1) / blo + 1, R[bel[i]] = i;
	repd(i, n, 1) L[bel[i]] = i;
	rep(i, 1, n) read(a[i]), a[i] = num(a[i]);
	rep(i, 1, bel[n]) {
		rep(j, 1, tot) f[i][j] = f[i-1][j], g[i][j] = g[i-1][j];
		rep(j, L[i], R[i]) f[i][a[j]]++, g[i][f[i][a[j]]]++;
	}
	read(m);
	int op, x, y;
	while(m--) {
		read(op, x, y);
		if(op == 1) {
			swap(x, y);
			rep(i, bel[x], bel[n]) g[i][f[i][a[x]]]--, f[i][a[x]]--;
			a[x] = num(y);
			rep(i, bel[x], bel[n]) f[i][a[x]]++, g[i][f[i][a[x]]]++;
		} else {
			if(x == 1) printf("%d\n", val[a[x]]);
			else {
				int *F = f[bel[y]-1], *G = g[bel[y]-1];
				rep(j, L[bel[y]], y) F[a[j]]++, G[F[a[j]]]++;
				printf("%d\n", (x & 1) ? G[F[a[y]]] : F[a[y]]);
				rep(j, L[bel[y]], y) G[F[a[j]]]--, F[a[j]]--;
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define pb push_back
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 200005
#define B 205
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(register int i = x; i <= (y); ++i)
#define repd(i, x, y) for(register int i = x; i >= (y); --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	read(x), read(args...);
}
int n, m, a[MAXN], tot, blo, bel[MAXN], L[MAXN], R[MAXN];
int f[B][MAXN], g[B][MAXN];
map <int, int> mp, val;
int num(int x) {
	if(!mp.count(x)) val[mp[x] = ++tot] = x;
	return mp[x];
}
int main() {
	read(n); blo = 500;
	rep(i, 1, n) bel[i] = (i-1) / blo + 1, R[bel[i]] = i;
	repd(i, n, 1) L[bel[i]] = i;
	rep(i, 1, n) read(a[i]), a[i] = num(a[i]);
	rep(i, 1, bel[n]) {
		rep(j, 1, tot) f[i][j] = f[i-1][j], g[i][j] = g[i-1][j];
		rep(j, L[i], R[i]) f[i][a[j]]++, g[i][f[i][a[j]]]++;
	}
	read(m);
	int op, x, y;
	while(m--) {
		read(op, x, y);
		if(op == 1) {
			swap(x, y);
			rep(i, bel[x], bel[n]) g[i][f[i][a[x]]]--, f[i][a[x]]--;
			a[x] = num(y);
			rep(i, bel[x], bel[n]) f[i][a[x]]++, g[i][f[i][a[x]]]++;
		} else {
			if(x == 1) printf("%d\n", val[a[x]]);
			else {
				int *F = f[bel[y]-1], *G = g[bel[y]-1];
				rep(j, L[bel[y]], y) F[a[j]]++, G[F[a[j]]]++;
				printf("%d\n", (x & 1) ? G[F[a[y]]] : F[a[y]]);
				rep(j, L[bel[y]], y) G[F[a[j]]]--, F[a[j]]--;
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define pb push_back
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 200005
#define B 300
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(register int i = x; i <= (y); ++i)
#define repd(i, x, y) for(register int i = x; i >= (y); --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	read(x), read(args...);
}
int n, m, a[MAXN], tot, blo, bel[MAXN], L[MAXN], R[MAXN];
int f[B][MAXN], g[B][MAXN];
map <int, int> mp, val;
int num(int x) {
	if(!mp.count(x)) val[mp[x] = ++tot] = x;
	return mp[x];
}
int main() {
	read(n); blo = 700;
	rep(i, 1, n) bel[i] = (i-1) / blo + 1, R[bel[i]] = i;
	repd(i, n, 1) L[bel[i]] = i;
	rep(i, 1, n) read(a[i]), a[i] = num(a[i]);
	rep(i, 1, bel[n]) {
		rep(j, 0, tot) f[i][j] = f[i-1][j];
		rep(j, 0, n) g[i][j] = g[i-1][j];
		rep(j, L[i], R[i]) f[i][a[j]]++, g[i][f[i][a[j]]]++;
	}
	read(m);
	int op, x, y;
	while(m--) {
		read(op, x, y);
		if(op == 1) {
			swap(x, y);
			rep(i, bel[x], bel[n]) g[i][f[i][a[x]]]--, f[i][a[x]]--;
			a[x] = num(y);
			rep(i, bel[x], bel[n]) f[i][a[x]]++, g[i][f[i][a[x]]]++;
		} else {
//			cout << x << ' ' << y << ' ' << a[y] << ' ' << val[a[y]] << endl;
			if(x == 1) printf("%d\n", val[a[y]]);
			else {
				int *F = f[bel[y]-1], *G = g[bel[y]-1];
				rep(j, L[bel[y]], y) F[a[j]]++, G[F[a[j]]]++;
				printf("%d\n", (x & 1) ? G[F[a[y]]] : F[a[y]]);
				repd(j, y, L[bel[y]]) G[F[a[j]]]--, F[a[j]]--;
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define pb push_back
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 200005
#define B 205
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(register int i = x; i <= (y); ++i)
#define repd(i, x, y) for(register int i = x; i >= (y); --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	read(x), read(args...);
}
int n, m, a[MAXN], tot, blo, bel[MAXN], L[MAXN], R[MAXN];
int f[B][MAXN], g[B][MAXN];
map <int, int> mp, val;
int num(int x) {
	if(!mp.count(x)) val[mp[x] = ++tot] = x;
	return mp[x];
}
int main() {
	read(n); blo = 500;
	rep(i, 1, n) bel[i] = (i-1) / blo + 1, R[bel[i]] = i;
	repd(i, n, 1) L[bel[i]] = i;
	rep(i, 1, n) read(a[i]), a[i] = num(a[i]);
	rep(i, 1, bel[n]) {
		rep(j, 1, tot) f[i][j] = f[i-1][j], g[i][j] = g[i-1][j];
		rep(j, L[i], R[i]) f[i][a[j]]++, g[i][f[i][a[j]]]++;
	}
	read(m);
	int op, x, y;
	while(m--) {
		read(op, x, y);
		if(op == 1) {
			swap(x, y);
			rep(i, bel[x], bel[n]) g[i][f[i][a[x]]]--, f[i][a[x]]--;
			a[x] = num(y);
			rep(i, bel[x], bel[n]) f[i][a[x]]++, g[i][f[i][a[x]]]++;
		} else {
			if(x == 1) printf("%d\n", val[a[y]]);
			else {
				int *F = f[bel[y]-1], *G = g[bel[y]-1];
				rep(j, L[bel[y]], y) F[a[j]]++, G[F[a[j]]]++;
				printf("%d\n", (x & 1) ? G[F[a[y]]] : F[a[y]]);
				repd(j, y, L[bel[y]]) G[F[a[j]]]--, F[a[j]]--;
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define il inline
#define pb push_back
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 100005
#define B 1005
#define MAXM
#define mod
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(register int i = x; i <= (y); ++i)
#define repd(i, x, y) for(register int i = x; i >= (y); --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
	char ch = getchar(); int f = 1; x = 0;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	x *= f;
}
template <typename T, typename... Args> il void read(T &x, Args &...args) {
	read(x), read(args...);
}
int n, m, a[MAXN], tot, blo, bel[MAXN], L[MAXN], R[MAXN];
int f[B][MAXN], g[B][MAXN];
map <int, int> mp, val;
int num(int x) {
	if(!mp.count(x)) val[mp[x] = ++tot] = x;
	return mp[x];
}
int main() {
	read(n); blo = sqrt(n);
	rep(i, 1, n) bel[i] = (i-1) / blo + 1, R[bel[i]] = i;
	repd(i, n, 1) L[bel[i]] = i;
	rep(i, 1, n) read(a[i]), a[i] = num(a[i]);
	rep(i, 1, bel[n]) {
		rep(j, 1, tot) f[i][j] = f[i-1][j], g[i][j] = g[i-1][j];
		rep(j, L[i], R[i]) f[i][a[j]]++, g[i][f[i][a[j]]]++;
	}
	read(m);
	int op, x, y;
	while(m--) {
		read(op, x, y);
		if(op == 1) {
			swap(x, y);
			rep(i, bel[x], bel[n]) g[i][f[i][a[x]]]--, f[i][a[x]]--;
			a[x] = num(y);
			rep(i, bel[x], bel[n]) f[i][a[x]]++, g[i][f[i][a[x]]]++;
		} else {
			if(x == 1) printf("%d\n", val[a[x]]);
			else {
				int *F = f[bel[y]-1], *G = g[bel[y]-1];
				rep(j, L[bel[y]], y) F[a[j]]++, G[F[a[j]]]++;
				printf("%d\n", (x & 1) ? G[F[a[y]]] : F[a[y]]);
				rep(j, L[bel[y]], y) G[F[a[j]]]--, F[a[j]]--;
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int maxN=200005,maxLEN=170;

int N,M,LEN;
int A[maxN],val[maxN],n;
int inb[maxN];int beg[maxLEN];
int f[maxLEN][maxN],g[maxLEN][maxN];
int tf[maxN],tg[maxN];

int Opt[maxN],X[maxN],Y[maxN];

int main(){
	scanf("%d",&N);LEN=min(N,(int)(2*sqrt(N)));
	for(int i=1;i<=N;i++){
		scanf("%d",&A[i]);val[++n]=A[i];
		inb[i]=(i-1)/LEN+1;if(inb[i]!=inb[i-1]) beg[inb[i]]=i;
	}
	beg[inb[N]+1]=N+1;
	scanf("%d",&M);
	for(int i=1;i<=M;i++){
		scanf("%d%d%d",&Opt[i],&X[i],&Y[i]);
		if(Opt[i]==1) val[++n]=X[i];
	}
	sort(val+1,val+n+1);n=unique(val+1,val+n+1)-val-1;

	for(int i=1;i<=M;i++) if(Opt[i]==1) X[i]=lower_bound(val+1,val+n+1,X[i])-val;
	for(int i=1;i<=N;i++){
		A[i]=lower_bound(val+1,val+n+1,A[i])-val;
		if(beg[inb[i]]==i)
			for(int j=1;j<=N;j++) f[inb[i]][j]=tf[j],g[inb[i]][j]=tg[j];
		tg[++tf[A[i]]]++;
	}

	for(int i=1;i<=M;i++){
		#define opt Opt[i]
		#define x X[i]
		#define y Y[i]
		if(opt==1){
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][f[j][A[y]]--]--;
			A[y]=x;
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][++f[j][A[y]]]++;
		}
		else{
			if(x==1){printf("%d\n",val[A[y]]);continue;}
			int ans2=f[inb[y]][A[y]];
			for(int j=beg[inb[y]];j<=y;j++)
				ans2+=(A[j]==A[y]);
			if(x%2==0){printf("%d\n",ans2);continue;}
			int ans3=g[inb[y]][ans2];
			for(int j=beg[inb[y]];j<=y;j++)
				ans3+=((++f[inb[y]][A[j]])==ans2);
			printf("%d\n",ans3);
			for(int j=beg[inb[y]];j<=y;j++)
				f[inb[y]][A[j]]--;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

const int maxN=300005,maxLEN=170;

int N,M,LEN;
int A[maxN],val[maxN],n;
int inb[maxN];int beg[maxLEN];
int f[maxLEN][maxN],g[maxLEN][maxN];
int tf[maxN],tg[maxN];

int Opt[maxN],X[maxN],Y[maxN];

int main(){
//	freopen("CF418E.in","r",stdin);
//	freopen("WA.out","w",stdout);
	scanf("%d",&N);LEN=min(N,(int)(2*sqrt(N)));
	for(int i=1;i<=N;i++){
		scanf("%d",&A[i]);val[++n]=A[i];
		inb[i]=(i-1)/LEN+1;if(inb[i]!=inb[i-1]) beg[inb[i]]=i;
	}
	beg[inb[N]+1]=N+1;
	scanf("%d",&M);
	for(int i=1;i<=M;i++){
		scanf("%d%d%d",&Opt[i],&X[i],&Y[i]);
		if(Opt[i]==1) val[++n]=X[i];
	}
	sort(val+1,val+n+1);n=unique(val+1,val+n+1)-val-1;

	for(int i=1;i<=M;i++) if(Opt[i]==1) X[i]=lower_bound(val+1,val+n+1,X[i])-val;
	for(int i=1;i<=N;i++){
		A[i]=lower_bound(val+1,val+n+1,A[i])-val;
		if(beg[inb[i]]==i)
			for(int j=1;j<=N;j++) f[inb[i]][j]=tf[j],g[inb[i]][j]=tg[j];
		tg[++tf[A[i]]]++;
	}

	for(int i=1;i<=M;i++){
		#define opt Opt[i]
		#define x X[i]
		#define y Y[i]
		if(opt==1){
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][f[j][A[y]]--]--;
			A[y]=x;
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][++f[j][A[y]]]++;
		}
		else{
			if(x==1){printf("%d\n",val[A[y]]);continue;}
			for(int j=beg[inb[y]];j<=y;j++)
				g[inb[y]][++f[inb[y]][A[j]]]++;
			if(x%2==0) printf("%d\n",f[inb[y]][A[y]]);
			else printf("%d\n",g[inb[y]][f[inb[y]][A[y]]]);
			for(int j=beg[inb[y]];j<=y;j++)
				g[inb[y]][f[inb[y]][A[j]]--]--;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

const int maxN=300005,maxLEN=170;

int N,M,LEN;
int A[maxN],val[maxN],n;
int inb[maxN];int beg[maxLEN];
int f[maxLEN][maxN],g[maxLEN][maxN];
int tf[maxN],tg[maxN];

int Opt[maxN],X[maxN],Y[maxN];

int main(){
	scanf("%d",&N);LEN=min(N,(int)(2*sqrt(N)));
	for(int i=1;i<=N;i++){
		scanf("%d",&A[i]);val[++n]=A[i];
		inb[i]=(i-1)/LEN+1;if(inb[i]!=inb[i-1]) beg[inb[i]]=i;
	}
	beg[inb[N]+1]=N+1;
	scanf("%d",&M);
	for(int i=1;i<=M;i++){
		scanf("%d%d%d",&Opt[i],&X[i],&Y[i]);
		if(Opt[i]==1) val[++n]=X[i];
	}
	sort(val+1,val+n+1);n=unique(val+1,val+n+1)-val-1;

	for(int i=1;i<=M;i++) if(Opt[i]==1) X[i]=lower_bound(val+1,val+n+1,X[i])-val;
	for(int i=1;i<=N;i++){
		A[i]=lower_bound(val+1,val+n+1,A[i])-val;
		if(beg[inb[i]]==i)
			for(int j=1;j<=max(n,N);j++) f[inb[i]][j]=tf[j],g[inb[i]][j]=tg[j];
		tg[++tf[A[i]]]++;
	}

	for(int i=1;i<=M;i++){
		#define opt Opt[i]
		#define x X[i]
		#define y Y[i]
		if(opt==1){
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][f[j][A[y]]--]--;
			A[y]=x;
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][++f[j][A[y]]]++;
		}
		else{
			if(x==1){printf("%d\n",val[A[y]]);continue;}
			for(int j=beg[inb[y]];j<=y;j++)
				g[inb[y]][++f[inb[y]][A[j]]]++;
			if(x%2==0) printf("%d\n",f[inb[y]][A[y]]);
			else printf("%d\n",g[inb[y]][f[inb[y]][A[y]]]);
			for(int j=beg[inb[y]];j<=y;j++)
				g[inb[y]][f[inb[y]][A[j]]--]--;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

const int maxN=200005,maxLEN=170;

int N,M,LEN;
int A[maxN],val[maxN],n;
int inb[maxN];int beg[maxLEN];
int f[maxLEN][maxN],g[maxLEN][maxN];
int tf[maxN],tg[maxN];

int Opt[maxN],X[maxN],Y[maxN];

int main(){
	scanf("%d",&N);LEN=min(N,(int)(2*sqrt(N)));
	for(int i=1;i<=N;i++){
		scanf("%d",&A[i]);val[++n]=A[i];
		inb[i]=(i-1)/LEN+1;if(inb[i]!=inb[i-1]) beg[inb[i]]=i;
	}
	beg[inb[N]+1]=N+1;
	scanf("%d",&M);
	for(int i=1;i<=M;i++){
		scanf("%d%d%d",&Opt[i],&X[i],&Y[i]);
		if(Opt[i]==1) val[++n]=X[i];
	}
	sort(val+1,val+n+1);n=unique(val+1,val+n+1)-val-1;

	for(int i=1;i<=M;i++) if(Opt[i]==1) X[i]=lower_bound(val+1,val+n+1,X[i])-val;
	for(int i=1;i<=N;i++){
		A[i]=lower_bound(val+1,val+n+1,A[i])-val;
		if(beg[inb[i]]==i)
			for(int j=1;j<=N;j++) f[inb[i]][j]=tf[j],g[inb[i]][j]=tg[j];
		tf[A[i]]++,tg[tf[A[i]]]++;
	}

	for(int i=1;i<=M;i++){
		#define opt Opt[i]
		#define x X[i]
		#define y Y[i]
		if(opt==1){
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][f[j][A[y]]]--,f[j][A[y]]--;
			A[y]=x;
			for(int j=inb[y]+1;j<=inb[N];j++)
				f[j][A[y]]++,g[j][f[j][A[y]]]++;
		}
		else{
			if(x==1){printf("%d\n",val[A[y]]);continue;}
			int ans2=f[inb[y]][A[y]];
			for(int j=beg[inb[y]];j<=y;j++)
				ans2+=(A[j]==A[y]);
			if(x%2==0){printf("%d\n",ans2);continue;}
			int ans3=g[inb[y]][ans2];
			for(int j=beg[inb[y]];j<=y;j++)
				ans3+=((++f[inb[y]][A[j]])==ans2);
			printf("%d\n",ans3);
			for(int j=beg[inb[y]];j<=y;j++)
				f[inb[y]][A[j]]--;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

const int maxN=200005,maxLEN=500;

int N,M,LEN;
int A[maxN],val[maxN<<1],n;
int inb[maxN];int beg[maxLEN];
int f[maxLEN][maxN],g[maxLEN][maxN];
int tf[maxN],tg[maxN];

int Opt[maxN],X[maxN],Y[maxN];

int main(){
	scanf("%d",&N);LEN=sqrt(N);
	for(int i=1;i<=N;i++){
		scanf("%d",&A[i]);val[++n]=A[i];
		inb[i]=(i-1)/LEN+1;if(inb[i]!=inb[i-1]) beg[inb[i]]=i;
	}
	beg[inb[N]+1]=N+1;
	scanf("%d",&M);
	for(int i=1;i<=M;i++){
		scanf("%d%d%d",&Opt[i],&X[i],&Y[i]);
		if(Opt[i]==1) val[++n]=X[i];
	}
	sort(val+1,val+n+1);n=unique(val+1,val+n+1)-val-1;

	for(int i=1;i<=M;i++) if(Opt[i]==1) X[i]=lower_bound(val+1,val+n+1,X[i])-val;
	for(int i=1;i<=N;i++){
		A[i]=lower_bound(val+1,val+n+1,A[i])-val;
		if(beg[inb[i]]==i)
			for(int j=1;j<=N;j++) f[inb[i]][j]=tf[j],g[inb[i]][j]=tg[j];
		tg[++tf[A[i]]]++;
	}

	for(int i=1;i<=M;i++){
		#define opt Opt[i]
		#define x X[i]
		#define y Y[i]
		if(opt==1){
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][f[j][A[y]]--]--;
			A[y]=x;
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][++f[j][A[y]]]++;
		}
		else{
			if(x==1){printf("%d\n",val[A[y]]);continue;}
			int ans2=f[inb[y]][A[y]];
			for(int j=beg[inb[y]];j<=y;j++)
				ans2+=(A[j]==A[y]);
			if(x%2==0){printf("%d\n",ans2);continue;}
			int ans3=g[inb[y]][ans2];
			for(int j=beg[inb[y]];j<=y;j++)
				ans3+=((++f[inb[y]][A[j]])==ans2);
			printf("%d\n",ans3);
			for(int j=beg[inb[y]];j<=y;j++)
				f[inb[y]][A[j]]--;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

const int maxN=200005,maxLEN=170;

int N,M,LEN;
int A[maxN],val[maxN],n;
int inb[maxN];int beg[maxLEN];
int f[maxLEN][maxN],g[maxLEN][maxN];
int tf[maxN],tg[maxN];

int Opt[maxN],X[maxN],Y[maxN];

int main(){
	scanf("%d",&N);LEN=min(N,(int)(2*sqrt(N)));
	for(int i=1;i<=N;i++){
		scanf("%d",&A[i]);val[++n]=A[i];
		inb[i]=(i-1)/LEN+1;if(inb[i]!=inb[i-1]) beg[inb[i]]=i;
	}
	beg[inb[N]+1]=N+1;
	scanf("%d",&M);
	for(int i=1;i<=M;i++){
		scanf("%d%d%d",&Opt[i],&X[i],&Y[i]);
		if(Opt[i]==1) val[++n]=X[i];
	}
	sort(val+1,val+n+1);n=unique(val+1,val+n+1)-val-1;

	for(int i=1;i<=M;i++) if(Opt[i]==1) X[i]=lower_bound(val+1,val+n+1,X[i])-val;
	for(int i=1;i<=N;i++){
		A[i]=lower_bound(val+1,val+n+1,A[i])-val;
		if(beg[inb[i]]==i)
			for(int j=1;j<=N;j++) f[inb[i]][j]=tf[j],g[inb[i]][j]=tg[j];
		tg[++tf[A[i]]]++;
	}

	for(int i=1;i<=M;i++){
		#define opt Opt[i]
		#define x X[i]
		#define y Y[i]
		if(opt==1){
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][f[j][A[y]]--]--;
			A[y]=x;
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][++f[j][A[y]]]++;
		}
		else{
			if(x==1){printf("%d\n",val[A[y]]);continue;}
			for(int j=beg[inb[y]];j<=y;j++)
				g[inb[y]][++f[inb[y]][A[j]]]++;
			if(x%2==0) printf("%d\n",f[inb[y]][A[y]]);
			else printf("%d\n",g[inb[y]][f[inb[y]][A[y]]]);
			for(int j=beg[inb[y]];j<=y;j++)
				g[inb[y]][f[inb[y]][A[j]]--]--;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

const int maxN=200005,maxLEN=350;

int N,M,LEN;
int A[maxN],val[maxN],n;
int inb[maxN];int beg[maxLEN];
int f[maxLEN][maxN],g[maxLEN][maxN];
int tf[maxN],tg[maxN];

int Opt[maxN],X[maxN],Y[maxN];

int main(){
	scanf("%d",&N);LEN=sqrt(N);
	for(int i=1;i<=N;i++){
		scanf("%d",&A[i]);val[++n]=A[i];
		inb[i]=(i-1)/LEN+1;if(inb[i]!=inb[i-1]) beg[inb[i]]=i;
	}
	beg[inb[N]+1]=N+1;
	scanf("%d",&M);
	for(int i=1;i<=M;i++){
		scanf("%d%d%d",&Opt[i],&X[i],&Y[i]);
		if(Opt[i]==1) val[++n]=X[i];
	}
	sort(val+1,val+n+1);n=unique(val+1,val+n+1)-val-1;

	for(int i=1;i<=M;i++) if(Opt[i]==1) X[i]=lower_bound(val+1,val+n+1,X[i])-val;
	for(int i=1;i<=N;i++){
		A[i]=lower_bound(val+1,val+n+1,A[i])-val;
		if(beg[inb[i]]==i)
			for(int j=1;j<=N;j++) f[inb[i]][j]=tf[j],g[inb[i]][j]=tg[j];
		tg[++tf[A[i]]]++;
	}

	for(int i=1;i<=M;i++){
		#define opt Opt[i]
		#define x X[i]
		#define y Y[i]
		if(opt==1){
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][f[j][A[y]]--]--;
			A[y]=x;
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][++f[j][A[y]]]++;
		}
		else{
			if(x==1){printf("%d\n",val[A[y]]);continue;}
			int ans2=f[inb[y]][A[y]];
			for(int j=beg[inb[y]];j<=y;j++)
				ans2+=(A[j]==A[y]);
			if(x%2==0){printf("%d\n",ans2);continue;}
			int ans3=g[inb[y]][ans2];
			for(int j=beg[inb[y]];j<=y;j++)
				ans3+=((++f[inb[y]][A[j]])==ans2);
			printf("%d\n",ans3);
			for(int j=beg[inb[y]];j<=y;j++)
				f[inb[y]][A[j]]--;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

const int maxN=300005,maxLEN=170;

int N,M,LEN;
int A[maxN],val[maxN],n;
int inb[maxN];int beg[maxLEN];
int f[maxLEN][maxN],g[maxLEN][maxN];
int tf[maxN],tg[maxN];

int Opt[maxN],X[maxN],Y[maxN];

int main(){
	scanf("%d",&N);LEN=min(N,(int)(2*sqrt(N)));
	for(int i=1;i<=N;i++){
		scanf("%d",&A[i]);val[++n]=A[i];
		inb[i]=(i-1)/LEN+1;if(inb[i]!=inb[i-1]) beg[inb[i]]=i;
	}
	beg[inb[N]+1]=N+1;
	scanf("%d",&M);
	for(int i=1;i<=M;i++){
		scanf("%d%d%d",&Opt[i],&X[i],&Y[i]);
		if(Opt[i]==1) val[++n]=X[i];
	}
	sort(val+1,val+n+1);n=unique(val+1,val+n+1)-val-1;

	for(int i=1;i<=M;i++) if(Opt[i]==1) X[i]=lower_bound(val+1,val+n+1,X[i])-val;
	for(int i=1;i<=N;i++){
		A[i]=lower_bound(val+1,val+n+1,A[i])-val;
		if(beg[inb[i]]==i)
			for(int j=1;j<=n;j++) f[inb[i]][j]=tf[j],g[inb[i]][j]=tg[j];
		tg[++tf[A[i]]]++;
	}

	for(int i=1;i<=M;i++){
		#define opt Opt[i]
		#define x X[i]
		#define y Y[i]
		if(opt==1){
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][f[j][A[y]]--]--;
			A[y]=x;
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][++f[j][A[y]]]++;
		}
		else{
			if(x==1){printf("%d\n",val[A[y]]);continue;}
			int debug=0;
			for(int j=1;j<=N;j++) debug+=g[inb[y]][j];
			for(int j=beg[inb[y]];j<=y;j++)
				g[inb[y]][++f[inb[y]][A[j]]]++;
			if(x%2==0) printf("%d\n",f[inb[y]][A[y]]);
			else printf("%d\n",g[inb[y]][f[inb[y]][A[y]]]);
			for(int j=beg[inb[y]];j<=y;j++)
				g[inb[y]][f[inb[y]][A[j]]--]--;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

const int maxN=200005,maxLEN=320;

int N,M,LEN;
int A[maxN],val[maxN],n;
int inb[maxN];int beg[maxLEN];
int f[maxLEN][maxN],g[maxLEN][maxN];
int tf[maxN],tg[maxN];

int Opt[maxN],X[maxN],Y[maxN];

int main(){
	scanf("%d",&N);LEN=sqrt(N);
	for(int i=1;i<=N;i++){
		scanf("%d",&A[i]);val[++n]=A[i];
		inb[i]=(i-1)/LEN+1;if(inb[i]!=inb[i-1]) beg[inb[i]]=i;
	}
	beg[inb[N]+1]=N+1;
	scanf("%d",&M);
	for(int i=1;i<=M;i++){
		scanf("%d%d%d",&Opt[i],&X[i],&Y[i]);
		if(Opt[i]==1) val[++n]=X[i];
	}
	sort(val+1,val+n+1);n=unique(val+1,val+n+1)-val-1;

	for(int i=1;i<=M;i++) if(Opt[i]==1) X[i]=lower_bound(val+1,val+n+1,X[i])-val;
	for(int i=1;i<=N;i++){
		A[i]=lower_bound(val+1,val+n+1,A[i])-val;
		if(beg[inb[i]]==i)
			for(int j=1;j<=N;j++) f[inb[i]][j]=tf[j],g[inb[i]][j]=tg[j];
		tg[++tf[A[i]]]++;
	}

	for(int i=1;i<=M;i++){
		#define opt Opt[i]
		#define x X[i]
		#define y Y[i]
		if(opt==1){
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][f[j][A[y]]--]--;
			A[y]=x;
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][++f[j][A[y]]]++;
		}
		else{
			if(x==1){printf("%d\n",val[A[y]]);continue;}
			int ans2=f[inb[y]][A[y]];
			for(int j=beg[inb[y]];j<=y;j++)
				ans2+=(A[j]==A[y]);
			if(x%2==0){printf("%d\n",ans2);continue;}
			int ans3=g[inb[y]][ans2];
			for(int j=beg[inb[y]];j<=y;j++)
				ans3+=((++f[inb[y]][A[j]])==ans2);
			printf("%d\n",ans3);
			for(int j=beg[inb[y]];j<=y;j++)
				f[inb[y]][A[j]]--;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

const int maxN=200005,maxLEN=170;

int N,M,LEN;
int A[maxN],val[maxN],n;
int inb[maxN];int beg[maxLEN];
int f[maxLEN][maxN],g[maxLEN][maxN];
int tf[maxN],tg[maxN];

int Opt[maxN],X[maxN],Y[maxN];

int main(){
	scanf("%d",&N);LEN=min(N,2*sqrt(N));
	for(int i=1;i<=N;i++){
		scanf("%d",&A[i]);val[++n]=A[i];
		inb[i]=(i-1)/LEN+1;if(inb[i]!=inb[i-1]) beg[inb[i]]=i;
	}
	beg[inb[N]+1]=N+1;
	scanf("%d",&M);
	for(int i=1;i<=M;i++){
		scanf("%d%d%d",&Opt[i],&X[i],&Y[i]);
		if(Opt[i]==1) val[++n]=X[i];
	}
	sort(val+1,val+n+1);n=unique(val+1,val+n+1)-val-1;

	for(int i=1;i<=M;i++) if(Opt[i]==1) X[i]=lower_bound(val+1,val+n+1,X[i])-val;
	for(int i=1;i<=N;i++){
		A[i]=lower_bound(val+1,val+n+1,A[i])-val;
		if(beg[inb[i]]==i)
			for(int j=1;j<=N;j++) f[inb[i]][j]=tf[j],g[inb[i]][j]=tg[j];
		tg[++tf[A[i]]]++;
	}

	for(int i=1;i<=M;i++){
		#define opt Opt[i]
		#define x X[i]
		#define y Y[i]
		if(opt==1){
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][f[j][A[y]]--]--;
			A[y]=x;
			for(int j=inb[y]+1;j<=inb[N];j++)
				g[j][++f[j][A[y]]]++;
		}
		else{
			if(x==1){printf("%d\n",val[A[y]]);continue;}
			int ans2=f[inb[y]][A[y]];
			for(int j=beg[inb[y]];j<=y;j++)
				ans2+=(A[j]==A[y]);
			if(x%2==0){printf("%d\n",ans2);continue;}
			int ans3=g[inb[y]][ans2];
			for(int j=beg[inb[y]];j<=y;j++)
				ans3+=((++f[inb[y]][A[j]])==ans2);
			printf("%d\n",ans3);
			for(int j=beg[inb[y]];j<=y;j++)
				f[inb[y]][A[j]]--;
		}
	}
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#define N 200010
#define M N*40
using namespace std;
int root[N],siz[M],ls[M],rs[M],cnt;
void insert(int &u,int l,int r,int p,int v)
{
    if(!u) u=++cnt;
    if(l==r){siz[u]+=v;return;}
    int mid=(l+r)>>1;
    if(p<=mid) insert(ls[u],l,mid,p,v);
    else insert(rs[u],mid+1,r,p,v);
    siz[u]=siz[ls[u]]+siz[rs[u]];
}
int answer(int u,int l,int r,int p)
{
    if(!u) return 0;
    if(l==r) return siz[u];
    int mid=(l+r)>>1;
    if(p<=mid) return answer(ls[u],l,mid,p);
    else return siz[ls[u]]+answer(rs[u],mid+1,r,p);
}
int answerb(int u,int l,int r,int p)
{
    if(!u) return 0;
    if(l==r) return siz[u];
    int mid=(l+r)>>1;
    if(p>mid) return answerb(rs[u],mid+1,r,p);
    else return siz[rs[u]]+answerb(ls[u],l,mid,p);
}
#define B 800
int bl[N*2];
struct Opt{
    int opt,x,y,id;
}q[N];
struct odpt{
    int t,y;
}qt[N];
bool operator <(const odpt a,const odpt b){return bl[a.y]==bl[b.y]?a.t<b.t:bl[a.y]<bl[b.y];}
int qtot;
int w[N],p[N*2],tto;
int swp[N],ans[N],tot[N];
int tval[N];
int lowbit(int x){return x&(-x);}
void t_insert(int x,int v){for(;x;x-=lowbit(x)) tval[x]+=v;}
int t_answer(int n,int x){int ans=0;for(;x<=n;x+=lowbit(x)) ans+=tval[x];return ans;}
void change(int n,int x,int y)
{
    if(x)
    {
        t_insert(tot[x],-1);
        tot[x]--;
        t_insert(tot[x],1);
    }
    if(y)
    {
        t_insert(tot[y],-1);
        tot[y]++;
        t_insert(tot[y],1);
    }
}
int main()
{
    int n,k;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&w[i]),p[++tto]=w[i];
    scanf("%d",&k);
    for(int i=1;i<=k;i++)
    {
        scanf("%d%d%d",&q[i].opt,&q[i].x,&q[i].y);
        q[i].id=i;
        if(q[i].opt==1) p[++tto]=q[i].y;
    }
    sort(p+1,p+tto+1);
    tto=unique(p+1,p+tto+1)-p-1;
    for(int i=1;i<=n;i++) w[i]=lower_bound(p+1,p+tto+1,w[i])-p;
    for(int i=1;i<=k;i++)
        if(q[i].opt==1) q[i].y=lower_bound(p+1,p+tto+1,q[i].y)-p;
    for(int i=1;i<=n;i++) insert(root[w[i]],1,n,i,1),swp[i]=w[i];
    for(int i=1;i<=k;i++)
    if((q[i].opt==2 && q[i].x%2==1 && q[i].x!=1))
    {
        ++qtot;
        qt[qtot]=(odpt){i,q[i].y};
    }
    for(int i=1;i<=n;i++) bl[i]=i/B;
    sort(qt+1,qt+qtot+1);
    for(int i=1;i<=k;i++)
    {
        int x=q[i].x,y=q[i].y;
        if(q[i].opt==2)
        {
            if(x==1){ans[i]=p[w[y]];continue;}
            if(x%2==0) ans[i]=answer(root[w[y]],1,n,y);
        }
        else
        {
            q[i].id=w[x];
            insert(root[w[x]],1,n,x,-1);
            w[x]=y;
            insert(root[w[x]],1,n,x,1);
        }
    }
    int r=0,t=0;
    for(int i=1;i<=n;i++) w[i]=swp[i];
    for(int i=1;i<=qtot;i++)
    {
        while(r<qt[i].y){++r;change(n,0,w[r]);}
        while(r>qt[i].y){change(n,w[r],0);--r;}
        for(;t<qt[i].t;)
        {
            t++;
            if(q[t].opt!=1) continue;
            if(q[t].x<=qt[i].y) change(n,w[q[t].x],q[t].y);
            w[q[t].x]=q[t].y;
        }
        for(;t>qt[i].t;t--)
        {
            if(q[t].opt!=1) continue;
            if(q[t].x<=qt[i].y) change(n,w[q[t].x],q[t].id);
            w[q[t].x]=q[t].id;
        }
        ans[qt[i].t]=t_answer(n,tot[w[qt[i].y]]);
    }
    for(int i=1;i<=k;i++)
    if(q[i].opt==2)printf("%d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#define N 200010
#define M N*40
using namespace std;
int root[N],siz[M],ls[M],rs[M],cnt;
void insert(int &u,int l,int r,int p,int v)
{
    if(!u) u=++cnt;
    if(l==r){siz[u]+=v;return;}
    int mid=(l+r)>>1;
    if(p<=mid) insert(ls[u],l,mid,p,v);
    else insert(rs[u],mid+1,r,p,v);
    siz[u]=siz[ls[u]]+siz[rs[u]];
}
int answer(int u,int l,int r,int p)
{
    if(!u) return 0;
    if(l==r) return siz[u];
    int mid=(l+r)>>1;
    if(p<=mid) return answer(ls[u],l,mid,p);
    else return siz[ls[u]]+answer(rs[u],mid+1,r,p);
}
int answerb(int u,int l,int r,int p)
{
    if(!u) return 0;
    if(l==r) return siz[u];
    int mid=(l+r)>>1;
    if(p>mid) return answerb(rs[u],mid+1,r,p);
    else return siz[rs[u]]+answerb(ls[u],l,mid,p);
}
#define B 800
int bl[N*2];
struct Opt{
    int opt,x,y,id;
}q[N];
struct odpt{
    int t,y;
}qt[N];
bool operator <(const odpt a,const odpt b){return bl[a.y]==bl[b.y]?a.t<b.t:bl[a.y]<bl[b.y];}
int qtot;
int w[N],p[N*2],tto;
int swp[N],ans[N],tot[N];
int tval[N];
int lowbit(int x){return x&(-x);}
void t_insert(int x,int v){for(;x;x-=lowbit(x)) tval[x]+=v;}
int t_answer(int n,int x){int ans=0;for(;x<=n;x+=lowbit(x)) ans+=tval[x];return ans;}
void change(int n,int x,int y)
{
    if(x)
    {
        t_insert(tot[x],-1);
        tot[x]--;
        t_insert(tot[x],1);
    }
    if(y)
    {
        t_insert(tot[y],-1);
        tot[y]++;
        t_insert(tot[y],1);
    }
}
int main()
{
    int n,k;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&w[i]),p[++tto]=w[i];
    scanf("%d",&k);
    for(int i=1;i<=k;i++)
    {
        scanf("%d%d%d",&q[i].opt,&q[i].x,&q[i].y);
        q[i].id=i;
        if(q[i].opt==1) p[++tto]=q[i].x;
    }
    sort(p+1,p+tto+1);
    tto=unique(p+1,p+tto+1)-p-1;
    for(int i=1;i<=n;i++) w[i]=lower_bound(p+1,p+tto+1,w[i])-p;
    for(int i=1;i<=k;i++)
        if(q[i].opt==1) q[i].x=lower_bound(p+1,p+tto+1,q[i].x)-p;
    for(int i=1;i<=n;i++) insert(root[w[i]],1,n,i,1),swp[i]=w[i];
    for(int i=1;i<=k;i++)
    if((q[i].opt==2 && q[i].x%2==1 && q[i].x!=1))
    {
        ++qtot;
        qt[qtot]=(odpt){i,q[i].y};
    }
    for(int i=1;i<=n;i++) bl[i]=i/B;
    sort(qt+1,qt+qtot+1);
    for(int i=1;i<=k;i++)
    {
        int x=q[i].x,y=q[i].y;
        if(q[i].opt==2)
        {
            if(x==1){ans[i]=p[w[y]];continue;}
            if(x%2==0) ans[i]=answer(root[w[y]],1,n,y);
        }
        else
        {
            q[i].id=w[y];
            insert(root[w[y]],1,n,y,-1);
            w[y]=x;
            insert(root[w[y]],1,n,y,1);
        }
    }
    int r=0,t=0;
    for(int i=1;i<=n;i++) w[i]=swp[i];
    for(int i=1;i<=qtot;i++)
    {
        while(r<qt[i].y){++r;change(n,0,w[r]);}
        while(r>qt[i].y){change(n,w[r],0);--r;}
        for(;t<qt[i].t;)
        {
            t++;
            if(q[t].opt!=1) continue;
            if(q[t].y<=qt[i].y) change(n,w[q[t].y],q[t].x);
            w[q[t].y]=q[t].x;
        }
        for(;t>qt[i].t;t--)
        {
            if(q[t].opt!=1) continue;
            if(q[t].y<=qt[i].y) change(n,w[q[t].y],q[t].id);
            w[q[t].y]=q[t].id;
        }
        ans[qt[i].t]=t_answer(n,tot[w[qt[i].y]]);
    }
    for(int i=1;i<=k;i++)
    if(q[i].opt==2)printf("%d\n",ans[i]);
    return 0;
}


#include <cstdio>
#include <cmath>
#include <map>
#include <algorithm>
#define fo(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
const int mn = 101000, mm = 60;
int F[mm][mn * 2], G[mm][mn], A[mn], V[mn * 2];
int n, m, len, N, tt;
map<int, int> Q;
int get(int v) {
  if (!Q.count(v)) Q[v] = ++tt, V[tt] = v;
  return Q[v];
}
int main() {
  scanf("%d", &n);
  len = min((int)(6 * sqrt(n)), n), N = (n - 1) / len + 1;
  fo(i, 0, n - 1) scanf("%d", A + i), A[i] = get(A[i]);
  fo(i, 1, N) {
    fo(j, 1, tt) F[i][j] = F[i - 1][j];
    fo(j, 1, n) G[i][j] = G[i - 1][j];
    fo(j, (i - 1) * len, min(i * len - 1, n - 1))++ G[i][++F[i][A[j]]];
  }
  scanf("%d", &m);
  for (; m--;) {
    int r, x, y, b;
    scanf("%d%d%d", &r, &x, &y);
    if (r == 1) {
      --y, b = y / len;
      fo(i, b + 1, N)-- G[i][F[i][A[y]]--];
      A[y] = get(x);
      fo(i, b + 1, N)++ G[i][++F[i][A[y]]];
      continue;
    }
    --y, b = y / len;
    if (x == 1) {
      printf("%d\n", V[A[y]]);
      continue;
    }
    x = (x - 2) % 2 + 2;
    int v0 = F[b][A[y]];
    fo(i, b * len, y) if (A[i] == A[y])++ v0;
    if (x == 2) {
      printf("%d\n", v0);
      continue;
    }
    int v1 = G[b][v0];
    fo(i, b * len, y) if (++F[b][A[i]] == v0)++ v1;
    printf("%d\n", v1);
    fo(i, b * len, y)-- F[b][A[i]];
  }
  return 0;
}


//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+100,block=1200;
int n,m,k,a[N],b[N*2],w,t[N*2];
int cnt[110][N*2],f[110][N],l;
struct node
{
	int op,x,y;
}q[N];
inline int lowbit(int x){return x&(-x);}
inline void change(int id,int x,int v)
{
	while (x>0)
	{
		f[id][x]+=v;
		x-=lowbit(x);
	}
}
inline int query(int id,int x)
{
	int ans=0;
	while (x<=m)
	{
		ans+=f[id][x];
		x+=lowbit(x);
	}
	return ans;
}
inline int read()
{
	int f=1,x=0;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
	return x*f;
}
int main()
{
	m=read();
	for (int i=1;i<=m;i++) a[i]=b[++w]=read();
	k=read();
	for (int i=1;i<=k;i++)
	{
		q[i].op=2-read(),q[i].x=read(),q[i].y=read();
		if (q[i].op==1) swap(q[i].x,q[i].y);
		if (q[i].op==1) b[++w]=q[i].y;
	}
	sort(b+1,b+1+w);
	w=unique(b+1,b+1+w)-b-1;
	for (int i=1;i<=m;i++) a[i]=lower_bound(b+1,b+w+1,a[i])-b;
	for (int i=1;i<=k;i++) if (q[i].op==1) q[i].y=lower_bound(b+1,b+w+1,q[i].y)-b;
	w=(m-1)/block+1;
	for (int i=1;i<=w;i++)
	{
		memcpy(f[i],f[i-1],sizeof(f[i]));
		memcpy(cnt[i],cnt[i-1],sizeof(cnt[i]));
		for (int j=(i-1)*block+1;j<=min(m,i*block);j++)
		{
			change(i,cnt[i][a[j]],-1);
			cnt[i][a[j]]++;
			change(i,cnt[i][a[j]],1);
		}
	}
	for (int i=1;i<=k;i++)
	{
		if (q[i].op==0)
		{
			if (q[i].x==1)
			{
				printf("%d\n",b[a[q[i].y]]);
				continue;
			}
			int id=(q[i].y-1)/block;
			for (int j=id*block+1;j<=q[i].y;j++)
			{
				change(id,cnt[id][a[j]],-1);
				cnt[id][a[j]]++;
				change(id,cnt[id][a[j]],1);
			}
			if (q[i].x&1) printf("%d\n",query(id,cnt[id][a[q[i].y]]));
			else printf("%d\n",cnt[id][a[q[i].y]]);
			for (int j=q[i].y;j>=id*block+1;j--)
			{
				change(id,cnt[id][a[j]],-1);
				cnt[id][a[j]]--;
				change(id,cnt[id][a[j]],1);
			}
		}
		else
		{
			int id=(q[i].x-1)/block+1;
			for (int j=id;j<=w;j++)
			{
				change(j,cnt[j][a[q[i].x]],-1);
				cnt[j][a[q[i].x]]--;
				change(j,cnt[j][a[q[i].x]],1);
				change(j,cnt[j][q[i].y],-1);
				cnt[j][q[i].y]++;
				change(j,cnt[j][q[i].y],1);
			}
			a[q[i].x]=q[i].y;
		}
	}
}


//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+100,block=1200;
int n,m,k,a[N],b[N*2],w,t[N*2];
int cnt[110][N*2],f[110][N],l;
struct node
{
	int op,x,y;
}q[N];
inline int lowbit(int x){return x&(-x);}
inline void change(int id,int x,int v)
{
	while (x>0)
	{
		f[id][x]+=v;
		x-=lowbit(x);
	}
}
inline int query(int id,int x)
{
	int ans=0;
	while (x<=m)
	{
		ans+=f[id][x];
		x+=lowbit(x);
	}
	return ans;
}
inline int read()
{
	int f=1,x=0;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
	return x*f;
}
int main()
{
	m=read();
	for (int i=1;i<=m;i++) a[i]=b[++w]=read();
	k=read();
	for (int i=1;i<=k;i++)
	{
		q[i].op=2-read(),q[i].x=read(),q[i].y=read();
		if (q[i].op==1) swap(q[i].x,q[i].y);
		if (q[i].op==1) b[++w]=q[i].y;
	}
	sort(b+1,b+1+w);
	w=unique(b+1,b+1+w)-b-1;
	for (int i=1;i<=m;i++) a[i]=lower_bound(b+1,b+w+1,a[i])-b;
	for (int i=1;i<=k;i++) if (q[i].op==1) q[i].y=lower_bound(b+1,b+w+1,q[i].y)-b;
	w=(m-1)/block+1;
	for (int i=1;i<=w;i++)
	{
		memcpy(f[i],f[i-1],sizeof(f[i]));
		memcpy(cnt[i],cnt[i-1],sizeof(cnt[i]));
		for (int j=(i-1)*block+1;j<=min(m,i*block);j++)
		{
			change(i,cnt[i][a[j]],-1);
			cnt[i][a[j]]++;
			change(i,cnt[i][a[j]],1);
		}
	}
	for (int i=1;i<=k;i++)
	{
		if (q[i].op==0)
		{
			if (q[i].x==1)
			{
				printf("%d\n",b[a[q[i].y]]);
				continue;
			}
			int id=(q[i].y-1)/block;
			for (int j=id*block+1;j<=q[i].y;j++)
			{
				change(id,cnt[id][a[j]],-1);
				cnt[id][a[j]]++;
				change(id,cnt[id][a[j]],1);
			}
			if (q[i].x&1) printf("%d\n",query(id,cnt[id][a[q[i].y]]));
			else printf("%d\n",cnt[id][a[q[i].y]]);
			for (int j=q[i].y;j>=id*block+1;j--)
			{
				change(id,cnt[id][a[j]],-1);
				cnt[id][a[j]]--;
				change(id,cnt[id][a[j]],1);
			}
		}
		else
		{
			int id=(q[i].x-1)/block+1;
			for (int j=id;j<=w;j++)
			{
				change(j,cnt[j][a[j]],-1);
				cnt[j][a[q[i].x]]--;
				change(j,cnt[j][a[j]],1);
				change(j,cnt[j][q[i].y],-1);
				cnt[j][q[i].y]++;
				change(j,cnt[j][q[i].y],1);
			}
			a[q[i].x]=q[i].y;
		}
	}
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+100,block=550;
int n,m,k,a[N],b[N*2],w,t[N*2];
int cnt[210][N*2],f[210][N],l;
struct node
{
	int op,x,y;
}q[N];
inline int lowbit(int x){return x&(-x);}
inline void change(int id,int x,int v)
{
	while (x>0)
	{
		f[id][x]+=v;
		x-=lowbit(x);
	}
}
inline int query(int id,int x)
{
	int ans=0;
	while (x<=m)
	{
		ans+=f[id][x];
		x+=lowbit(x);
	}
	return ans;
}
inline int read()
{
	int f=1,x=0;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
	return x*f;
}
int main()
{
	m=read();
	for (int i=1;i<=m;i++) a[i]=b[++w]=read();
	k=read();
	for (int i=1;i<=k;i++)
	{
		q[i].op=2-read(),q[i].x=read(),q[i].y=read();
		if (q[i].op==1) swap(q[i].x,q[i].y);
		if (q[i].op==1) b[++w]=q[i].y;
	}
	sort(b+1,b+1+w);
	w=unique(b+1,b+1+w)-b-1;
	for (int i=1;i<=m;i++) a[i]=lower_bound(b+1,b+w+1,a[i])-b;
	for (int i=1;i<=k;i++) if (q[i].op==1) q[i].y=lower_bound(b+1,b+w+1,q[i].y)-b;
	w=(m-1)/block+1;
	for (int i=1;i<=w;i++)
	{
		memcpy(f[i],f[i-1],sizeof(f[i]));
		memcpy(cnt[i],cnt[i-1],sizeof(cnt[i]));
		for (int j=(i-1)*block+1;j<=min(m,i*block);j++)
		{
			change(i,cnt[i][a[j]],-1);
			cnt[i][a[j]]++;
			change(i,cnt[i][a[j]],1);
		}
	}
	for (int i=1;i<=k;i++)
	{
		if (q[i].op==0)
		{
			if (q[i].x==1)
			{
				printf("%d\n",b[a[q[i].y]]);
				continue;
			}
			int id=(q[i].y-1)/block;
			for (int j=id*block+1;j<=q[i].y;j++)
			{
				change(id,cnt[id][a[j]],-1);
				cnt[id][a[j]]++;
				change(id,cnt[id][a[j]],1);
			}
			if (q[i].x&1) printf("%d\n",query(id,cnt[id][a[q[i].y]]));
			else printf("%d\n",cnt[id][a[q[i].y]]);
			for (int j=q[i].y;j>=id*block+1;j--)
			{
				change(id,cnt[id][a[j]],-1);
				cnt[id][a[j]]--;
				change(id,cnt[id][a[j]],1);
			}
		}
		else
		{
			int id=(q[i].x-1)/block+1;
			for (int j=id;j<=w;j++)
			{
				change(j,cnt[j][a[q[i].x]],-1);
				cnt[j][a[q[i].x]]--;
				change(j,cnt[j][a[q[i].x]],1);
				change(j,cnt[j][q[i].y],-1);
				cnt[j][q[i].y]++;
				change(j,cnt[j][q[i].y],1);
			}
			a[q[i].x]=q[i].y;
		}
	}
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define re register int
using namespace std;
const int N=1e5+100,block=550;
int n,m,k,a[N],b[N*2],w,t[N*2];
int cnt[210][N*2],f[210][N],l,vi[N*2];
int p[N*2],g;
struct node
{
	int op,x,y;
}q[N];
inline int lowbit(int x){return x&(-x);}
inline void change(int id,int x,int v)
{
	while (x>0)
	{
		f[id][x]+=v;
		x-=lowbit(x);
	}
}
inline int query(int id,int x)
{
	int ans=0;
	while (x<=m)
	{
		ans+=f[id][x];
		x+=lowbit(x);
	}
	return ans;
}
inline int read()
{
	int f=1,x=0;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
	return x*f;
}
inline int min(int a,int b){return((a<b)?a:b);}
inline int lower(int x)
{
	int l=1,r=w;
	while (l<r)
	{
		int mid=(l+r)>>1;
		if (b[mid]>=x) r=mid;
		else l=mid+1;
	}
	return r;
}
int main()
{
	n=read();m=read();k=read();
	for (re i=1;i<=m;++i) a[i]=b[++w]=read();
	for (re i=1;i<=k;++i)
	{
		q[i].op=read(),q[i].x=read(),q[i].y=read();
		if (q[i].op==1) b[++w]=q[i].y;
	}
	sort(b+1,b+1+w);
	w=unique(b+1,b+1+w)-b-1;
	for (re i=1;i<=m;++i) a[i]=lower(a[i]);
	for (re i=1;i<=k;++i) if (q[i].op==1) q[i].y=lower(q[i].y);
	w=(m-1)/block+1;
	for (re i=1;i<=w;++i)
	{
		memcpy(f[i],f[i-1],sizeof(f[i]));
		memcpy(cnt[i],cnt[i-1],sizeof(cnt[i]));
		g=0;
		for (re j=(i-1)*block+1;j<=min(m,i*block);++j)
		{
			if (vi[a[j]]){cnt[i][a[j]]++;continue;}
			change(i,cnt[i][a[j]],-1);
			cnt[i][a[j]]++;
			p[++g]=a[j];vi[a[j]]=1;
		}
		for (re j=1;j<=g;++j) change(i,cnt[i][p[j]],1),vi[p[j]]=0;
	}
	for (re i=1;i<=k;++i)
	{
		if (q[i].op==0)
		{
			if (q[i].x==1)
			{
				printf("%d\n",b[a[q[i].y]]);
				continue;
			}
			int id=(q[i].y-1)/block;
			g=0;
			for (re j=id*block+1;j<=q[i].y;++j)
			{
				if (vi[a[j]]){cnt[id][a[j]]++;continue;}
				change(id,cnt[id][a[j]],-1);
				cnt[id][a[j]]++;
				p[++g]=a[j];vi[a[j]]=1;
			}
			for (re j=1;j<=g;++j) change(id,cnt[id][p[j]],1),vi[p[j]]=0;
			if (q[i].x&1) printf("%d\n",query(id,cnt[id][a[q[i].y]]));
			else printf("%d\n",cnt[id][a[q[i].y]]);
			g=0;
			for (re j=q[i].y;j>=id*block+1;--j)
			{
				if (vi[a[j]]){cnt[id][a[j]]--;continue;}
				change(id,cnt[id][a[j]],-1);
				cnt[id][a[j]]--;
				p[++g]=a[j];vi[a[j]]=1;
			}
			for (re j=1;j<=g;++j) change(id,cnt[id][p[j]],1),vi[p[j]]=0;
		}
		else
		{
			int id=(q[i].x-1)/block+1;
			for (re j=id;j<=w;++j)
			{
				change(j,cnt[j][a[q[i].x]],-1);
				cnt[j][a[q[i].x]]--;
				change(j,cnt[j][a[q[i].x]],1);
				change(j,cnt[j][q[i].y],-1);
				cnt[j][q[i].y]++;
				change(j,cnt[j][q[i].y],1);
			}
			a[q[i].x]=q[i].y;
		}
	}
}