競技プログラミング用 C++ ライブラリ
test/min_plus_concave.test.cpp · GitHub で見る
#define PROBLEM "https://judge.yosupo.jp/problem/min_plus_convolution_concave_arbitrary"
#include "../convolution/min_plus_convolution.hpp"
#include "../components/fast_io_bulk.hpp"
#include <array>
#include <cstdint>
#include <vector>
int main() {
FastInput input;
FastOutput output;
int n = 0, m = 0;
input.read(n), input.read(m);
std::vector<std::int64_t> a(n), b(m);
auto read = [&](std::vector<std::int64_t> &values) {
std::size_t offset = 0;
read_unsigned_blocks(input, values.size(), [&](const std::uint32_t *block, std::size_t count) {
for (std::size_t i = 0; i < count; ++i) values[offset + i] = block[i];
offset += count;
});
};
read(a), read(b);
const auto result = min_plus_convolution_concave(a, b);
write_unsigned_records<1>(output, result.size(), [&](std::size_t i) {
return std::array{std::uint32_t(result[i])};
});
}
#line 1 "test/min_plus_concave.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/min_plus_convolution_concave_arbitrary"
#line 1 "convolution/min_plus_convolution.hpp"
#line 1 "components/smawk.hpp"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <numeric>
#include <vector>
template <class Better>
std::vector<int> smawk(int length, const std::vector<int> &columns, Better better) {
assert(length >= 0 && (!length || !columns.empty()));
if (!length) return {};
std::vector<int> best(length);
auto solve = [&](auto &&self, std::size_t first, std::size_t stride, const std::vector<int> &cols) -> void {
int rows = (length - 1 - first) / stride + 1;
std::vector<int> reduced;
reduced.reserve(std::min<std::size_t>(rows, cols.size()));
for (int col : cols) {
while (!reduced.empty() && better(first + (int(reduced.size()) - 1) * stride, reduced.back(), col)) reduced.pop_back();
if (int(reduced.size()) < rows) reduced.push_back(col);
}
if (rows > 1) self(self, first + stride, stride * 2, reduced);
std::size_t left = 0;
for (std::size_t row = first; row < std::size_t(length); row += 2 * stride) {
std::size_t right = left;
if (row + stride < std::size_t(length))
while (reduced[right] != best[row + stride]) ++right;
else right = reduced.size() - 1;
best[row] = reduced[left];
for (std::size_t i = left + 1; i <= right; ++i)
if (better(row, best[row], reduced[i])) best[row] = reduced[i];
left = right;
}
};
solve(solve, 0, 1, columns);
return best;
}
template <class Better>
std::vector<int> smawk(int rows, int columns, Better better) {
assert(columns >= 0);
std::vector<int> cols(columns);
std::iota(cols.begin(), cols.end(), 0);
return smawk(rows, cols, better);
}
#line 1 "components/min_plus_convolution_integral.hpp"
#line 5 "components/min_plus_convolution_integral.hpp"
#include <bit>
#line 8 "components/min_plus_convolution_integral.hpp"
#include <cstdint>
#include <limits>
#include <optional>
#include <memory>
#include <type_traits>
#line 14 "components/min_plus_convolution_integral.hpp"
#if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__))
#include <immintrin.h>
#endif
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace min_plus_convolution_integral_detail {
template <class T>
inline constexpr bool supported =
#if defined(__SIZEOF_INT128__)
std::is_integral_v<T> && sizeof(T) <= 8 && !std::is_same_v<T, bool>;
#else
false;
#endif
inline bool available() {
#if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__)) && !defined(MIN_PLUS_CONVOLUTION_DISABLE_AVX2)
static const bool enabled = __builtin_cpu_supports("avx2");
return enabled;
#else
return false;
#endif
}
#if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__))
struct Kernel {
using U = std::uint32_t;
static constexpr int group = 16, pad = 24;
__attribute__((target("avx2"))) static U minimum(__m256i value) {
auto x = _mm_min_epu32(_mm256_castsi256_si128(value), _mm256_extracti128_si256(value, 1));
x = _mm_min_epu32(x, _mm_shuffle_epi32(x, 0x4e));
x = _mm_min_epu32(x, _mm_shuffle_epi32(x, 0xb1));
return U(_mm_cvtsi128_si32(x));
}
__attribute__((target("avx2"))) static void take(const U *a, const U *b, int row, int column, __m256i indices,
__m256i &best, __m256i &best_column) {
const auto reverse = _mm256_setr_epi32(7, 6, 5, 4, 3, 2, 1, 0);
const auto x = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(a + row - column - 7));
const auto y = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(b + column));
const auto value = _mm256_add_epi32(y, _mm256_permutevar8x32_epi32(x, reverse));
const auto low = _mm256_min_epu32(best, value);
best_column = _mm256_blendv_epi8(indices, best_column, _mm256_cmpeq_epi32(low, best));
best = low;
}
__attribute__((target("avx2"))) static int argmin(const U *a, const U *b, int row, int low, int high) {
if (high - low < 8) {
int best = low;
U value = a[row - low] + b[low];
for (int i = low + 1; i <= high; ++i) {
U next = a[row - i] + b[i];
if (next < value) best = i, value = next;
}
return best;
}
const auto lanes = _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7);
auto column = _mm256_add_epi32(_mm256_set1_epi32(low), lanes);
auto best = _mm256_set1_epi32(-1), best_column = column;
int last = high - 7;
for (int i = low; i < last; i += 8) {
take(a, b, row, i, column, best, best_column);
column = _mm256_add_epi32(column, _mm256_set1_epi32(8));
}
column = _mm256_add_epi32(_mm256_set1_epi32(last), lanes);
take(a, b, row, last, column, best, best_column);
auto mask = _mm256_cmpeq_epi32(best, _mm256_set1_epi32(std::bit_cast<int>(minimum(best))));
return int(minimum(_mm256_or_si256(best_column, _mm256_xor_si256(mask, _mm256_set1_epi32(-1)))));
}
#if defined(__SIZEOF_INT128__)
template <class T, bool Minimum>
__attribute__((target("avx512f"))) static __m512i merge_step64(const T *a, const T *b) {
auto best = _mm512_add_epi64(_mm512_set1_epi64(a[0]), _mm512_loadu_si512(b + 1));
for (int q = 1; q <= 8; ++q) {
auto next = _mm512_add_epi64(_mm512_set1_epi64(a[q]), _mm512_loadu_si512(b + 1 - q));
if constexpr (std::is_signed_v<T>) {
if constexpr (Minimum) best = _mm512_min_epi64(best, next);
else best = _mm512_max_epi64(best, next);
} else {
if constexpr (Minimum) best = _mm512_min_epu64(best, next);
else best = _mm512_max_epu64(best, next);
}
}
return best;
}
template <class T, bool Minimum>
__attribute__((target("avx512f"))) static void merge64(const T *a, const T *b, int n, int m, T *result) {
constexpr int chains = 4;
int count = n + m - 1, length = (count + chains * 8 - 1) / (chains * 8) * 8;
int x[chains], y[chains], position[chains], end[chains];
for (int s = 0; s < chains; ++s) {
int k = s * length, low = std::max(0, k + 1 - m), high = std::min(k, n - 1);
while (low < high) {
int mid = low + (high - low) / 2;
auto left = __int128_t(a[mid + 1]) - __int128_t(a[mid]), right = __int128_t(b[k - mid]) - __int128_t(b[k - mid - 1]);
if (Minimum ? left >= right : left <= right) high = mid;
else low = mid + 1;
}
x[s] = low, y[s] = k - low, position[s] = k, end[s] = std::min(count, k + length);
result[k] = T(__int128_t(a[x[s]]) + __int128_t(b[y[s]]));
}
const auto reverse = _mm512_setr_epi64(7, 6, 5, 4, 3, 2, 1, 0);
for (int t = 0; t < length; t += 8) for (int s = 0; s < chains; ++s) {
int &i = x[s], &j = y[s], &k = position[s];
if (k + 8 < end[s] && i + 8 < n && j >= 7 && j + 8 < m) {
auto values = merge_step64<T, Minimum>(a + i, b + j);
_mm512_storeu_si512(result + k + 1, values);
auto diagonal = _mm512_add_epi64(_mm512_loadu_si512(a + i), _mm512_permutexvar_epi64(reverse, _mm512_loadu_si512(b + j + 1)));
unsigned mask = _mm512_cmpeq_epi64_mask(diagonal, _mm512_permutexvar_epi64(_mm512_set1_epi64(7), values));
int q = std::countr_zero(mask | 0x100u);
i += q, j += 8 - q, k += 8;
} else {
int last = std::min(end[s] - 1, k + 8);
while (k < last) {
bool left = j + 1 == m;
if (i + 1 < n && j + 1 < m) {
auto u = __int128_t(a[i + 1]) - __int128_t(a[i]), v = __int128_t(b[j + 1]) - __int128_t(b[j]);
left = Minimum ? u < v : v < u;
}
if (left) ++i;
else ++j;
result[++k] = T(__int128_t(a[i]) + __int128_t(b[j]));
}
}
}
}
#endif
__attribute__((target("avx2"))) static bool multiply(const U *a, const U *b, int n, int m, U *result,
std::size_t capacity, std::uint64_t budget) {
if (n <= 0 || m <= 0 || n > std::numeric_limits<int>::max() - m - 2 * pad) return false;
int length = n + m - 1, groups = (length + group - 1) / group;
int size = std::bit_ceil(unsigned(groups));
if (capacity < std::size_t(groups) * group) return false;
std::vector<U> opt(std::size_t(size) + 1, U(m - 1));
opt[0] = 0;
for (int step = size / 2; step; step /= 2)
for (int t = step; t < groups; t += 2 * step) {
int row = t * group;
int low = std::max(int(opt[t - step]), std::max(0, row + 1 - n));
int high = std::min(int(opt[t + step]), row);
std::uint64_t work = std::uint64_t(high - low) + 1;
if (work > budget) return false;
budget -= work;
opt[t] = U(argmin(a, b, row, low, high));
}
for (int t = 0; t < groups; ++t) {
int row = t * group;
int low = std::max(int(opt[t]), std::max(0, row + 1 - n));
int high = std::min(int(opt[t + 1]), row + group - 1);
auto x = _mm256_set1_epi32(-1), y = x;
for (int j = low; j <= high; ++j) {
auto value = _mm256_set1_epi32(int(b[j]));
x = _mm256_min_epu32(x, _mm256_add_epi32(value, _mm256_loadu_si256(reinterpret_cast<const __m256i *>(a + row - j))));
y = _mm256_min_epu32(y, _mm256_add_epi32(value, _mm256_loadu_si256(reinterpret_cast<const __m256i *>(a + row - j + 8))));
}
_mm256_storeu_si256(reinterpret_cast<__m256i *>(result + row), x);
_mm256_storeu_si256(reinterpret_cast<__m256i *>(result + row + 8), y);
}
return true;
}
};
#endif
template <class T, bool Minimum>
std::optional<std::vector<T>> multiply(const std::vector<T> &a, const std::vector<T> &b) {
#if defined(__SIZEOF_INT128__)
static_assert(supported<T>);
using W = __int128_t;
constexpr W lowest = W(std::numeric_limits<T>::lowest()), highest = W(std::numeric_limits<T>::max());
if (a.empty() || b.empty()) return std::vector<T>{};
if (a.size() > std::size_t(std::numeric_limits<int>::max()) || b.size() > std::size_t(std::numeric_limits<int>::max()) - a.size()) return std::nullopt;
int n = int(a.size()), m = int(b.size()), length = n + m - 1;
std::vector<T> result;
if (n == 1 || m == 1) {
result.reserve(length);
for (int i = 0; i < length; ++i) {
W value = W(a[n == 1 ? 0 : i]) + W(b[m == 1 ? 0 : i]);
if (value < lowest || value > highest) return std::nullopt;
result.push_back(T(value));
}
return result;
}
int low = 0, high = n - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (Minimum ? a[mid] <= a[mid + 1] : a[mid] >= a[mid + 1]) high = mid;
else low = mid + 1;
}
const T *extreme = a.data() + low;
const T *endpoint = a.data() + ((Minimum ? a.front() < a.back() : a.back() < a.front()) ? n - 1 : 0);
const T *amin = Minimum ? extreme : endpoint, *amax = Minimum ? endpoint : extreme;
T bmin = b.front(), bmax = b.front();
for (T value : b) bmin = std::min(bmin, value), bmax = std::max(bmax, value);
if (W(*amin) + W(bmin) < lowest || W(*amax) + W(bmax) > highest) return std::nullopt;
auto same_curvature = [&]<class D>() {
D previous = D(W(b[1]) - W(b[0]));
for (int i = 2; i < m; ++i) {
D next = D(W(b[i]) - W(b[i - 1]));
if (Minimum ? next < previous : previous < next) return false;
previous = next;
}
return true;
};
bool convex = W(bmax) - W(bmin) <= std::numeric_limits<std::int64_t>::max() ? same_curvature.template operator()<std::int64_t>() : same_curvature.template operator()<W>();
if (convex) {
#if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__))
if constexpr (sizeof(T) == 8) if (length >= 4096 && length < std::numeric_limits<int>::max() - 128
&& std::int64_t(std::max(n, m)) <= 4 * std::int64_t(std::min(n, m)) && available() && __builtin_cpu_supports("avx512f")) {
W afirst = W(a[1]) - W(a[0]), alast = W(a[n - 1]) - W(a[n - 2]);
W bfirst = W(b[1]) - W(b[0]), blast = W(b[m - 1]) - W(b[m - 2]);
if (Minimum ? bfirst < alast && afirst < blast : alast < bfirst && blast < afirst) {
result.resize(length);
Kernel::merge64<T, Minimum>(a.data(), b.data(), n, m, result.data());
return result;
}
}
#endif
result.reserve(length);
auto merge = [&]<class Difference>() {
int i = 0, j = 0;
result.push_back(T(W(a[0]) + W(b[0])));
Difference left = Difference(W(a[1]) - W(a[0])), right = Difference(W(b[1]) - W(b[0]));
while (i + 1 < n && j + 1 < m) {
if (Minimum ? left < right : right < left) {
++i;
if (i + 1 < n) left = Difference(W(a[i + 1]) - W(a[i]));
} else {
++j;
if (j + 1 < m) right = Difference(W(b[j + 1]) - W(b[j]));
}
result.push_back(T(W(a[i]) + W(b[j])));
}
while (++i < n) result.push_back(T(W(a[i]) + W(b[j])));
--i;
while (++j < m) result.push_back(T(W(a[i]) + W(b[j])));
};
if (W(*amax) - W(*amin) <= std::numeric_limits<std::int64_t>::max()
&& W(bmax) - W(bmin) <= std::numeric_limits<std::int64_t>::max()) merge.template operator()<std::int64_t>();
else merge.template operator()<W>();
return result;
}
#if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__))
if (length < 256 || !available() || n > std::numeric_limits<int>::max() - m - 2 * Kernel::pad) return std::nullopt;
W arange = W(*amax) - W(*amin), brange = W(bmax) - W(bmin);
if (arange + 2 * brange >= W(std::numeric_limits<std::uint32_t>::max())) return std::nullopt;
std::size_t capacity = std::vector<std::uint32_t>{}.max_size();
std::size_t groups = (std::size_t(length) + Kernel::group - 1) / Kernel::group;
if (capacity < 2 * Kernel::pad || std::size_t(n) > capacity - 2 * Kernel::pad || std::size_t(m) > capacity || groups > capacity / Kernel::group) return std::nullopt;
W abase = Minimum ? W(*amin) : W(*amax), bbase = Minimum ? W(bmin) : W(bmax), offset = abase + bbase;
auto x = std::make_unique_for_overwrite<std::uint32_t[]>(std::size_t(n) + 2 * Kernel::pad);
auto y = std::make_unique_for_overwrite<std::uint32_t[]>(m);
auto values = std::make_unique_for_overwrite<std::uint32_t[]>(groups * Kernel::group);
std::uint32_t padding = std::numeric_limits<std::uint32_t>::max() - std::uint32_t(brange);
std::fill_n(x.get(), Kernel::pad, padding);
std::fill_n(x.get() + Kernel::pad + n, Kernel::pad, padding);
for (int i = 0; i < n; ++i) x[Kernel::pad + i] = std::uint32_t(Minimum ? W(a[i]) - abase : abase - W(a[i]));
for (int i = 0; i < m; ++i) y[i] = std::uint32_t(Minimum ? W(b[i]) - bbase : bbase - W(b[i]));
if (!Kernel::multiply(x.get() + Kernel::pad, y.get(), n, m, values.get(), groups * Kernel::group, std::uint64_t(n + m) * 16)) return std::nullopt;
result.reserve(length);
for (int i = 0; i < length; ++i) result.push_back(T(Minimum ? offset + values[i] : offset - values[i]));
return result;
#endif
#endif
return std::nullopt;
}
}
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 1 "components/min_plus_convolution_envelope.hpp"
#line 9 "components/min_plus_convolution_envelope.hpp"
#include <span>
#line 14 "components/min_plus_convolution_envelope.hpp"
#if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__))
#include <immintrin.h>
#endif
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace min_plus_convolution_envelope_detail {
struct Envelope {
struct Entry { int offset, end; std::uint32_t bias; };
#if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__))
template <int step>
__attribute__((target("avx2"))) static int window(std::span<const std::uint32_t> a, const Entry &x, const Entry &y, int t) {
constexpr int shift = step == 1 ? 0 : -7;
auto u = _mm256_add_epi32(_mm256_loadu_si256(reinterpret_cast<const __m256i *>(a.data() + x.offset + step * t + shift)), _mm256_set1_epi32(int(x.bias)));
auto v = _mm256_add_epi32(_mm256_loadu_si256(reinterpret_cast<const __m256i *>(a.data() + y.offset + step * t + shift)), _mm256_set1_epi32(int(y.bias)));
auto sign = _mm256_set1_epi32(std::numeric_limits<std::int32_t>::min());
unsigned loses = unsigned(_mm256_movemask_ps(_mm256_castsi256_ps(_mm256_cmpgt_epi32(_mm256_xor_si256(u, sign), _mm256_xor_si256(v, sign)))));
if constexpr (step == 1) return std::countr_zero(loses | 0x100u);
else return std::countl_zero((loses << 24) | 0x800000u);
}
#endif
template <int step>
static int crossing(std::span<const std::uint32_t> a, const Entry &x, const Entry &y, int low, int high, [[maybe_unused]] bool simd) {
while (high - low > 8) {
int mid = low + (high - low) / 2;
(a[x.offset + step * mid] + x.bias <= a[y.offset + step * mid] + y.bias ? low : high) = mid;
}
#if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__))
int p = x.offset + step * low, q = y.offset + step * low, n = int(a.size());
if (simd && (step == 1 ? std::max(p, q) < n - 7 : std::min(p, q) >= 7)) return low + window<step>(a, x, y, low) - 1;
#endif
while (high - low > 1) {
int mid = low + (high - low) / 2;
(a[x.offset + step * mid] + x.bias <= a[y.offset + step * mid] + y.bias ? low : high) = mid;
}
return low;
}
template <int step>
static void sweep(std::span<const std::uint32_t> a, std::span<const std::uint32_t> b, std::vector<std::uint32_t> &c,
int first, int entering, int count, std::vector<Entry> &stack, bool simd) {
int n = int(a.size());
stack.clear();
auto value = [&](const Entry &e, int t) { return a[e.offset + step * t] + e.bias; };
auto take = [&] { auto e = stack.back(); stack.pop_back(); return e; };
Entry top{first - entering, n - 1, b[entering]};
c[first] = std::min(c[first], value(top, 0));
int t = 1;
for (; t < count; ++t) {
while (top.end < t) top = take();
std::uint32_t best = value(top, t);
int column = entering + step * t;
Entry next{first - column, 0, b[column]};
if (std::uint32_t v = value(next, t); v <= best) {
int low = t;
for (;;) {
if (value(next, top.end) > value(top, top.end)) {
next.end = crossing<step>(a, next, top, low, top.end, simd);
stack.push_back(top);
break;
}
low = top.end;
if (stack.empty()) { next.end = n - 1; break; }
top = take();
}
top = next;
best = v;
}
int row = first + step * t;
c[row] = std::min(c[row], best);
}
stack.push_back(top);
while (t < n) {
Entry e = take();
if (e.end < t) continue;
int last = e.end, lo = first + step * t, hi = first + step * last;
if (hi < lo) std::swap(lo, hi);
int column = first - e.offset;
for (int row = lo; row <= hi; ++row) c[row] = std::min(c[row], a[row - column] + e.bias);
t = last + 1;
}
}
static std::vector<std::uint32_t> run(std::span<const std::uint32_t> a, std::span<const std::uint32_t> b) {
int n = int(a.size()), m = int(b.size());
std::vector<std::uint32_t> c(n + m - 1, std::numeric_limits<std::uint32_t>::max());
std::vector<Entry> stack;
stack.reserve(std::min(n, m));
bool simd = false;
#if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__)) && !defined(MIN_PLUS_CONVOLUTION_DISABLE_AVX2)
simd = __builtin_cpu_supports("avx2");
#endif
for (int j = 0; j < m; j += n) {
int end = std::min(m, j + n);
sweep<1>(a, b, c, j, j, end - j, stack, simd);
sweep<-1>(a, b, c, end + n - 2, end - 1, end - j, stack, simd);
}
return c;
}
};
template <class T, bool minimum>
std::optional<std::vector<T>> multiply(const std::vector<T> &a, const std::vector<T> &b) {
#if defined(__SIZEOF_INT128__)
static_assert(std::is_integral_v<T> && sizeof(T) <= 8 && !std::is_same_v<T, bool>);
using W = __int128_t;
if (a.empty() || b.empty()) return std::vector<T>{};
if (a.size() > std::size_t(std::numeric_limits<int>::max()) || b.size() > std::size_t(std::numeric_limits<int>::max()) - a.size()) return std::nullopt;
int n = int(a.size()), m = int(b.size());
int low = 0, high = n - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (minimum ? a[mid] >= a[mid + 1] : a[mid] <= a[mid + 1]) high = mid;
else low = mid + 1;
}
T extreme = a[low], endpoint = minimum ? std::min(a.front(), a.back()) : std::max(a.front(), a.back());
W amin = minimum ? W(endpoint) : W(extreme), amax = minimum ? W(extreme) : W(endpoint);
T bmin_value = b.front(), bmax_value = b.front();
for (T value : b) bmin_value = std::min(bmin_value, value), bmax_value = std::max(bmax_value, value);
W bmin = W(bmin_value), bmax = W(bmax_value);
if (amin + bmin < W(std::numeric_limits<T>::lowest()) || amax + bmax > W(std::numeric_limits<T>::max()) || amax - amin + bmax - bmin > W(std::numeric_limits<std::uint32_t>::max())) return std::nullopt;
W abase = minimum ? amin : amax, bbase = minimum ? bmin : bmax, offset = abase + bbase;
auto x = std::make_unique_for_overwrite<std::uint32_t[]>(n);
auto y = std::make_unique_for_overwrite<std::uint32_t[]>(m);
for (int i = 0; i < n; ++i) x[i] = std::uint32_t(minimum ? W(a[i]) - abase : abase - W(a[i]));
for (int i = 0; i < m; ++i) y[i] = std::uint32_t(minimum ? W(b[i]) - bbase : bbase - W(b[i]));
auto c = Envelope::run({x.get(), std::size_t(n)}, {y.get(), std::size_t(m)});
std::vector<T> result(c.size());
for (std::size_t i = 0; i < c.size(); ++i) result[i] = T(minimum ? offset + c[i] : offset - c[i]);
return result;
#else
return std::nullopt;
#endif
}
}
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 11 "convolution/min_plus_convolution.hpp"
#include <functional>
#line 15 "convolution/min_plus_convolution.hpp"
#include <utility>
#line 17 "convolution/min_plus_convolution.hpp"
namespace min_plus_convolution_detail {
template <class T, class Compare>
std::vector<T> multiply(const std::vector<T> &a, const std::vector<T> &b, Compare compare) {
if (a.empty() || b.empty()) return {};
assert(a.size() <= std::size_t(std::numeric_limits<int>::max()) && b.size() <= std::size_t(std::numeric_limits<int>::max()) - a.size());
if constexpr (min_plus_convolution_integral_detail::supported<T>
&& (std::is_same_v<Compare, std::less<T>> || std::is_same_v<Compare, std::greater<T>>))
if (auto result = min_plus_convolution_integral_detail::multiply<T, std::is_same_v<Compare, std::less<T>>>(a, b)) return std::move(*result);
int n = a.size(), m = b.size(), length = n + m - 1;
std::vector<int> columns(m);
std::iota(columns.begin(), columns.end(), 0);
auto better = [&](int row, int x, int y) {
if (row < y) return false;
if (row - x >= n) return true;
return compare(a[row - y] + b[y], a[row - x] + b[x]);
};
auto best = smawk(length, columns, better);
std::vector<T> result;
result.reserve(length);
for (int i = 0; i < length; ++i) result.push_back(a[i - best[i]] + b[best[i]]);
return result;
}
template <class T, class Compare>
std::vector<T> opposite(const std::vector<T> &a, const std::vector<T> &b, Compare compare) {
if (a.empty() || b.empty()) return {};
assert(a.size() <= std::size_t(std::numeric_limits<int>::max()) && b.size() <= std::size_t(std::numeric_limits<int>::max()) - a.size());
if constexpr (min_plus_convolution_integral_detail::supported<T>
&& (std::is_same_v<Compare, std::less<T>> || std::is_same_v<Compare, std::greater<T>>))
if (auto result = min_plus_convolution_envelope_detail::multiply<T, std::is_same_v<Compare, std::less<T>>>(a, b)) return std::move(*result);
int n = a.size(), m = b.size(), length = n + m - 1;
std::vector<T> result(length, a[0] + b[0]);
std::vector<std::uint8_t> done(length);
auto solve = [&](auto &&self, int l, int r, int parent_l, int parent_r) -> void {
int low = std::max(0, r - n), high = std::min(m, l + 1);
std::vector<int> columns;
for (int i = high - 1; i >= low;) {
if (parent_l <= i && i < parent_r) i = parent_l - 1;
else columns.push_back(i--);
}
if (!columns.empty()) {
auto best = smawk(r - l, columns, [&](int row, int x, int y) {
return compare(a[l + row - y] + b[y], a[l + row - x] + b[x]);
});
for (int i = l; i < r; ++i) {
T value = a[i - best[i - l]] + b[best[i - l]];
if (!done[i] || compare(value, result[i])) result[i] = value;
done[i] = 1;
}
}
if (r - l > 1) {
int mid = l + (r - l) / 2;
self(self, l, mid, low, high);
self(self, mid, r, low, high);
}
};
solve(solve, 0, length, 0, 0);
return result;
}
}
template <class T>
std::vector<T> min_plus_convolution_convex(const std::vector<T> &a, const std::vector<T> &b) {
return min_plus_convolution_detail::multiply(a, b, std::less<T>{});
}
template <class T>
std::vector<T> max_plus_convolution_concave(const std::vector<T> &a, const std::vector<T> &b) {
return min_plus_convolution_detail::multiply(a, b, std::greater<T>{});
}
template <class T>
std::vector<T> min_plus_convolution_concave(const std::vector<T> &a, const std::vector<T> &b) {
return min_plus_convolution_detail::opposite(a, b, std::less<T>{});
}
template <class T>
std::vector<T> max_plus_convolution_convex(const std::vector<T> &a, const std::vector<T> &b) {
return min_plus_convolution_detail::opposite(a, b, std::greater<T>{});
}
#line 1 "components/fast_io_bulk.hpp"
#line 1 "components/fast_io_output.hpp"
#line 1 "components/fast_io.hpp"
#line 5 "components/fast_io.hpp"
#include <array>
#line 8 "components/fast_io.hpp"
#include <charconv>
#line 11 "components/fast_io.hpp"
#include <cstdio>
#include <cstring>
#line 14 "components/fast_io.hpp"
#include <stdexcept>
#include <string>
#include <string_view>
#line 18 "components/fast_io.hpp"
#if defined(__unix__) || defined(__APPLE__)
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace fast_io_detail { struct BulkInput; struct BulkOutput; }
class FastInput {
friend struct fast_io_detail::BulkInput;
static constexpr std::size_t capacity = 1 << 16;
FILE *file = nullptr;
std::unique_ptr<char[]> buffer;
const char *current = "", *end = current;
std::string scratch;
void *mapping = nullptr;
std::size_t mapped_size = 0;
bool refill() {
if (!file) return false;
std::size_t n = std::fread(buffer.get(), 1, capacity, file);
current = buffer.get();
end = current + n;
return n;
}
static const char *scan(const char *p, const char *end) {
while (end - p >= 8) {
std::uint64_t x;
std::memcpy(&x, p, 8);
if ((x - 0x2121212121212121ULL) & ~x & 0x8080808080808080ULL) break;
p += 8;
}
while (p != end && static_cast<unsigned char>(*p) > ' ') ++p;
return p;
}
public:
explicit FastInput(FILE *input = stdin) {
#if defined(__unix__) || defined(__APPLE__)
struct stat status;
int fd = fileno(input);
auto offset = ftello(input);
if (fd >= 0 && offset >= 0 && !fstat(fd, &status) && S_ISREG(status.st_mode) && status.st_size > offset) {
mapped_size = status.st_size;
mapping = mmap(nullptr, mapped_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapping != MAP_FAILED) {
current = static_cast<const char *>(mapping) + offset;
end = static_cast<const char *>(mapping) + mapped_size;
return;
}
mapping = nullptr;
}
#endif
file = input;
buffer = std::make_unique_for_overwrite<char[]>(capacity);
}
explicit FastInput(std::string_view input) : current(input.empty() ? "" : input.data()), end(current + input.size()) {}
FastInput(const FastInput &) = delete;
FastInput &operator=(const FastInput &) = delete;
~FastInput() {
#if defined(__unix__) || defined(__APPLE__)
if (mapping) munmap(mapping, mapped_size);
#endif
}
std::string_view token() {
while (true) {
while (current != end && static_cast<unsigned char>(*current) <= ' ') ++current;
if (current != end) break;
if (!refill()) return {};
}
const char *begin = current;
current = scan(current, end);
if (current != end || !file) return {begin, std::size_t(current - begin)};
scratch.assign(begin, end);
while (refill()) {
current = scan(current, end);
scratch.append(buffer.get(), std::size_t(current - buffer.get()));
if (current != end) break;
}
return scratch;
}
template <class T>
bool read(T &value) {
if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>) {
while (true) {
while (current != end && static_cast<unsigned char>(*current) <= ' ') ++current;
if (current != end) break;
if (!refill()) return false;
}
if constexpr (std::is_unsigned_v<T> && sizeof(T) == 4 && std::endian::native == std::endian::little) {
if (end - current >= 11) {
std::uint64_t x;
std::memcpy(&x, current, 8);
if (!(((x + 0x4646464646464646ULL) | (x - 0x3030303030303030ULL)) & 0x8080808080808080ULL)) {
x &= 0x0f0f0f0f0f0f0f0fULL;
x = (x * 10 + (x >> 8)) & 0x00ff00ff00ff00ffULL;
x = (x * 100 + (x >> 16)) & 0x0000ffff0000ffffULL;
std::uint64_t v = std::uint32_t(x * 10000 + (x >> 32));
int n = 8;
for (; n < 10 && static_cast<unsigned char>(current[n]) - unsigned('0') < 10; ++n)
v = v * 10 + (current[n] - '0');
if (static_cast<unsigned char>(current[n]) <= ' ' && v <= 0xffffffffULL) {
current += n;
value = T(v);
return true;
}
}
}
}
T parsed{};
auto result = std::from_chars(current, end, parsed);
if (result.ec == std::errc{} && (result.ptr != end ? static_cast<unsigned char>(*result.ptr) <= ' ' : !file)) {
current = result.ptr;
value = parsed;
return true;
}
}
auto s = token();
if (s.empty()) return false;
if constexpr (std::is_same_v<T, bool>) {
assert(s == "0" || s == "1");
value = s[0] == '1';
} else if constexpr (std::is_integral_v<T>) {
auto result = std::from_chars(s.data(), s.data() + s.size(), value);
assert(result.ec == std::errc{} && result.ptr == s.data() + s.size());
(void)result;
} else value = T(s);
return true;
}
};
class FastOutput {
friend struct fast_io_detail::BulkOutput;
inline static constexpr auto quads = [] {
std::array<std::array<char, 4>, 10000> table{};
for (int i = 0; i < 10000; ++i)
table[i] = {char('0' + i / 1000), char('0' + i / 100 % 10), char('0' + i / 10 % 10), char('0' + i % 10)};
return table;
}();
FILE *file;
std::unique_ptr<char[]> buffer = std::make_unique_for_overwrite<char[]>(1 << 16);
std::size_t capacity = 1 << 16, length = 0;
char *reserve(std::size_t n) {
if (n > capacity - length) {
if (!flush()) throw std::runtime_error("output failed");
if (n > capacity) {
std::size_t next = std::max(n, 2 * capacity);
buffer = std::make_unique_for_overwrite<char[]>(next);
capacity = next;
}
}
return buffer.get() + length;
}
public:
explicit FastOutput(FILE *output = stdout) : file(output) {}
FastOutput(const FastOutput &) = delete;
FastOutput &operator=(const FastOutput &) = delete;
~FastOutput() { flush(); }
bool flush() {
std::size_t offset = 0;
while (offset < length) {
auto n = std::fwrite(buffer.get() + offset, 1, length - offset, file);
if (!n) {
std::memmove(buffer.get(), buffer.get() + offset, length - offset);
length -= offset;
return false;
}
offset += n;
}
length = 0;
return std::fflush(file) == 0;
}
void write(char c) { *reserve(1) = c; ++length; }
void write(__uint128_t value) {
char digits[40];
char *end = digits + sizeof(digits), *begin = end;
do { *--begin = char('0' + value % 10); value /= 10; } while (value);
write(std::string_view(begin, end - begin));
}
void write(__int128_t value) {
__uint128_t magnitude = __uint128_t(value);
if (value < 0) write('-'), magnitude = 0 - magnitude;
write(magnitude);
}
void write(std::string_view s) {
char *out = reserve(s.size());
if (!s.empty()) std::memcpy(out, s.data(), s.size());
length += s.size();
}
template <class T> requires std::is_integral_v<T>
void write(T value) {
char *out = reserve(32);
if constexpr (std::is_same_v<T, bool>) *out = '0' + value, ++length;
else if constexpr (std::is_unsigned_v<T> && sizeof(T) == 4) {
char *p = out;
if (value >= 100000000) {
auto high = value / 100000000, low = value % 100000000;
p = std::to_chars(p, p + 2, high).ptr;
std::memcpy(p, quads[low / 10000].data(), 4);
std::memcpy(p + 4, quads[low % 10000].data(), 4);
p += 8;
} else if (value >= 10000) {
p = std::to_chars(p, p + 4, value / 10000).ptr;
std::memcpy(p, quads[value % 10000].data(), 4);
p += 4;
} else p = std::to_chars(p, p + 4, value).ptr;
length += p - out;
} else length += std::to_chars(out, out + 32, value).ptr - out;
}
template <class T> requires requires(const T &value, char *out) { value.decimal_capacity(); value.to_chars(out); }
void write(const T &value) {
char *out = reserve(value.decimal_capacity());
length += value.to_chars(out) - out;
}
template <class T>
void println(const T &value) { write(value); write('\n'); }
};
#line 7 "components/fast_io_output.hpp"
namespace fast_io_detail {
struct BulkOutput {
static char *small(char *out, std::uint32_t value) {
unsigned n = 1 + (value >= 10) + (value >= 100) + (value >= 1000);
if constexpr (std::endian::native == std::endian::little) {
std::uint32_t word;
std::memcpy(&word, FastOutput::quads[value].data(), 4);
word >>= (4 - n) * 8;
std::memcpy(out, &word, 4);
} else std::memcpy(out, FastOutput::quads[value].data() + 4 - n, n);
return out + n;
}
static char *number(char *out, std::uint32_t value) {
if (value >= 100000000) {
out = small(out, value / 100000000);
value %= 100000000;
std::memcpy(out, FastOutput::quads[value / 10000].data(), 4);
std::memcpy(out + 4, FastOutput::quads[value % 10000].data(), 4);
return out + 8;
}
if (value >= 10000) {
out = small(out, value / 10000);
std::memcpy(out, FastOutput::quads[value % 10000].data(), 4);
return out + 4;
}
return small(out, value);
}
template<std::size_t Fields, class Read>
#if defined(__GNUC__) || defined(__clang__)
[[gnu::noinline]]
#endif
static void records(FastOutput &out, std::size_t count, Read read) {
constexpr auto bytes = 11 * Fields;
constexpr auto block = std::max<std::size_t>(1, (1 << 16) / bytes);
for (std::size_t base = 0; base < count;) {
const auto n = std::min(count - base, block);
char *begin = out.reserve(n * bytes), *p = begin;
for (std::size_t i = 0; i < n; ++i) {
auto values = read(base + i);
for (std::size_t j = 0; j < Fields; ++j) {
p = number(p, std::uint32_t(values[j]));
*p++ = j + 1 == Fields ? '\n' : ' ';
}
}
out.length += p - begin;
base += n;
}
}
static void write(FastOutput &out, std::string_view text) {
if (text.size() <= (1 << 16)) return out.write(text);
if (out.length && !out.flush()) throw std::runtime_error("output failed");
std::size_t done = 0;
while (done < text.size()) {
const auto written = std::fwrite(text.data() + done, 1, text.size() - done, out.file);
if (!written) throw std::runtime_error("output failed");
done += written;
}
}
};
}
template<std::size_t Fields, class Read>
void write_unsigned_records(FastOutput &output, std::size_t count, Read read) {
static_assert(Fields > 0 && Fields <= std::numeric_limits<std::size_t>::max() / 11);
fast_io_detail::BulkOutput::records<Fields>(output, count, std::move(read));
}
#line 8 "components/fast_io_bulk.hpp"
#if defined(__linux__) && defined(__x86_64__) && defined(__GNUC__) && !defined(__clang__)
#include <immintrin.h>
#pragma GCC push_options
_Pragma("GCC target(\"avx2\")")
#pragma GCC optimize("O3")
namespace fast_io_detail {
struct BulkInput {
alignas(32) static inline constexpr auto rows = [] {
std::array<std::array<unsigned char, 16>, 11> r{};
for (int n = 0; n <= 10; ++n)
for (int i = 0; i < 16; ++i) r[n][i] = i < 16 - n ? 128 : i - (16 - n);
return r;
}();
static constexpr std::size_t kChunk = 1 << 17, kCapacity = kChunk / 4 + 66;
static unsigned separators(__m256i bytes) {
return unsigned(_mm256_movemask_epi8(_mm256_cmpeq_epi8(_mm256_max_epu8(bytes, _mm256_set1_epi8(' ')), _mm256_set1_epi8(' '))));
}
[[gnu::always_inline]] static __m256i two(const char *&p, __m256i &invalid) {
const auto bytes = _mm256_loadu_si256(reinterpret_cast<const __m256i *>(p));
const unsigned sep = separators(bytes), a = std::countr_zero(sep), b = std::countr_zero(sep & (sep - 1));
if (a < 1 || a > 10 || b <= a + 1 || b - a - 1 > 10) [[unlikely]] {
invalid = _mm256_set1_epi32(-1);
p += std::min(b + 1, 32u);
return _mm256_setzero_si256();
}
const auto window = _mm256_inserti128_si256(bytes, _mm_loadu_si128(reinterpret_cast<const __m128i *>(p + a + 1)), 1);
const auto control = _mm256_set_m128i(_mm_loadu_si128(reinterpret_cast<const __m128i *>(rows[b - a - 1].data())),
_mm_loadu_si128(reinterpret_cast<const __m128i *>(rows[a].data())));
const auto digits = _mm256_shuffle_epi8(_mm256_sub_epi8(window, _mm256_set1_epi8('0')), control);
invalid = _mm256_or_si256(invalid, _mm256_subs_epu8(digits, _mm256_set1_epi8(9)));
p += b + 1;
return _mm256_madd_epi16(_mm256_maddubs_epi16(digits, _mm256_set1_epi16(0x010a)), _mm256_set1_epi32(0x00010064));
}
static std::uint32_t one(const char *&p, bool &bad) {
const auto bytes = _mm_loadu_si128(reinterpret_cast<const __m128i *>(p));
const auto spaces = _mm_cmpeq_epi8(_mm_max_epu8(bytes, _mm_set1_epi8(' ')), _mm_set1_epi8(' '));
const unsigned n = std::countr_zero(unsigned(_mm_movemask_epi8(spaces)) | 0x10000u);
if (n < 1 || n > 10) [[unlikely]] {
bad = true;
p += std::min(n + 1, 16u);
return 0;
}
const auto digits = _mm_shuffle_epi8(_mm_sub_epi8(bytes, _mm_set1_epi8('0')),
_mm_loadu_si128(reinterpret_cast<const __m128i *>(rows[n].data())));
const auto invalid = _mm_subs_epu8(digits, _mm_set1_epi8(9));
bad |= !_mm_testz_si128(invalid, invalid);
const auto groups = _mm_madd_epi16(_mm_maddubs_epi16(digits, _mm_set1_epi16(0x010a)), _mm_set1_epi32(0x00010064));
const auto halves = _mm_madd_epi16(_mm_packus_epi32(groups, groups), _mm_set1_epi32(0x00012710));
const std::uint64_t value = std::uint64_t(unsigned(_mm_cvtsi128_si32(halves))) * 100000000 + unsigned(_mm_extract_epi32(halves, 1));
bad |= value > 0xffffffffULL;
p += n + 1;
return std::uint32_t(value);
}
static const char *after_separator(const char *p) {
const unsigned mask = separators(_mm256_loadu_si256(reinterpret_cast<const __m256i *>(p)));
return mask ? p + std::countr_zero(mask) + 1 : nullptr;
}
static std::size_t lockstep(const char *(&streams)[4], const char *const (&end)[4], unsigned *const (&out)[4], __m256i &invalid) {
const char *p[4] = {streams[0], streams[1], streams[2], streams[3]};
std::size_t done = 0;
for (;;) {
std::ptrdiff_t left = end[0] - p[0];
for (int k = 1; k < 4; ++k) left = std::min(left, end[k] - p[k]);
const auto steps = std::min(left / 33, std::ptrdiff_t((kCapacity - done) / 2));
if (steps <= 0) break;
for (auto j = steps; j; --j, done += 2) {
const auto g0 = two(p[0], invalid), g1 = two(p[1], invalid), g2 = two(p[2], invalid), g3 = two(p[3], invalid);
const auto k = _mm256_set1_epi32(0x00012710);
const auto h01 = _mm256_castsi256_ps(_mm256_madd_epi16(_mm256_packus_epi32(g0, g1), k));
const auto h23 = _mm256_castsi256_ps(_mm256_madd_epi16(_mm256_packus_epi32(g2, g3), k));
const auto high = _mm256_castps_si256(_mm256_shuffle_ps(h01, h23, 0x88));
const auto low = _mm256_castps_si256(_mm256_shuffle_ps(h01, h23, 0xdd));
const auto overflow = _mm256_or_si256(_mm256_cmpgt_epi32(high, _mm256_set1_epi32(42)),
_mm256_and_si256(_mm256_cmpeq_epi32(high, _mm256_set1_epi32(42)), _mm256_cmpgt_epi32(low, _mm256_set1_epi32(94967295))));
invalid = _mm256_or_si256(invalid, overflow);
const auto value = _mm256_add_epi32(_mm256_mullo_epi32(high, _mm256_set1_epi32(100000000)), low);
const auto q = _mm256_permutevar8x32_epi32(value, _mm256_setr_epi32(0, 4, 1, 5, 2, 6, 3, 7));
const auto q01 = _mm256_castsi256_si128(q), q23 = _mm256_extracti128_si256(q, 1);
_mm_storel_epi64(reinterpret_cast<__m128i *>(out[0] + done), q01);
_mm_storeh_pd(reinterpret_cast<double *>(out[1] + done), _mm_castsi128_pd(q01));
_mm_storel_epi64(reinterpret_cast<__m128i *>(out[2] + done), q23);
_mm_storeh_pd(reinterpret_cast<double *>(out[3] + done), _mm_castsi128_pd(q23));
}
}
for (int k = 0; k < 4; ++k) streams[k] = p[k];
return done;
}
template <class Emit>
static std::size_t chunk_blocks(const char *&p, const char *limit, std::size_t count, unsigned *const (&out)[4], Emit &&emit) {
const auto bytes = std::min({kChunk, 2 * (count - 32), std::size_t(limit - p) - 64}) & ~std::size_t(3);
const std::size_t stream = bytes / 4;
const char *p1 = after_separator(p + stream), *p2 = after_separator(p + 2 * stream);
const char *p3 = after_separator(p + 3 * stream), *last = after_separator(p + bytes);
if (!p1 || !p2 || !p3 || !last) return 0;
const char *s[4] = {p, p1, p2, p3}, *const end[4] = {p1, p2, p3, last};
auto invalid = _mm256_setzero_si256();
const auto done = lockstep(s, end, out, invalid);
bool bad = !_mm256_testz_si256(invalid, invalid);
std::size_t n[4];
for (int k = 0; k < 4; ++k) {
n[k] = done;
while (s[k] < end[k] && n[k] < kCapacity) out[k][n[k]++] = one(s[k], bad);
bad |= s[k] != end[k];
}
const auto parsed = n[0] + n[1] + n[2] + n[3];
if (bad || parsed > count) return 0;
for (int k = 0; k < 4; ++k) emit(out[k], n[k]);
p = last;
return parsed;
}
static std::size_t chunk(const char *&p, const char *limit, unsigned *dst, std::size_t count, unsigned *scratch) {
unsigned *const out[4] = {dst, scratch, scratch + kCapacity, scratch + 2 * kCapacity};
bool first = true;
unsigned *next = dst;
return chunk_blocks(p, limit, count, out, [&](const unsigned *values, std::size_t n) {
if (!first) std::memcpy(next, values, n * sizeof(unsigned));
first = false;
next += n;
});
}
template <class Emit>
static void read_blocks(FastInput &in, std::size_t count, Emit emit) {
std::unique_ptr<unsigned[]> scratch;
std::size_t done = 0;
bool chunks = true;
while (done < count) {
const char *p = in.current;
while (p != in.end && static_cast<unsigned char>(*p) <= ' ') ++p;
while (chunks && count - done >= 1024 && in.end - p >= 1088) {
if (!scratch) scratch = std::make_unique_for_overwrite<unsigned[]>(4 * kCapacity);
unsigned *const out[4] = {scratch.get(), scratch.get() + kCapacity, scratch.get() + 2 * kCapacity, scratch.get() + 3 * kCapacity};
const auto parsed = chunk_blocks(p, in.end, count - done, out, emit);
if (!parsed) {
chunks = false;
break;
}
done += parsed;
}
unsigned tail[64];
std::size_t used = 0;
while (done + 2 <= count && in.end - p >= 32) {
const char *next = p;
auto invalid = _mm256_setzero_si256();
const auto groups = two(next, invalid);
if (!_mm256_testz_si256(invalid, invalid)) break;
const auto halves = _mm256_madd_epi16(_mm256_packus_epi32(groups, groups), _mm256_set1_epi32(0x00012710));
const unsigned h0 = _mm256_extract_epi32(halves, 0), l0 = _mm256_extract_epi32(halves, 1);
const unsigned h1 = _mm256_extract_epi32(halves, 4), l1 = _mm256_extract_epi32(halves, 5);
if (h0 > 42 || (h0 == 42 && l0 > 94967295) || h1 > 42 || (h1 == 42 && l1 > 94967295)) break;
tail[used++] = h0 * 100000000 + l0;
tail[used++] = h1 * 100000000 + l1;
if (used == 64) {
emit(tail, used);
used = 0;
}
done += 2;
p = next;
}
if (used) emit(tail, used);
in.current = p;
if (done < count) {
unsigned value = 0;
in.read(value);
emit(&value, 1);
++done;
}
}
}
static void read(FastInput &in, unsigned *out, std::size_t count) {
read_blocks(in, count, [&](const unsigned *values, std::size_t n) {
std::memcpy(out, values, n * sizeof(unsigned));
out += n;
});
}
};
}
#pragma GCC pop_options
#endif
namespace fast_io_detail {
template <class Emit, std::size_t... I>
void emit_record(Emit &emit, const std::uint32_t *values, std::index_sequence<I...>) {
emit(values[I]...);
}
}
template <class Emit>
void read_unsigned_blocks(FastInput &input, std::size_t count, Emit emit) {
#if defined(__linux__) && defined(__x86_64__) && defined(__GNUC__) && !defined(__clang__)
if (__builtin_cpu_supports("avx2")) {
fast_io_detail::BulkInput::read_blocks(input, count, emit);
return;
}
#endif
std::uint32_t values[64];
while (count) {
const auto n = std::min<std::size_t>(count, 64);
for (std::size_t i = 0; i < n; ++i) input.read(values[i]);
emit(values, n);
count -= n;
}
}
inline void read_unsigned(FastInput &input, std::span<std::uint32_t> out) {
read_unsigned_blocks(input, out.size(), [&](const std::uint32_t *values, std::size_t count) {
std::memcpy(out.data(), values, count * sizeof(std::uint32_t));
out = out.subspan(count);
});
}
template <std::size_t Fields, class Emit>
void read_unsigned_records(FastInput &input, std::size_t count, Emit emit) {
static_assert(Fields > 0);
if (count > std::numeric_limits<std::size_t>::max() / Fields) throw std::length_error("input size");
std::array<std::uint32_t, Fields> pending;
std::size_t used = 0;
constexpr auto indices = std::make_index_sequence<Fields>();
read_unsigned_blocks(input, count * Fields, [&](const std::uint32_t *values, std::size_t n) {
std::size_t i = 0;
if (used) {
while (used < Fields && i < n) pending[used++] = values[i++];
if (used == Fields) {
fast_io_detail::emit_record(emit, pending.data(), indices);
used = 0;
}
}
for (; i < n && n - i >= Fields; i += Fields) fast_io_detail::emit_record(emit, values + i, indices);
while (i < n) pending[used++] = values[i++];
});
}
#line 7 "test/min_plus_concave.test.cpp"
int main() {
FastInput input;
FastOutput output;
int n = 0, m = 0;
input.read(n), input.read(m);
std::vector<std::int64_t> a(n), b(m);
auto read = [&](std::vector<std::int64_t> &values) {
std::size_t offset = 0;
read_unsigned_blocks(input, values.size(), [&](const std::uint32_t *block, std::size_t count) {
for (std::size_t i = 0; i < count; ++i) values[offset + i] = block[i];
offset += count;
});
};
read(a), read(b);
const auto result = min_plus_convolution_concave(a, b);
write_unsigned_records<1>(output, result.size(), [&](std::size_t i) {
return std::array{std::uint32_t(result[i])};
});
}