ArgMax Monoid
(monoid/arg_max.hpp)
- View this file on GitHub
- Last update: 2026-07-21 20:17:47+09:00
- Include:
#include "monoid/arg_max.hpp"
Overview
A monoid for finding both the maximum value and its relative order in a range. If there are multiple maximum values, it returns the earliest one.
This is defined as a type alias of ArgMin using std::greater. For the minimum counterpart, see monoid/arg_min.hpp.
Example
#include "ds/segtree/segtree.hpp"
#include "monoid/arg_max.hpp"
#include <iostream>
#include <vector>
using ArgMaxM = m1une::monoid::ArgMax<long long>;
int main() {
std::vector<long long> A = {4, 8, 5, 8, 2};
m1une::ds::Segtree<ArgMaxM> seg(A);
auto res = seg.prod(0, A.size());
std::cout << "Max Value: " << res.value << "\n"; // Output: 8
std::cout << "Order: " << res.ord << "\n"; // Output: 1 (Order 1 is chosen over order 3)
return 0;
}
Interface and Complexity
This is a stateless algebra tag. Generic data structures use its public
value_type, id(), and op(a, b) members. If the type also provides helpers
such as make(...) or inv(x), they are described above or in the documented
properties.
Each static operation runs in the cost of the underlying operation shown in the
properties. Scalar monoids are $O(1)$; monoids whose value_type stores several
items, permutations, or matrices scale with that stored size.
Depends on
Verified with
Code
#ifndef M1UNE_MONOID_ARG_MAX_HPP
#define M1UNE_MONOID_ARG_MAX_HPP 1
#include <functional>
#include <limits>
#include "arg_min.hpp"
namespace m1une {
namespace monoid {
// Monoid for finding the maximum value and its corresponding index.
// Defined as a type alias of ArgMin using std::greater.
template <typename T, T Id = std::numeric_limits<T>::lowest()>
using ArgMax = ArgMin<T, Id, std::greater<T>>;
} // namespace monoid
} // namespace m1une
#endif // M1UNE_MONOID_ARG_MAX_HPP#line 1 "monoid/arg_max.hpp"
#include <functional>
#include <limits>
#line 1 "monoid/arg_min.hpp"
#line 6 "monoid/arg_min.hpp"
namespace m1une {
namespace monoid {
template <typename T>
struct ArgMinNode {
T value;
long long size;
long long ord;
};
// Monoid for finding the optimal value (minimum by default) and its relative order.
// Ties are broken by choosing the earlier element.
template <typename T, T Id = std::numeric_limits<T>::max(), typename Compare = std::less<T>>
struct ArgMin {
using value_type = ArgMinNode<T>;
static constexpr bool commutative = false;
static constexpr value_type id() {
return {Id, 0, -1};
}
static constexpr value_type op(const value_type& a, const value_type& b) {
if (a.size == 0) return b;
if (b.size == 0) return a;
long long size = a.size + b.size;
if (Compare()(a.value, b.value)) return {a.value, size, a.ord};
if (Compare()(b.value, a.value)) return {b.value, size, b.ord + a.size};
return {a.value, size, a.ord};
}
static constexpr value_type make(const T& val) {
return {val, 1, 0};
}
};
} // namespace monoid
} // namespace m1une
#line 8 "monoid/arg_max.hpp"
namespace m1une {
namespace monoid {
// Monoid for finding the maximum value and its corresponding index.
// Defined as a type alias of ArgMin using std::greater.
template <typename T, T Id = std::numeric_limits<T>::lowest()>
using ArgMax = ArgMin<T, Id, std::greater<T>>;
} // namespace monoid
} // namespace m1une