#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0' || ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int
M=50005,
N=2000005,
inf=2147483647;
int n,m,tot;
int v[M],root[M<<2];
struct Splay{
int pre,son[2],sz,num,occ;
}tree[N];
void newnode(int id,int x){
tree[id].num=x;
tree[id].occ=tree[id].sz=1;
tree[id].pre=tree[id].son[0]=tree[id].son[1]=0;
}
void up(int id){
if (id){
int l=tree[id].son[0],r=tree[id].son[1];
tree[id].sz=tree[id].occ;
if (l) tree[id].sz+=tree[l].sz;
if (r) tree[id].sz+=tree[r].sz;
}
}
void Rotate(int x){
int y=tree[x].pre,z=tree[y].pre,l,r;
if (tree[y].son[0]==x) l=0; else l=1;
r=l^1;
if (z)
if (tree[z].son[0]==y) tree[z].son[0]=x;
else tree[z].son[1]=x;
tree[x].pre=z; tree[y].pre=x;
tree[y].son[l]=tree[x].son[r];
tree[tree[x].son[r]].pre=y;
tree[x].son[r]=y;
up(y);
}
void splay(int rt,int x,int goal){
while (tree[x].pre!=goal){
int y=tree[x].pre,z=tree[y].pre;
if (z!=goal)
if (tree[y].son[0]==x^tree[z].son[0]==y)
Rotate(x); else Rotate(y);
Rotate(x);
}
up(x);
if (!goal) root[rt]=x;
}
void insert(int rt,int id,int x){
int u=root[rt],v;
while (u){
v=u;
if (tree[u].num==x){
tree[u].occ++,tree[u].sz++;
splay(rt,u,0);
return;
}
if (tree[u].num>x) u=tree[u].son[0];
else u=tree[u].son[1];
}
if (!root[rt]){
newnode(id,x);
root[rt]=id;
} else{
newnode(id,x);
tree[id].pre=v;
if (tree[v].num>x) tree[v].son[0]=id;
else tree[v].son[1]=id;
splay(rt,id,0);
}
}
int findmax(int x){
while (tree[x].son[1]) x=tree[x].son[1];
return x;
}
int findnum(int rt,int x){
int u=root[rt];
while (u){
if (tree[u].num==x) return u;
if (tree[u].num>x) u=tree[u].son[0];
else u=tree[u].son[1];
}
}
int findpre(int rt,int x){
int u=root[rt],ma=-inf;
while (u)
if (x>tree[u].num){
ma=max(ma,tree[u].num);
u=tree[u].son[1];
} else u=tree[u].son[0];
return ma;
}
int findsuc(int rt,int x){
int u=root[rt],mi=inf;
while (u)
if (x<tree[u].num){
mi=min(mi,tree[u].num);
u=tree[u].son[0];
} else u=tree[u].son[1];
return mi;
}
int findminer(int rt,int x){
int u=root[rt],t=0;
while (u)
if (tree[u].num>x) u=tree[u].son[0];
else
if (tree[u].num<x) t+=tree[u].occ+tree[tree[u].son[0]].sz,
u=tree[u].son[1];
else{
t+=tree[tree[u].son[0]].sz;
return t;
}
return t;
}
void change(int rt,int x,int New){
int u=findnum(rt,x);
splay(rt,u,0);
if (tree[u].occ>1)
tree[u].occ--,tree[u].sz--;
else
if (tree[u].sz==1) root[rt]=0;
else
if (!tree[u].son[0]){
root[rt]=tree[u].son[1];
tree[tree[u].son[1]].pre=0;
} else
if (!tree[u].son[1]){
root[rt]=tree[u].son[0];
tree[tree[u].son[0]].pre=0;
} else{
splay(rt,findmax(tree[u].son[0]),u);
tree[tree[u].son[0]].son[1]=tree[u].son[1];
tree[tree[u].son[1]].pre=tree[u].son[0];
tree[tree[u].son[0]].pre=0;
root[rt]=tree[u].son[0];
up(tree[u].son[0]);
}
if (tree[u].occ) insert(rt,++tot,New);
else insert(rt,u,New);
}
void build(int id,int L,int R){
for (int i=L;i<=R;i++) insert(id,++tot,v[i]);
if (L==R) return;
int mid=(L+R)>>1;
build(id<<1,L,mid);
build(id<<1|1,mid+1,R);
}
int getminer(int id,int L,int R,int gl,int gr,int num){
if (L>=gl && R<=gr) return findminer(id,num);
int mid=(L+R)>>1;
if (gr<=mid) return getminer(id<<1,L,mid,gl,gr,num); else
if (gl>mid) return getminer(id<<1|1,mid+1,R,gl,gr,num);
else
return getminer(id<<1,L,mid,gl,mid,num)+
getminer(id<<1|1,mid+1,R,mid+1,gr,num);
}
int getkth(int L,int R,int k){
int lef=0,rig=inf,tmp;
while (lef<rig){
int mid=(lef+rig)>>1;
tmp=getminer(1,1,n,L,R,mid)+1;
if (tmp-1<k) lef=mid+1;
else rig=mid;
}
return lef-1;
}
int getpre(int id,int L,int R,int gl,int gr,int num){
if (L>=gl && R<=gr) return findpre(id,num);
int mid=(L+R)>>1;
if (gr<=mid) return getpre(id<<1,L,mid,gl,gr,num); else
if (gl>mid) return getpre(id<<1|1,mid+1,R,gl,gr,num);
else
return max(getpre(id<<1,L,mid,gl,mid,num),
getpre(id<<1|1,mid+1,R,mid+1,gr,num));
}
int getsuc(int id,int L,int R,int gl,int gr,int num){
if (L>=gl && R<=gr) return findsuc(id,num);
int mid=(L+R)>>1;
int t=inf,t1=inf;
if (gr<=mid) t=getsuc(id<<1,L,mid,gl,gr,num); else
if (gl>mid) t1=getsuc(id<<1|1,mid+1,R,gl,gr,num);
else
t=getsuc(id<<1,L,mid,gl,mid,num),
t1=getsuc(id<<1|1,mid+1,R,mid+1,gr,num);
return min(t,t1);
}
void update(int id,int L,int R,int g,int New){
change(id,v[g],New);
if (L==R){
v[g]=New;
return;
}
int mid=(L+R)>>1;
if (g<=mid) update(id<<1,L,mid,g,New);
else update(id<<1|1,mid+1,R,g,New);
}
int main(){
n=read(),m=read();
for (int i=1;i<=n;i++) v[i]=read();
tot=0;
build(1,1,n);
int opt,x,y,t;
while (m--){
opt=read();
if (opt==3){
x=read(),t=read();
update(1,1,n,x,t);
} else{
x=read(),y=read(),t=read();
if (opt==1) printf("%d\n",getminer(1,1,n,x,y,t)+1);
if (opt==2) printf("%d\n",getkth(x,y,t));
if (opt==4) printf("%d\n",getpre(1,1,n,x,y,t));
if (opt==5) printf("%d\n",getsuc(1,1,n,x,y,t));
}
}
return 0;
}