73 printf(
"=== SCENARIO 1: Biodiversity Survey ===\n\n");
74 printf(
"A wildlife camera trap records species IDs over 12 hours.\n");
75 printf(
"We want to know how many distinct species appeared in each\n");
76 printf(
"time window.\n\n");
81 {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8};
84 int data[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8};
85 for (
int i = 0; i < 12; ++i)
98 printf(
"%-20s %s\n",
"Time Window",
"Distinct Species");
99 printf(
"%-20s %s\n",
"-------------------",
"----------------");
100 printf(
"%-20s %zu\n",
"[h0, h3] morning",
ans(0));
101 printf(
"%-20s %zu\n",
"[h4, h8] midday",
ans(1));
102 printf(
"%-20s %zu\n",
"[h0, h11] full day",
ans(2));
103 printf(
"%-20s %zu\n",
"[h2, h5] late morn",
ans(3));
104 printf(
"%-20s %zu\n",
"[h9, h11] evening",
ans(4));
109 for (
size_t i =
l; i <=
r; ++i)
119 printf(
"\nAll assertions passed!\n\n");
128 printf(
"=== SCENARIO 2: Powerful Array ===\n\n");
129 printf(
"Given array a[], for a range [l,r] compute:\n");
130 printf(
" sum over distinct x in a[l..r] of cnt(x)^2 * x\n\n");
135 printf(
"Array: [1, 2, 1, 1, 2, 3]\n\n");
137 auto ans =
mo.solve({
145 printf(
"%-15s %s\n",
"Range",
"Power");
146 printf(
"%-15s %s\n",
"-----------",
"-----");
159 printf(
"\nAll assertions passed!\n\n");
168 printf(
"=== SCENARIO 3: Election Polling ===\n\n");
169 printf(
"Voters report their preferred candidate (1-4) over time.\n");
170 printf(
"Find the most popular candidate in each polling window.\n\n");
177 int data[] = {2, 3, 2, 1, 2, 3, 3, 1, 4, 3};
178 for (
int i = 0; i < 10; ++i)
182 auto ans =
mo.solve({
189 printf(
"%-20s %-10s %s\n",
"Window",
"Freq",
"Candidate");
190 printf(
"%-20s %-10s %s\n",
"-------------------",
"----",
"---------");
192 const char * labels[] = {
"[v0,v9] all",
"[v0,v4] first",
193 "[v5,v9] second",
"[v2,v6] middle"};
194 for (
size_t i = 0; i < 4; ++i)
195 printf(
"%-20s %-10zu %d\n", labels[i],
ans(i).first,
ans(i).second);
200 for (
size_t i =
l; i <=
r; ++i)
205 mx = std::max(
mx, it.get_curr().second);
214 printf(
"\nAll assertions passed!\n\n");
232 long long x = data(idx);
238 long long x = data(idx);
247 printf(
"=== SCENARIO 4: Custom Policy — Network Packet Analysis ===\n\n");
248 printf(
"Packet sizes (bytes) captured over 10 time slots.\n");
249 printf(
"Query: sum of squared sizes in each window (for variance\n");
250 printf(
"analysis / anomaly detection).\n\n");
254 {15, 8, 22, 3, 17, 11, 9, 25, 6, 14};
256 int data[] = {15, 8, 22, 3, 17, 11, 9, 25, 6, 14};
259 for (
int i = 0; i < 10; ++i)
263 auto ans =
mo.solve({
270 printf(
"%-20s %s\n",
"Window",
"Sum of Squares");
271 printf(
"%-20s %s\n",
"-------------------",
"--------------");
273 const char * labels[] = {
"[0,9] all",
"[0,4] first",
274 "[5,9] second",
"[2,7] burst"};
275 for (
size_t i = 0; i < 4; ++i)
276 printf(
"%-20s %lld\n", labels[i],
ans(i));
279 auto brute_ssq = [&](
size_t l,
size_t r) ->
long long {
281 for (
size_t i =
l; i <=
r; ++i)
282 s += (
long long)data[i] * data[i];
291 printf(
"\nAll assertions passed!\n\n");
300 printf(
"Mo's Algorithm — Offline Range Queries\n");
301 printf(
"======================================\n\n");
308 printf(
"All scenarios completed successfully.\n");
Simple dynamic array with automatic resizing and functional operations.
Self-adjusting dynamic hash table.
Key * insert(const Key &key)
Inserts key into the hash set.
Offline range query engine using Mo's algorithm.
static void biodiversity_survey()
static void network_packet_analysis()
static void election_polling()
static void powerful_array()
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.
Open addressing hash map using linear probing.
Custom Mo policy: sum of squared packet sizes in a window.
answer_type answer() const
void init(const Array< int > &, size_t)
void add(const Array< int > &data, size_t idx)
void remove(const Array< int > &data, size_t idx)
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
Mo's algorithm for offline range queries.