競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_7_D"
#include <iostream>
#include "../../../src/data_structures/counter.hpp"
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int q;
std::cin >> q;
Counter<long long> s;
long long total = 0; // 要素数 (重複を含む)
while (q--) {
int op;
std::cin >> op;
if (op == 0) {
long long x;
std::cin >> x;
s.inc(x, 1);
total++;
std::cout << total << '\n';
} else if (op == 1) {
long long x;
std::cin >> x;
std::cout << s[x] << '\n';
} else if (op == 2) {
long long x;
std::cin >> x;
total -= s[x];
s[x] = 0; // 個数を 0 にすると取り除かれる
} else {
long long l, r;
std::cin >> l >> r;
for (auto it = s.lower_bound(l); it != s.end() && it->first <= r; ++it) {
for (long long i = 0; i < it->second; i++) std::cout << it->first << '\n';
}
}
}
}#line 1 "test/aoj/itp2/itp2-7-d.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_7_D"
#include <iostream>
#line 2 "src/data_structures/counter.hpp"
/**
* @file counter.hpp
* @brief 値の個数を数える多重集合 (Counter)
*/
#include <cassert>
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <map>
#include <optional>
#include <set>
#include <type_traits>
#include <utility>
#include <vector>
#line 2 "src/utility/traits.hpp"
/**
* @file traits.hpp
* @brief 型特性
*/
#line 9 "src/utility/traits.hpp"
#include <functional>
#line 14 "src/utility/traits.hpp"
#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 20 "src/data_structures/counter.hpp"
template <typename T = long long>
struct Counter {
using key_type = T;
using mapped_type = long long;
using value_type = std::pair<key_type, mapped_type>;
private:
struct CompareFrequency {
bool operator()(const value_type& a, const value_type& b) const {
return a.second > b.second || (a.second == b.second && a.first < b.first);
}
};
std::map<key_type, mapped_type> data;
std::set<value_type, CompareFrequency> frequency;
mapped_type total_count = 0;
public:
void inc(const key_type& key, const mapped_type& t = 1) {
set(key, get(key) + t);
}
void dec(const key_type& key, const mapped_type& t = 1) {
set(key, get(key) - t);
}
Counter() = default;
explicit Counter(const key_type& x) { set(x, 1); }
/**
* @brief 範囲の要素を数える
* Counter 自身と key_type (std::string など範囲でもあるもの) はこちらで受け取らない
*/
#if __cplusplus >= 202002L
template <class C>
requires(!std::same_as<std::decay_t<C>, Counter> && !std::convertible_to<C, key_type> &&
requires(C& c) { std::begin(c); std::end(c); })
#else
template <class C, ENABLE_IF_T(!std::is_same_v<std::decay_t<C>, Counter> && !std::is_convertible_v<C, key_type>),
class = decltype(std::begin(std::declval<C&>()), std::end(std::declval<C&>()))>
#endif
explicit Counter(C&& v) {
for (const key_type& a : v) {
inc(a);
}
}
Counter(std::initializer_list<value_type> init) {
for (const auto& p : init) {
inc(p.first, p.second);
}
}
Counter& operator+=(const Counter& o) {
for (const auto& [i, c] : o.data) {
inc(i, c);
}
return *this;
}
friend Counter operator+(const Counter& a, const Counter& b) {
Counter res(a);
return res += b;
}
Counter& operator-=(const Counter& o) {
// 自分自身を引くと、走査中の o.data の要素を dec が消してしまう
if (this == &o) {
data.clear();
frequency.clear();
total_count = 0;
return *this;
}
for (const auto& [i, c] : o.data) {
dec(i, c);
}
return *this;
}
friend Counter operator-(const Counter& a, const Counter& b) {
Counter res(a);
return res -= b;
}
auto begin() const { return data.begin(); }
auto end() const { return data.end(); }
/** @brief key 以上の最初の値の読み取り専用イテレータ (なければ end()) */
auto lower_bound(const key_type& key) const { return data.lower_bound(key); }
/** @brief key より大きい最初の値の読み取り専用イテレータ (なければ end()) */
auto upper_bound(const key_type& key) const { return data.upper_bound(key); }
/**
* @brief 異なる値の個数 (重複は数えない)
*/
long long size() const {
return static_cast<long long>(data.size());
}
/**
* @brief 重複を含めた要素数 (個数の総和)
* @note Python の collections.Counter.total() と同じ。O(1)
*/
mapped_type total() const {
return total_count;
}
mapped_type get(const key_type& key) const {
auto itr = data.find(key);
if (itr == data.end()) return 0;
return itr->second;
}
void set(const key_type& key, const mapped_type& t) {
assert(t >= 0);
auto itr = data.find(key);
total_count += t - (itr == data.end() ? 0 : itr->second);
if (itr != data.end()) frequency.erase(*itr);
if (t > 0) {
data[key] = t;
frequency.emplace(key, t);
} else if (itr != data.end()) {
data.erase(itr);
}
}
struct Mode {
T value;
long long count;
};
/**
* @brief 最頻値とその個数を返す。同数なら最小の値を選び、空なら std::nullopt を返す
* @note O(1)
*/
std::optional<Mode> mode() const {
if (frequency.empty()) return std::nullopt;
const auto& [value, count] = *frequency.begin();
return Mode{value, count};
}
/**
* @brief 個数の降順、同数なら値の昇順に並べた内部 set の const 参照 (コピーしない)
* @note O(1)。Counter の更新は参照先にも反映される。更新した値の要素へのイテレータは失効する
*/
const auto& by_frequency() const { return frequency; }
std::vector<value_type> most_common() const {
return {frequency.begin(), frequency.end()};
}
/**
* @brief 個数の多い順に上位 k 件を返す。同数なら値の昇順。k >= 0
* @note k が異なる値の個数より大きければ全件を返す。O(min(k, size()))
*/
std::vector<value_type> most_common(long long k) const {
assert(k >= 0);
if (k > size()) k = size();
std::vector<value_type> result;
result.reserve(k);
auto itr = frequency.begin();
for (long long i = 0; i < k; ++i, ++itr) result.push_back(*itr);
return result;
}
class CounterReference {
Counter& base;
key_type key;
public:
CounterReference(Counter& base, key_type key) : base(base), key(key) {}
CounterReference& operator=(const mapped_type& x) {
base.set(key, x);
return *this;
}
CounterReference& operator+=(const mapped_type& x) {
base.inc(key, x);
return *this;
}
CounterReference& operator-=(const mapped_type& x) {
base.dec(key, x);
return *this;
}
operator mapped_type() const {
return base.get(key);
}
};
mapped_type operator[](const key_type& key) const {
return get(key);
}
CounterReference operator[](const key_type& key) {
return CounterReference(*this, key);
}
};
template <typename T>
Counter(std::vector<T>) -> Counter<T>;
#line 6 "test/aoj/itp2/itp2-7-d.test.cpp"
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int q;
std::cin >> q;
Counter<long long> s;
long long total = 0; // 要素数 (重複を含む)
while (q--) {
int op;
std::cin >> op;
if (op == 0) {
long long x;
std::cin >> x;
s.inc(x, 1);
total++;
std::cout << total << '\n';
} else if (op == 1) {
long long x;
std::cin >> x;
std::cout << s[x] << '\n';
} else if (op == 2) {
long long x;
std::cin >> x;
total -= s[x];
s[x] = 0; // 個数を 0 にすると取り除かれる
} else {
long long l, r;
std::cin >> l >> r;
for (auto it = s.lower_bound(l); it != s.end() && it->first <= r; ++it) {
for (long long i = 0; i < it->second; i++) std::cout << it->first << '\n';
}
}
}
}