64# ifndef TPL_MO_ALGORITHM_H
65# define TPL_MO_ALGORITHM_H
70# include <type_traits>
103 template <
typename P,
typename T>
105 size_t idx,
size_t n)
107 typename P::answer_type;
109 { p.add(data, idx) };
110 { p.remove(data, idx) };
111 { p.answer() } -> std::convertible_to<typename P::answer_type>;
140 template <
typename T,
class Policy>
211 for (
auto it = lst.
get_it(); it.has_curr(); it.next_ne())
212 data_(i++) = it.get_curr();
219 std::is_nothrow_move_constructible_v<Policy>) = default;
225 std::is_nothrow_move_assignable_v<Policy>) = default;
228 [[nodiscard]] constexpr
size_t size() const noexcept
234 [[nodiscard]]
constexpr bool is_empty() const noexcept
236 return data_.size() == 0;
241 noexcept(std::swap(std::declval<Policy &>(), std::declval<Policy &>())))
243 data_.swap(other.data_);
244 std::swap(
pol_, other.pol_);
269 for (
size_t i = 0; i < ranges.size(); ++i)
270 queries(i) = {ranges(i).first, ranges(i).second, i};
271 return solve(queries);
293 return solve(ranges);
320 const size_t n =
data_.size();
321 const size_t q = queries.
size();
327 for (
size_t i = 0; i < q; ++i)
330 <<
"Gen_Mo_Algorithm::solve: query " << i
331 <<
" r=" << queries(i).r <<
" >= n=" << n;
333 <<
"Gen_Mo_Algorithm::solve: query " << i
334 <<
" l=" << queries(i).l <<
" > r=" << queries(i).r;
336 <<
"Gen_Mo_Algorithm::solve: query " << i
337 <<
" id=" << queries(i).id <<
" >= q=" << q;
340 const size_t block = std::max<size_t>(1,
static_cast<size_t>(std::sqrt(
static_cast<double>(n))));
346 const size_t ba = a.
l / block;
347 const size_t bb = b.
l / block;
351 return (ba & 1) ? (a.
r > b.
r) : (a.
r < b.
r);
359 size_t cur_l = queries(0).l;
360 size_t cur_r = queries(0).l;
364 while (cur_r < queries(0).
r)
367 answers(queries(0).
id) =
pol_.answer();
370 for (
size_t i = 1; i < q; ++i)
372 const size_t ql = queries(i).l;
373 const size_t qr = queries(i).r;
391 answers(queries(i).
id) =
pol_.answer();
411 template <
typename T>
428 if (++
freq[data(idx)] == 1)
434 if (--
freq[data(idx)] == 0)
452 template <
typename T>
469 const auto x =
static_cast<long long>(data(idx));
470 sum += (2 *
cnt[data(idx)] + 1) * x;
476 const auto x =
static_cast<long long>(data(idx));
478 sum -= (2 *
cnt[data(idx)] + 1) * x;
498 template <
typename T>
520 const T & x = data(idx);
521 const size_t old_f =
freq[x];
535 const T & x = data(idx);
536 const size_t old_f =
freq[x];
549 it.has_curr(); it.next_ne())
550 if (it.get_curr().second ==
max_freq)
562 it.has_curr(); it.next_ne())
563 if (it.get_curr().second ==
max_freq)
582 template <
typename T>
589 template <
typename T>
596 template <
typename T>
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
static Array create(const size_t n)
Create an array with n logical elements.
Doubly-linked list (defined in tpl_dynList.H).
Offline range query engine using Mo's algorithm.
Array< answer_type > solve(Array< Mo_Query > queries) const
Core solver: answer all Mo_Query objects.
Array< answer_type > solve(const std::initializer_list< std::pair< size_t, size_t > > il) const
Solve queries given as (l, r) pairs (initializer list).
Gen_Mo_Algorithm(Gen_Mo_Algorithm &&) noexcept(std::is_nothrow_move_constructible_v< Array< T > > and std::is_nothrow_move_constructible_v< Policy >)=default
Gen_Mo_Algorithm(Array< T > and arr, Policy p=Policy())
Construct from an Array<T> (move).
Gen_Mo_Algorithm(const DynList< T > &lst, Policy p=Policy())
Construct from a DynList<T>.
constexpr bool is_empty() const noexcept
True if the data array is empty.
Array< answer_type > solve(const Array< std::pair< size_t, size_t > > &ranges) const
Solve queries given as (l, r) pairs.
void swap(Gen_Mo_Algorithm &other) noexcept(noexcept(std::swap(std::declval< Policy & >(), std::declval< Policy & >())))
Swap with other in O(1).
Gen_Mo_Algorithm(std::initializer_list< T > il, Policy p=Policy())
Construct from an initializer list.
constexpr size_t size() const noexcept
Number of elements in the data array.
typename Policy::answer_type answer_type
Gen_Mo_Algorithm(const Array< T > &arr, Policy p=Policy())
Construct from an Array<T>.
Gen_Mo_Algorithm(const Gen_Mo_Algorithm &)=default
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Concept constraining a policy for Mo's algorithm.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
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.
answer_type answer() const
void add(const Array< T > &data, size_t idx)
void remove(const Array< T > &data, size_t idx)
MapOLhash< T, size_t > freq
void init(const Array< T > &, size_t)
Open addressing hash map using linear probing.
typename Base::Iterator Iterator
Iterator type for traversing the map.
A query for Mo's algorithm: inclusive range [l, r] with id.
size_t id
Original query index (for reordering answers).
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).
void init(const Array< T > &, size_t)
void remove(const Array< T > &data, size_t idx)
void add(const Array< T > &data, size_t idx)
MapOLhash< T, long long > cnt
answer_type answer() const
Policy: range mode (most frequent element).
void remove(const Array< T > &data, size_t idx)
std::pair< size_t, T > answer_type
MapOLhash< T, size_t > freq
void init(const Array< T > &, size_t)
answer_type answer() const
void add(const Array< T > &data, size_t idx)
MapOLhash< size_t, size_t > cnt_of_cnt
Dynamic array container with automatic resizing.
Alias for htlist.H (DynList implementation).
Dynamic map with open hashing.
Comprehensive sorting algorithms and search utilities for Aleph-w.