This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include <iostream>
#include "library/util/coordinate_compressor.hpp"
template <typename T>
struct NaiveCompressor {
static constexpr int absent = suisen::CoordinateCompressorBuilder<T>::Compressor::absent;
NaiveCompressor(const std::vector<T>& a) : _sorted(a) {
std::sort(_sorted.begin(), _sorted.end());
_sorted.erase(std::unique(_sorted.begin(), _sorted.end()), _sorted.end());
// check if strictly increasing
assert(std::adjacent_find(_sorted.begin(), _sorted.end(), std::greater_equal<T>()) == _sorted.end());
}
// Return the minimum registered value greater than `e`. if not exists, return `default_value`.
T min_gt(const T& e, const T& default_value) const {
auto it = std::find_if(_sorted.begin(), _sorted.end(), [&](const T& x) { return x > e; });
if (it != _sorted.begin()) assert(*std::prev(it) <= e);
if (it != _sorted.end()) assert(*it > e);
return it == _sorted.end() ? default_value : *it;
}
// Return the minimum registered value greater than or equal to `e`. if not exists, return `default_value`.
T min_geq(const T& e, const T& default_value) const {
auto it = std::find_if(_sorted.begin(), _sorted.end(), [&](const T& x) { return x >= e; });
if (it != _sorted.begin()) assert(*std::prev(it) < e);
if (it != _sorted.end()) assert(*it >= e);
return it == _sorted.end() ? default_value : *it;
}
// Return the maximum registered value less than `e`. if not exists, return `default_value`
T max_lt(const T& e, const T& default_value) const {
auto it = std::find_if(_sorted.rbegin(), _sorted.rend(), [&](const T& x) { return x < e; });
if (it != _sorted.rbegin()) assert(*std::prev(it) >= e);
if (it != _sorted.rend()) assert(*it < e);
return it == _sorted.rend() ? default_value : *it;
}
// Return the maximum registered value less than or equal to `e`. if not exists, return `default_value`
T max_leq(const T& e, const T& default_value) const {
auto it = std::find_if(_sorted.rbegin(), _sorted.rend(), [&](const T& x) { return x <= e; });
if (it != _sorted.rbegin()) assert(*std::prev(it) > e);
if (it != _sorted.rend()) assert(*it <= e);
return it == _sorted.rend() ? default_value : *it;
}
// Return the compressed index of the minimum registered value greater than `e`. if not exists, return `compressor.size()`.
int min_gt_index(const T& e) const {
int i = std::find_if(_sorted.begin(), _sorted.end(), [&](const T& x) { return x > e; }) - _sorted.begin();
if (i > 0) assert(_sorted[i - 1] <= e);
if (i < int(_sorted.size())) assert(_sorted[i] > e);
else assert(i == int(_sorted.size()));
return i;
}
// Return the compressed index of the minimum registered value greater than or equal to `e`. if not exists, return `compressor.size()`.
int min_geq_index(const T& e) const {
int i = std::find_if(_sorted.begin(), _sorted.end(), [&](const T& x) { return x >= e; }) - _sorted.begin();
if (i > 0) assert(_sorted[i - 1] < e);
if (i < int(_sorted.size())) assert(_sorted[i] >= e);
else assert(i == int(_sorted.size()));
return i;
}
// Return the compressed index of the maximum registered value less than `e`. if not exists, return -1.
int max_lt_index(const T& e) const {
int i = _sorted.rend() - std::find_if(_sorted.rbegin(), _sorted.rend(), [&](const T& x) { return x < e; }) - 1;
if (i + 1 < int(_sorted.size())) assert(_sorted[i + 1] >= e);
if (i >= 0) assert(_sorted[i] < e);
else assert(i == -1);
return i;
}
// Return the compressed index of the maximum registered value less than or equal to `e`. if not exists, return -1.
int max_leq_index(const T& e) const {
int i = _sorted.rend() - std::find_if(_sorted.rbegin(), _sorted.rend(), [&](const T& x) { return x <= e; }) - 1;
if (i + 1 < int(_sorted.size())) assert(_sorted[i + 1] > e);
if (i >= 0) assert(_sorted[i] <= e);
else assert(i == -1);
return i;
}
private:
std::vector<T> _sorted;
};
void test(std::vector<int> a, int l, int r, int default_value) {
NaiveCompressor<int> comp_naive(a);
auto comp = suisen::CoordinateCompressorBuilder<int>::build(a);
for (int i = l; i <= r; ++i) {
assert(comp_naive.max_leq(i, default_value) == comp.max_leq(i, default_value));
assert(comp_naive.max_lt (i, default_value) == comp.max_lt (i, default_value));
assert(comp_naive.min_geq(i, default_value) == comp.min_geq(i, default_value));
assert(comp_naive.min_gt (i, default_value) == comp.min_gt (i, default_value));
assert(comp_naive.max_leq_index(i) == comp.max_leq_index(i));
assert(comp_naive.max_lt_index (i) == comp.max_lt_index (i));
assert(comp_naive.min_geq_index(i) == comp.min_geq_index(i));
assert(comp_naive.min_gt_index (i) == comp.min_gt_index (i));
}
}
void tests() {
test({ 1, 3, 6, 3, 10, 1 }, 0, 11, -1);
test({ -5, -4, 1, 4, -4, -6, 10, 12, 14, 14 }, -7, 15, 7);
test({ }, -10, +10, 0);
test({ 1, 1, 1, 1, 1, 1 }, 0, 2, 0);
}
int main() {
tests();
std::cout << "Hello World" << std::endl;
return 0;
}#line 1 "test/src/util/coordinate_compressor/dummy.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include <iostream>
#line 1 "library/util/coordinate_compressor.hpp"
#include <algorithm>
#include <cassert>
#include <vector>
#line 1 "library/type_traits/type_traits.hpp"
#include <limits>
#line 6 "library/type_traits/type_traits.hpp"
#include <type_traits>
namespace suisen {
template <typename ...Constraints> using constraints_t = std::enable_if_t<std::conjunction_v<Constraints...>, std::nullptr_t>;
template <typename T, typename = std::nullptr_t> struct bitnum { static constexpr int value = 0; };
template <typename T> struct bitnum<T, constraints_t<std::is_integral<T>>> { static constexpr int value = std::numeric_limits<std::make_unsigned_t<T>>::digits; };
template <typename T> static constexpr int bitnum_v = bitnum<T>::value;
template <typename T, size_t n> struct is_nbit { static constexpr bool value = bitnum_v<T> == n; };
template <typename T, size_t n> static constexpr bool is_nbit_v = is_nbit<T, n>::value;
template <typename T, typename = std::nullptr_t> struct safely_multipliable { using type = T; };
template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 32>>> { using type = long long; };
template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 64>>> { using type = __int128_t; };
template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 32>>> { using type = unsigned long long; };
template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 64>>> { using type = __uint128_t; };
template <typename T> using safely_multipliable_t = typename safely_multipliable<T>::type;
template <typename T, typename = void> struct rec_value_type { using type = T; };
template <typename T> struct rec_value_type<T, std::void_t<typename T::value_type>> {
using type = typename rec_value_type<typename T::value_type>::type;
};
template <typename T> using rec_value_type_t = typename rec_value_type<T>::type;
template <typename T> class is_iterable {
template <typename T_> static auto test(T_ e) -> decltype(e.begin(), e.end(), std::true_type{});
static std::false_type test(...);
public:
static constexpr bool value = decltype(test(std::declval<T>()))::value;
};
template <typename T> static constexpr bool is_iterable_v = is_iterable<T>::value;
template <typename T> class is_writable {
template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::ostream&>() << e, std::true_type{});
static std::false_type test(...);
public:
static constexpr bool value = decltype(test(std::declval<T>()))::value;
};
template <typename T> static constexpr bool is_writable_v = is_writable<T>::value;
template <typename T> class is_readable {
template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::istream&>() >> e, std::true_type{});
static std::false_type test(...);
public:
static constexpr bool value = decltype(test(std::declval<T>()))::value;
};
template <typename T> static constexpr bool is_readable_v = is_readable<T>::value;
} // namespace suisen
#line 9 "library/util/coordinate_compressor.hpp"
namespace suisen {
template <typename T>
class CoordinateCompressorBuilder {
public:
struct Compressor {
public:
static constexpr int absent = -1;
// default constructor
Compressor() : _xs(std::vector<T>{}) {}
// Construct from strictly sorted vector
Compressor(const std::vector<T> &xs) : _xs(xs) {
assert(is_strictly_sorted(xs));
}
// Return the number of distinct keys.
int size() const {
return _xs.size();
}
// Check if the element is registered.
bool has_key(const T &e) const {
return std::binary_search(_xs.begin(), _xs.end(), e);
}
// Compress the element. if not registered, returns `default_value`. (default: Compressor::absent)
int comp(const T &e, int default_value = absent) const {
const int res = min_geq_index(e);
return res != size() and _xs[res] == e ? res : default_value;
}
// Restore the element from the index.
T decomp(const int compressed_index) const {
return _xs[compressed_index];
}
// Compress the element. Equivalent to call `comp(e)`
int operator[](const T &e) const {
return comp(e);
}
// Return the minimum registered value greater than `e`. if not exists, return `default_value`.
T min_gt(const T &e, const T &default_value) const {
auto it = std::upper_bound(_xs.begin(), _xs.end(), e);
return it == _xs.end() ? default_value : *it;
}
// Return the minimum registered value greater than or equal to `e`. if not exists, return `default_value`.
T min_geq(const T &e, const T &default_value) const {
auto it = std::lower_bound(_xs.begin(), _xs.end(), e);
return it == _xs.end() ? default_value : *it;
}
// Return the maximum registered value less than `e`. if not exists, return `default_value`
T max_lt(const T &e, const T &default_value) const {
auto it = std::upper_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>());
return it == _xs.rend() ? default_value : *it;
}
// Return the maximum registered value less than or equal to `e`. if not exists, return `default_value`
T max_leq(const T &e, const T &default_value) const {
auto it = std::lower_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>());
return it == _xs.rend() ? default_value : *it;
}
// Return the compressed index of the minimum registered value greater than `e`. if not exists, return `compressor.size()`.
int min_gt_index(const T &e) const {
return std::upper_bound(_xs.begin(), _xs.end(), e) - _xs.begin();
}
// Return the compressed index of the minimum registered value greater than or equal to `e`. if not exists, return `compressor.size()`.
int min_geq_index(const T &e) const {
return std::lower_bound(_xs.begin(), _xs.end(), e) - _xs.begin();
}
// Return the compressed index of the maximum registered value less than `e`. if not exists, return -1.
int max_lt_index(const T &e) const {
return int(_xs.rend() - std::upper_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>())) - 1;
}
// Return the compressed index of the maximum registered value less than or equal to `e`. if not exists, return -1.
int max_leq_index(const T &e) const {
return int(_xs.rend() - std::lower_bound(_xs.rbegin(), _xs.rend(), e, std::greater<T>())) - 1;
}
private:
std::vector<T> _xs;
static bool is_strictly_sorted(const std::vector<T> &v) {
return std::adjacent_find(v.begin(), v.end(), std::greater_equal<T>()) == v.end();
}
};
CoordinateCompressorBuilder() : _xs(std::vector<T>{}) {}
explicit CoordinateCompressorBuilder(const std::vector<T> &xs) : _xs(xs) {}
explicit CoordinateCompressorBuilder(std::vector<T> &&xs) : _xs(std::move(xs)) {}
template <typename Gen, constraints_t<std::is_invocable_r<T, Gen, int>> = nullptr>
CoordinateCompressorBuilder(const int n, Gen generator) {
reserve(n);
for (int i = 0; i < n; ++i) push(generator(i));
}
// Attempt to preallocate enough memory for specified number of elements.
void reserve(int n) {
_xs.reserve(n);
}
// Add data.
void push(const T &first) {
_xs.push_back(first);
}
// Add data.
void push(T &&first) {
_xs.push_back(std::move(first));
}
// Add data in the range of [first, last).
template <typename Iterator>
auto push(const Iterator &first, const Iterator &last) -> decltype(std::vector<T>{}.push_back(*first), void()) {
for (auto it = first; it != last; ++it) _xs.push_back(*it);
}
// Add all data in the container. Equivalent to `push(iterable.begin(), iterable.end())`.
template <typename Iterable>
auto push(const Iterable &iterable) -> decltype(std::vector<T>{}.push_back(*iterable.begin()), void()) {
push(iterable.begin(), iterable.end());
}
// Add data.
template <typename ...Args>
void emplace(Args &&...args) {
_xs.emplace_back(std::forward<Args>(args)...);
}
// Build compressor.
auto build() {
std::sort(_xs.begin(), _xs.end()), _xs.erase(std::unique(_xs.begin(), _xs.end()), _xs.end());
return Compressor {_xs};
}
// Build compressor from vector.
static auto build(const std::vector<T> &xs) {
return CoordinateCompressorBuilder(xs).build();
}
// Build compressor from vector.
static auto build(std::vector<T> &&xs) {
return CoordinateCompressorBuilder(std::move(xs)).build();
}
// Build compressor from generator.
template <typename Gen, constraints_t<std::is_invocable_r<T, Gen, int>> = nullptr>
static auto build(const int n, Gen generator) {
return CoordinateCompressorBuilder<T>(n, generator).build();
}
private:
std::vector<T> _xs;
};
} // namespace suisen
#line 6 "test/src/util/coordinate_compressor/dummy.test.cpp"
template <typename T>
struct NaiveCompressor {
static constexpr int absent = suisen::CoordinateCompressorBuilder<T>::Compressor::absent;
NaiveCompressor(const std::vector<T>& a) : _sorted(a) {
std::sort(_sorted.begin(), _sorted.end());
_sorted.erase(std::unique(_sorted.begin(), _sorted.end()), _sorted.end());
// check if strictly increasing
assert(std::adjacent_find(_sorted.begin(), _sorted.end(), std::greater_equal<T>()) == _sorted.end());
}
// Return the minimum registered value greater than `e`. if not exists, return `default_value`.
T min_gt(const T& e, const T& default_value) const {
auto it = std::find_if(_sorted.begin(), _sorted.end(), [&](const T& x) { return x > e; });
if (it != _sorted.begin()) assert(*std::prev(it) <= e);
if (it != _sorted.end()) assert(*it > e);
return it == _sorted.end() ? default_value : *it;
}
// Return the minimum registered value greater than or equal to `e`. if not exists, return `default_value`.
T min_geq(const T& e, const T& default_value) const {
auto it = std::find_if(_sorted.begin(), _sorted.end(), [&](const T& x) { return x >= e; });
if (it != _sorted.begin()) assert(*std::prev(it) < e);
if (it != _sorted.end()) assert(*it >= e);
return it == _sorted.end() ? default_value : *it;
}
// Return the maximum registered value less than `e`. if not exists, return `default_value`
T max_lt(const T& e, const T& default_value) const {
auto it = std::find_if(_sorted.rbegin(), _sorted.rend(), [&](const T& x) { return x < e; });
if (it != _sorted.rbegin()) assert(*std::prev(it) >= e);
if (it != _sorted.rend()) assert(*it < e);
return it == _sorted.rend() ? default_value : *it;
}
// Return the maximum registered value less than or equal to `e`. if not exists, return `default_value`
T max_leq(const T& e, const T& default_value) const {
auto it = std::find_if(_sorted.rbegin(), _sorted.rend(), [&](const T& x) { return x <= e; });
if (it != _sorted.rbegin()) assert(*std::prev(it) > e);
if (it != _sorted.rend()) assert(*it <= e);
return it == _sorted.rend() ? default_value : *it;
}
// Return the compressed index of the minimum registered value greater than `e`. if not exists, return `compressor.size()`.
int min_gt_index(const T& e) const {
int i = std::find_if(_sorted.begin(), _sorted.end(), [&](const T& x) { return x > e; }) - _sorted.begin();
if (i > 0) assert(_sorted[i - 1] <= e);
if (i < int(_sorted.size())) assert(_sorted[i] > e);
else assert(i == int(_sorted.size()));
return i;
}
// Return the compressed index of the minimum registered value greater than or equal to `e`. if not exists, return `compressor.size()`.
int min_geq_index(const T& e) const {
int i = std::find_if(_sorted.begin(), _sorted.end(), [&](const T& x) { return x >= e; }) - _sorted.begin();
if (i > 0) assert(_sorted[i - 1] < e);
if (i < int(_sorted.size())) assert(_sorted[i] >= e);
else assert(i == int(_sorted.size()));
return i;
}
// Return the compressed index of the maximum registered value less than `e`. if not exists, return -1.
int max_lt_index(const T& e) const {
int i = _sorted.rend() - std::find_if(_sorted.rbegin(), _sorted.rend(), [&](const T& x) { return x < e; }) - 1;
if (i + 1 < int(_sorted.size())) assert(_sorted[i + 1] >= e);
if (i >= 0) assert(_sorted[i] < e);
else assert(i == -1);
return i;
}
// Return the compressed index of the maximum registered value less than or equal to `e`. if not exists, return -1.
int max_leq_index(const T& e) const {
int i = _sorted.rend() - std::find_if(_sorted.rbegin(), _sorted.rend(), [&](const T& x) { return x <= e; }) - 1;
if (i + 1 < int(_sorted.size())) assert(_sorted[i + 1] > e);
if (i >= 0) assert(_sorted[i] <= e);
else assert(i == -1);
return i;
}
private:
std::vector<T> _sorted;
};
void test(std::vector<int> a, int l, int r, int default_value) {
NaiveCompressor<int> comp_naive(a);
auto comp = suisen::CoordinateCompressorBuilder<int>::build(a);
for (int i = l; i <= r; ++i) {
assert(comp_naive.max_leq(i, default_value) == comp.max_leq(i, default_value));
assert(comp_naive.max_lt (i, default_value) == comp.max_lt (i, default_value));
assert(comp_naive.min_geq(i, default_value) == comp.min_geq(i, default_value));
assert(comp_naive.min_gt (i, default_value) == comp.min_gt (i, default_value));
assert(comp_naive.max_leq_index(i) == comp.max_leq_index(i));
assert(comp_naive.max_lt_index (i) == comp.max_lt_index (i));
assert(comp_naive.min_geq_index(i) == comp.min_geq_index(i));
assert(comp_naive.min_gt_index (i) == comp.min_gt_index (i));
}
}
void tests() {
test({ 1, 3, 6, 3, 10, 1 }, 0, 11, -1);
test({ -5, -4, 1, 4, -4, -6, 10, 12, 14, 14 }, -7, 15, 7);
test({ }, -10, +10, 0);
test({ 1, 1, 1, 1, 1, 1 }, 0, 2, 0);
}
int main() {
tests();
std::cout << "Hello World" << std::endl;
return 0;
}