競技プログラミング用の C++ ライブラリ
#include "src/utility/vector_func.hpp"std::vector に演算子と便利な関数を追加します。計算量の $n$ は v の長さ、$m$ は w の長さです。
std::vector<int> a = {1, 2, 3};
a--; // 0-indexed にする: {0, 1, 2}
auto s = cumulative_sum(a); // {0, 0, 1, 3}
auto b = a | 2; // {0, 1, 2, 0, 1, 2}
auto idx = argsort(a, std::greater<>()); // 値の降順に並べた添字: {2, 1, 0}
auto c = select(a, idx); // {2, 1, 0}
p = argsort(v) は「k 番目に小さい要素の添字が p[k]」という置換です。これをもう一度 argsort すると逆置換になり、「v[i] が何番目に小さいか」が得られます (APL・Uiua の ⍋⍋・⍏⍏)。argsort は安定なので、同率は添字の小さい順に別の順位になり、結果は必ず $0, \ldots, n-1$ の置換です。転倒数に渡すときなど、置換がほしいときはこれを使います。2 回目の argsort は逆置換を求めているだけなので、inverse_permutation(argsort(v)) と書いても同じ結果になります。こちらは 2 回目が $O(n)$ で済みますが、全体の計算量は 1 回目のソートで決まり、$O(n \log n)$ のままです。APL や Uiua に慣れているなら argsort(argsort(v)) のほうが読みやすく、「逆置換を取っている」と明示したいなら inverse_permutation(argsort(v)) を使います。
同率の扱いで、次の 3 つを使い分けます。
| 書き方 |
{20, 10, 20, 30} の結果 |
同率の扱い |
|---|---|---|
argsort(argsort(v))inverse_permutation(argsort(v))
|
{1, 0, 2, 3} |
添字の小さい順に別の順位を付ける (置換になる) |
rank(v) |
{1, 0, 1, 3} |
同じ順位にして、次の順位を飛ばす |
Compress(v).ids(v) |
{1, 0, 1, 2} |
同じ番号にして、詰める (座標圧縮) |
| 演算子 | 説明 | 計算量 |
|---|---|---|
++v、v++、--v、v--
|
全要素を ±1 (入力の 1-indexed を 0-indexed に直すときなど) | $O(n)$ |
v + x、x + v、v += x
|
全要素に x を足す |
$O(n)$ |
v * x、x * v、v *= x
|
全要素に x を掛ける |
$O(n)$ |
v + w、v += w
|
要素ごとの和 (短い方は 0 で埋める) | $O(\max(n, m))$ |
v * w、v *= w
|
要素ごとの積 (長い方の余りは 0 になる) | $O(\max(n, m))$ |
v |= w |
w を末尾に連結 (extend と同じ) |
$O(m)$ |
v | k、k | v、v |= k
|
v を k 回繰り返す (k は 0 以上。負なら assert で落ちる) |
$O(nk)$ |
| 関数 | 説明 | 計算量 |
|---|---|---|
argsort(v) / argsort(v, comp) / argsort(v, comp, proj)
|
値を昇順 (comp の順) に安定ソートしたときの添字のリスト (std::vector<long long>)。APL の grade up。降順は argsort(v, std::greater<>())。proj を渡すと comp(proj(a), proj(b)) で比べる (std::ranges::sort と同じ。キーだけなら argsort(v, {}, key)、メンバポインタ &T::x も渡せる)。argsort(argsort(v)) は各要素の順位 (同率は添字の小さい順に別の順位を付けた置換) で、inverse_permutation(argsort(v)) と同じ |
$O(n \log n)$ |
inverse_permutation(p) |
逆置換 (ret[p[i]] = i)。p は整数型の vector で、$0, \ldots, n-1$ の置換でなければ assert で落ちる。inverse_permutation(argsort(v)) は argsort(argsort(v)) と同じ |
$O(n)$ |
rank(v) / rank(v, comp) / rank(v, comp, proj)
|
各要素の順位 (v[i] より前に来る要素の個数)。comp・proj は argsort と同じ。同率は同じ順位で、次の順位は飛ばす ({30, 10, 30, 20} なら {2, 0, 2, 1})。飛ばさずに詰めるなら 座標圧縮 の Compress(v).ids(v)
|
$O(n \log n)$ |
select(v, idx) |
{v[idx[0]], v[idx[1]], ...}。idx は整数型の vector で、範囲外なら assert で落ちる。select(a, argsort(b)) で b の昇順に a を並べる |
$O(\lvert \mathit{idx} \rvert)$ |
cumulative_sum(v) |
先頭に 0 を付けた累積和 (長さ size + 1) |
$O(n)$ |
iota(n) |
{0, 1, ..., n-1} |
$O(n)$ |
unique(v) |
ソートして重複を除いたコピー | $O(n \log n)$ |
abs(v) |
全要素の絶対値 | $O(n)$ |
extend(v, w) |
w を末尾に連結 |
$O(m)$ |
assign(v, w) |
w の内容で置き換える (型の変換つき) |
$O(m)$ |
radix_convert(x, base = 10) |
整数 x を base 進数の桁のリスト (上位桁から) にする。x = 0 なら空のリスト。x >= 0、base >= 2 でなければ assert で落ちる |
$O(\log_{base} x)$ |
radix_convert(digits, base = 10) |
base 進数の桁のリスト (上位桁から) を整数に戻す。base >= 2 で、各桁が $[0, \mathit{base})$ になければ assert で落ちる |
桁数に比例 |
hamming_distance
sorted、sum、bisect_left など) は コンテナに対する関数
#pragma once
/**
* @file vector_func.hpp
* @brief vector に対する演算子と関数
*/
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <functional>
#include <numeric>
#include <type_traits>
#include <vector>
#include "traits.hpp"
/**
* @brief 値を proj で写して comp の順に安定ソートしたときの添字の並び (APL の grade up)
*/
template <typename T, typename F = std::less<>, typename P = internal_traits::identity>
std::vector<long long> argsort(const std::vector<T>& v, F comp = F(), P proj = P()) {
std::vector<long long> ret(v.size());
std::iota(ret.begin(), ret.end(), 0LL);
std::stable_sort(ret.begin(), ret.end(), [&](long long i, long long j) {
return std::invoke(comp, std::invoke(proj, v[i]), std::invoke(proj, v[j]));
});
return ret;
}
/**
* @brief 各要素の順位 (proj で写して comp の順で v[i] より前に来る要素の個数)。同率は同じ順位で、次の順位は飛ばす
*/
template <typename T, typename F = std::less<>, typename P = internal_traits::identity>
std::vector<long long> rank(const std::vector<T>& v, F comp = F(), P proj = P()) {
const long long n = v.size();
std::vector<long long> p = argsort(v, comp, proj), ret(n);
for (long long i = 0; i < n; i++) {
ret[p[i]] = i > 0 && !std::invoke(comp, std::invoke(proj, v[p[i - 1]]), std::invoke(proj, v[p[i]])) ? ret[p[i - 1]] : i;
}
return ret;
}
/**
* @brief 逆置換 (ret[p[i]] = i)。p は 0, ..., n-1 の置換であること
*/
#if __cplusplus >= 202002L
template <typename I>
requires(std::integral<I>)
#else
template <typename I, ENABLE_IF_T(std::is_integral_v<I>)>
#endif
std::vector<long long> inverse_permutation(const std::vector<I>& p) {
const long long n = p.size();
std::vector<long long> ret(n, -1);
for (long long i = 0; i < n; i++) {
const long long x = static_cast<long long>(p[i]);
assert(0 <= x && x < n && ret[x] == -1);
ret[x] = i;
}
return ret;
}
/**
* @brief {v[idx[0]], v[idx[1]], ...} (Uiua の select)
*/
#if __cplusplus >= 202002L
template <typename T, typename I>
requires(std::integral<I>)
#else
template <typename T, typename I, ENABLE_IF_T(std::is_integral_v<I>)>
#endif
std::vector<T> select(const std::vector<T>& v, const std::vector<I>& idx) {
std::vector<T> ret;
ret.reserve(idx.size());
for (I i : idx) {
assert(0 <= static_cast<long long>(i) && static_cast<long long>(i) < static_cast<long long>(v.size()));
ret.push_back(v[i]);
}
return ret;
}
template <typename T>
inline std::vector<T>& operator++(std::vector<T>& v) {
for (auto& e : v) e++;
return v;
}
template <typename T>
inline std::vector<T> operator++(std::vector<T>& v, int) {
auto res = v;
for (auto& e : v) e++;
return res;
}
template <typename T>
inline std::vector<T>& operator--(std::vector<T>& v) {
for (auto& e : v) e--;
return v;
}
template <typename T>
inline std::vector<T> operator--(std::vector<T>& v, int) {
auto res = v;
for (auto& e : v) e--;
return res;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator+=(std::vector<T>& v1, const std::vector<U>& v2) {
if (v2.size() > v1.size()) {
v1.resize(v2.size());
}
for (std::size_t i = 0; i < v2.size(); i++) {
v1[i] += v2[i];
}
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const std::vector<T>& v1, const std::vector<U>& v2) {
std::vector<T> res(v1);
return res += v2;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator+=(std::vector<T>& v, const U& u) {
for (T& e : v) {
e += u;
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res += u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res += u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator*=(std::vector<T>& v1, const std::vector<U>& v2) {
if (v2.size() > v1.size()) {
v1.resize(v2.size());
}
for (std::size_t i = 0; i < v2.size(); i++) {
v1[i] *= v2[i];
}
for (std::size_t i = v2.size(); i < v1.size(); i++) {
v1[i] *= U(0);
}
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const std::vector<T>& v1, const std::vector<U>& v2) {
std::vector<T> res(v1);
return res *= v2;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator*=(std::vector<T>& v, const U& u) {
for (T& e : v) {
e *= u;
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res *= u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res *= u;
}
template <typename T, typename U>
inline std::vector<T>& assign(std::vector<T>& v1, const std::vector<U>& v2) {
v1.assign(v2.begin(), v2.end());
return v1;
}
template <typename T, typename U>
inline std::vector<T>& extend(std::vector<T>& v1, const std::vector<U>& v2) {
v1.insert(v1.end(), v2.begin(), v2.end());
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator|=(std::vector<T>& v1, const std::vector<U>& v2) {
return extend(v1, v2);
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T>& operator|=(std::vector<T>& v, const U& u) {
if constexpr (std::is_signed_v<U>) assert(u >= 0);
std::vector<T> w(v);
v.clear();
for (long long i = 0; i < static_cast<long long>(u); i++) {
extend(v, w);
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T> operator|(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res |= u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T> operator|(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res |= u;
}
template <typename T>
inline std::vector<T> abs(const std::vector<T>& v) {
std::vector<T> ret;
ret.reserve(v.size());
for (const T& e : v) ret.push_back(std::abs(e));
return ret;
}
template <typename T>
std::vector<T> cumulative_sum(std::vector<T> v) {
v.insert(v.begin(), T{});
std::vector<T> ret(v.size());
std::partial_sum(v.begin(), v.end(), ret.begin());
return ret;
}
#if __cplusplus >= 202002L
template <typename T>
requires(std::integral<T>)
#else
template <typename T, ENABLE_IF_T(std::is_integral_v<T>)>
#endif
std::vector<T> iota(T n) {
assert(n >= 0);
std::vector<T> ret(n);
std::iota(ret.begin(), ret.end(), 0);
return ret;
}
template <typename T>
std::vector<T> unique(const std::vector<T>& v) {
std::vector<T> res(v);
std::sort(res.begin(), res.end());
res.erase(std::unique(res.begin(), res.end()), res.end());
return res;
}
inline long long radix_convert(const std::vector<long long>& v, int base = 10) {
assert(base >= 2);
long long res = 0;
for (long long d : v) {
assert(0 <= d && d < base);
res = res * base + d;
}
return res;
}
inline std::vector<long long> radix_convert(long long v, int base = 10) {
assert(v >= 0 && base >= 2);
std::vector<long long> res;
while (v > 0) {
res.push_back(v % base);
v /= base;
}
std::reverse(res.begin(), res.end());
return res;
}#line 2 "src/utility/vector_func.hpp"
/**
* @file vector_func.hpp
* @brief vector に対する演算子と関数
*/
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <functional>
#include <numeric>
#include <type_traits>
#include <vector>
#line 2 "src/utility/traits.hpp"
/**
* @file traits.hpp
* @brief 型特性
*/
#line 10 "src/utility/traits.hpp"
#include <iterator>
#include <set>
#line 13 "src/utility/traits.hpp"
#include <utility>
#if __cplusplus >= 202002L
#include <concepts>
#include <ranges>
#endif
namespace internal_traits {
using std::begin, std::end;
#if __cplusplus >= 202002L
template <class T>
struct is_range_impl : std::bool_constant < requires(T& t) {
begin(t);
end(t);
}>{};
#else
template <class T, class = void>
struct is_range_impl : std::false_type {};
template <class T>
struct is_range_impl<T, std::void_t<decltype(begin(std::declval<T&>()), end(std::declval<T&>()))>> : std::true_type {};
#endif
// サイズ付き範囲から要素を直接構築するコンストラクタで共有する。
#if __cplusplus >= 202002L
template <class R, class S>
concept sized_constructible_range = std::ranges::input_range<R> && std::ranges::sized_range<R> &&
std::is_constructible_v<S, std::ranges::range_reference_t<R>>;
template <class R>
auto range_size(R& r) {
return std::ranges::size(r);
}
template <class R>
auto range_begin(R& r) {
return std::ranges::begin(r);
}
template <class R>
using range_value_t = std::ranges::range_value_t<R>;
#else
using std::size;
template <class R>
auto range_size(R& r) -> decltype(size(r)) {
return size(r);
}
template <class R>
auto range_begin(R& r) -> decltype(begin(r)) {
return begin(r);
}
template <class R>
using range_value_t = typename std::iterator_traits<decltype(range_begin(std::declval<R&>()))>::value_type;
template <class R, class S, class = void>
struct is_sized_constructible_range : std::false_type {};
template <class R, class S>
struct is_sized_constructible_range<R, S, std::void_t<decltype(range_size(std::declval<R&>())), decltype(++std::declval<decltype(begin(std::declval<R&>()))&>()), decltype(begin(std::declval<R&>()) != end(std::declval<R&>())), decltype(*begin(std::declval<R&>()))>>
: std::is_constructible<S, decltype(*begin(std::declval<R&>()))> {};
#endif
} // namespace internal_traits
template <class T>
struct is_range : internal_traits::is_range_impl<T>::type {};
template <class T>
inline constexpr bool is_range_v = is_range<T>::value;
#if __cplusplus >= 202002L
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(); }; \
} \
template <class T> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T>> {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, class U> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(std::declval<U>()); }; \
} \
template <class T, class U> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T, U>> {}; \
template <class T, class U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#else
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T> \
struct has_##func_name##_impl<T, std::void_t<decltype(std::declval<T>().func_name())>> \
: std::true_type {}; \
} \
template <class T> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T>::type {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, typename U, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T, typename U> \
struct has_##func_name##_impl<T, U, std::void_t<decltype(std::declval<T>().func_name(std::declval<U>()))>> \
: std::true_type {}; \
} \
template <class T, typename U> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T, U>::type {}; \
template <class T, typename U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#endif
HAS_METHOD(repr)
HAS_METHOD(type_str)
HAS_METHOD(initializer_str)
HAS_METHOD(max)
HAS_METHOD(min)
HAS_METHOD(reversed)
HAS_METHOD(sorted)
HAS_METHOD(sum)
HAS_METHOD(product)
HAS_METHOD(product_xor)
HAS_METHOD_ARG(count)
HAS_METHOD_ARG(find)
HAS_METHOD_ARG(lower_bound)
HAS_METHOD_ARG(upper_bound)
#define ENABLE_IF_T_IMPL(expr) std::enable_if_t<expr, std::nullptr_t> = nullptr
#define ENABLE_IF_T(...) ENABLE_IF_T_IMPL((__VA_ARGS__))
namespace internal_traits {
// 射影 (proj) の既定値
#if __cplusplus >= 202002L
using identity = std::identity;
#else
struct identity {
using is_transparent = void;
template <class T>
constexpr T&& operator()(T&& x) const noexcept {
return std::forward<T>(x);
}
};
#endif
} // namespace internal_traits
template <class C>
using mem_value_type = typename C::value_type;
template <class C>
using mem_difference_type = typename C::difference_type;
namespace internal_traits {
template <class T, class = void>
struct is_sorted_container_impl : std::false_type {};
template <class T>
struct is_sorted_container_impl<std::set<T>> : std::true_type {};
template <class T>
struct is_sorted_container_impl<std::multiset<T>> : std::true_type {};
} // namespace internal_traits
template <class T>
struct is_sorted_container : internal_traits::is_sorted_container_impl<T>::type {};
template <class T>
inline constexpr bool is_sorted_container_v = is_sorted_container<T>::value;
#line 18 "src/utility/vector_func.hpp"
/**
* @brief 値を proj で写して comp の順に安定ソートしたときの添字の並び (APL の grade up)
*/
template <typename T, typename F = std::less<>, typename P = internal_traits::identity>
std::vector<long long> argsort(const std::vector<T>& v, F comp = F(), P proj = P()) {
std::vector<long long> ret(v.size());
std::iota(ret.begin(), ret.end(), 0LL);
std::stable_sort(ret.begin(), ret.end(), [&](long long i, long long j) {
return std::invoke(comp, std::invoke(proj, v[i]), std::invoke(proj, v[j]));
});
return ret;
}
/**
* @brief 各要素の順位 (proj で写して comp の順で v[i] より前に来る要素の個数)。同率は同じ順位で、次の順位は飛ばす
*/
template <typename T, typename F = std::less<>, typename P = internal_traits::identity>
std::vector<long long> rank(const std::vector<T>& v, F comp = F(), P proj = P()) {
const long long n = v.size();
std::vector<long long> p = argsort(v, comp, proj), ret(n);
for (long long i = 0; i < n; i++) {
ret[p[i]] = i > 0 && !std::invoke(comp, std::invoke(proj, v[p[i - 1]]), std::invoke(proj, v[p[i]])) ? ret[p[i - 1]] : i;
}
return ret;
}
/**
* @brief 逆置換 (ret[p[i]] = i)。p は 0, ..., n-1 の置換であること
*/
#if __cplusplus >= 202002L
template <typename I>
requires(std::integral<I>)
#else
template <typename I, ENABLE_IF_T(std::is_integral_v<I>)>
#endif
std::vector<long long> inverse_permutation(const std::vector<I>& p) {
const long long n = p.size();
std::vector<long long> ret(n, -1);
for (long long i = 0; i < n; i++) {
const long long x = static_cast<long long>(p[i]);
assert(0 <= x && x < n && ret[x] == -1);
ret[x] = i;
}
return ret;
}
/**
* @brief {v[idx[0]], v[idx[1]], ...} (Uiua の select)
*/
#if __cplusplus >= 202002L
template <typename T, typename I>
requires(std::integral<I>)
#else
template <typename T, typename I, ENABLE_IF_T(std::is_integral_v<I>)>
#endif
std::vector<T> select(const std::vector<T>& v, const std::vector<I>& idx) {
std::vector<T> ret;
ret.reserve(idx.size());
for (I i : idx) {
assert(0 <= static_cast<long long>(i) && static_cast<long long>(i) < static_cast<long long>(v.size()));
ret.push_back(v[i]);
}
return ret;
}
template <typename T>
inline std::vector<T>& operator++(std::vector<T>& v) {
for (auto& e : v) e++;
return v;
}
template <typename T>
inline std::vector<T> operator++(std::vector<T>& v, int) {
auto res = v;
for (auto& e : v) e++;
return res;
}
template <typename T>
inline std::vector<T>& operator--(std::vector<T>& v) {
for (auto& e : v) e--;
return v;
}
template <typename T>
inline std::vector<T> operator--(std::vector<T>& v, int) {
auto res = v;
for (auto& e : v) e--;
return res;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator+=(std::vector<T>& v1, const std::vector<U>& v2) {
if (v2.size() > v1.size()) {
v1.resize(v2.size());
}
for (std::size_t i = 0; i < v2.size(); i++) {
v1[i] += v2[i];
}
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const std::vector<T>& v1, const std::vector<U>& v2) {
std::vector<T> res(v1);
return res += v2;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator+=(std::vector<T>& v, const U& u) {
for (T& e : v) {
e += u;
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res += u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res += u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator*=(std::vector<T>& v1, const std::vector<U>& v2) {
if (v2.size() > v1.size()) {
v1.resize(v2.size());
}
for (std::size_t i = 0; i < v2.size(); i++) {
v1[i] *= v2[i];
}
for (std::size_t i = v2.size(); i < v1.size(); i++) {
v1[i] *= U(0);
}
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const std::vector<T>& v1, const std::vector<U>& v2) {
std::vector<T> res(v1);
return res *= v2;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator*=(std::vector<T>& v, const U& u) {
for (T& e : v) {
e *= u;
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res *= u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res *= u;
}
template <typename T, typename U>
inline std::vector<T>& assign(std::vector<T>& v1, const std::vector<U>& v2) {
v1.assign(v2.begin(), v2.end());
return v1;
}
template <typename T, typename U>
inline std::vector<T>& extend(std::vector<T>& v1, const std::vector<U>& v2) {
v1.insert(v1.end(), v2.begin(), v2.end());
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator|=(std::vector<T>& v1, const std::vector<U>& v2) {
return extend(v1, v2);
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T>& operator|=(std::vector<T>& v, const U& u) {
if constexpr (std::is_signed_v<U>) assert(u >= 0);
std::vector<T> w(v);
v.clear();
for (long long i = 0; i < static_cast<long long>(u); i++) {
extend(v, w);
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T> operator|(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res |= u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T> operator|(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res |= u;
}
template <typename T>
inline std::vector<T> abs(const std::vector<T>& v) {
std::vector<T> ret;
ret.reserve(v.size());
for (const T& e : v) ret.push_back(std::abs(e));
return ret;
}
template <typename T>
std::vector<T> cumulative_sum(std::vector<T> v) {
v.insert(v.begin(), T{});
std::vector<T> ret(v.size());
std::partial_sum(v.begin(), v.end(), ret.begin());
return ret;
}
#if __cplusplus >= 202002L
template <typename T>
requires(std::integral<T>)
#else
template <typename T, ENABLE_IF_T(std::is_integral_v<T>)>
#endif
std::vector<T> iota(T n) {
assert(n >= 0);
std::vector<T> ret(n);
std::iota(ret.begin(), ret.end(), 0);
return ret;
}
template <typename T>
std::vector<T> unique(const std::vector<T>& v) {
std::vector<T> res(v);
std::sort(res.begin(), res.end());
res.erase(std::unique(res.begin(), res.end()), res.end());
return res;
}
inline long long radix_convert(const std::vector<long long>& v, int base = 10) {
assert(base >= 2);
long long res = 0;
for (long long d : v) {
assert(0 <= d && d < base);
res = res * base + d;
}
return res;
}
inline std::vector<long long> radix_convert(long long v, int base = 10) {
assert(v >= 0 && base >= 2);
std::vector<long long> res;
while (v > 0) {
res.push_back(v % base);
v /= base;
}
std::reverse(res.begin(), res.end());
return res;
}