//Query : Find minimum in range
//Update: Add v to all elements in range
#include <algorithm>
using namespace std;
#define INF 2e9
#define NMAX 100010
#define TMAX ((1<<18)+10)
int A[ NMAX] ;
int tree[ TMAX] ;
int incr[ TMAX] ;
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 ; }
inline void push( int node, int node2) {
incr[ node2] + = incr[ node] ;
tree[ node2] + = incr[ node] ;
}
void init( int node, int s, int e) {
if ( s == e) {
incr[ node] = 0 ;
tree[ node] = A[ s] ;
return ;
}
int l,r,m;
l = left( node) , r = right( node) ,m = ( s+ e) / 2 ;
init( l,s,m) ;
init( r,m+ 1 ,e) ;
incr[ node] = 0 ;
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 ;
push( node,l) ;
push( node,r) ;
incr[ node] = 0 ;
return min( query( l,s,m,i,j) ,query( r,m+ 1 ,e,i,j) ) ;
}
void update( int node, int s, int e, int i, int j, int val) {
if ( s > j || e< i) return ;
if ( s >= i && e <= j) {
tree[ node] + = val;
incr[ node] + = val;
return ;
}
int l,r,m;
l = left( node) , r = right( node) , m = ( s+ e) / 2 ;
push( node,l) ;
push( node,r) ;
incr[ node] = 0 ;
update( l,s,m,i,j,val) ;
update( r,m+ 1 ,e,i,j,val) ;
tree[ node] = min( tree[ l] ,tree[ r] ) ;
}
//calling query and init and update
void init( ) {
init( 0 ,0 ,N- 1 ) ;
}
int query( int i, int j) {
return query( 0 ,0 ,N- 1 ,i,j) ;
}
void update( int i, int j, int val) {
update( 0 ,0 ,N- 1 ,i,j,val) ;
}
Ly9RdWVyeSA6IEZpbmQgbWluaW11bSBpbiByYW5nZQovL1VwZGF0ZTogQWRkIHYgdG8gYWxsIGVsZW1lbnRzIGluIHJhbmdlCiA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