ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LinkCutTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File LinkCutTree.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
85/*--------------------------------------------------------------------------*/
86/*----------------------------- DEFINITIONS --------------------------------*/
87/*--------------------------------------------------------------------------*/
88
89#ifndef __ADS_LinkCutTree
90 #define __ADS_LinkCutTree
91/* self-identification: #endif constant */
92
93/*--------------------------------------------------------------------------*/
94/*------------------------------ INCLUDES ----------------------------------*/
95/*--------------------------------------------------------------------------*/
96
97#include "ADS/Contract.h"
98#include "ADS/Modification.h"
99#include "ADS/Portable.h"
100
101#include <concepts>
102#include <cstddef>
103#include <cstdint>
104#include <functional>
105#include <utility>
106#include <vector>
107
108/*--------------------------------------------------------------------------*/
109/*--------------------------- NAMESPACE ------------------------------------*/
110/*--------------------------------------------------------------------------*/
111
112namespace ADS
113{
114
115/*--------------------------------------------------------------------------*/
116/*-------------------------- CLASS LinkCutTree -----------------------------*/
117/*--------------------------------------------------------------------------*/
118
122template< class T = long long , class Op = std::plus< T > ,
123 class Policy = NoObservers >
125{
126
127/*--------------------------------------------------------------------------*/
128 public:
129 using Value = T;
130
144 using Subject = typename Policy::template bind< modifications , LinkCutTree >;
145
149 [[nodiscard]] static constexpr std::uint64_t edgeOf( int u , int v ) {
150 const int a = u < v ? u : v , b = u < v ? v : u;
151 return( ( std::uint64_t( std::uint32_t( a ) ) << 32 ) |
152 std::uint32_t( b ) );
153 }
154
156 [[nodiscard]] static constexpr std::pair< int , int > ends( int u , int v ) {
157 return( u < v ? std::pair( u , v ) : std::pair( v , u ) );
158 }
159
163 explicit LinkCutTree( int n = 0 , const T & id = T() , Op op = Op() );
164
169 template< class... O >
170 requires( sizeof...( O ) > 0 )
171 LinkCutTree( int n , const T & id , Op op , O &... o );
172
174 [[nodiscard]] int n( void ) const { return( int( f_t.size() ) ); }
175
177 int addNode( const T & x );
178
181 [[nodiscard]] const T & value( int v ) const;
182
186 void setValue( int v , const T & x );
187
202 void evert( int v );
203
219 [[nodiscard]] int findRoot( int v );
220
223 void useSplay( void );
224
226 [[nodiscard]] bool splayed( void ) const { return( f_splay ); }
227
244 [[nodiscard]] bool connected( int u , int v );
245
258 void link( int u , int v );
259
274 void cut( int u , int v );
275
292 bool cutParent( int v );
293
306 [[nodiscard]] int lca( int u , int v );
307
322 [[nodiscard]] T aggregate( int u , int v );
323
324/*--------------------------- THE OBSERVERS --------------------------------*/
325
327 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
329 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
330
333 template< class O >
334 int attach( O & o )
335 requires( Subject::dynamic )
336 {
337 return( f_sub.attach( o ) );
338 }
339
342 void detach( int id )
343 requires( Subject::dynamic )
344 {
345 f_sub.detach( id );
346 }
347
348/*--------------------------------------------------------------------------*/
349 private:
350 static constexpr int NONE = -1;
351
353 int addNodePlain( const T & x );
354 void setValuePlain( int v , const T & x );
355 void linkPlain( int u , int v );
356 void cutPlain( int u , int v );
357 bool cutParentPlain( int v );
358
360 [[nodiscard]] int parentOf( int v );
361
363 int addNodeTold( const T & x );
364 void setValueTold( int v , const T & x );
365 void linkTold( int u , int v );
366 void cutTold( int u , int v );
367 bool cutParentTold( int v );
368
370 ADS_COLD_PATH int addNodeCold( const T & x ) { return( addNodeTold( x ) ); }
371 ADS_COLD_PATH void setValueCold( int v , const T & x ) {
372 setValueTold( v , x );
373 }
374 ADS_COLD_PATH void linkCold( int u , int v ) { linkTold( u , v ); }
375 ADS_COLD_PATH void cutCold( int u , int v ) { cutTold( u , v ); }
376 ADS_COLD_PATH bool cutParentCold( int v ) { return( cutParentTold( v ) ); }
377
379 [[nodiscard]] bool isRoot( int x ) const;
380
382 void pull( int x );
383
385 void push( int x );
386
388 void rotate( int x );
389
409 void splay( int x );
410
428 int access( int v );
429
432 int rootOf( int v , bool keep = false );
433
436 void reverseUp( int v );
437
442 [[nodiscard]] bool joined( int u , int v );
443
446 void count( void ) {
447 if( ( ++f_ops & 15 ) == 0 )
448 recount();
449 }
450
453 void recount( void );
454
457 [[nodiscard]] bool wellFormed( void ) const;
458
463 struct Node
464 {
465 int ch[ 2 ];
466 int par;
467 char rev;
468 T val;
469 T agg;
470 };
471
472 std::vector< Node > f_t;
473 std::vector< int > f_p;
475 std::vector< T > f_pv;
477 std::vector< int > f_path;
478 int f_pathOf = NONE;
479 long long f_pathAt = -1;
480 long long f_version = 0;
481 bool f_splay = false;
482 long long f_ops = 0;
483 long long f_steps = 0;
485 double f_mean = 0;
490 static constexpr double f_limit = 512;
491 std::vector< int > f_stack;
492 T f_id;
493 Op f_op;
495
496 }; // end( class LinkCutTree )
497
498/*--------------------------------------------------------------------------*/
499
500} // end( namespace ADS )
501
502/*--------------------------------------------------------------------------*/
503/*-------------------------- IMPLEMENTATION --------------------------------*/
504/*--------------------------------------------------------------------------*/
505
506#include "ADS/tree/impl/LinkCutTree.tpp"
507
508/*--------------------------------------------------------------------------*/
509
510#endif /* LinkCutTree.h included */
511
512/*--------------------------------------------------------------------------*/
513/*------------------------ End File LinkCutTree.h --------------------------*/
514/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
int findRoot(int v)
the root of the tree of , in amortized time
mod::Inserted< int, int, T > NodeInserted
a node added, its number as handle and key, with its value
Definition LinkCutTree.h:132
const T & value(int v) const
the value of
void useSplay(void)
the splay trees from now on, whatever the lengths of the paths, in time
bool splayed(void) const
whether the splay trees are in use
Definition LinkCutTree.h:226
static constexpr std::pair< int, int > ends(int u, int v)
the ends of the edge , the least first
Definition LinkCutTree.h:156
void setValue(int v, const T &x)
the value of set to , in amortized time
LinkCutTree(int n=0, const T &id=T(), Op op=Op())
the forest of nodes with no edge, each of value , the identity of op
mod::Inserted< std::uint64_t, std::pair< int, int > > EdgeInserted
an edge added, by edgeOf() and the pair of its ends
Definition LinkCutTree.h:136
T aggregate(int u, int v)
the aggregate by Op of the values of the nodes on the path between and , ends included,...
bool connected(int u, int v)
whether and are in the same tree, in amortized time
int attach(O &o)
attaches the observer and gives its number
Definition LinkCutTree.h:334
T Value
the type of the values
Definition LinkCutTree.h:129
int n(void) const
the number of nodes
Definition LinkCutTree.h:174
void detach(int id)
detaches the observer number
Definition LinkCutTree.h:342
mod::Erased< std::uint64_t, std::pair< int, int > > EdgeErased
an edge removed, by edgeOf() and the pair of its ends
Definition LinkCutTree.h:138
int lca(int u, int v)
the lowest common ancestor of and in the trees as rooted now, in amortized time
LinkCutTree(int n, const T &id, Op op, O &... o)
the forest of nodes with no edge, each of value , the identity of op, the static observers o....
static constexpr std::uint64_t edgeOf(int u, int v)
the handle of the edge , the same for : the least end in the high 32 bits, the other in the low ones
Definition LinkCutTree.h:149
mod::Assigned< int, T > ValueChanged
the value of a node changed
Definition LinkCutTree.h:134
void evert(int v)
made the root of its tree, in amortized time
bool cutParent(int v)
the edge from to its parent removed, in the trees as rooted now, the root of its new tree; false if...
int addNode(const T &x)
a new node of value , alone, and its number
void cut(int u, int v)
the edge removed, in amortized time
const Subject & subject(void) const
the subject
Definition LinkCutTree.h:329
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition LinkCutTree.h:327
mod::list< NodeInserted, ValueChanged, EdgeInserted, EdgeErased, mod::Rebuilt > modifications
the events: a node added, a value changed, an edge added or removed, anything changed at the end of a...
Definition LinkCutTree.h:141
typename Policy::template bind< modifications, LinkCutTree > Subject
the subject
Definition LinkCutTree.h:144
void link(int u, int v)
the edge , which joins two trees, made a child of , in amortized time
Changed< Value, H, V > Assigned
the value of an item changed
Definition Modification.h:623
#define ADS_COLD_PATH
a function of a rare path: never inlined, laid out away from the hot code
Definition Portable.h:107
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
Definition ADT.h:96
the item of handle , key and value erased, told before the removal outside a batch
Definition Modification.h:568
the item of handle , key and value inserted
Definition Modification.h:536
anything may have changed: forget and recompute
Definition Modification.h:708
a list of types, the events of a structure [see Modification.h]
Definition Modification.h:482