This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include "../../../template/template.cpp"
#include "../../../data-structure/monoid/add.hpp"
#include "../../../data-structure/monoid/affine.hpp"
#include "../../../data-structure/monoid/and.hpp"
#include "../../../data-structure/monoid/or.hpp"
#include "../../../data-structure/monoid/gcd.hpp"
#include "../../../data-structure/monoid/lcm.hpp"
#include "../../../data-structure/monoid/min.hpp"
#include "../../../data-structure/monoid/max.hpp"
#include "../../../data-structure/monoid/min_index.hpp"
#include "../../../data-structure/monoid/max_index.hpp"
#include "../../../data-structure/monoid/sum_count.hpp"
#include "../../../data-structure/monoid/rolling_hash.hpp"
#include "../../../data-structure/monoid/kadane.hpp"
template<class M> void identity(typename M::value_type x) {
assert(M::op(M::e(), x) == x && M::op(x, M::e()) == x);
}
int main() {
for(int a = 0; a <= 20; ++a) for(int b = 0; b <= 20; ++b) {
assert(MonoidAdd<int>::op(a, b) == a + b);
assert(MonoidAND<unsigned>::op(a, b) == unsigned(a & b));
assert(MonoidOR<unsigned>::op(a, b) == unsigned(a | b));
assert(MonoidMin<int>::op(a, b) == min(a, b));
assert(MonoidMax<int>::op(a, b) == max(a, b));
int gcd_expected = 0;
for(int d = 1; d <= max(a, b); ++d) if(a % d == 0 && b % d == 0) gcd_expected = d;
assert(MonoidGCD<int>::op(a, b) == gcd_expected);
int lcm_expected = 0;
if(a && b) { lcm_expected = 1; while(lcm_expected % a || lcm_expected % b) ++lcm_expected; }
assert(MonoidLCM<int>::op(a, b) == lcm_expected);
identity<MonoidAdd<int>>(a); identity<MonoidAND<unsigned>>(a);
identity<MonoidOR<unsigned>>(a); identity<MonoidGCD<int>>(a);
identity<MonoidLCM<int>>(a); identity<MonoidMin<int>>(a); identity<MonoidMax<int>>(a);
pair<int, int> x{a, 3}, y{b, 5};
identity<MonoidMinWithIndex<int>>(x); identity<MonoidMaxWithIndex<int>>(x);
assert(MonoidMinWithIndex<int>::op(x, y) == min(x, y));
assert(MonoidMaxWithIndex<int>::op(x, y) == max(x, y));
using Count = MonoidSumCount<ll, int>;
Count::S c; c = Count::S(a, b);
auto combined = Count::op(c, Count::S(b, a));
assert(combined.sum == a + b && combined.cnt == a + b);
assert(Count::op(c, Count::e()).sum == a && Count::op(Count::e(), c).cnt == b);
}
assert((MonoidMin<int, 100>::e() == 100));
assert((MonoidMax<int, -100>::e() == -100));
assert((MonoidMinWithIndex<int, 100>::e() == make_pair(100, -1)));
assert((MonoidMaxWithIndex<int, -100>::e() == make_pair(-100, -1)));
for(ll a = -3; a <= 3; ++a) for(ll b = -3; b <= 3; ++b) {
pair<ll, ll> f{a, b}, g{2, -1};
identity<MonoidAffine<ll>>(f); identity<MonoidAffine<ll, true>>(f);
auto left = MonoidAffine<ll>::op(f, g);
auto right = MonoidAffine<ll, true>::op(f, g);
for(ll x = -3; x <= 3; ++x) {
assert(left.first * x + left.second == a * (2 * x - 1) + b);
assert(right.first * x + right.second == 2 * (a * x + b) - 1);
}
}
using Hash = MonoidRollingHash<64>;
Hash::S h; h = Hash::e();
auto base = Hash::op(Hash::S(0, 1), Hash::S(1, 1)).h;
// Repeated calls must reuse the same base and powers in C++17.
assert(Hash::op(Hash::S(0, 1), Hash::S(1, 1)).h == base);
Hash::mint expected = 0, power = 1;
for(int i = 1; i <= 30; ++i) {
h = Hash::op(h, Hash::S(i, 1)); expected += power * i; power *= base;
assert(h.h == expected && h.len == i);
assert(Hash::op(Hash::e(), h).h == h.h && Hash::op(h, Hash::e()).h == h.h);
}
using K = MonoidKadane<ll>;
vector<ll> a{-7, 2, 3, -8, 9, -1};
for(int l = 0; l < int(a.size()); ++l) for(int r = l + 1; r <= int(a.size()); ++r) {
K::S x; x = K::e();
ll sum = 0, prefix = LLONG_MIN, suffix = LLONG_MIN, best = LLONG_MIN;
for(int i = l; i < r; ++i) {
x = K::op(x, K::S(a[i], a[i], a[i], a[i]));
sum += a[i]; prefix = max(prefix, sum);
ll part = 0;
for(int j = i; j < r; ++j) { part += a[j]; best = max(best, part); }
suffix = max(suffix, part);
}
assert(x.sum == sum && x.lmax == prefix && x.rmax == suffix && x.allmax == best);
}
INT(a0, b0); print(a0 + b0);
}#line 1 "test/mytest/data-structure/monoids.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 1 "template/template.cpp"
// #region Macros
#include <bits/stdc++.h>
using namespace std;
// input output utils
namespace siro53_io {
// https://maspypy.github.io/library/other/io_old.hpp
struct has_val_impl {
template <class T>
static auto check(T &&x) -> decltype(x.val(), std::true_type{});
template <class T> static auto check(...) -> std::false_type;
};
template <class T>
class has_val : public decltype(has_val_impl::check<T>(std::declval<T>())) {
};
// debug
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void dump(const T t) {
cerr << t;
}
template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
void dump(const T t) {
cerr << t;
}
template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
void dump(const T &t) {
cerr << t.val();
}
void dump(__int128_t n) {
if(n == 0) {
cerr << '0';
return;
} else if(n < 0) {
cerr << '-';
n = -n;
}
string s;
while(n > 0) {
s += (char)('0' + n % 10);
n /= 10;
}
reverse(s.begin(), s.end());
cerr << s;
}
void dump(const string &s) { cerr << s; }
void dump(const char *s) {
int n = (int)strlen(s);
for(int i = 0; i < n; i++) cerr << s[i];
}
template <class T1, class T2> void dump(const pair<T1, T2> &p) {
cerr << '(';
dump(p.first);
cerr << ',';
dump(p.second);
cerr << ')';
}
template <class T> void dump(const vector<T> &v) {
cerr << '{';
for(int i = 0; i < (int)v.size(); i++) {
dump(v[i]);
if(i < (int)v.size() - 1) cerr << ',';
}
cerr << '}';
}
template <class T> void dump(const set<T> &s) {
cerr << '{';
for(auto it = s.begin(); it != s.end(); it++) {
dump(*it);
if(next(it) != s.end()) cerr << ',';
}
cerr << '}';
}
template <class Key, class Value> void dump(const map<Key, Value> &mp) {
cerr << '{';
for(auto it = mp.begin(); it != mp.end(); it++) {
dump(*it);
if(next(it) != mp.end()) cerr << ',';
}
cerr << '}';
}
template <class Key, class Value>
void dump(const unordered_map<Key, Value> &mp) {
cerr << '{';
for(auto it = mp.begin(); it != mp.end(); it++) {
dump(*it);
if(next(it) != mp.end()) cerr << ',';
}
cerr << '}';
}
template <class T> void dump(const deque<T> &v) {
cerr << '{';
for(int i = 0; i < (int)v.size(); i++) {
dump(v[i]);
if(i < (int)v.size() - 1) cerr << ',';
}
cerr << '}';
}
template <class T> void dump(queue<T> q) {
cerr << '{';
while(!q.empty()) {
dump(q.front());
if((int)q.size() > 1) cerr << ',';
q.pop();
}
cerr << '}';
}
void debug_print() { cerr << endl; }
template <class Head, class... Tail>
void debug_print(const Head &h, const Tail &...t) {
dump(h);
if(sizeof...(Tail)) dump(' ');
debug_print(t...);
}
// print
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void print_single(const T t) {
cout << t;
}
template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
void print_single(const T t) {
cout << t;
}
template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
void print_single(const T t) {
cout << t.val();
}
void print_single(__int128_t n) {
if(n == 0) {
cout << '0';
return;
} else if(n < 0) {
cout << '-';
n = -n;
}
string s;
while(n > 0) {
s += (char)('0' + n % 10);
n /= 10;
}
reverse(s.begin(), s.end());
cout << s;
}
void print_single(const string &s) { cout << s; }
void print_single(const char *s) {
int n = (int)strlen(s);
for(int i = 0; i < n; i++) cout << s[i];
}
template <class T1, class T2> void print_single(const pair<T1, T2> &p) {
print_single(p.first);
cout << ' ';
print_single(p.second);
}
template <class T> void print_single(const vector<T> &v) {
for(int i = 0; i < (int)v.size(); i++) {
print_single(v[i]);
if(i < (int)v.size() - 1) cout << ' ';
}
}
template <class T> void print_single(const set<T> &s) {
for(auto it = s.begin(); it != s.end(); it++) {
print_single(*it);
if(next(it) != s.end()) cout << ' ';
}
}
template <class T> void print_single(const deque<T> &v) {
for(int i = 0; i < (int)v.size(); i++) {
print_single(v[i]);
if(i < (int)v.size() - 1) cout << ' ';
}
}
template <class T> void print_single(queue<T> q) {
while(!q.empty()) {
print_single(q.front());
if((int)q.size() > 1) cout << ' ';
q.pop();
}
}
void print() { cout << '\n'; }
template <class Head, class... Tail>
void print(const Head &h, const Tail &...t) {
print_single(h);
if(sizeof...(Tail)) print_single(' ');
print(t...);
}
// input
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void input_single(T &t) {
cin >> t;
}
template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
void input_single(T &t) {
cin >> t;
}
template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
void input_single(T &t) {
cin >> t;
}
void input_single(__int128_t &n) {
string s;
cin >> s;
if(s == "0") {
n = 0;
return;
}
bool is_minus = false;
if(s[0] == '-') {
s = s.substr(1);
is_minus = true;
}
n = 0;
for(int i = 0; i < (int)s.size(); i++) n = n * 10 + (int)(s[i] - '0');
if(is_minus) n = -n;
}
void input_single(string &s) { cin >> s; }
template <class T1, class T2> void input_single(pair<T1, T2> &p) {
input_single(p.first);
input_single(p.second);
}
template <class T> void input_single(vector<T> &v) {
for(auto &e : v) input_single(e);
}
void input() {}
template <class Head, class... Tail> void input(Head &h, Tail &...t) {
input_single(h);
input(t...);
}
}; // namespace siro53_io
#ifdef DEBUG
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debug_print(__VA_ARGS__)
#else
#define debug(...) (void(0))
#endif
// io setup
struct Setup {
Setup() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} __Setup;
using namespace siro53_io;
// types
using ll = long long;
using i128 = __int128_t;
// input macros
#define INT(...) \
int __VA_ARGS__; \
input(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
input(__VA_ARGS__)
#define STRING(...) \
string __VA_ARGS__; \
input(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
input(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
input(__VA_ARGS__)
#define LD(...) \
long double __VA_ARGS__; \
input(__VA_ARGS__)
#define UINT(...) \
unsigned int __VA_ARGS__; \
input(__VA_ARGS__)
#define ULL(...) \
unsigned long long __VA_ARGS__; \
input(__VA_ARGS__)
#define VEC(name, type, len) \
vector<type> name(len); \
input(name);
#define VEC2(name, type, len1, len2) \
vector name(len1, vector<type>(len2)); \
input(name);
// other macros
// https://trap.jp/post/1224/
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < int(n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(RALL(v))
#define UNIQUE(v) \
sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()), v.shrink_to_fit()
#define REV(v) reverse(ALL(v))
#define SZ(v) ((int)(v).size())
#define MIN(v) (*min_element(ALL(v)))
#define MAX(v) (*max_element(ALL(v)))
// util const
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// util functions
void Case(int i) { cout << "Case #" << i << ": "; }
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
template <class T> inline bool chmax(T &a, T b) {
return (a < b ? a = b, true : false);
}
template <class T> inline bool chmin(T &a, T b) {
return (a > b ? a = b, true : false);
}
template <class T, int dim>
auto make_vector_impl(vector<int>& sizes, const T &e) {
if constexpr(dim == 1) {
return vector(sizes[0], e);
} else {
int n = sizes[dim - 1];
sizes.pop_back();
return vector(n, make_vector_impl<T, dim - 1>(sizes, e));
}
}
template <class T, int dim>
auto make_vector(const int (&sizes)[dim], const T &e = T()) {
vector<int> s(dim);
for(int i = 0; i < dim; i++) s[i] = sizes[dim - i - 1];
return make_vector_impl<T, dim>(s, e);
}
vector<int> iota_gen(int n, int start = 0) {
vector<int> ord(n);
iota(ord.begin(), ord.end(), start);
return ord;
}
template<typename T>
vector<int> ord_sort(const vector<T>& v, bool greater = false) {
auto ord = iota_gen((int)v.size());
sort(ALL(ord), [&](int i, int j) {
if(greater) return v[i] > v[j];
return v[i] < v[j];
});
return ord;
}
// #endregion Macros
#line 2 "data-structure/monoid/add.hpp"
template <typename T> struct MonoidAdd {
using value_type = T;
inline static T op(const T &l, const T &r) { return (l + r); }
inline static T e() { return T(0); }
};
#line 2 "data-structure/monoid/affine.hpp"
#line 4 "data-structure/monoid/affine.hpp"
template <typename T, bool rev = false> struct MonoidAffine {
using S = std::pair<T, T>;
using value_type = S;
inline static S op(const S &l, const S &r) {
if(rev) return S(r.first * l.first, r.first * l.second + r.second);
return S(l.first * r.first, l.first * r.second + l.second);
}
inline static S e() { return S(T(1), T(0)); }
};
#line 2 "data-structure/monoid/and.hpp"
template <typename T> struct MonoidAND {
using value_type = T;
inline static T op(const T &l, const T &r) { return (l & r); }
inline static T e() { return T(~T(0)); }
};
#line 2 "data-structure/monoid/or.hpp"
template <typename T> struct MonoidOR {
using value_type = T;
inline static T op(const T &l, const T &r) { return (l | r); }
inline static T e() { return T(0); }
};
#line 2 "data-structure/monoid/gcd.hpp"
#line 4 "data-structure/monoid/gcd.hpp"
template <typename T> struct MonoidGCD {
using value_type = T;
inline static T op(const T& l, const T& r) {
return std::gcd(l, r);
}
inline static T e() { return T(0); }
};
#line 2 "data-structure/monoid/lcm.hpp"
#line 4 "data-structure/monoid/lcm.hpp"
template <typename T> struct MonoidLCM {
using value_type = T;
inline static T op(const T& l, const T& r) {
return std::lcm(l, r);
}
inline static T e() { return T(1); }
};
#line 2 "data-structure/monoid/min.hpp"
#line 5 "data-structure/monoid/min.hpp"
template <typename T, T INF = std::numeric_limits<T>::max()> struct MonoidMin {
using value_type = T;
inline static T op(const T &l, const T &r) { return std::min(l, r); }
inline static T e() { return INF; }
};
#line 2 "data-structure/monoid/max.hpp"
#line 5 "data-structure/monoid/max.hpp"
template <typename T, T MINUS_INF = std::numeric_limits<T>::min()> struct MonoidMax {
using value_type = T;
inline static T op(const T &l, const T &r) { return std::max(l, r); }
inline static T e() { return MINUS_INF; }
};
#line 2 "data-structure/monoid/min_index.hpp"
#line 6 "data-structure/monoid/min_index.hpp"
template <typename T, T INF = std::numeric_limits<T>::max()> struct MonoidMinWithIndex {
using S = std::pair<T, int>;
using value_type = S;
inline static S op(const S &l, const S &r) { return std::min(l, r); }
inline static S e() {
return std::make_pair(INF, -1);
}
};
#line 2 "data-structure/monoid/max_index.hpp"
#line 6 "data-structure/monoid/max_index.hpp"
template <typename T, T MINUS_INF = std::numeric_limits<T>::min()> struct MonoidMaxWithIndex {
using S = std::pair<T, int>;
using value_type = S;
inline static S op(const S &l, const S &r) { return std::max(l, r); }
inline static S e() {
return std::make_pair(MINUS_INF, -1);
}
};
#line 2 "data-structure/monoid/sum_count.hpp"
template <typename sum_t, typename count_t> struct MonoidSumCount {
struct S {
sum_t sum;
count_t cnt;
S() = default;
S(sum_t sum, count_t cnt): sum(sum), cnt(cnt) {}
};
using value_type = S;
inline static S op(const S& l, const S& r) {
return S(l.sum + r.sum, l.cnt + r.cnt);
}
inline static S e() { return S(sum_t(0), count_t(0)); }
};
#line 2 "data-structure/monoid/rolling_hash.hpp"
#line 5 "data-structure/monoid/rolling_hash.hpp"
#line 2 "string/rolling-hash.hpp"
#line 8 "string/rolling-hash.hpp"
#line 2 "modint/modint_2_61.hpp"
#line 5 "modint/modint_2_61.hpp"
// ローリングハッシュ用 modint
// https://qiita.com/keymoon/items/11fac5627672a6d6a9f6
class ModInt_2_61 {
public:
using M = ModInt_2_61;
ModInt_2_61() : x(0) {}
ModInt_2_61(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
unsigned long long val() const { return x; }
M &operator+=(const M &m) {
if((x += m.x) >= mod) x -= mod;
return *this;
}
M &operator-=(const M &m) {
if((x += mod - m.x) >= mod) x -= mod;
return *this;
}
M &operator*=(const M &m) {
__uint128_t t = (__uint128_t)x * m.x;
unsigned long long na = t >> 61;
unsigned long long nb = t & mod;
if((na += nb) >= mod) na -= mod;
x = na;
return *this;
}
M &operator/=(const M &m) {
*this *= m.inv();
return *this;
}
M operator-() const { return M(-(long long)x); }
M operator+(const M &m) const { return M(*this) += m; }
M operator-(const M &m) const { return M(*this) -= m; }
M operator*(const M &m) const { return M(*this) *= m; }
M operator/(const M &m) const { return M(*this) /= m; }
bool operator==(const M &m) const { return (x == m.x); }
bool operator!=(const M &m) const { return (x != m.x); }
M inv() const {
long long a = x, b = mod, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
std::swap(a -= t * b, b);
std::swap(u -= t * v, v);
}
return M(u);
}
M pow(unsigned long long n) const {
M ret(1), mul(x);
while(n > 0) {
if(n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend std::ostream &operator<<(std::ostream &os, const M &p) {
return os << p.x;
}
friend std::istream &operator>>(std::istream &is, M &a) {
long long t;
is >> t;
a = M(t);
return (is);
}
static constexpr unsigned long long get_mod() { return mod; }
private:
unsigned long long x;
static constexpr unsigned long long mod = (1LL << 61) - 1;
};
#line 10 "string/rolling-hash.hpp"
struct RollingHash {
using u64 = unsigned long long;
using mint = ModInt_2_61;
static constexpr u64 mod = (1ULL << 61) - 1;
const u64 base;
std::vector<mint> hashed, power;
explicit RollingHash(const std::vector<int> &v, u64 base) : base(base) {
int n = (int)v.size();
hashed.assign(n + 1, 0);
power.assign(n + 1, 0);
power[0] = 1;
for(int i = 0; i < n; i++) {
power[i + 1] = power[i] * base;
hashed[i + 1] = (hashed[i] * base) + v[i];
}
}
explicit RollingHash(const std::string &s, u64 base) : base(base) {
int n = (int)s.size();
hashed.assign(n + 1, 0);
power.assign(n + 1, 0);
power[0] = 1;
for(int i = 0; i < n; i++) {
power[i + 1] = power[i] * base;
hashed[i + 1] = (hashed[i] * base) + s[i];
}
}
static inline u64 gen_base() {
std::random_device seed_gen;
std::mt19937_64 engine(seed_gen());
std::uniform_int_distribution<u64> rand(2, mod - 2);
return rand(engine);
}
mint get(int l, int r) {
assert(0 <= l);
assert(l <= r);
assert(r < (int)power.size());
return (hashed[r] - (hashed[l] * power[r - l]));
}
mint connect(mint h1, mint h2, int h2len) {
return (h1 * power[h2len] + h2);
}
int get_lcp(RollingHash &b, int l1, int r1, int l2, int r2) {
assert(mod == b.mod);
int len = std::min(r1 - l1, r2 - l2);
int low = -1, high = len + 1;
while(high - low > 1) {
int mid = (low + high) >> 1;
if(get(l1, l1 + mid) == b.get(l2, l2 + mid)) {
low = mid;
} else {
high = mid;
}
}
return low;
}
};
#line 7 "data-structure/monoid/rolling_hash.hpp"
template <int N> struct MonoidRollingHash {
public:
using mint = ModInt_2_61;
struct S {
mint h;
int len;
S() {}
S(mint h, int len) : h(h), len(len) {}
};
using value_type = S;
static inline S op(const S &a, const S &b) {
static const auto parameters = gen();
const auto &powers = parameters.second;
mint h = a.h + b.h * powers[a.len];
int len = a.len + b.len;
return S(h, len);
}
static inline S e() { return S(0, 0); }
private:
static std::pair<mint, std::array<mint, N>> gen() {
mint base = RollingHash::gen_base();
std::array<mint, N> powers;
powers[0] = 1;
for(int i = 1; i < N; i++) powers[i] = powers[i - 1] * base;
return std::make_pair(base, powers);
}
};
#line 2 "data-structure/monoid/kadane.hpp"
#line 5 "data-structure/monoid/kadane.hpp"
// ref: https://hotman78.hatenablog.com/entry/2020/06/17/102519
template <typename T> struct MonoidKadane {
struct S {
T lmax, rmax, allmax, sum;
S() = default;
S(T lmax, T rmax, T allmax, T sum)
: lmax(lmax), rmax(rmax), allmax(allmax), sum(sum) {}
};
using value_type = S;
inline static S op(const S &l, const S &r) {
const T m = std::numeric_limits<T>::lowest();
if(l.lmax == m && l.rmax == m && l.allmax == m && l.sum == T(0)) return r;
if(r.lmax == m && r.rmax == m && r.allmax == m && r.sum == T(0)) return l;
T lmax = std::max(l.lmax, l.sum + r.lmax);
T rmax = std::max(r.rmax, l.rmax + r.sum);
T allmax = std::max({l.allmax, r.allmax, l.rmax + r.lmax});
T sum = l.sum + r.sum;
return S(lmax, rmax, allmax, sum);
}
inline static S e() {
T m = std::numeric_limits<T>::lowest();
return S(m, m, m, T(0));
}
};
#line 16 "test/mytest/data-structure/monoids.test.cpp"
template<class M> void identity(typename M::value_type x) {
assert(M::op(M::e(), x) == x && M::op(x, M::e()) == x);
}
int main() {
for(int a = 0; a <= 20; ++a) for(int b = 0; b <= 20; ++b) {
assert(MonoidAdd<int>::op(a, b) == a + b);
assert(MonoidAND<unsigned>::op(a, b) == unsigned(a & b));
assert(MonoidOR<unsigned>::op(a, b) == unsigned(a | b));
assert(MonoidMin<int>::op(a, b) == min(a, b));
assert(MonoidMax<int>::op(a, b) == max(a, b));
int gcd_expected = 0;
for(int d = 1; d <= max(a, b); ++d) if(a % d == 0 && b % d == 0) gcd_expected = d;
assert(MonoidGCD<int>::op(a, b) == gcd_expected);
int lcm_expected = 0;
if(a && b) { lcm_expected = 1; while(lcm_expected % a || lcm_expected % b) ++lcm_expected; }
assert(MonoidLCM<int>::op(a, b) == lcm_expected);
identity<MonoidAdd<int>>(a); identity<MonoidAND<unsigned>>(a);
identity<MonoidOR<unsigned>>(a); identity<MonoidGCD<int>>(a);
identity<MonoidLCM<int>>(a); identity<MonoidMin<int>>(a); identity<MonoidMax<int>>(a);
pair<int, int> x{a, 3}, y{b, 5};
identity<MonoidMinWithIndex<int>>(x); identity<MonoidMaxWithIndex<int>>(x);
assert(MonoidMinWithIndex<int>::op(x, y) == min(x, y));
assert(MonoidMaxWithIndex<int>::op(x, y) == max(x, y));
using Count = MonoidSumCount<ll, int>;
Count::S c; c = Count::S(a, b);
auto combined = Count::op(c, Count::S(b, a));
assert(combined.sum == a + b && combined.cnt == a + b);
assert(Count::op(c, Count::e()).sum == a && Count::op(Count::e(), c).cnt == b);
}
assert((MonoidMin<int, 100>::e() == 100));
assert((MonoidMax<int, -100>::e() == -100));
assert((MonoidMinWithIndex<int, 100>::e() == make_pair(100, -1)));
assert((MonoidMaxWithIndex<int, -100>::e() == make_pair(-100, -1)));
for(ll a = -3; a <= 3; ++a) for(ll b = -3; b <= 3; ++b) {
pair<ll, ll> f{a, b}, g{2, -1};
identity<MonoidAffine<ll>>(f); identity<MonoidAffine<ll, true>>(f);
auto left = MonoidAffine<ll>::op(f, g);
auto right = MonoidAffine<ll, true>::op(f, g);
for(ll x = -3; x <= 3; ++x) {
assert(left.first * x + left.second == a * (2 * x - 1) + b);
assert(right.first * x + right.second == 2 * (a * x + b) - 1);
}
}
using Hash = MonoidRollingHash<64>;
Hash::S h; h = Hash::e();
auto base = Hash::op(Hash::S(0, 1), Hash::S(1, 1)).h;
// Repeated calls must reuse the same base and powers in C++17.
assert(Hash::op(Hash::S(0, 1), Hash::S(1, 1)).h == base);
Hash::mint expected = 0, power = 1;
for(int i = 1; i <= 30; ++i) {
h = Hash::op(h, Hash::S(i, 1)); expected += power * i; power *= base;
assert(h.h == expected && h.len == i);
assert(Hash::op(Hash::e(), h).h == h.h && Hash::op(h, Hash::e()).h == h.h);
}
using K = MonoidKadane<ll>;
vector<ll> a{-7, 2, 3, -8, 9, -1};
for(int l = 0; l < int(a.size()); ++l) for(int r = l + 1; r <= int(a.size()); ++r) {
K::S x; x = K::e();
ll sum = 0, prefix = LLONG_MIN, suffix = LLONG_MIN, best = LLONG_MIN;
for(int i = l; i < r; ++i) {
x = K::op(x, K::S(a[i], a[i], a[i], a[i]));
sum += a[i]; prefix = max(prefix, sum);
ll part = 0;
for(int j = i; j < r; ++j) { part += a[j]; best = max(best, part); }
suffix = max(suffix, part);
}
assert(x.sum == sum && x.lmax == prefix && x.rmax == suffix && x.allmax == best);
}
INT(a0, b0); print(a0 + b0);
}