--------------------------------------------------------------------------------------------------------------
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<iostream>
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( x , c ) memset( x , c , sizeof( x ) )
#define Rep( i , n ) for( int i = 1 ; i <= n ; ++i )
#define M( l , r ) ( ( ( l ) + ( r ) ) >> 1 )
using namespace std;
const int maxn = 50000 + 5;
const int maxnode = 1500000;
const int inf = 1e9;
const unsigned int A = 654321 , B = 54321;
int seq[ maxn ] , mn , mx , n;
unsigned int P = 820;
// treap node
struct Node {
Node* ch[ 2 ];
int s , v;
unsigned int r;
Node() : s( 0 ) { }
inline void upd() {
s = ch[ 0 ] -> s + ch[ 1 ] -> s + 1;
}
} pool[ maxnode ] , *pt = pool , *null = pt++;
Node* newNode( int v = 0 ) {
pt -> v = v;
( P *= A ) += B;
pt -> r = P;
pt -> s = 1;
pt -> ch[ 0 ] = pt -> ch[ 1 ] = null;
return pt++;
}
struct treap {
Node* root;
treap() {
root = null;
}
void rot( Node* &t , int d ) {
Node* k = t -> ch[ d ^ 1 ];
t -> ch[ d ^ 1 ] = k -> ch[ d ];
k -> ch[ d ] = t;
t -> upd() , k -> upd();
t = k;
}
void ins( Node* &t , int v ) {
if( t == null )
t = newNode( v );
else {
int d = v > t -> v;
ins( t -> ch[ d ] , v );
if( t -> ch[ d ] -> r > t -> r )
rot( t , d ^ 1 );
}
t -> upd();
}
void del( Node* &t , int v ) {
int d = t -> v == v ? -1 : ( t -> v < v );
if( d == -1 ) {
if( t -> ch[ 0 ] != null & t -> ch[ 1 ] != null ) {
int h = t -> ch[ 0 ] -> r > t -> ch[ 1 ] -> r;
rot( t , h ) , del( t -> ch[ h ] , v );
} else
t = t -> ch[ 0 ] == null ? t -> ch[ 1 ] : t -> ch[ 0 ];
} else
del( t -> ch[ d ] , v );
if( t != null ) t -> upd();
}
int select( int k ) {
for( Node* t = root ; ; ) {
int s = t -> ch[ 0 ] -> s + 1;
if( k == s + 1 ) return t -> v;
if( k < s )
t = t -> ch[ 0 ];
else
k -= s + 1 , t = t -> ch[ 1 ];
}
}
int rank( int v ) {
int ans = 0;
for( Node* t = root ; t != null ; ) {
if( t -> v < v )
ans += t -> ch[ 0 ] -> s + 1 , t = t -> ch[ 1 ];
else
t = t -> ch[ 0 ];
}
return ans;
}
int pred( int v ) {
int ans = -inf;
for( Node* t = root ; t != null ; ) {
if( t -> v < v )
ans = max( ans , t -> v ) , t = t -> ch[ 1 ];
else
t = t -> ch[ 0 ];
}
return ans;
}
int succ( int v ) {
int ans = inf;
for( Node* t = root ; t != null ; ) {
if( t -> v > v )
ans = min( t -> v , ans ) , t = t -> ch[ 0 ];
else
t = t -> ch[ 1 ];
}
return ans;
}
} TREAP[ maxn << 1 ] , *pit = TREAP;
// segment tree node
struct node {
node *l , *r;
treap* x;
} mem[ maxn << 1 ] , *pT = mem , *Rt;
int L , R , v;
void build( node* t , int l , int r ) {
t -> x = pit++;
for( int i = l ; i <= r ; i++ )
t -> x -> ins( t -> x -> root , seq[ i ] );
if( r > l ) {
int m = M( l , r );
build( t -> l = pT++ , l , m );
build( t -> r = pT++ , m + 1 , r );
}
}
void change( node* t , int l , int r ) {
if( r < L || l > L ) return;
t -> x -> del( t -> x -> root , seq[ L ] );
t -> x -> ins( t -> x -> root , v );
if( l == r ) return;
int m = M( l , r );
L <= m ? change( t -> l , l , m ) : change( t -> r , m + 1 , r );
}
int rank( node* t , int l , int r ) {
if( L <= l && r <= R )
return t -> x -> rank( v );
int m = M( l , r );
return ( L <= m ? rank( t -> l , l , m ) : 0 ) +
( m < R ? rank( t -> r , m + 1 , r ) : 0 );
}
int pred( node* t , int l , int r ) {
if( L <= l && r <= R )
return t -> x -> pred( v );
int m = M( l , r );
if( R <= m ) return pred( t -> l , l , m );
else if( L > m ) return pred( t -> r , m + 1 , r );
else return max( pred( t -> l , l , m ) , pred( t -> r , m + 1 , r ) );
}
int succ( node* t , int l , int r ) {
if( L <= l && r <= R )
return t -> x -> succ( v );
int m = M( l , r );
if( R <= m ) return succ( t -> l , l , m );
else if( L > m ) return succ( t -> r , m + 1 , r );
else return min( succ( t -> l , l , m ) , succ( t -> r , m + 1 , r ) );
}
int select( int k ) {
int l = mn , r = mx , ans;
while( l <= r ) {
v = M( l , r );
int h = rank( Rt , 1 , n );
if( rank( Rt , 1 , n ) < k )
ans = v , l = v + 1;
else
r = v - 1;
}
return ans;
}
inline void read( int &x ) {
x = 0;
int sign = 1;
char c = getchar();
for( ; ! isdigit( c ) ; c = getchar() )
if( c == ‘-‘ ) sign = -1;
for( ; isdigit( c ) ; c = getchar() )
x = x * 10 + c - ‘0‘;
x *= sign;
}
int main() {
// freopen( "test.in" , "r" , stdin );
int m;
cin >> n >> m;
Rep( i , n ) {
read( seq[ i ] );
mn = i == 1 ? seq[ i ] : min( seq[ i ] , mn );
mx = i == 1 ? seq[ i ] : max( seq[ i ] , mx );
}
build( Rt = pT++ , 1 , n );
int opt;
while( m-- ) {
read( opt ) , read( L );
switch( opt ) {
case 1 : read( R ); read( v ); printf( "%d\n" , rank( Rt , 1 , n ) + 1 ); break;
case 2 : read( R ); read( v ); printf( "%d\n" , select( v ) ); break;
case 3 : read( v ); change( Rt , 1 , n ); seq[ L ] = v; mn = min( v , mn ); mx = max( mx , v ); break;
case 4 : read( R ); read( v ); printf( "%d\n" , pred( Rt , 1 , n ) ); break;
case 5 : read( R ); read( v ); printf( "%d\n" , succ( Rt , 1 , n ) ); break;
}
}
return 0;
}
--------------------------------------------------------------------------------------------------------------