98 return sum + values(u);
109 int brute_subtree_sum(
const Array<int> & values,
114 for (
size_t v = 0; v < hld.
size(); ++v)
164 const std::array<const char *, 9> labels = {
165 "Central",
"North",
"West",
"South",
"N-A",
166 "N-B",
"S-A",
"S-A1",
"S-A2"};
172 for (
size_t i = 0; i < hld.
size(); ++i)
173 values(i) = hld.
node_of(i)->get_info();
175 std::cout <<
"=== Aurora Power Grid (HLD path queries) ===\n\n";
176 std::cout <<
"Node mapping (id -> label, base position):\n";
177 for (
size_t i = 0; i < hld.
size(); ++i)
178 std::cout << std::format(
" id={} {:<8} pos={}\n",
181 std::cout <<
"\nPath maintenance cost queries:\n";
190 const std::array<Query, 4> queries = {{
191 {na, nb,
"N-A -> N-B"},
192 {na, sa2,
"N-A -> S-A2"},
193 {west, sa1,
"West -> S-A1"},
194 {central, sa2,
"Central -> S-A2"}
197 for (
const auto & q : queries)
199 const size_t u = hld.
id_of(q.u);
200 const size_t v = hld.
id_of(q.v);
203 const int brute = brute_path_sum(values, hld, u, v);
205 std::cout << std::format(
" {:<18} HLD={:>3} brute={:>3}\n",
207 assert(ans == brute);
210 std::cout <<
"\nSubtree (service area) totals:\n";
211 for (
auto * x : {central, north, south, sa})
213 const size_t id = hld.
id_of(x);
215 const int brute = brute_subtree_sum(values, hld,
id);
216 std::cout << std::format(
" {:<8} subtree total = {:>3}\n",
218 assert(ans == brute);
221 std::cout <<
"\nBudget changes (point updates):\n";
222 std::cout <<
" +5 to S-A2, set West to 26\n";
224 const size_t id_sa2 = hld.
id_of(sa2);
225 const size_t id_west = hld.
id_of(west);
231 values(id_west) = 26;
233 const int after_path = grid.
query_path(north, sa2);
234 const int brute_after_path = brute_path_sum(values, hld, hld.
id_of(north), hld.
id_of(sa2));
235 std::cout << std::format(
" North -> S-A2 after update: HLD={} brute={}\n",
236 after_path, brute_after_path);
237 assert(after_path == brute_after_path);
239 std::cout <<
"\nHow HLD splits path N-A -> S-A2 into base-array segments:\n";
245 std::cout << std::format(
" segment [{}, {}] ({})\n",
246 l,
r, (reversed ?
"reversed" :
"forward"));
249 std::cout <<
"\nAll assertions passed.\n";
Heavy-Light and Centroid decomposition on rooted trees represented as Aleph graphs.
WeightedDigraph::Node Node
Simple dynamic array with automatic resizing and functional operations.
static Array create(const size_t n)
Create an array with n logical elements.
Segment-tree-powered path/subtree queries over an HLD layout.
T query_path(const Node *u, const Node *v) const
Path query between two nodes in O(log^2 n).
const Gen_Heavy_Light_Decomposition< GT, SA > & decomposition() const noexcept
T query_path_id(const size_t u, const size_t v) const
Path query between two node IDs in O(log^2 n).
T query_subtree_id(const size_t id) const
Subtree query for node ID in O(log n).
void update_node_id(const size_t id, const T &delta)
Point update: a[id] = Op(a[id], delta).
void set_node_id(const size_t id, const T &value)
Point assignment: a[id] = value.
Heavy-Light Decomposition over a rooted tree represented as an Aleph graph.
size_t parent_id(const size_t id) const
size_t position_of_id(const size_t id) const
size_t depth_of_id(const size_t id) const
size_t id_of(const Node *node) const
Node * node_of(const size_t id) const
bool is_ancestor_id(const size_t u, const size_t v) const
True iff u is ancestor of v in the rooted tree.
void for_each_path_segment(const Node *u, const Node *v, F &&visit) const
Enumerate path segments from u to v in path order.
size_t size() const noexcept
Graph implemented with double-linked adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
_Graph_Node Node
The graph type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
int main()
Executable example demonstrating heavy-light decomposition on a small tree.
Main namespace for Aleph-w library functions.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Arc of graph implemented with double-linked adjacency lists.
Generic graph and digraph implementations.