89# ifndef TPL_MO_ON_TREES_H
90# define TPL_MO_ON_TREES_H
97# include <type_traits>
128 typename GT::Node_Arc_Iterator;
129 { g.
vsize() } -> std::convertible_to<size_t>;
131 {
typename GT::Node_Arc_Iterator(p) };
170 template <AlephGraph GT,
class Policy>
177 using T =
typename Node::Node_Type;
181 static constexpr size_t NONE = ~static_cast<size_t>(0);
216 <<
"Gen_Mo_On_Trees: root node not found in graph";
240 <<
"Gen_Mo_On_Trees: root node not found in graph";
248 for (
auto it =
typename GT::Node_Arc_Iterator(p);
249 it.has_curr(); it.next_ne())
251 auto * a = it.get_curr();
267 for (
size_t i = 0; i <
n_; ++i)
271 for (
size_t i = 0; i <
n_; ++i)
274 size_t sub_timer = 0;
275 size_t path_timer = 0;
277 struct Frame {
size_t id;
size_t child_idx; };
281 visited(root_id) =
true;
285 tin_(root_id) = sub_timer;
289 first_(root_id) = path_timer;
293 stk.
push({root_id, 0});
297 auto & fr = stk.
top();
300 while (fr.child_idx < adj(fr.id).size())
302 const size_t nid = adj(fr.id)(fr.child_idx++);
310 tin_(nid) = sub_timer;
325 tout_(fr.id) = sub_timer - 1;
327 last_(fr.id) = path_timer;
336 <<
"Gen_Mo_On_Trees: graph is not connected (not a tree)";
340 while ((
static_cast<size_t>(1) <<
log_n_) <
n_)
345 for (
size_t i = 0; i <
log_n_ *
n_; ++i)
348 for (
size_t v = 0; v <
n_; ++v)
352 for (
size_t v = 0; v <
n_; ++v)
359 size_t lca(
size_t u,
size_t v)
const
372 for (
int k =
static_cast<int>(
log_n_) - 1;
k >= 0; --
k)
379 return up_(0 *
n_ + u);
387 const size_t q = queries.
size();
388 const size_t block = std::max<size_t>(
389 1,
static_cast<size_t>(std::sqrt(
static_cast<double>(n))));
394 const size_t ba = a.
l / block;
395 const size_t bb = b.
l / block;
398 return (ba & 1) ? (a.
r > b.
r) : (a.
r < b.
r);
405 size_t cur_l = queries(0).l;
406 size_t cur_r = queries(0).l;
407 pol_.add(data, cur_l);
409 while (cur_r < queries(0).
r)
410 pol_.add(data, ++cur_r);
412 answers(queries(0).
id) =
pol_.answer();
414 for (
size_t i = 1; i < q; ++i)
416 const size_t ql = queries(i).l;
417 const size_t qr = queries(i).r;
420 pol_.add(data, ++cur_r);
423 pol_.add(data, --cur_l);
426 pol_.remove(data, cur_r--);
429 pol_.remove(data, cur_l++);
431 answers(queries(i).
id) =
pol_.answer();
478 noexcept(std::is_nothrow_move_constructible_v<Policy>)
491 [[nodiscard]]
size_t size() const noexcept {
return n_; }
494 [[nodiscard]]
bool is_empty() const noexcept {
return n_ == 0; }
519 const size_t q = query_roots.
size();
524 for (
size_t i = 0; i < q; ++i)
528 <<
"subtree_solve: query " << i <<
" node not in tree";
529 const size_t id = p->second;
548 std::initializer_list<Node*> il)
const
577 const Array<std::pair<Node*, Node*>> & query_pairs)
const
579 const size_t q = query_pairs.size();
585 size_t l,
r, id, lca_id;
589 for (
size_t i = 0; i < q; ++i)
594 <<
"path_solve: query " << i <<
" node not in tree";
596 size_t u = pu->second;
597 size_t v = pv->second;
602 if (
const size_t ancestor =
lca(u, v); ancestor == u)
609 const size_t tour_sz = 2 *
n_;
610 const size_t block = std::max<size_t>(
611 1,
static_cast<size_t>(
612 std::sqrt(
static_cast<double>(tour_sz))));
615 [block](
const Path_Query & a,
const Path_Query & b)
617 const size_t ba = a.l / block;
618 const size_t bb = b.l / block;
621 return (ba & 1) ? (a.r > b.r) : (a.r < b.r);
628 for (
size_t i = 0; i <
n_; ++i)
632 auto toggle = [&](
const size_t pos)
634 if (
const size_t nid =
flat_node_(pos); active(nid))
647 size_t cur_l = pqueries(0).l;
648 size_t cur_r = pqueries(0).l;
651 while (cur_r < pqueries(0).
r)
654 if (pqueries(0).lca_id !=
NONE)
657 answers(pqueries(0).
id) =
pol_.answer();
661 answers(pqueries(0).
id) =
pol_.answer();
664 for (
size_t i = 1; i < q; ++i)
666 const size_t ql = pqueries(i).l;
667 const size_t qr = pqueries(i).r;
669 while (cur_r < qr) toggle(++cur_r);
670 while (cur_l > ql) toggle(--cur_l);
671 while (cur_r > qr) toggle(cur_r--);
672 while (cur_l < ql) toggle(cur_l++);
674 if (pqueries(i).lca_id !=
NONE)
677 answers(pqueries(i).
id) =
pol_.answer();
681 answers(pqueries(i).
id) =
pol_.answer();
700 std::initializer_list<std::pair<Node*, Node*>> il)
const
735 template <
typename T,
class Policy>
736 requires MoPolicy<Policy, T>
744 static constexpr size_t NONE = ~size_t(0);
777 if (
root_ ==
nullptr)
794 c = c->get_right_sibling())
795 rev_children.
push(c);
819 Node * mp = it.get_curr();
827 for (
size_t pid = 0; pid <
n_; ++pid)
833 c = c->get_right_sibling())
839 neighbors.
append(pp->second);
854 for (
size_t i = 0; i <
n_; ++i)
858 for (
size_t i = 0; i <
n_; ++i)
861 size_t sub_timer = 0;
862 size_t path_timer = 0;
864 struct Frame {
size_t id;
size_t child_idx; };
868 visited(root_id) =
true;
872 tin_(root_id) = sub_timer;
876 first_(root_id) = path_timer;
880 stk.
push({root_id, 0});
884 auto & fr = stk.
top();
887 while (fr.child_idx < adj(fr.id).size())
889 const size_t nid = adj(fr.id)(fr.child_idx++);
897 tin_(nid) = sub_timer;
912 tout_(fr.id) = sub_timer - 1;
914 last_(fr.id) = path_timer;
923 <<
"Gen_Mo_On_Tree_Node: tree traversal inconsistency";
927 while ((
static_cast<size_t>(1) <<
log_n_) <
n_)
932 for (
size_t i = 0; i <
log_n_ *
n_; ++i)
935 for (
size_t v = 0; v <
n_; ++v)
939 for (
size_t v = 0; v <
n_; ++v)
946 size_t lca(
size_t u,
size_t v)
const
959 for (
int k =
static_cast<int>(
log_n_) - 1;
k >= 0; --
k)
966 return up_(0 *
n_ + u);
974 const size_t q = queries.
size();
975 const size_t block = std::max<size_t>(
976 1,
static_cast<size_t>(std::sqrt(
static_cast<double>(nn))));
981 const size_t ba = a.
l / block;
982 const size_t bb = b.
l / block;
985 return (ba & 1) ? (a.
r > b.
r) : (a.
r < b.
r);
992 size_t cur_l = queries(0).l;
993 size_t cur_r = queries(0).l;
994 pol_.add(data, cur_l);
996 while (cur_r < queries(0).
r)
997 pol_.add(data, ++cur_r);
999 answers(queries(0).
id) =
pol_.answer();
1001 for (
size_t i = 1; i < q; ++i)
1003 const size_t ql = queries(i).l;
1004 const size_t qr = queries(i).r;
1006 while (cur_r < qr)
pol_.add(data, ++cur_r);
1007 while (cur_l > ql)
pol_.add(data, --cur_l);
1008 while (cur_r > qr)
pol_.remove(data, cur_r--);
1009 while (cur_l < ql)
pol_.remove(data, cur_l++);
1011 answers(queries(i).
id) =
pol_.answer();
1036 <<
"Gen_Mo_On_Tree_Node: root is null";
1059 noexcept(std::is_nothrow_move_constructible_v<Policy>)
1082 noexcept(
noexcept(
swap(other)))
1103 if constexpr (not std::is_nothrow_swappable_v<Policy>)
1105 std::swap(
root_, other.root_);
1106 if constexpr (std::is_nothrow_swappable_v<Policy>)
1108 std::swap(
n_, other.n_);
1118 std::swap(
log_n_, other.log_n_);
1125 [[nodiscard]]
size_t size() const noexcept {
return n_; }
1147 const size_t q = query_roots.
size();
1152 for (
size_t i = 0; i < q; ++i)
1156 <<
"subtree_solve: query " << i <<
" node not in tree";
1157 const size_t id = p->second;
1176 std::initializer_list<Node*> il)
const
1197 const Array<std::pair<Node*, Node*>> & query_pairs)
const
1199 const size_t q = query_pairs.size();
1205 size_t l,
r, id, lca_id;
1209 for (
size_t i = 0; i < q; ++i)
1214 <<
"path_solve: query " << i <<
" node not in tree";
1216 size_t u = pu->second;
1217 size_t v = pv->second;
1222 const size_t ancestor =
lca(u, v);
1231 const size_t tour_sz = 2 *
n_;
1232 const size_t block = std::max<size_t>(
1233 1,
static_cast<size_t>(
1234 std::sqrt(
static_cast<double>(tour_sz))));
1237 [block](
const Path_Query & a,
const Path_Query & b)
1239 const size_t ba = a.l / block;
1240 const size_t bb = b.l / block;
1243 return (ba & 1) ? (a.r > b.r) : (a.r < b.r);
1250 for (
size_t i = 0; i <
n_; ++i)
1253 auto toggle = [&](
const size_t pos)
1255 if (
const size_t nid =
flat_node_(pos); active(nid))
1258 active(nid) =
false;
1267 size_t cur_l = pqueries(0).l;
1268 size_t cur_r = pqueries(0).l;
1271 while (cur_r < pqueries(0).
r)
1274 if (pqueries(0).lca_id !=
NONE)
1277 answers(pqueries(0).
id) =
pol_.answer();
1281 answers(pqueries(0).
id) =
pol_.answer();
1283 for (
size_t i = 1; i < q; ++i)
1285 const size_t ql = pqueries(i).l;
1286 const size_t qr = pqueries(i).r;
1288 while (cur_r < qr) toggle(++cur_r);
1289 while (cur_l > ql) toggle(--cur_l);
1290 while (cur_r > qr) toggle(cur_r--);
1291 while (cur_l < ql) toggle(cur_l++);
1293 if (pqueries(i).lca_id !=
NONE)
1296 answers(pqueries(i).
id) =
pol_.answer();
1300 answers(pqueries(i).
id) =
pol_.answer();
1319 std::initializer_list<std::pair<Node*, Node*>> il)
const
1361 template <
typename T>
1369 template <
typename T>
1377 template <
typename T>
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void swap(Array &s) noexcept
Swap this with s
static Array create(const size_t n)
Create an array with n logical elements.
Iterator get_it()
Iterator on the modifiable array, positioned on entry 0.
T & append()
Allocate a new entry to the end of array.
Dynamic stack of elements of generic type T based on a singly linked list.
T & top()
Return a modifiable reference to the top item of the stack.
bool is_empty() const noexcept
Check if the stack is empty.
T pop()
Remove and return the top item of the stack.
T & push(const T &data)
Push an item by copy onto the top of the stack.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Offline subtree and path queries on N-ary trees (Tree_Node).
static constexpr size_t NONE
Array< answer_type > path_solve(std::initializer_list< std::pair< Node *, Node * > > il) const
Solve path queries (initializer-list overload).
Gen_Mo_On_Tree_Node(Gen_Mo_On_Tree_Node &&other) noexcept(std::is_nothrow_move_constructible_v< Policy >)
Take the tables of another engine, which is left empty.
size_t size() const noexcept
Number of nodes in the tree.
void swap(Gen_Mo_On_Tree_Node &other) noexcept(std::is_nothrow_swappable_v< Policy >)
Exchange in O(1) this engine and another.
size_t lca(size_t u, size_t v) const
MapOLhash< Node *, size_t > node_to_id_
typename Policy::answer_type answer_type
Gen_Mo_On_Tree_Node & operator=(const Gen_Mo_On_Tree_Node &other)=default
Copy the tables of another engine.
Array< size_t > flat_node_
Gen_Mo_On_Tree_Node(const Gen_Mo_On_Tree_Node &other)=default
Copy the tables of another engine.
Array< answer_type > mo_sweep(const Array< T > &data, Array< Mo_Query > queries, size_t nn) const
Array< answer_type > path_solve(const Array< std::pair< Node *, Node * > > &query_pairs) const
Answer path queries on the N-ary tree.
Array< Node * > id_to_node_
Array< answer_type > subtree_solve(std::initializer_list< Node * > il) const
Solve subtree queries (initializer-list overload).
Array< answer_type > subtree_solve(const Array< Node * > &query_roots) const
Answer subtree queries on the N-ary tree.
Gen_Mo_On_Tree_Node(Node *root, Policy p=Policy())
Construct a Mo's algorithm query engine for N-ary trees.
bool is_empty() const noexcept
True if the tree is empty.
Offline subtree and path queries on trees via Mo's algorithm.
typename Policy::answer_type answer_type
typename Node::Node_Type T
Array< answer_type > path_solve(std::initializer_list< std::pair< Node *, Node * > > il) const
Solve path queries (initializer-list overload).
static constexpr size_t NONE
Array< answer_type > subtree_solve(const Array< Node * > &query_roots) const
Answer subtree queries using Mo's algorithm.
Gen_Mo_On_Trees(Gen_Mo_On_Trees &&other) noexcept(std::is_nothrow_move_constructible_v< Policy >)
Take the tables of another engine, which is left empty, on the same graph.
Array< Node * > id_to_node_
bool is_empty() const noexcept
True if the tree is empty.
Array< size_t > flat_node_
Array< answer_type > subtree_solve(std::initializer_list< Node * > il) const
Answer subtree queries from an initializer list.
Gen_Mo_On_Trees(const Gen_Mo_On_Trees &other)=default
Copy the tables of another engine, on the same graph.
Gen_Mo_On_Trees(const GT &g, Node *root, Policy p=Policy())
Construct a Mo's algorithm query engine for tree queries.
size_t lca(size_t u, size_t v) const
MapOLhash< Node *, size_t > node_to_id_
size_t size() const noexcept
Number of nodes in the tree.
Array< answer_type > mo_sweep(const Array< T > &data, Array< Mo_Query > queries, size_t n) const
Array< answer_type > path_solve(const Array< std::pair< Node *, Node * > > &query_pairs) const
Answer path queries between node pairs.
typename Node::Node_Type Node_Type
The arc class type.
Graph_Node< Node_Info > Node
The graph type.
Graph_Arc< Arc_Info > Arc
The node class type.
Forward declaration used by CRTP helpers before the full node definition.
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
Tree_Node * get_parent() const noexcept
Returns the parent of this.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
void for_each_node(Operation &operation) const
Unconditionally traverse all the nodes of graph and on each one perform an operation.
constexpr size_t vsize() const noexcept
See get_num_nodes().
Concept constraining a policy for Mo's algorithm.
Concept constraining a graph type usable for Mo on Trees.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
void introsort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using introsort (introspective sort).
Policy: count distinct elements in a range.
Open addressing hash map using linear probing.
Data & find(const Key &key)
Find and return the value for a key.
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Search for a key in the map.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair (copy semantics).
A query for Mo's algorithm: inclusive range [l, r] with id.
size_t l
Left endpoint (inclusive, 0-based).
size_t r
Right endpoint (inclusive, 0-based).
Policy: "powerful array" sum = sum(cnt[x]^2 * x).
Policy: range mode (most frequent element).
Dynamic array container with automatic resizing.
Dynamic stack implementation based on linked lists.
Alias for htlist.H (DynList implementation).
Dynamic map with open hashing.
Mo's algorithm for offline range queries.
Comprehensive sorting algorithms and search utilities for Aleph-w.
General tree (n-ary tree) node.