#include <bits/stdc++.h>
using namespace std;
#define f(a) ((a==NULL)?(0):(a->val))
const int MAXN=(1<<15);
int N;
int arr [MAXN];
struct node
{
node * l; node * r; int val;
node (node * a, node * b, int c){l=a, r=b; val=c;}
};
node * seg [2*MAXN];
int find (int start, int end, node * a)
{
if (start==end) return start;
if (f(a->l)) return find(start,(start+end)/2, a->l);
return find((start+end)/2+1, end, a->r);
}
int query (int start, int end, int i, int node)
{
if (start>i || end<i) return 0;
if (start==end) {return find(1, 10000, seg[node]);}
return max(query(start, (start+end)/2, i, 2*node), query((start+end)/2+1, end, i, 2*node+1));
}
void insert (node * &a, int start, int end, int x)
{
if (start>x || end<x) return;
if (a==NULL) a=new node (NULL, NULL, 0);
if (start==end){a->val++; return;}
insert(a->l, start, (start+end)/2, x);
insert(a->r, (start+end)/2+1, end, x);
a->val=f(a->l)+f(a->r);
}
void build (int start, int end, int node)
{
if (start!=end){
build(start,(start+end)/2, 2*node);
build((start+end)/2+1,end, 2*node+1); }
for (int g=start; g<=end; g++) insert(seg[node], 1, 10000, arr[g]);
}
void remove (node * &a, int start, int end, int x)
{
if (start>x || end<x) return;
if (start==end)
{
a->val--; if (a->val==0) a=NULL; return;
}
remove(a->l, start, (start+end)/2, x);
remove(a->r, (start+end)/2+1, end, x);
a->val=f(a->l)+f(a->r);
if (a->val==0) a=NULL;
}
int queryans (int start, int end, node * t, int k)
{
if (t==NULL || t->val==0 || end<=k) return 0;
if (start>k) return t->val;
return queryans(start,(start+end)/2, t->l, k)+queryans((start+end)/2+1, end, t->r, k);
}
int findans (int start, int end, int i, int j, int node, int k)
{
if (start>j || end<i) return 0;
if(start>=i && end<=j)
{
return queryans (1, 10000, seg[node], k);
}
return findans(start, (start+end)/2, i, j, 2*node, k)+findans((start+end)/2+1, end, i, j, 2*node+1, k);
}
void update (int start, int end, int i, int node, int toremove,int toinsert)
{
if (start>i || end<i) return;
remove (seg[node], 1, 10000, toremove);
insert (seg[node], 1, 10000, toinsert);
if (start!=end){update(start, (start+end)/2, i, 2*node, toremove, toinsert); update((start+end)/2+1, end, i, 2*node+1, toremove, toinsert);}
}
int main()
{
scanf("%d", &N); for (int g=1; g<=N; g++) cin >> arr[g];
build (1, N, 1);
int Q; scanf("%d", &Q);
for (int g=0; g<Q; g++)
{
int type; scanf("%d", &type);
if (type)
{
int i, j, k; scanf("%d %d %d", &i, &j, &k);
printf("%d\n", findans(1, N, i, j, 1, k));
}
else
{
int i, v; scanf("%d %d", &i, &v);
int x=query(1, N, i, 1);
update (1, N, i, 1, x, v);
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZihhKSAoKGE9PU5VTEwpPygwKTooYS0+dmFsKSkKY29uc3QgaW50IE1BWE49KDE8PDE1KTsgCmludCBOOyAKaW50IGFyciBbTUFYTl07IApzdHJ1Y3Qgbm9kZQp7CiAgICBub2RlICogbDsgbm9kZSAqIHI7IGludCB2YWw7IAogICAgbm9kZSAobm9kZSAqIGEsIG5vZGUgKiBiLCBpbnQgYyl7bD1hLCByPWI7IHZhbD1jO30KfTsgCm5vZGUgKiBzZWcgWzIqTUFYTl07IAppbnQgZmluZCAoaW50IHN0YXJ0LCBpbnQgZW5kLCBub2RlICogYSkKewogICBpZiAoc3RhcnQ9PWVuZCkgcmV0dXJuIHN0YXJ0OwogICBpZiAoZihhLT5sKSkgcmV0dXJuIGZpbmQoc3RhcnQsKHN0YXJ0K2VuZCkvMiwgYS0+bCk7CiAgIHJldHVybiBmaW5kKChzdGFydCtlbmQpLzIrMSwgZW5kLCBhLT5yKTsgCn0KaW50IHF1ZXJ5IChpbnQgc3RhcnQsIGludCBlbmQsIGludCBpLCBpbnQgbm9kZSkKewogICAgaWYgKHN0YXJ0PmkgfHwgZW5kPGkpIHJldHVybiAwOyAKICAgIGlmIChzdGFydD09ZW5kKSB7cmV0dXJuIGZpbmQoMSwgMTAwMDAsIHNlZ1tub2RlXSk7fSAKICA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