Icpc Reference
Icpc Reference
Listings
articulation.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
cht–aliens–wqs.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
cht–monotonic.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
cht–norestriction.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
dijkstra.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
fenwick.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
fenwick–rangeupdate.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
floyd.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
geometry.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
lca.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
pbds.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
persistent–segtree.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
qexp.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
scc–mice.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
segtree.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
segtree–lazy.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
segtree–lazy–progression.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
segtree–lazy–sum.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
sieve.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
smjleo–template.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
suffix–array.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
trie–xor.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
ufds.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
ufds–rank.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
wavelet.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1
1 articulation.cpp
// code f o r bombing2
// f i n d i n g a r t i c u l a t i o n p o i n t s
// whats t h e max number o f r e s u l t i n g components ?
#include ” b i t s / s t d c ++.h”
using namespace s t d ;
#define int long long
int ans = 0 ;
int depth [MXN] ;
int mdep [MXN] ;
bool v i s [MXN] ;
signed main ( ) {
i o s b a s e : : s y n c w i t h s t d i o ( f a l s e ) ; c i n . t i e (NULL) ; c o u t . t i e (NULL) ;
c i n >> n >> m;
f o r ( int i =0; i <m; i ++) {
int a , b ;
c i n >> a >> b ;
a d j [ a ] . push back ( b ) ;
a d j [ b ] . push back ( a ) ;
}
2
d f s ( 0 , −1, 0 ) ;
2 cht-aliens-wqs.cpp
// WQS / A l i e n s t r i c k code
// a l i e n s b i t
long long t a k e p h o t o s ( signed N, signed m, signed k , s t d : : v e c t o r <signed> r ,
s t d : : v e c t o r <signed> c ) {
// t r y w i t h b i n s e a r c h
// a t t h i s p o i n t , no e x a c t e x i s t s
i f ( low == −1) {
return s o l v e ( h i g h ) . f i r s t ;
}
i f ( h i g h == −1) {
return s o l v e ( low ) . f i r s t ;
}
3
int x2 = w i t h h i g h . second , y2 = w i t h h i g h . f i r s t ;
// l i n e a r r e c t i f i c a t i o n
return y1 + ( y2 − y1 ) ∗ ( k − x1 ) / ( x2 − x1 ) ;
#undef int
3 cht-monotonic.cpp
// t h i s i s a c t u a l l y NOI 2020 d i s c h a r g i n g code
int n ;
const int MXN = 1 0 0 0 0 0 5 ;
int t [MXN] , dp [MXN] ;
struct Li ne {
int a , b ;
Li ne ( int m, int c ) {
a = m;
b = c;
}
int v a l u e ( int x ) {
return a ∗ x + b ;
}
};
// d e s c e n d i n g h u l l
int c u r r ;
CHT( ) {
curr = 0;
}
void addLine ( Li ne c ) {
4
while ( h u l l . s i z e ( ) >= 2 ) {
Li ne a = h u l l [ h u l l . s i z e ( ) −2];
Li ne b = h u l l . back ( ) ;
// i f ( x i n t e r ( a , b ) >= x i n t e r ( b , c ) ) {
int128 l = (b . b − a . b) ;
int128 m = b. a − c . a ;
int128 e = ( c . b − b . b) ;
int128 f = a . a − b. a ;
i f ( l ∗ m >= e ∗ f ) {
h u l l . pop back ( ) ;
}
else {
break ;
}
}
h u l l . push back ( c ) ;
}
int low = 0 ;
int h i g h = h u l l . s i z e ( ) − 1 ;
return l i n e s [ h u l l [ c u r r ] ] . v a l u e ( x ) ;
}
} cht ;
signed main ( ) {
// ran ( ) ; f a s t ( ) ;
c i n >> n ;
f o r ( int i =1; i<=n ; i ++) {
c i n >> t [ i ] ;
t [ i ] = max( t [ i ] , t [ i −1]) ;
5
}
c o u t << dp [ n ] << e n d l ;
4 cht-norestriction.cpp
// CHT w i t h no r e s t r i c t i o n s
struct chtDynamic {
struct l i n e {
l l m, b ; l d x ;
l l v a l ; bool i s Q u e r y ;
l i n e ( l l m = 0 , l l b = 0) {
m = m;
b = b;
val = 0;
x = −i n f ;
isQuery = false ;
}
l l e v a l ( l l x ) const { return m ∗ x + b ; }
bool p a r a l l e l ( const l i n e &l ) const { return m == l .m; }
l d i n t e r s e c t ( const l i n e &l ) const {
return p a r a l l e l ( l ) ? i n f : 1 . 0 ∗ ( l . b − b ) / (m − l .m) ;
}
bool operator < ( const l i n e &l ) const {
i f ( l . i s Q u e r y ) return x < l . v a l ;
e l s e return m < l .m;
}
};
s e t <l i n e > h u l l ;
typedef s e t <l i n e > : : i t e r a t o r i t e r ;
6
bool bad ( const l i n e &l 1 , const l i n e &l 2 , const l i n e &l 3 ) {
return l 1 . i n t e r s e c t ( l 3 ) <= l 1 . i n t e r s e c t ( l 2 ) ;
}
bool bad ( i t e r i t ) {
return cPrev ( i t ) && cNext ( i t ) && bad ( ∗ prev ( i t ) , ∗ i t , ∗ next ( i t ) ) ;
}
i t e r update ( i t e r i t ) {
i f ( ! cPrev ( i t ) ) return i t ;
l d x = i t −> i n t e r s e c t ( ∗ prev ( i t ) ) ;
l i n e tmp ( ∗ i t ) ; tmp . x = x ;
it = hull . erase ( it ) ;
return h u l l . i n s e r t ( i t , tmp ) ;
}
void addLine ( l l m, l l b ) {
l i n e l (m, b ) ;
i t e r i t = h u l l . lower bound ( l ) ;
i f ( i t != h u l l . end ( ) && l . p a r a l l e l ( ∗ i t ) ) {
i f ( i t −> b < b ) i t = h u l l . e r a s e ( i t ) ;
e l s e return ;
}
i t = hull . insert ( it , l ) ;
i f ( bad ( i t ) ) return ( void ) h u l l . e r a s e ( i t ) ;
i t = update ( i t ) ;
i f ( cPrev ( i t ) ) update ( prev ( i t ) ) ;
i f ( cNext ( i t ) ) update ( next ( i t ) ) ;
}
l l query ( l l x ) const {
if ( hull . empty ( ) ) return − i n f ;
line q; q . val = x , q . isQuery = 1 ;
iter it = −−h u l l . lower bound ( q ) ;
return i t −> e v a l ( x ) ;
}
} ds ;
struct chtDynamicMin {
struct l i n e {
l l m, b ; l d x ;
l l v a l ; bool i s Q u e r y ;
l i n e ( l l m = 0 , l l b = 0) {
m = m;
b = b;
val = 0;
x = −i n f ;
isQuery = false ;
}
7
l l e v a l ( l l x ) const { return m ∗ x + b ; }
bool p a r a l l e l ( const l i n e &l ) const { return m == l .m; }
l d i n t e r s e c t ( const l i n e &l ) const {
return p a r a l l e l ( l ) ? i n f : 1 . 0 ∗ ( l . b − b ) / (m − l .m) ;
}
bool operator < ( const l i n e &l ) const {
i f ( l . i s Q u e r y ) return x < l . v a l ;
e l s e return m < l .m;
}
};
s e t <l i n e > h u l l ;
typedef s e t <l i n e > : : i t e r a t o r i t e r ;
i t e r update ( i t e r i t ) {
i f ( ! cPrev ( i t ) ) return i t ;
l d x = i t −> i n t e r s e c t ( ∗ prev ( i t ) ) ;
l i n e tmp ( ∗ i t ) ; tmp . x = x ;
it = hull . erase ( it ) ;
return h u l l . i n s e r t ( i t , tmp ) ;
}
void addLine ( l l m, l l b ) {
m ∗= −1;
b ∗= −1;
l i n e l (m, b ) ;
i t e r i t = h u l l . lower bound ( l ) ;
i f ( i t != h u l l . end ( ) && l . p a r a l l e l ( ∗ i t ) ) {
i f ( i t −> b < b ) i t = h u l l . e r a s e ( i t ) ;
e l s e return ;
}
i t = hull . insert ( it , l ) ;
i f ( bad ( i t ) ) return ( void ) h u l l . e r a s e ( i t ) ;
i t = update ( i t ) ;
i f ( cPrev ( i t ) ) update ( prev ( i t ) ) ;
i f ( cNext ( i t ) ) update ( next ( i t ) ) ;
}
l l query ( l l x ) const {
i f ( h u l l . empty ( ) ) return i n f ;
l i n e q ; q . val = x , q . isQuery = 1 ;
8
i t e r i t = −−h u l l . lower bound ( q ) ;
return − i t −> e v a l ( x ) ;
}
} dsmin ;
5 dijkstra.cpp
v e c t o r < p a i r <int , int> > a d j L i s t [N ] ; // p a i r . f i r s t i s t h e node , p a i r . second
i s the edge weight
int d i s t [N ] ;
p r i o r i t y q u e u e < p a i r <int , int >, v e c t o r < p a i r <int , int> >, g r e a t e r <> > pq ;
dist [S] = 0;
pq . push ( m a k e p a i r ( 0 , S ) ) ;
while ( ! pq . empty ( ) ) {
auto c u r = pq . top ( ) ;
pq . pop ( ) ;
int x = c u r . second , d = c u r . f i r s t ;
i f ( d > d i s t [ x ] ) continue ;
f o r ( auto i t : a d j L i s t [ x ] ) {
int nx = i t . f i r s t , nd = d+i t . s e c o n d ;
i f ( d i s t [ nx ] <= nd ) continue ;
d i s t [ nx ] = nd ;
pq . push ( { nd , nx } ) ;
}
}
6 fenwick.cpp
int fw [N ] ;
// f e n w i c k t r e e i s 1−i n d e x e d
9
7 fenwick-rangeupdate.cpp
int fw [N] , fw2 [N ] ;
8 floyd.cpp
int adjMat [N ] [ N ] ;
// a d j a c e n c y matrix , s e t i t t o INT MAX i f e d g e doesn ’ t e x i s t
9 geometry.cpp
#include <b i t s / s t d c ++.h>
using namespace s t d ;
using l l = long long ;
template<c l a s s T>
struct P oi nt {
typedef P oi nt P ;
T x, y;
e x p l i c i t P oi nt (T x=0, T y=0) : x ( x ) , y ( y ) {}
bool operator <(P p ) const { return t i e ( x , y ) < t i e ( p . x , p . y ) ; }
bool operator==(P p ) const { return t i e ( x , y )==t i e ( p . x , p . y ) ; }
P operator+(P p ) const { return P( x+p . x , y+p . y ) ; }
P operator −(P p ) const { return P( x−p . x , y−p . y ) ; }
P operator ∗ (T d ) const { return P( x∗d , y∗d ) ; }
P operator / (T d ) const { return P( x/d , y/d ) ; }
T dot (P p ) const { return x∗p . x + y∗p . y ; }
10
T c r o s s (P p ) const { return x∗p . y − y∗p . x ; }
T c r o s s (P a , P b ) const { return ( a−∗t h i s ) . c r o s s ( b−∗t h i s ) ; }
v e c t o r <P> c o n v e x H u l l ( v e c t o r <P> p t s ) {
i f ( ( int ) p t s . s i z e ( ) <= 1 ) return p t s ;
s o r t ( p t s . b e g i n ( ) , p t s . end ( ) ) ;
v e c t o r <P> h ( p t s . s i z e ( ) +1) ;
int s = 0 , t = 0 ;
f o r ( int i t = 2 ; i t −−; s = −−t , r e v e r s e ( p t s . b e g i n ( ) , p t s . end ( ) ) )
f o r (P p : p t s ) {
while ( t >= s + 2 && h [ t − 2 ] . c r o s s ( h [ t −1] , p ) <= 0 ) t −−;
h [ t++] = p ;
}
return {h . b e g i n ( ) , h . b e g i n ( ) + t − ( t == 2 && h [ 0 ] == h [ 1 ] ) } ;
}
// g i v e n t h e l i n e s−>e , f i n d where p s t a n d s
template<c l a s s P>
int s i d e O f (P s , P e , P p ) { return sgn ( s . c r o s s ( e , p ) ) ; }
// g i v e n t h e l i n e segment s−>e , f i n d w h e t h e r p i s on i t
template<c l a s s P> bool onSegment (P s , P e , P p ) {
return p . c r o s s ( s , e ) == 0 && ( s − p ) . dot ( e − p ) <= 0 ;
}
// g i v e n a convex p o l y g o n ( ccw o r d e r ) , t e s t w h e t h e r p i s i n i t
bool i n H u l l ( const v e c t o r <P>& l , P p , bool s t r i c t = true ) {
int a = 1 , b = l . s i z e ( ) − 1 , r = ! s t r i c t ;
i f ( ( int ) l . s i z e ( ) < 3 ) return r && onSegment ( l [ 0 ] , l . back ( ) , p ) ;
i f ( s i d e O f ( l [ 0 ] , l [ a ] , l [ b ] ) > 0 ) swap ( a , b ) ;
i f ( s i d e O f ( l [ 0 ] , l [ a ] , p ) >= r | | s i d e O f ( l [ 0 ] , l [ b ] , p )<= −r )
return f a l s e ;
while ( abs ( a − b ) > 1 ) {
int c = ( a + b ) / 2 ;
( sideOf ( l [ 0 ] , l [ c ] , p) > 0 ? b : a) = c ;
}
return sgn ( l [ a ] . c r o s s ( l [ b ] , p ) ) < r ;
}
int main ( ) {
ios base : : sync with stdio ( false ) ; cin . t i e ( nullptr ) ;
11
}
10 lca.cpp
const int N = 1 0 0 0 5 0 ;
const int LOGN = 1 7 ; // l o g 2 (MAXN)
int p [LOGN+ 1 ] [N] , h [N ] ; // h : h e i g h t
long long d [N ] ; // d i s t : sum o f e d g e w e i g h t from r o o t
v e c t o r <p a i r <int , int> > a d j L i s t [N ] ; // node , w e i g h t
b i t s e t <N> v i s i t e d ;
void d f s ( int x ) {
i f ( v i s i t e d [ x ] ) return ;
visited [ x ] = 1;
f o r ( int k = 0 ; k < LOGN; ++k ) {
i f ( p [ k ] [ x ] == −1) break ;
p [ k +1][ x ] = p [ k ] [ p [ k ] [ x ] ] ;
}
f o r ( auto i t : a d j L i s t [ x ] ) {
i f ( v i s i t e d [ i t . f i r s t ] ) continue ;
p [0][ it . first ] = x;
d [ i t . f i r s t ] = d [ x ] + i t . second ;
h[ it . f i r s t ] = h[ x ] + 1;
dfs ( i t . f i r s t ) ;
}
}
/∗ advance b by h [ b ] − h [ a ] p a r e n t s ∗/
f o r ( int k = 0 , d = h [ b ] − h [ a ] ; k < LOGN; ++k ) {
i f ( d & (1<<k ) ) b = p [ k ] [ b ] ;
}
i f ( a == b ) return a ;
a s s e r t ( h [ a ] == h [ b ] ) ; // same h e i g h t
11 pbds.cpp
#include <b i t s / s t d c ++.h>
#include <b i t s / e x t c ++.h>
using namespace s t d ;
using namespace gnu pbds ;
12
template <typename K, typename V>
using pbds map = t r e e <K, V, l e s s <K>, r b t r e e t a g ,
t r e e o r d e r s t a t i s t i c s n o d e u p d a t e >;
12 persistent-segtree.cpp
#include <b i t s / s t d c ++.h>
using namespace s t d ;
using l l = long long ;
struct Vertex {
Vertex ∗ l , ∗ r ;
int sum ;
int main ( ) {
13
ios base : : sync with stdio ( false ) ; cin . t i e ( nullptr ) ;
13 qexp.cpp
// AˆB mod M, M <= ( 2 ˆ 3 1 )−1
14 scc-mice.cpp
// code f i n d s s c c
// s t o r e s i n u f d s
// b e s t ( ) and a l l i s j u s t dp e x t e n s i o n , t h i s i s s o l v i n g f o r p a r t i c u l a r
problem
#include ” b i t s / s t d c ++.h”
using namespace s t d ;
int n , e ;
const int MXN = 1 0 0 0 0 5 ;
v e c t o r <int> a d j [MXN] , i n v [MXN] , ng [MXN] ;
s t a c k <int> p r o c ;
bool v i s [MXN] ;
int mo [MXN] ;
int comp [MXN] ;
void s e t u p ( ) {
f o r ( int i =0; i <MXN; i ++) par [ i ] = i ;
}
int p a r e n t ( int n ) {
i f ( n == par [ n ] ) return n ;
return par [ n ] = p a r e n t ( par [ n ] ) ;
}
14
void d f s ( int node ) {
i f ( ! v i s [ node ] ) {
v i s [ node ] = true ;
f o r ( int i =0; i <a d j [ node ] . s i z e ( ) ; i ++) {
i f ( ! v i s [ a d j [ node ] [ i ] ] ) d f s ( a d j [ node ] [ i ] ) ;
}
p r o c . push ( node ) ;
}
}
int s c o [MXN] ;
int b e s t ( int n ) {
i f ( s c o [ n ] != −1) return s c o [ n ] ;
int a = 0 ;
f o r ( int i =0; i <ng [ n ] . s i z e ( ) ; i ++) {
a = max( a , b e s t ( ng [ n ] [ i ] ) ) ;
}
return s c o [ n ] = a + mo [ n ] ;
}
int main ( ) {
c i n >> n >> e ;
while ( ! p r o c . empty ( ) ) {
int a = p r o c . top ( ) ;
p r o c . pop ( ) ;
assign (a , a) ;
}
15
f o r ( int i =0; i <e ; i ++) {
i f ( p a r e n t ( f [ i ] . f i r s t ) != p a r e n t ( f [ i ] . s e c o n d ) ) {
// c o u t << p a r e n t ( f [ i ] . f i r s t ) << ’ > ’ << p a r e n t ( f [ i ] . second ) <<
endl ;
ng [ p a r e n t ( f [ i ] . f i r s t ) ] . push back ( p a r e n t ( f [ i ] . s e c o n d ) ) ;
}
}
c o u t << b e s t ( p a r e n t ( 0 ) ) << e n d l ;
15 segtree.cpp
template<typename T>
struct s e g t r e e {
// r o o t node i s a t 1 , node k has nodes 2 k and 2 k+1 as c h i l d r e n
T t r a n s (T i , T j ) {
// t r a n s i t i o n , change i f r e q u i r e d
return i + j ;
}
T∗ t r e e ;
int l e n ;
s e g t r e e ( int l e n g t h ) { // b u i l d t h e t r e e
len = length ;
t r e e = new T[ 2 ∗ l e n ] ;
f o r ( int i = l e n ; i < 2∗ l e n ; i ++) t r e e [ i ] = 0 ; // d e f a u l t v a l u e ,
change i f r e q u i r e d
f o r ( int i = l e n −1; i > 0 ; i −−) t r e e [ i ] = t r a n s ( t r e e [ 2 ∗ i ] , t r e e [ 2 ∗ i
+1]) ;
}
16
l ++;
}
i f ( r % 2 == 0 ) { // l e f t c h i l d o f p a r e n t
// s h o u l d not i n c l u d e p a r e n t node
result = trans ( result , tree [ r ] ) ;
r −−;
}
l /= 2 ;
r /= 2 ;
}
i f ( l == r ) r e s u l t = t r a n s ( r e s u l t , t r e e [ l ] ) ;
return r e s u l t ;
}
};
16 segtree-lazy.cpp
#include <b i t s / s t d c ++.h>
using namespace s t d ;
struct node {
node ∗ l , ∗ r ;
int val , s , m, e , l a z y a d d ;
node ( int s , int e ) : s ( s ) , e ( e ) , m( ( s+ e ) / 2 ) , v a l ( 0 ) , l a z y a d d ( 0 ) , l
(NULL) , r (NULL) {
i f ( s != e ) l = new node ( s , m) , r = new node (m+1, e ) ;
}
int v a l u e ( ) { // r e t u r n s t h e v a l u e o f t h e c u r r e n t node a f t e r l a z y
propagating
i f ( s == e ) return v a l + l a z y a d d ;
v a l += l a z y a d d ;
l −>l a z y a d d += lazyadd , r−>l a z y a d d += l a z y a d d ;
lazyadd = 0 ;
return v a l ;
}
void add ( int x , int y , int v ) {
i f ( s == x && e == y ) l a z y a d d += v ;
else {
i f ( x > m) r−>add ( x , y , v ) ;
e l s e i f ( y <= m) l −>add ( x , y , v ) ;
e l s e l −>add ( x , m, v ) , r−>add (m+1, y , v ) ;
v a l = min ( l −>v a l u e ( ) , r−>v a l u e ( ) ) ; // Change h e r e t o max
}
}
int query ( int x , int y ) {
value () ;
i f ( s == x && e == y ) return v a l u e ( ) ;
i f ( x > m) return r−>query ( x , y ) ;
i f ( y <= m) return l −>query ( x , y ) ;
return min ( l −>query ( x , m) , r−>query (m+1, y ) ) ; // Change h e r e t o max
}
};
int main ( ) {
int N, a [ 1 0 0 ] ;
/∗ I n c l u s i v e ∗/
17
node r o o t ( 0 , N−1) ;
/∗ I n i t i a l i z e i t t o an a r r a y v a l u e ∗/
f o r ( int i = 0 ; i < N; ++i ) r o o t . add ( i , i , a [ i ] ) ; // Add i s i n c l u s i v e
and i s O( l o g N)
r o o t . query ( 5 , 1 0 0 0 ) ; // Query i s O( l o g N) and i n c l u s i v e
}
17 segtree-lazy-progression.cpp
#include <b i t s / s t d c ++.h>
#pragma GCC o p t i m i z e ( ” O f a s t ” )
#pragma GCC t a r g e t ( ”avx , avx2 , fma” )
using namespace s t d ;
#define int long long
#define n l ’ \n ’
#define i o i o s b a s e : : s y n c w i t h s t d i o ( f a l s e ) ; c i n . t i e ( 0 ) ; c o u t . t i e ( 0 )
mt19937 rng ( ( unsigned ) chrono : : s t e a d y c l o c k : : now ( ) . t i m e s i n c e e p o c h ( ) . count
() ) ;
const int mod = 1 0 0 0 0 0 0 0 0 7 , mod2 = 9 9 8 2 4 4 3 5 3 ;
// change t h i s
const int N = 3 0 0 0 0 5 ;
int n , q , a r r [N ] ;
struct t d a t a {
int pf , s f , pfk , s f k , sub , l e n g t h ;
};
struct node {
int s , e , m, l a z y a d d = 0 , l a z y s e t = 0 , sum = 0 ;
tdata val ;
bool s t = f a l s e ;
node ∗ l , ∗ r ;
// p r e f i x
r e s . p f = a . pf , r e s . p f k = a . p f k ;
i f ( r e s . p f == a . l e n g t h && a . p f k == b . p f k ) r e s . p f += b . p f ;
// s u f f i x
res . s f = b . sf , res . sfk = b . sfk ;
i f ( r e s . s f == b . l e n g t h && a . s f k == b . s f k ) r e s . s f += a . s f ;
// sub
r e s . sub = max( { min ( a . s f , a . l e n g t h −1) , min ( b . pf , b . l e n g t h −1) , a . sub ,
b . sub } ) ;
i f ( a . s f k == b . p f k ) r e s . sub = max( r e s . sub , min ( a . s f , a . l e n g t h −1) +
min ( b . pf , b . l e n g t h −1) ) ;
r e s . sub = min ( r e s . sub , r e s . l e n g t h − 2 ) ; // cannot be p r e f i x or
suffix
return r e s ;
}
18
void merge ( ) {
i f ( s == e ) return ;
propag ( ) ;
l −>propag ( ) ; r−>propag ( ) ;
sum = l −>sum + r−>sum ;
v a l = f u c k ( l −>val , r−>v a l ) ;
}
void s e l f s e t ( int v ) {
st = 1;
l a z y s e t = val . pfk = val . s f k = v ;
lazyadd = 0 ;
sum = ( e−s +1) ∗ v ;
v a l . p f = v a l . s f = ( e−s +1) ;
v a l . sub = max( v a l . l e n g t h − 2 , 0 l l ) ;
}
void s e l f a d d ( int v ) {
if ( st ) {
s e l f s e t (v + lazyset ) ;
return ;
}
l a z y a d d += v ;
sum += ( e−s +1) ∗ v ;
// pf , s f e t c doesn ’ t change , o n l y t h e k v a l u e c h a n g e s
v a l . p f k += v , v a l . s f k += v ;
}
void propag ( ) {
i f ( s == e ) return ;
if ( st ) {
l −> s e l f s e t ( l a z y s e t ) ;
r−> s e l f s e t ( l a z y s e t ) ;
lazyset = st = 0;
}
i f ( lazyadd ) {
l −>s e l f a d d ( l a z y a d d ) ;
r−>s e l f a d d ( l a z y a d d ) ;
lazyadd = 0 ;
}
}
19
else {
propag ( ) ;
i f ( x > m) r−>add ( x , y , v ) ;
e l s e i f ( y <= m) l −>add ( x , y , v ) ;
e l s e l −>add ( x , m, v ) , r−>add (m+1, y , v ) ;
merge ( ) ;
}
}
signed main ( ) {
io ;
c i n >> n >> q ;
f o r ( int i =1; i<=n ; i ++) c i n >> a r r [ i ] ;
r o o t = new node ( 1 , n ) ;
while ( q−−) {
int wh ;
c i n >> wh ;
i f (wh == 1 ) {
// sum
int l , r , s , c ;
c i n >> l >> r >> s >> c ;
r o o t −>add ( l , l , s ) ;
i f ( l < r ) r o o t −>add ( l +1, r , c ) ;
i f ( r < n ) r o o t −>add ( r +1, r +1, −( s + ( r−l ) ∗ c ) ) ;
} e l s e i f (wh == 2 ) {
// s e t
int l , r , s , c ;
20
c i n >> l >> r >> s >> c ;
int x = 0 , y ;
i f ( l > 1 ) x = r o o t −>rsum ( 1 , l −1) ;
i f ( r < n ) y = r o o t −>rsum ( 1 , r +1) ;
r o o t −>s e t ( l , l , s−x ) ;
i f ( l < r ) r o o t −>s e t ( l +1, r , c ) ;
i f ( r < n) {
int yy = r o o t −>rsum ( 1 , r +1) ;
r o o t −>add ( r +1, r +1, y − yy ) ;
}
} else {
// q u e r y
int l , r ;
c i n >> l >> r ;
t d a t a ans = r o o t −>query ( l , r ) ;
int r e s = max( { ans . pf , ans . s f +1, ans . sub +1}) ;
// c o u t << ans . p f << ’ ’ << ans . s f << ’ ’ << ans . sub << n l ;
r e s = min ( r e s , ans . l e n g t h ) ;
c o u t << r e s << n l ;
}
}
}
18 segtree-lazy-sum.cpp
struct SegmentTree {
int n ;
v e c t o r <int> a , t , l a z y ;
void i n i t ( int x ) {
n = x;
t . a s s i g n (4 ∗ n , 0) ;
lazy . a s s i g n (4 ∗ n , 0) ;
}
21
b u i l d ( r c ( i ) , mid + 1 , r ) ;
t [ i ] = merge ( t [ l c ( i ) ] , t [ r c ( i ) ] ) ;
}
19 sieve.cpp
b i t s e t <N+1> i s P r i m e ;
v e c t o r <int> p r i m e L i s t ;
/∗ I n i t i a l i z i n g e v e r y number ab o ve 1 t o be prime ∗/
isPrime . r e s e t () ;
isPrime . f l i p () ;
isPrime [ 0 ] = isPrime [ 1 ] = 0;
22
p r i m e L i s t . push back ( i ) ;
f o r ( int j = i ; j <= N/ i ; ++j ) i s P r i m e [ i ∗ j ] = 0 ;
}
20 smjleo-template.cpp
#include <b i t s / s t d c ++.h>
using namespace s t d ;
#define int long long
#define n l ’ \n ’
#define i o i o s b a s e : : s y n c w i t h s t d i o ( f a l s e ) ; c i n . t i e ( 0 ) ; c o u t . t i e ( 0 )
const int mod = 1 0 0 0 0 0 0 0 0 7 ;
const int mod2 = 9 9 8 2 4 4 3 5 3 ;
// change t h i s
const int N = 5 0 0 0 0 5 ;
signed main ( ) {
io ;
21 suffix-array.cpp
#include <b i t s / s t d c ++.h>
using namespace s t d ;
using l l = long long ;
v e c t o r <int> s o r t c y c l i c s h i f t s ( s t r i n g const& s ) {
int n = s . s i z e ( ) ;
const int a l p h a b e t = 2 5 6 ;
v e c t o r <int> p ( n ) , c ( n ) , c n t (max( a l p h a b e t , n ) , 0 ) ;
// b a s e c a s e ( s o r t t h e f i r s t c h a r s )
c [p [ 0 ] ] = 0;
int c l a s s e s = 1 ;
23
v e c t o r <int> pn ( n ) , cn ( n ) ;
// s o r t t h e second h a l v e s
// s o r t t h e f i r s t h a l v e s
cn [ p [ 0 ] ] = 0 ;
classes = 1;
c . swap ( cn ) ;
}
return p ;
}
v e c t o r <int> s u f f i x a r r a y c o n s t r u c t i o n ( s t r i n g s ) {
s += ” $ ” ;
v e c t o r <int> s o r t e d s h i f t s = s o r t c y c l i c s h i f t s ( s ) ;
s o r t e d s h i f t s . erase ( s o r t e d s h i f t s . begin () ) ;
return s o r t e d s h i f t s ;
}
24
int k = 0 ;
v e c t o r <int> l c p ( n−1, 0 ) ;
int j = p [ rank [ i ] + 1 ] ;
l c p [ rank [ i ] ] = k ;
if (k)
k−−;
}
return l c p ;
}
v e c t o r <int> p r e f i x f u n c t i o n ( s t r i n g s ) {
int n = ( int ) s . l e n g t h ( ) ;
v e c t o r <int> p i ( n ) ;
i f ( s [ i ] == s [ j ] )
j ++;
pi [ i ] = j ;
}
return p i ;
}
int main ( ) {
ios base : : sync with stdio ( false ) ; cin . t i e ( nullptr ) ;
s t r i n g s ; c i n >> s ;
auto p = s u f f i x a r r a y c o n s t r u c t i o n ( s ) ;
22 trie-xor.cpp
const int b i t s = 2 9 ;
struct XORTrie {
25
struct Node {
array <int , 2> c h i l d ;
int c n t ;
Node ( ) {
cnt = 0 ;
c h i l d [ 0 ] = −1;
c h i l d [ 1 ] = −1;
}
};
v e c t o r <Node> t ;
XORTrie ( ) {
t . emplace back ( ) ;
t . emplace back ( ) ;
};
void c r e a t e c h i l d r e n ( int i ) {
i f ( t [ i ] . c h i l d [ 0 ] > −1) return ;
idx = t [ idx ] . c h i l d [ v i s i t ] ;
t [ i d x ] . c n t++;
}
}
t [ i d x ] . cnt −−;
}
}
26
int i d x = 1 ;
int r e s = 0 ;
c r e a t e c h i l d r e n ( idx ) ;
return r e s ;
}
};
23 ufds.cpp
int num groups , p [N ] ;
int par ( int x ) { return ( p [ x ] == x ) ? x : p [ x ] = par ( p [ x ] ) ; }
bool isSameGroup ( int a , int b ) { return par ( a ) == par ( b ) ; }
void merge ( int a , int b ) {
num groups −= ( ! isSameGroup ( a , b ) ;
p [ par ( a ) ] = par ( b ) ;
}
/∗ I n i t i a l i z e ∗/
f o r ( int i = 0 ; i < N; ++i ) p [ i ] = i ;
num groups = N;
24 ufds-rank.cpp
int num groups , p [N] , g r p s i z e [N ] ;
int par ( int x ) { return ( p [ x ] == x ) ? x : p [ x ] = par ( p [ x ] ) ; }
bool isSameGroup ( int a , int b ) { return par ( a ) == par ( b ) ; }
void merge ( int a , int b ) {
a = par ( a ) , b = par ( b ) ;
num groups −= ( ! isSameGroup ( a , b ) ) ;
if ( grpsize [ a ] < grpsize [b ]) {
p[a] = b;
g r p s i z e [ b ] += g r p s i z e [ a ] ;
} else {
p[b] = a;
g r p s i z e [ a ] += g r p s i z e [ b ] ;
}
}
/∗ I n i t i a l i z e ∗/
f o r ( int i = 0 ; i < N; ++i ) p [ i ] = i , g r p s i z e [ i ] = 1 ;
num groups = N;
25 wavelet.cpp
27
#include <b i t s / s t d c ++.h>
using namespace s t d ;
#define gc g e t c h a r u n l o c k e d
#define f o ( i , n ) f o r ( i =0; i <n ; i ++)
#define Fo ( i , k , n ) f o r ( i=k ; k<n? i <n : i >n ; k<n? i +=1: i −=1)
#define l l long long
#define s i ( x ) s c a n f ( ”%d” ,&x )
#define s l ( x ) s c a n f ( ”%l l d ” ,&x )
#define s s ( s ) s c a n f ( ”%s ” , s )
#define p i ( x ) p r i n t f ( ”%d\n” , x )
#define p l ( x ) p r i n t f ( ”%l l d \n” , x )
#define ps ( s ) p r i n t f ( ”%s \n” , s )
#define deb ( x ) c o u t << #x << ”=” << x << e n d l
#define deb2 ( x , y ) c o u t << #x << ”=” << x << ” , ” << #y << ”=” << y << e n d l
#define pb push back
#define mp m a k e p a i r
#define F f i r s t
#define S s e c o n d
#define a l l ( x ) x . b e g i n ( ) , x . end ( )
#define c l r ( x ) memset ( x , 0 , s i z e o f ( x ) )
#define s o r t a l l ( x ) s o r t ( a l l ( x ) )
#define t r ( i t , a ) f o r ( auto i t = a . b e g i n ( ) ; i t != a . end ( ) ; i t ++)
#define PI 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6
typedef p a i r <int , int> p i i ;
typedef p a i r <l l , l l > pl ;
typedef v e c t o r <int> vi ;
typedef v e c t o r <l l > vl ;
typedef v e c t o r <p i i > vpii ;
typedef v e c t o r <pl> vpl ;
typedef v e c t o r <vi > vvi ;
typedef v e c t o r <vl > vvl ;
int mpow( int base , int exp ) ;
void i p g r a p h ( int m) ;
void d f s ( int u , int par ) ;
const int mod = 1 0 0 0 0 0 0 0 0 7 ;
const int N = 3 e5 , M = N;
//=======================
const int MAX = 1 e6 ;
v i g [N ] ;
int a [N ] ;
// //////////////////////////////////////////////////////////////
struct w a v e l e t t r e e {
#define v i v e c t o r <int>
#define pb push back
int l o , h i ;
wavelet tree ∗l , ∗r ;
vi b ;
// nos a r e i n range [ x , y ]
// a r r a y i n d i c e s a r e [ from , t o )
w a v e l e t t r e e ( int ∗ from , int ∗ to , int x , int y ) {
lo = x , hi = y ;
i f ( l o == h i or from >= t o ) return ;
int mid = ( l o+h i ) / 2 ;
auto f = [ mid ] ( int x ) {
return x <= mid ;
28
};
b . r e s e r v e ( to−from +1) ;
b . pb ( 0 ) ;
f o r ( auto i t = from ; i t != t o ; i t ++)
b . pb ( b . back ( ) + f ( ∗ i t ) ) ;
// s e e how lambda f u n c t i o n i s used h e r e
auto p i v o t = s t a b l e p a r t i t i o n ( from , to , f ) ;
l = new w a v e l e t t r e e ( from , p i v o t , l o , mid ) ;
r = new w a v e l e t t r e e ( p i v o t , to , mid+1, h i ) ;
}
// k t h s m a l l e s t e l e m e n t i n [ l , r ]
int kth ( int l , int r , int k ) {
i f ( l > r ) return 0 ;
i f ( l o == h i ) return l o ;
int i n L e f t = b [ r ] − b [ l − 1 ] ;
int l b = b [ l − 1 ] ; //amt o f nos i n f i r s t ( l −1) nos t h a t go i n l e f t
int rb = b [ r ] ; //amt o f nos i n f i r s t ( r ) nos t h a t go i n l e f t
i f ( k <= i n L e f t ) return this−>l −>kth ( l b +1, rb , k ) ;
return this−>r−>kth ( l −lb , r−rb , k−i n L e f t ) ;
}
// c ou n t o f nos i n [ l , r ] e q u a l t o k
int count ( int l , int r , int k ) {
i f ( l > r or k < l o or k > h i ) return 0 ;
i f ( l o == h i ) return r − l + 1 ;
int l b = b [ l −1] , rb = b [ r ] , mid = ( l o+h i ) / 2 ;
i f ( k <= mid ) return this−>l −>count ( l b +1, rb , k ) ;
return this−>r−>count ( l −lb , r−rb , k ) ;
}
˜ wavelet tree () {
delete l ;
delete r ;
}
};
// ////////////////////////////////////////////////////////////////////
int main ( )
{
ios base : : sync with stdio ( false ) ;
c i n . t i e (NULL) ;
s r a n d ( time (NULL) ) ;
int i , n , k , j , q , l , r ;
c i n >> n ;
f o ( i , n ) c i n >> a [ i + 1 ] ;
w a v e l e t t r e e T( a+1, a+n+1, 1 , MAX) ;
c i n >> q ;
while ( q−−){
int x ;
29
c i n >> x ;
c i n >> l >> r >> k ;
i f ( x == 0 ) {
// k t h s m a l l e s t
c o u t << ”Kth s m a l l e s t : ” ;
c o u t << T . kth ( l , r , k ) << e n d l ;
}
i f ( x == 1 ) {
// l e s s than or e q u a l t o K
c o u t << ”LTE : ” ;
c o u t << T . LTE( l , r , k ) << e n d l ;
}
i f ( x == 2 ) {
// c ou n t o c c u r e n c e o f K i n [ l , r ]
c o u t << ” Occurence o f K: ” ;
c o u t << T . count ( l , r , k ) << e n d l ;
}
}
return 0 ;
}
30