//Range query for minimum, point update
#include <algorithm>
using namespace std;
#define INF 2e9
#define NMAX 100010
#define TMAX ((1<<18)+10) //size should be 2**(ceil(log2(N)) +1)
int A[ NMAX] ;
int tree[ TMAX] ;
int lookup[ NMAX] ;
int N;
inline int left( int x) { return x* 2 + 1 ; }
inline int right( int x) { return x* 2 + 2 ; }
inline int parent( int x) { return ( x+ 1 ) / 2 - 1 ; }
void init( int node, int s, int e) {
if ( s == e) {
tree[ node] = A[ s] ;
lookup[ s] = node; //lookup for bottom up update
return ;
}
int l,r,m;
l = left( node) , r = right( node) ,m = ( s+ e) / 2 ;
init( l,s,m) ;
init( r,m+ 1 ,e) ;
tree[ node] = min( tree[ l] ,tree[ r] ) ;
}
int query( int node, int s, int e, int i, int j) {
if ( s > j || e < i) return INF;
if ( s >= i && e <= j) return tree[ node] ;
int l,r,m;
l = left( node) , r = right( node) , m = ( s+ e) / 2 ;
return min( query( l,s,m,i,j) ,query( r,m+ 1 ,e,i,j) ) ;
}
void update( int i, int val) {
A[ i] = val;
int x = lookup[ i] ;
tree[ x] = val;
while ( x) {
x = parent( x) ;
tree[ x] = min( tree[ left( x) ] ,tree[ right( x) ] ) ;
}
}
//calling query and init
void init( ) {
init( 0 ,0 ,N- 1 ) ;
}
int query( int i, int j) {
return query( 0 ,0 ,N- 1 ,i,j) ;
}
Ly9SYW5nZSBxdWVyeSBmb3IgbWluaW11bSwgcG9pbnQgdXBkYXRlCgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgSU5GIDJlOQojZGVmaW5lIE5NQVggMTAwMDEwCiNkZWZpbmUgVE1BWCAoKDE8PDE4KSsxMCkJLy9zaXplIHNob3VsZCBiZSAyKiooY2VpbChsb2cyKE4pKSArMSkKCmludCBBW05NQVhdOwoKaW50IHRyZWVbVE1BWF07CmludCBsb29rdXBbTk1BWF07CgppbnQgTjsKCmlubGluZSBpbnQgbGVmdChpbnQgeCl7IHJldH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