This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include "../../../template/template.cpp"
#include "../../../misc/compress.hpp"
#include "../../../misc/floor-ceil-div.hpp"
#include "../../../misc/rle.hpp"
#include "../../../misc/inversion-number.hpp"
#include "../../../misc/mo.hpp"
#include "../../../misc/timer.hpp"
#include "../../../random/rng.hpp"
int main() {
for(ll a = -100; a <= 100; ++a) for(ll b = -20; b <= 20; ++b) if(b) {
assert(floor_div(a, b) == ll(floor((long double)a / b)));
assert(ceil_div(a, b) == ll(ceil((long double)a / b)));
}
Compress<int> compression; assert(compression.size() == 0);
for(int x : {7, -2, 7, 0}) compression.push_back(x);
compression.build(); const auto& c = compression;
assert(c.size() == 3 && c[0] == -2 && c[1] == 0 && c[2] == 7);
for(int x = -5; x <= 10; ++x) {
vector<int> ref{-2, 0, 7}; assert(c.get(x) == lower_bound(ref.begin(), ref.end(), x) - ref.begin());
}
Compress<int> constructed(vector<int>{7, -2, 7, 0}); assert(constructed.size() == c.size());
assert(runlength_encoding(string()).empty() && runlength_encoding(vector<int>{}).empty());
assert((runlength_encoding(string("aaabbcaa")) == vector<pair<char, int>>{ {'a', 3}, {'b', 2}, {'c', 1}, {'a', 2} }));
assert((runlength_encoding(vector<int>{1, 1, -2, 3, 3}) == vector<pair<int, int>>{ {1, 2}, {-2, 1}, {3, 2} }));
mt19937 rng(64);
for(int n = 0; n <= 30; ++n) {
vector<int> a(n); for(auto& x : a) x = rng() % n;
ll expected = 0; for(int i = 0; i < n; ++i) for(int j = i + 1; j < n; ++j) expected += a[i] > a[j];
assert(inversion_number<ll>(a) == expected);
Mo mo(n); mo.set_bucketsize(0);
vector<pair<int, int>> ranges;
for(int l = 0; l < n; ++l) for(int r = l + 1; r <= n; ++r) { mo.insert(l, r); ranges.emplace_back(l, r); }
mo.build(); deque<int> current; vector<bool> seen(ranges.size());
auto al = [&](int i) { current.push_front(i); };
auto ar = [&](int i) { current.push_back(i); };
auto dl = [&](int i) { assert(!current.empty() && current.front() == i); current.pop_front(); };
auto dr = [&](int i) { assert(!current.empty() && current.back() == i); current.pop_back(); };
for(int t = 0; t < int(ranges.size()); ++t) {
int q = mo.process(al, ar, dl, dr); assert(q >= 0 && q < int(ranges.size()) && !seen[q]); seen[q] = true;
auto [l, r] = ranges[q]; vector<int> expected(r - l); iota(expected.begin(), expected.end(), l);
assert(vector<int>(current.begin(), current.end()) == expected);
}
assert(mo.process(al, ar, dl, dr) == -1 && mo.process(al, ar, dl, dr) == -1);
}
Timer timer;
auto lower = chrono::high_resolution_clock::now(); timer.reset(); auto upper = chrono::high_resolution_clock::now();
assert(lower <= timer.start_time && timer.start_time <= upper);
timer.start_time = chrono::high_resolution_clock::now() - chrono::milliseconds(100);
assert(timer.elapsed() >= 100 && timer() >= 100);
RNG32 r32; RNG64 r64; RNG_0_1 real;
for(int i = 0; i < 1000; ++i) {
int a = r32.randint(-5, 3), b = r32.randint(7);
ll c = r64.randint(-1000000000000LL, 1000000000000LL), d = r64.randint(7);
double x = real.rand(), y = real();
assert(-5 <= a && a < 3 && 0 <= b && b < 7);
assert(-1000000000000LL <= c && c < 1000000000000LL && 0 <= d && d < 7);
assert(0 <= x && x < 1 && 0 <= y && y < 1);
}
assert(r32.randint(4, 5) == 4 && r64.randint(-9, -8) == -9);
INT(a0, b0); print(a0 + b0);
}#line 1 "test/mytest/misc/utilities.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 1 "template/template.cpp"
// #region Macros
#include <bits/stdc++.h>
using namespace std;
// input output utils
namespace siro53_io {
// https://maspypy.github.io/library/other/io_old.hpp
struct has_val_impl {
template <class T>
static auto check(T &&x) -> decltype(x.val(), std::true_type{});
template <class T> static auto check(...) -> std::false_type;
};
template <class T>
class has_val : public decltype(has_val_impl::check<T>(std::declval<T>())) {
};
// debug
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void dump(const T t) {
cerr << t;
}
template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
void dump(const T t) {
cerr << t;
}
template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
void dump(const T &t) {
cerr << t.val();
}
void dump(__int128_t n) {
if(n == 0) {
cerr << '0';
return;
} else if(n < 0) {
cerr << '-';
n = -n;
}
string s;
while(n > 0) {
s += (char)('0' + n % 10);
n /= 10;
}
reverse(s.begin(), s.end());
cerr << s;
}
void dump(const string &s) { cerr << s; }
void dump(const char *s) {
int n = (int)strlen(s);
for(int i = 0; i < n; i++) cerr << s[i];
}
template <class T1, class T2> void dump(const pair<T1, T2> &p) {
cerr << '(';
dump(p.first);
cerr << ',';
dump(p.second);
cerr << ')';
}
template <class T> void dump(const vector<T> &v) {
cerr << '{';
for(int i = 0; i < (int)v.size(); i++) {
dump(v[i]);
if(i < (int)v.size() - 1) cerr << ',';
}
cerr << '}';
}
template <class T> void dump(const set<T> &s) {
cerr << '{';
for(auto it = s.begin(); it != s.end(); it++) {
dump(*it);
if(next(it) != s.end()) cerr << ',';
}
cerr << '}';
}
template <class Key, class Value> void dump(const map<Key, Value> &mp) {
cerr << '{';
for(auto it = mp.begin(); it != mp.end(); it++) {
dump(*it);
if(next(it) != mp.end()) cerr << ',';
}
cerr << '}';
}
template <class Key, class Value>
void dump(const unordered_map<Key, Value> &mp) {
cerr << '{';
for(auto it = mp.begin(); it != mp.end(); it++) {
dump(*it);
if(next(it) != mp.end()) cerr << ',';
}
cerr << '}';
}
template <class T> void dump(const deque<T> &v) {
cerr << '{';
for(int i = 0; i < (int)v.size(); i++) {
dump(v[i]);
if(i < (int)v.size() - 1) cerr << ',';
}
cerr << '}';
}
template <class T> void dump(queue<T> q) {
cerr << '{';
while(!q.empty()) {
dump(q.front());
if((int)q.size() > 1) cerr << ',';
q.pop();
}
cerr << '}';
}
void debug_print() { cerr << endl; }
template <class Head, class... Tail>
void debug_print(const Head &h, const Tail &...t) {
dump(h);
if(sizeof...(Tail)) dump(' ');
debug_print(t...);
}
// print
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void print_single(const T t) {
cout << t;
}
template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
void print_single(const T t) {
cout << t;
}
template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
void print_single(const T t) {
cout << t.val();
}
void print_single(__int128_t n) {
if(n == 0) {
cout << '0';
return;
} else if(n < 0) {
cout << '-';
n = -n;
}
string s;
while(n > 0) {
s += (char)('0' + n % 10);
n /= 10;
}
reverse(s.begin(), s.end());
cout << s;
}
void print_single(const string &s) { cout << s; }
void print_single(const char *s) {
int n = (int)strlen(s);
for(int i = 0; i < n; i++) cout << s[i];
}
template <class T1, class T2> void print_single(const pair<T1, T2> &p) {
print_single(p.first);
cout << ' ';
print_single(p.second);
}
template <class T> void print_single(const vector<T> &v) {
for(int i = 0; i < (int)v.size(); i++) {
print_single(v[i]);
if(i < (int)v.size() - 1) cout << ' ';
}
}
template <class T> void print_single(const set<T> &s) {
for(auto it = s.begin(); it != s.end(); it++) {
print_single(*it);
if(next(it) != s.end()) cout << ' ';
}
}
template <class T> void print_single(const deque<T> &v) {
for(int i = 0; i < (int)v.size(); i++) {
print_single(v[i]);
if(i < (int)v.size() - 1) cout << ' ';
}
}
template <class T> void print_single(queue<T> q) {
while(!q.empty()) {
print_single(q.front());
if((int)q.size() > 1) cout << ' ';
q.pop();
}
}
void print() { cout << '\n'; }
template <class Head, class... Tail>
void print(const Head &h, const Tail &...t) {
print_single(h);
if(sizeof...(Tail)) print_single(' ');
print(t...);
}
// input
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void input_single(T &t) {
cin >> t;
}
template <class T, enable_if_t<is_floating_point<T>::value, int> = 0>
void input_single(T &t) {
cin >> t;
}
template <class T, typename enable_if<has_val<T>::value>::type * = nullptr>
void input_single(T &t) {
cin >> t;
}
void input_single(__int128_t &n) {
string s;
cin >> s;
if(s == "0") {
n = 0;
return;
}
bool is_minus = false;
if(s[0] == '-') {
s = s.substr(1);
is_minus = true;
}
n = 0;
for(int i = 0; i < (int)s.size(); i++) n = n * 10 + (int)(s[i] - '0');
if(is_minus) n = -n;
}
void input_single(string &s) { cin >> s; }
template <class T1, class T2> void input_single(pair<T1, T2> &p) {
input_single(p.first);
input_single(p.second);
}
template <class T> void input_single(vector<T> &v) {
for(auto &e : v) input_single(e);
}
void input() {}
template <class Head, class... Tail> void input(Head &h, Tail &...t) {
input_single(h);
input(t...);
}
}; // namespace siro53_io
#ifdef DEBUG
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debug_print(__VA_ARGS__)
#else
#define debug(...) (void(0))
#endif
// io setup
struct Setup {
Setup() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} __Setup;
using namespace siro53_io;
// types
using ll = long long;
using i128 = __int128_t;
// input macros
#define INT(...) \
int __VA_ARGS__; \
input(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
input(__VA_ARGS__)
#define STRING(...) \
string __VA_ARGS__; \
input(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
input(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
input(__VA_ARGS__)
#define LD(...) \
long double __VA_ARGS__; \
input(__VA_ARGS__)
#define UINT(...) \
unsigned int __VA_ARGS__; \
input(__VA_ARGS__)
#define ULL(...) \
unsigned long long __VA_ARGS__; \
input(__VA_ARGS__)
#define VEC(name, type, len) \
vector<type> name(len); \
input(name);
#define VEC2(name, type, len1, len2) \
vector name(len1, vector<type>(len2)); \
input(name);
// other macros
// https://trap.jp/post/1224/
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < int(n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(RALL(v))
#define UNIQUE(v) \
sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()), v.shrink_to_fit()
#define REV(v) reverse(ALL(v))
#define SZ(v) ((int)(v).size())
#define MIN(v) (*min_element(ALL(v)))
#define MAX(v) (*max_element(ALL(v)))
// util const
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// util functions
void Case(int i) { cout << "Case #" << i << ": "; }
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
template <class T> inline bool chmax(T &a, T b) {
return (a < b ? a = b, true : false);
}
template <class T> inline bool chmin(T &a, T b) {
return (a > b ? a = b, true : false);
}
template <class T, int dim>
auto make_vector_impl(vector<int>& sizes, const T &e) {
if constexpr(dim == 1) {
return vector(sizes[0], e);
} else {
int n = sizes[dim - 1];
sizes.pop_back();
return vector(n, make_vector_impl<T, dim - 1>(sizes, e));
}
}
template <class T, int dim>
auto make_vector(const int (&sizes)[dim], const T &e = T()) {
vector<int> s(dim);
for(int i = 0; i < dim; i++) s[i] = sizes[dim - i - 1];
return make_vector_impl<T, dim>(s, e);
}
vector<int> iota_gen(int n, int start = 0) {
vector<int> ord(n);
iota(ord.begin(), ord.end(), start);
return ord;
}
template<typename T>
vector<int> ord_sort(const vector<T>& v, bool greater = false) {
auto ord = iota_gen((int)v.size());
sort(ALL(ord), [&](int i, int j) {
if(greater) return v[i] > v[j];
return v[i] < v[j];
});
return ord;
}
// #endregion Macros
#line 2 "misc/compress.hpp"
#line 5 "misc/compress.hpp"
template <typename T> class Compress {
public:
Compress() = default;
explicit Compress(const std::vector<T> &v) : dat(v) {
build();
}
void push_back(T val) { dat.push_back(val); }
void build() {
std::sort(dat.begin(), dat.end());
dat.erase(std::unique(dat.begin(), dat.end()), dat.end());
}
int get(T val) const {
int pos = std::lower_bound(dat.begin(), dat.end(), val) - dat.begin();
return pos;
}
T operator[](int i) const { return dat[i]; }
size_t size() const { return dat.size(); }
private:
std::vector<T> dat;
};
#line 2 "misc/floor-ceil-div.hpp"
template <typename T> T ceil_div(T a, T b) {
if(b < 0) a = -a, b = -b;
return (a >= 0 ? (a + b - 1) / b : a / b);
}
template <typename T> T floor_div(T a, T b) {
if(b < 0) a = -a, b = -b;
return (a >= 0 ? a / b : (a - b + 1) / b);
}
#line 2 "misc/rle.hpp"
#line 6 "misc/rle.hpp"
template<class T>
std::vector<std::pair<T, int>> runlength_encoding(const std::vector<T>& v) {
if(v.empty()) return {};
std::vector<std::pair<T, int>> ret = {{v[0], 1}};
for(size_t i = 1; i < v.size(); i++) {
if(ret.back().first == v[i]) ret.back().second++;
else ret.emplace_back(v[i], 1);
}
return ret;
}
inline std::vector<std::pair<char, int>> runlength_encoding(const std::string& s) {
if(s.empty()) return {};
std::vector<std::pair<char, int>> ret = {{s[0], 1}};
for(size_t i = 1; i < s.size(); i++) {
if(ret.back().first == s[i]) ret.back().second++;
else ret.emplace_back(s[i], 1);
}
return ret;
}
#line 2 "misc/inversion-number.hpp"
#line 2 "data-structure/BIT.hpp"
#line 5 "data-structure/BIT.hpp"
template <typename T> class BIT {
public:
BIT() = default;
explicit BIT(int N) : N(N), dat(N + 1, 0) {}
T sum(int r) {
T ret = 0;
for(; r >= 1; r -= lsb(r)) ret += dat[r];
return ret;
}
T sum(int l, int r) {
assert(l <= r);
if(l == r) return T(0);
return (sum(r) - sum(l));
}
void add(int pos, T val) {
for(int i = pos + 1; i <= N; i += lsb(i)) dat[i] += val;
}
int lower_bound(T val) {
int pos = 0;
int k = 1;
while(2 * k <= N) k <<= 1;
for(; k >= 1; k >>= 1) {
if(pos + k <= N and dat[pos + k] < val) {
pos += k;
val -= dat[pos];
}
}
return pos + 1;
}
private:
int N;
std::vector<T> dat;
inline int lsb(int i) { return i & (-i); }
};
#line 4 "misc/inversion-number.hpp"
template<class T>
T inversion_number(const std::vector<int>& v) {
int N = (int)v.size();
BIT<int> bt(N);
T res = 0;
for(int i = 0; i < N; i++) {
res += i - bt.sum(v[i]+1);
bt.add(v[i], 1);
}
return res;
}
#line 2 "misc/mo.hpp"
#line 8 "misc/mo.hpp"
class Mo {
public:
explicit Mo(int n)
: n(n), l(0), r(0), idx(0), BUCKET_SIZE(std::sqrt(n)), isBuilt(false) {}
void insert(int l, int r) {
assert(0 <= l and l < r and r <= n);
L.push_back(l);
R.push_back(r);
}
void set_bucketsize(int bs) {
assert(!isBuilt);
BUCKET_SIZE = std::max(1, bs);
}
void build() {
ord.resize(L.size());
std::iota(ord.begin(), ord.end(), 0);
std::sort(ord.begin(), ord.end(), [&](int a, int b) {
if(L[a] / BUCKET_SIZE != L[b] / BUCKET_SIZE) {
return (L[a] < L[b]);
}
return (R[a] < R[b]);
});
isBuilt = true;
}
template <class AddLeft, class AddRight, class DelLeft, class DelRight>
int process(const AddLeft &add_left, const AddRight &add_right,
const DelLeft &del_left, const DelRight &del_right) {
assert(isBuilt);
if(idx == (int)ord.size()) return -1;
int qid = ord[idx++];
while(l > L[qid]) add_left(--l);
while(r < R[qid]) add_right(r++);
while(l < L[qid]) del_left(l++);
while(r > R[qid]) del_right(--r);
return qid;
}
private:
std::vector<int> L, R, ord;
int n, l, r, idx;
int BUCKET_SIZE;
bool isBuilt;
};
#line 2 "misc/timer.hpp"
#line 4 "misc/timer.hpp"
struct Timer {
std::chrono::high_resolution_clock::time_point start_time;
Timer() { reset(); }
// タイマーをリセットする
void reset() {
start_time = std::chrono::high_resolution_clock::now();
}
// タイマーセットしてから何 milliseconds 経過したかを返す
long long elapsed() {
auto end_time = std::chrono::high_resolution_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
return elapsed_time.count();
}
// タイマーセットしてから何 milliseconds 経過したかを返す
auto operator()() { return elapsed(); }
};
#line 2 "random/rng.hpp"
#line 5 "random/rng.hpp"
class RNG32 {
public:
RNG32() : mt(std::chrono::steady_clock::now().time_since_epoch().count()) {}
// [l, r)
int randint(int l, int r) {
std::uniform_int_distribution<int> dist(l, r - 1);
return dist(mt);
}
int randint(int r) { return randint(0, r); }
private:
std::mt19937 mt;
};
class RNG64 {
public:
RNG64() : mt(std::chrono::steady_clock::now().time_since_epoch().count()) {}
// [l, r)
long long randint(long long l, long long r) {
std::uniform_int_distribution<long long> dist(l, r - 1);
return dist(mt);
}
long long randint(long long r) { return randint(0, r); }
private:
std::mt19937_64 mt;
};
class RNG_0_1 {
public:
RNG_0_1() : mt(std::chrono::steady_clock::now().time_since_epoch().count()) {}
// [0.0, 1.0)
double rand() {
auto bits = mt() >> 11;
return (double)bits / denomitor;
}
auto operator()() { return rand(); }
private:
std::mt19937_64 mt;
const double denomitor = 1LL << 53;
};
#line 10 "test/mytest/misc/utilities.test.cpp"
int main() {
for(ll a = -100; a <= 100; ++a) for(ll b = -20; b <= 20; ++b) if(b) {
assert(floor_div(a, b) == ll(floor((long double)a / b)));
assert(ceil_div(a, b) == ll(ceil((long double)a / b)));
}
Compress<int> compression; assert(compression.size() == 0);
for(int x : {7, -2, 7, 0}) compression.push_back(x);
compression.build(); const auto& c = compression;
assert(c.size() == 3 && c[0] == -2 && c[1] == 0 && c[2] == 7);
for(int x = -5; x <= 10; ++x) {
vector<int> ref{-2, 0, 7}; assert(c.get(x) == lower_bound(ref.begin(), ref.end(), x) - ref.begin());
}
Compress<int> constructed(vector<int>{7, -2, 7, 0}); assert(constructed.size() == c.size());
assert(runlength_encoding(string()).empty() && runlength_encoding(vector<int>{}).empty());
assert((runlength_encoding(string("aaabbcaa")) == vector<pair<char, int>>{ {'a', 3}, {'b', 2}, {'c', 1}, {'a', 2} }));
assert((runlength_encoding(vector<int>{1, 1, -2, 3, 3}) == vector<pair<int, int>>{ {1, 2}, {-2, 1}, {3, 2} }));
mt19937 rng(64);
for(int n = 0; n <= 30; ++n) {
vector<int> a(n); for(auto& x : a) x = rng() % n;
ll expected = 0; for(int i = 0; i < n; ++i) for(int j = i + 1; j < n; ++j) expected += a[i] > a[j];
assert(inversion_number<ll>(a) == expected);
Mo mo(n); mo.set_bucketsize(0);
vector<pair<int, int>> ranges;
for(int l = 0; l < n; ++l) for(int r = l + 1; r <= n; ++r) { mo.insert(l, r); ranges.emplace_back(l, r); }
mo.build(); deque<int> current; vector<bool> seen(ranges.size());
auto al = [&](int i) { current.push_front(i); };
auto ar = [&](int i) { current.push_back(i); };
auto dl = [&](int i) { assert(!current.empty() && current.front() == i); current.pop_front(); };
auto dr = [&](int i) { assert(!current.empty() && current.back() == i); current.pop_back(); };
for(int t = 0; t < int(ranges.size()); ++t) {
int q = mo.process(al, ar, dl, dr); assert(q >= 0 && q < int(ranges.size()) && !seen[q]); seen[q] = true;
auto [l, r] = ranges[q]; vector<int> expected(r - l); iota(expected.begin(), expected.end(), l);
assert(vector<int>(current.begin(), current.end()) == expected);
}
assert(mo.process(al, ar, dl, dr) == -1 && mo.process(al, ar, dl, dr) == -1);
}
Timer timer;
auto lower = chrono::high_resolution_clock::now(); timer.reset(); auto upper = chrono::high_resolution_clock::now();
assert(lower <= timer.start_time && timer.start_time <= upper);
timer.start_time = chrono::high_resolution_clock::now() - chrono::milliseconds(100);
assert(timer.elapsed() >= 100 && timer() >= 100);
RNG32 r32; RNG64 r64; RNG_0_1 real;
for(int i = 0; i < 1000; ++i) {
int a = r32.randint(-5, 3), b = r32.randint(7);
ll c = r64.randint(-1000000000000LL, 1000000000000LL), d = r64.randint(7);
double x = real.rand(), y = real();
assert(-5 <= a && a < 3 && 0 <= b && b < 7);
assert(-1000000000000LL <= c && c < 1000000000000LL && 0 <= d && d < 7);
assert(0 <= x && x < 1 && 0 <= y && y < 1);
}
assert(r32.randint(4, 5) == 4 && r64.randint(-9, -8) == -9);
INT(a0, b0); print(a0 + b0);
}