71 std::cout <<
"=== SCENARIO 1: El Arbol del Tiempo ===\n\n";
72 std::cout <<
"A historian tracks the oldest artifact per century.\n";
73 std::cout <<
"Index i represents century (i+1).\n\n";
90 std::cout <<
"Initial oldest dates per century (negative = BC):\n";
91 for (
size_t i = 0; i <
timeline.size(); ++i)
92 std::cout <<
" Century " << (i + 1) <<
": " <<
timeline.
get(i) <<
"\n";
94 std::cout <<
"\nOldest artifact in centuries 3-7: "
96 std::cout <<
"Oldest artifact in centuries 1-5: "
100 std::cout <<
"\nNew discovery: artifact from -2000 found in century 5!\n";
103 std::cout <<
"Oldest artifact in centuries 3-7 (updated): "
111 std::cout <<
"=== SCENARIO 2: Ajustes Salariales ===\n\n";
112 std::cout <<
"HR system: 8 departments, salary adjustments and payroll queries.\n\n";
117 std::cout <<
"Initial payroll per department (in $K):\n";
118 for (
size_t i = 0; i <
payroll.size(); ++i)
119 std::cout <<
" Dept " << i <<
": $" <<
payroll.
get(i) <<
"K\n";
121 std::cout <<
"\nTotal payroll (all depts): $"
122 <<
payroll.query(0, 7) <<
"K\n";
125 std::cout <<
"\nCorporate raises: departments 2-5 get +$10K\n";
128 std::cout <<
"Total payroll (all depts): $"
129 <<
payroll.query(0, 7) <<
"K\n";
130 std::cout <<
"Payroll for depts 2-5: $"
131 <<
payroll.query(2, 5) <<
"K\n";
134 std::cout <<
"\nAnother raise: departments 0-3 get +$5K\n";
137 std::cout <<
"Updated payroll per department:\n";
138 for (
size_t i = 0; i <
payroll.size(); ++i)
139 std::cout <<
" Dept " << i <<
": $" <<
payroll.get(i) <<
"K\n";
141 std::cout <<
"Total payroll: $" <<
payroll.query(0, 7) <<
"K\n";
148 std::cout <<
"=== SCENARIO 3: Balanceo de Servidores (Beats) ===\n\n";
149 std::cout <<
"Data center: 8 racks with varying CPU load (%).\n\n";
153 std::cout <<
"Initial loads per rack:\n";
154 for (
size_t i = 0; i <
loads.size(); ++i)
155 std::cout <<
" Rack " << i <<
": " <<
loads.
get(i) <<
"%\n";
157 std::cout <<
"\nTotal load: " <<
loads.query_sum(0, 7) <<
"%\n";
158 std::cout <<
"Max load: " <<
loads.query_max(0, 7) <<
"%\n";
159 std::cout <<
"Min load: " <<
loads.query_min(0, 7) <<
"%\n";
162 std::cout <<
"\nOperations: cap all loads at 80% (chmin)\n";
163 loads.chmin(0, 7, 80);
165 std::cout <<
"After capping:\n";
166 for (
size_t i = 0; i <
loads.size(); ++i)
167 std::cout <<
" Rack " << i <<
": " <<
loads.get(i) <<
"%\n";
168 std::cout <<
"Total load: " <<
loads.query_sum(0, 7) <<
"%\n";
169 std::cout <<
"Max load: " <<
loads.query_max(0, 7) <<
"%\n";
172 std::cout <<
"\nOperations: set minimum floor at 50% for racks 0-3 (chmax)\n";
173 loads.chmax(0, 3, 50);
175 std::cout <<
"After floor:\n";
176 for (
size_t i = 0; i <
loads.size(); ++i)
177 std::cout <<
" Rack " << i <<
": " <<
loads.get(i) <<
"%\n";
178 std::cout <<
"Total load: " <<
loads.query_sum(0, 7) <<
"%\n";
185 std::cout <<
"=== SCENARIO 4: RMQ dinamico vs estatico ===\n\n";
187 const std::vector<int> data = {9, 4, 7, 1, 8, 2, 6, 3, 5, 0};
195 std::cout <<
"Data: {9, 4, 7, 1, 8, 2, 6, 3, 5, 0}\n\n";
197 std::cout <<
"Query | Sparse Table (O(1)) | Segment Tree (O(lg n))\n";
198 std::cout <<
"----------|--------------------|-----------------------\n";
201 std::cout <<
"[" <<
l <<
", " <<
r <<
"] | "
202 << std::setw(19) <<
sparse.query(
l,
r) <<
" | "
203 << std::setw(21) <<
segtree.query(
l,
r) <<
"\n";
210 std::cout <<
"\nNow update a[3] = 100 (only Segment Tree supports this):\n";
213 std::cout <<
"Segment Tree min[0, 9] after update: "
214 <<
segtree.query(0, 9) <<
"\n";
215 std::cout <<
"Sparse Table min[0, 9] (unchanged): "
216 <<
sparse.query(0, 9) <<
"\n";
218 std::cout <<
"\nWhen to use each:\n";
219 std::cout <<
" Sparse Table: static data, many queries, O(1) per query\n";
220 std::cout <<
" Segment Tree: dynamic data with updates, O(lg n) per query\n";
221 std::cout <<
" Fenwick Tree: dynamic data, invertible operation (e.g. sum)\n";
value_type get(const size_t i) const
Retrieve the value a[i].
T get(const size_t i) const
Retrieve the value a[i].
Ji Driver's Segment Tree (Segment Tree Beats).
T get(const size_t i) const
Retrieve the value at position i.
Main namespace for Aleph-w library functions.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
static void scenario_2_ajustes_salariales()
static void scenario_1_el_arbol_del_tiempo()
static void scenario_3_balanceo_servidores()
static void scenario_4_rmq_comparison()
Lazy segment tree for range add + range sum.
Min Segment Tree for range minimum queries with point updates.
Sparse Table for range minimum queries.
Segment trees for dynamic range queries with lazy propagation.
Sparse Table for static range queries in O(1).