regular backup
This commit is contained in:
parent
fe63403776
commit
e87bc2b483
28
src/bin/a.cc
28
src/bin/a.cc
|
@ -540,13 +540,29 @@ void prep() {
|
|||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n);
|
||||
if (n % 2 == 0) {
|
||||
cout << (n / 2 + 1) * (n / 2 + 1) << '\n';
|
||||
} else {
|
||||
int base = ((n - 1) / 2 + 1);
|
||||
cout << 4 * base * base - (base - 1) * base * 2 << '\n';
|
||||
int n;
|
||||
unordered_set<string> oc;
|
||||
while (1) {
|
||||
read(string, s);
|
||||
if (s[0] >= '0' and s[0] <= '9') {
|
||||
n = stoi(s);
|
||||
break;
|
||||
}
|
||||
string nw;
|
||||
for (auto&& c : s) {
|
||||
if (c >= 'A' and c <= 'Z') {
|
||||
nw += c - 'A' + 'a';
|
||||
} else if (c >= 'a' and c <= 'z') {
|
||||
nw += c;
|
||||
}
|
||||
}
|
||||
oc.emplace(nw);
|
||||
}
|
||||
while (n--) {
|
||||
read(string, s);
|
||||
oc.erase(s);
|
||||
}
|
||||
cout << oc.size() << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
27
src/bin/b.cc
27
src/bin/b.cc
|
@ -337,7 +337,7 @@ ll inverse(ll a, ll b) {
|
|||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; ll(1) * i * i <= x; i++) {
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
|
@ -527,7 +527,7 @@ constexpr std::array<T, N> __initarray(const T& value) {
|
|||
}
|
||||
/*******************************************************/
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
|
@ -540,26 +540,9 @@ void prep() {
|
|||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(ll, p, q);
|
||||
if (p % q == 0) {
|
||||
auto f = decompose(q);
|
||||
ll pp = p;
|
||||
ll res = INFLL;
|
||||
for (auto&& [x, c, t] : f) {
|
||||
deb(x, c, t);
|
||||
ll pw = 1;
|
||||
while (pp % x == 0) {
|
||||
pp /= x;
|
||||
pw *= x;
|
||||
}
|
||||
assert(pw % t == 0);
|
||||
chmin(res, pw / t * x);
|
||||
}
|
||||
assert(gcd(pp, q) == 1);
|
||||
cout << p / res << '\n';
|
||||
} else {
|
||||
cout << p << '\n';
|
||||
}
|
||||
read(string, s);
|
||||
string s1(s.rbegin(), s.rend());
|
||||
cout << s1 << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
57
src/bin/c.cc
57
src/bin/c.cc
|
@ -527,7 +527,7 @@ constexpr std::array<T, N> __initarray(const T& value) {
|
|||
}
|
||||
/*******************************************************/
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
|
@ -540,25 +540,48 @@ void prep() {
|
|||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
using mll = MLL<MDL>;
|
||||
read(int, n, k);
|
||||
vector dp(k + 1, vector<mll>(n + 2));
|
||||
vector dp1(k + 1, vector<mll>(n + 2));
|
||||
for (int i = 1; i <= k; ++i) {
|
||||
dp[i][n + 1] = 1;
|
||||
dp1[i][n + 1] = 1;
|
||||
for (int j = n; j; --j) {
|
||||
dp[i][j] = (i > 1) + dp[i][j + 1];
|
||||
dp1[i][j] = dp[i][j + 1];
|
||||
if (i - 2 >= 1) {
|
||||
dp[i][j] += dp1[i - 2][j - 1];
|
||||
read(int, n);
|
||||
vector<int> a(n + 1);
|
||||
a[0] = 2200;
|
||||
adj(ch, n);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
read(int, f, x);
|
||||
edge(ch, i, f);
|
||||
a[i] = x;
|
||||
}
|
||||
vector<ll> ps(n + 1);
|
||||
vector<bool> valid(n + 1);
|
||||
{
|
||||
auto dfs = [&] (auto dfs, int v, int pa) -> void {
|
||||
int son = 0;
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
son += 1;
|
||||
dfs(dfs, u, v);
|
||||
ps[v] += ps[u];
|
||||
}
|
||||
}
|
||||
for (int j = 1; j <= n; ++j) {
|
||||
dp1[i][j] += dp1[i][j - 1];
|
||||
if (not son) {
|
||||
ps[v] = a[v];
|
||||
} else {
|
||||
valid[v] = 1;
|
||||
}
|
||||
};
|
||||
dfs(dfs, 0, 0);
|
||||
}
|
||||
if (ps[0] > a[0]) {
|
||||
cout << "NO\n";
|
||||
return;
|
||||
}
|
||||
|
||||
sort(a.begin() + 1, a.end());
|
||||
sort(ps.begin() + 1, ps.end());
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
if (ps[i] > a[i]) {
|
||||
cout << "NO\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
cout << dp[k][1] << '\n';
|
||||
cout << "YES\n";
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
|
@ -0,0 +1,794 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
template<typename Addable_Info_t, typename Tag_t, typename Sequence = std::vector<Addable_Info_t>> class segtree {
|
||||
private:
|
||||
using size_type = uint64_t;
|
||||
using info_type = Addable_Info_t;
|
||||
using tag_type = Tag_t;
|
||||
size_type _max;
|
||||
vector<info_type> d;
|
||||
vector<tag_type> b;
|
||||
|
||||
void pull(size_type p) {
|
||||
d[p] = d[p * 2] + d[p * 2 + 1];
|
||||
}
|
||||
|
||||
void push(size_type p, size_type left_len, size_type right_len) {
|
||||
d[p * 2].apply(b[p], left_len), d[p * 2 + 1].apply(b[p], right_len);
|
||||
b[p * 2].apply(b[p]), b[p * 2 + 1].apply(b[p]);
|
||||
b[p] = tag_type();
|
||||
}
|
||||
|
||||
void set(size_type s, size_type t, size_type p, size_type x, const info_type& c) {
|
||||
if (s == t) {
|
||||
d[p] = c;
|
||||
return;
|
||||
}
|
||||
size_type m = s + (t - s >> 1);
|
||||
if (s != t) push(p, m - s + 1, t - m);
|
||||
if (x <= m) set(s, m, p * 2, x, c);
|
||||
else set(m + 1, t, p * 2 + 1, x, c);
|
||||
pull(p);
|
||||
}
|
||||
|
||||
void range_apply(size_type s, size_type t, size_type p, size_type l, size_type r, const tag_type& c) {
|
||||
if (l <= s && t <= r) {
|
||||
d[p].apply(c, t - s + 1);
|
||||
b[p].apply(c);
|
||||
return;
|
||||
}
|
||||
size_type m = s + (t - s >> 1);
|
||||
push(p, m - s + 1, t - m);
|
||||
if (l <= m) range_apply(s, m, p * 2, l, r, c);
|
||||
if (r > m) range_apply(m + 1, t, p * 2 + 1, l, r, c);
|
||||
pull(p);
|
||||
}
|
||||
|
||||
int binary_search(size_type s, size_type t, size_type p, int tot) {
|
||||
if (s == t) {
|
||||
return s;
|
||||
}
|
||||
size_type m = s + (t - s >> 1);
|
||||
if (d[p * 2].cnt >= tot) return binary_search(s, m, p * 2, tot);
|
||||
else return binary_search(m + 1, t, p * 2 + 1, tot - d[p * 2].cnt);
|
||||
}
|
||||
|
||||
info_type range_query(size_type s, size_type t, size_type p, size_type l, size_type r) {
|
||||
if (l <= s && t <= r) {
|
||||
return d[p];
|
||||
}
|
||||
size_type m = s + (t - s >> 1);
|
||||
info_type res = {};
|
||||
push(p, m - s + 1, t - m);
|
||||
if (l <= m) res = res + range_query(s, m, p * 2, l, r);
|
||||
if (r > m) res = res + range_query(m + 1, t, p * 2 + 1, l, r);
|
||||
return res;
|
||||
}
|
||||
|
||||
void build(const Sequence& a, size_type s, size_type t, size_type p) {
|
||||
if (s == t) {
|
||||
d[p] = a[s];
|
||||
return;
|
||||
}
|
||||
int m = s + (t - s >> 1);
|
||||
build(a, s, m, p * 2);
|
||||
build(a, m + 1, t, p * 2 + 1);
|
||||
pull(p);
|
||||
}
|
||||
public:
|
||||
segtree(size_type __max) : d(4 * __max), b(4 * __max), _max(__max - 1) {}
|
||||
segtree(const Sequence& a) : segtree(a.size()) {
|
||||
build(a, {}, _max, 1);
|
||||
}
|
||||
|
||||
void set(size_type i, const info_type& c) {
|
||||
set({}, _max, 1, i, c);
|
||||
}
|
||||
|
||||
void range_apply(size_type l, size_type r, const tag_type& c) {
|
||||
range_apply({}, _max, 1, l, r, c);
|
||||
}
|
||||
|
||||
void apply(size_type i, const tag_type& c) {
|
||||
range_apply(i, i, c);
|
||||
}
|
||||
|
||||
info_type range_query(size_type l, size_type r) {
|
||||
return range_query({}, _max, 1, l, r);
|
||||
}
|
||||
|
||||
int binary_search(int cnt) {
|
||||
return binary_search({}, _max, 1, cnt);
|
||||
}
|
||||
|
||||
info_type query(size_type i) {
|
||||
return range_query(i, i);
|
||||
}
|
||||
|
||||
Sequence serialize() {
|
||||
Sequence res = {};
|
||||
for (size_type i = 0; i <= _max; ++i) {
|
||||
res.push_back(query(i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
const vector<info_type>& get_d() {
|
||||
return d;
|
||||
}
|
||||
};
|
||||
|
||||
struct Tag {
|
||||
ll val = 0;
|
||||
int cnt = 0;
|
||||
void apply(const Tag& rhs) {
|
||||
val += rhs.val;
|
||||
cnt += rhs.cnt;
|
||||
}
|
||||
};
|
||||
|
||||
struct Info {
|
||||
ll val = 0;
|
||||
int cnt = 0;
|
||||
void apply(const Tag& rhs, size_t len) {
|
||||
val += rhs.val;
|
||||
cnt += rhs.cnt;
|
||||
}
|
||||
};
|
||||
|
||||
Info operator+(const Info &a, const Info &b) {
|
||||
return {
|
||||
.val = a.val + b.val,
|
||||
.cnt = a.cnt + b.cnt,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n, m, k);
|
||||
readvec(int, a, n);
|
||||
read(int, p);
|
||||
readvec(int, x, p);
|
||||
read(int, q);
|
||||
readvec(int, y, q);
|
||||
|
||||
transform(x.begin(), x.end(), x.begin(), expr(x - 1, auto x));
|
||||
transform(y.begin(), y.end(), y.begin(), expr(y - 1, auto y));
|
||||
|
||||
vector<int> both, left, right;
|
||||
vector<int> mark(n);
|
||||
for (auto&& i : x) {
|
||||
mark[i] += 1;
|
||||
}
|
||||
for (auto&& i : y) {
|
||||
mark[i] += 1;
|
||||
}
|
||||
ll left_sum = 0, right_sum = 0;
|
||||
for (auto&& i : x) {
|
||||
if (mark[i] == 2) {
|
||||
both.emplace_back(i);
|
||||
} else {
|
||||
left.emplace_back(i);
|
||||
left_sum += a[i];
|
||||
}
|
||||
}
|
||||
for (auto&& i : y) {
|
||||
if (mark[i] == 1) {
|
||||
right.emplace_back(i);
|
||||
right_sum += a[i];
|
||||
}
|
||||
}
|
||||
|
||||
sort_by_key(both.begin(), both.end(), expr(a[i], auto i));
|
||||
sort_by_key(left.begin(), left.end(), expr(a[i], auto i));
|
||||
sort_by_key(right.begin(), right.end(), expr(a[i], auto i));
|
||||
|
||||
vector<int> oc(a.begin(), a.end());
|
||||
sort(oc.begin(), oc.end());
|
||||
int t = unique(oc.begin(), oc.end()) - oc.begin();
|
||||
oc.resize(t);
|
||||
auto get = [&] (int x) { return lower_bound(oc.begin(), oc.end(), x) - oc.begin(); };
|
||||
|
||||
ll res = INFLL;
|
||||
|
||||
segtree<Info, Tag> tr(t);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (mark[i] == 0) {
|
||||
tr.apply(get(a[i]), { a[i], 1 });
|
||||
}
|
||||
}
|
||||
|
||||
ll both_sum = 0;
|
||||
for (int i = 0; i <= both.size(); ++i) {
|
||||
if (i) both_sum += a[both[i - 1]];
|
||||
int left_rem = max(0, k - i), right_rem = max(0, k - i);
|
||||
while (left.size() > left_rem) {
|
||||
int v = popback(left);
|
||||
left_sum -= a[v];
|
||||
tr.apply(get(a[v]), { a[v], 1 });
|
||||
}
|
||||
while (right.size() > right_rem) {
|
||||
int v = popback(right);
|
||||
right_sum -= a[v];
|
||||
tr.apply(get(a[v]), { a[v], 1 });
|
||||
}
|
||||
if (left.size() < left_rem or right.size() < right_rem) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int rem = m - (i + left_rem + right_rem);
|
||||
if (rem < 0) continue;
|
||||
int split = tr.binary_search(rem);
|
||||
auto [sum, cnt] = tr.range_query(0, split);
|
||||
if (cnt < rem) continue;
|
||||
sum -= ll(cnt - rem) * oc[split];
|
||||
chmin(res, both_sum + left_sum + right_sum + sum);
|
||||
}
|
||||
|
||||
cout << (res == INFLL ? -1 : res) << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,784 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
class quick_union {
|
||||
private:
|
||||
vector<size_t> c, sz;
|
||||
public:
|
||||
quick_union(size_t n) : c(n), sz(n) {
|
||||
iota(c.begin(), c.end(), 0);
|
||||
sz.assign(n, 1);
|
||||
}
|
||||
|
||||
size_t query(size_t i) {
|
||||
if (c[i] != i) c[i] = query(c[i]);
|
||||
return c[i];
|
||||
}
|
||||
|
||||
void merge(size_t i, size_t j) {
|
||||
if (connected(i, j)) return;
|
||||
sz[query(j)] += sz[query(i)];
|
||||
c[query(i)] = query(j);
|
||||
}
|
||||
|
||||
bool connected(size_t i, size_t j) {
|
||||
return query(i) == query(j);
|
||||
}
|
||||
|
||||
size_t query_size(size_t i) {
|
||||
return sz[query(i)];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
constexpr int B = 316;
|
||||
read(int, n, m, q);
|
||||
adj(ch, n);
|
||||
quick_union qu(n + 1);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
read(int, u, v);
|
||||
qu.merge(u, v);
|
||||
edge(ch, u, v);
|
||||
}
|
||||
|
||||
vector<pii> cc;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
if (qu.query(i) == i) {
|
||||
cc.emplace_back(i, qu.query_size(i));
|
||||
}
|
||||
}
|
||||
|
||||
m = cc.size();
|
||||
|
||||
vector<int> diameter(n + 1);
|
||||
{
|
||||
int p, q;
|
||||
int maxd;
|
||||
auto dfs = [&] (auto dfs, int v, int pa, int d) -> void {
|
||||
if (chmax(maxd, d)) {
|
||||
q = v;
|
||||
}
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
dfs(dfs, u, v, d + 1);
|
||||
}
|
||||
};
|
||||
for (auto&& [root, cnt] : cc) {
|
||||
p = root, maxd = -1;
|
||||
dfs(dfs, p, 0, 0);
|
||||
p = q, maxd = -1;
|
||||
dfs(dfs, p, 0, 0);
|
||||
diameter[root] = maxd;
|
||||
}
|
||||
}
|
||||
|
||||
vector<int> max_depth(n + 1);
|
||||
vector<int> depth(n + 1);
|
||||
vector<int> d(n + 1);
|
||||
vector<int> idx(n + 1);
|
||||
{
|
||||
int j;
|
||||
auto dfs = [&] (auto dfs, int v, int pa, int d) -> void {
|
||||
idx[v] = j;
|
||||
max_depth[v] = depth[v] = d;
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
dfs(dfs, u, v, d + 1);
|
||||
chmax(max_depth[v], max_depth[u]);
|
||||
}
|
||||
};
|
||||
for (int i = 0; i < m; ++i) {
|
||||
auto&& [v, cnt] = cc[i];
|
||||
j = i;
|
||||
dfs(dfs, v, 0, 0);
|
||||
}
|
||||
}
|
||||
{
|
||||
auto dfs = [&] (auto dfs, int v, int pa, int up) -> void {
|
||||
multiset<int> ds;
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
ds.emplace(max_depth[u] - depth[v]);
|
||||
}
|
||||
chmax(d[v], max(up, ds.empty() ? -INF : *ds.rbegin()));
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
ds.erase(ds.find(max_depth[u] - depth[v]));
|
||||
dfs(dfs, u, v, max({up + 1, 1, ds.empty() ? -INF : *ds.rbegin() + 1}));
|
||||
ds.emplace(max_depth[u] - depth[v]);
|
||||
}
|
||||
};
|
||||
for (auto&& [v, cnt] : cc) {
|
||||
dfs(dfs, v, 0, -INF);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
auto work = [&] (int i, int j) -> ld {
|
||||
auto&& [rootleft, totleft] = cc[i];
|
||||
vector<int> cnt(totleft + 1);
|
||||
{
|
||||
auto dfs = [&] (auto dfs, int v, int pa) -> void {
|
||||
cnt[d[v]] += 1;
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
dfs(dfs, u, v);
|
||||
}
|
||||
};
|
||||
dfs(dfs, rootleft, 0);
|
||||
}
|
||||
vector<int> ps(totleft + 2);
|
||||
vector<ll> pssum(totleft + 2);
|
||||
for (int i = 1; i <= totleft + 1; ++i) {
|
||||
ps[i] = ps[i - 1] + cnt[i - 1];
|
||||
pssum[i] = pssum[i - 1] + ll(1) * cnt[i - 1] * (i - 1);
|
||||
}
|
||||
auto&& [rootright, totright] = cc[j];
|
||||
ld curr = 0;
|
||||
int treed = max(diameter[rootleft], diameter[rootright]);
|
||||
{
|
||||
auto dfs = [&] (auto dfs, int v, int pa) -> void {
|
||||
curr += ll(1) * ps[clamp(treed - d[v] - 1, 0, totleft + 1)] * treed + ll(1) * (ps[totleft + 1] - ps[clamp(treed - d[v] - 1, 0, totleft + 1)]) * (d[v] + 1) + (pssum[totleft + 1] - pssum[clamp(treed - d[v] - 1, 0, totleft + 1)]);
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
dfs(dfs, u, v);
|
||||
}
|
||||
};
|
||||
dfs(dfs, rootright, 0);
|
||||
}
|
||||
return ld(1) * curr / (ll(1) * totleft * totright);
|
||||
};
|
||||
|
||||
vector<int> rev(m, -1);
|
||||
int t = 0;
|
||||
for (int i = 0; i < m; ++i) {
|
||||
auto&& [v, tot] = cc[i];
|
||||
if (tot < B) continue;
|
||||
rev[i] = t++;
|
||||
}
|
||||
vector pre(m, vector<ld>(t));
|
||||
// debug(t);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
if (rev[i] == -1) continue;
|
||||
auto&& [rootleft, totleft] = cc[i];
|
||||
vector<int> cnt(n + 1);
|
||||
{
|
||||
auto dfs = [&] (auto dfs, int v, int pa) -> void {
|
||||
cnt[d[v]] += 1;
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
dfs(dfs, u, v);
|
||||
}
|
||||
};
|
||||
dfs(dfs, rootleft, 0);
|
||||
}
|
||||
vector<int> ps(totleft + 2);
|
||||
vector<ll> pssum(totleft + 2);
|
||||
for (int i = 1; i <= totleft + 1; ++i) {
|
||||
ps[i] = ps[i - 1] + cnt[i - 1];
|
||||
pssum[i] = pssum[i - 1] + ll(1) * cnt[i - 1] * (i - 1);
|
||||
}
|
||||
for (int j = 0; j < m; ++j) {
|
||||
if (j == i) continue;
|
||||
auto&& [rootright, totright] = cc[j];
|
||||
ld curr = 0;
|
||||
int treed = max(diameter[rootleft], diameter[rootright]);
|
||||
{
|
||||
auto dfs = [&] (auto dfs, int v, int pa) -> void {
|
||||
curr += ll(1) * ps[clamp(treed - d[v] - 1, 0, totleft + 1)] * treed + ll(1) * (ps[totleft + 1] - ps[clamp(treed - d[v] - 1, 0, totleft + 1)]) * (d[v] + 1) + (pssum[totleft + 1] - pssum[clamp(treed - d[v] - 1, 0, totleft + 1)]);
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
dfs(dfs, u, v);
|
||||
}
|
||||
};
|
||||
dfs(dfs, rootright, 0);
|
||||
}
|
||||
pre[j][rev[i]] = ld(1) * curr / (ll(1) * totleft * totright);
|
||||
}
|
||||
}
|
||||
|
||||
cout << fixed << setprecision(50);
|
||||
// int cnt = 0;
|
||||
while (q--) {
|
||||
read(int, u, v);
|
||||
u = qu.query(u), v = qu.query(v);
|
||||
if (u == v) {
|
||||
cout << "-1\n";
|
||||
} else if (max(cc[idx[u]].second, cc[idx[v]].second) < B) {
|
||||
// deb(cc[idx[u]].second, cc[idx[v]].second);
|
||||
// cnt += 1;
|
||||
// debug(cnt);
|
||||
cout << (long double)work(idx[u], idx[v]) << '\n';
|
||||
} else {
|
||||
if (cc[idx[v]].second >= B) {
|
||||
swap(u, v);
|
||||
}
|
||||
cout << (long double)pre[idx[v]][rev[idx[u]]] << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -1 +1 @@
|
|||
Subproject commit 6ce8d08534507e063e06d58570725ec272166fd4
|
||||
Subproject commit b33b8ed3f4e64e62b354eb3acf0f92ec93a41f75
|
74
src/bin/d.cc
74
src/bin/d.cc
|
@ -527,7 +527,7 @@ constexpr std::array<T, N> __initarray(const T& value) {
|
|||
}
|
||||
/*******************************************************/
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
|
@ -545,7 +545,7 @@ template <typename T> struct point {
|
|||
point(const T& x, const T& y) : x(x), y(y) {}
|
||||
|
||||
inline T square() const { return x * x + y * y; }
|
||||
inline ld norm() const { return sqrt(double(square())); }
|
||||
inline ld norm() const { return sqrt((long double)(square())); }
|
||||
|
||||
inline point operator+(const point& rhs) const { return point(x + rhs.x, y + rhs.y); }
|
||||
inline point operator-(const point& rhs) const { return point(x - rhs.x, y - rhs.y); }
|
||||
|
@ -590,32 +590,60 @@ template <typename T> struct point {
|
|||
};
|
||||
|
||||
|
||||
struct convex_hull {
|
||||
int n, m;
|
||||
vector<int> on_hull;
|
||||
// WARN: counter-clockwise
|
||||
vector<int> hull;
|
||||
|
||||
// WARN: sort the points before initializing
|
||||
template <typename T>
|
||||
convex_hull(const vector<point<T>>& a) : n(a.size()), on_hull(n) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
int m = hull.size();
|
||||
// WARN: keeping the points that are on edges
|
||||
while (m > 1 and (a[hull[m - 1]] - a[hull[m - 2]]) * (a[i] - a[hull[m - 2]]) < 0) {
|
||||
on_hull[hull[m - 1]] -= 1;
|
||||
hull.pop_back();
|
||||
m -= 1;
|
||||
}
|
||||
on_hull[i] += 1;
|
||||
hull.emplace_back(i);
|
||||
}
|
||||
int low = hull.size();
|
||||
|
||||
for (int i = n - 2; ~i; --i) {
|
||||
int m = hull.size();
|
||||
while (m > low and (a[hull[m - 1]] - a[hull[m - 2]]) * (a[i] - a[hull[m - 2]]) < 0) {
|
||||
on_hull[hull[m - 1]] -= 1;
|
||||
hull.pop_back();
|
||||
m -= 1;
|
||||
}
|
||||
on_hull[i] += 1;
|
||||
hull.emplace_back(i);
|
||||
}
|
||||
if (n > 1) {
|
||||
hull.pop_back();
|
||||
}
|
||||
m = hull.size();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
using point = point<ll>;
|
||||
read(int, n);
|
||||
vector<pll> pts;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
read(ll, x, y, u, v);
|
||||
if (u - x == 0) {
|
||||
if (v > y) pts.emplace_back(0, 1);
|
||||
else pts.emplace_back(0, -1);
|
||||
} else if (v - y == 0) {
|
||||
if (u < x) pts.emplace_back(-1, 0);
|
||||
else pts.emplace_back(1, 0);
|
||||
} else {
|
||||
ll g = gcd(u - x, v - y);
|
||||
pts.emplace_back((u - x) / g, (v - y) / g);
|
||||
readvec(point, a, n);
|
||||
vector<ld> dp(n + 1, -INFLL);
|
||||
dp[0] = 0;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
for (int j = 1; j <= i; ++j) {
|
||||
chmax(dp[i], dp[j - 1] + (a[i - 1] - a[j - 1]).norm());
|
||||
}
|
||||
}
|
||||
ll res= 0;
|
||||
unordered_map<pll, int, pair_hash> oc;
|
||||
for (auto&& [x, y] : pts) {
|
||||
if (oc.count({-x, -y})) {
|
||||
res += oc[{-x, -y}];
|
||||
}
|
||||
oc[{x, y}] += 1;
|
||||
}
|
||||
cout << res << '\n';
|
||||
cout << fixed << setprecision(50);
|
||||
cout << (long double)dp[n] << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
87
src/bin/e.cc
87
src/bin/e.cc
|
@ -1,5 +1,4 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#include <algorithm>
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
|
@ -528,7 +527,7 @@ constexpr std::array<T, N> __initarray(const T& value) {
|
|||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
|
@ -541,15 +540,81 @@ void prep() {
|
|||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n);
|
||||
vector<int> a(n);
|
||||
iota(a.begin(), a.end(), 1);
|
||||
int res = 0;
|
||||
for (int i = 1; i <= n; ++i) res xor_eq i;
|
||||
cout << res << '\n';
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cout << a << '\n';
|
||||
rotate(a.begin(), a.begin() + 1, a.end());
|
||||
read(int, n, m);
|
||||
if (n % 4 == 0 or m % 4 == 0) {
|
||||
int s = 0;
|
||||
if (m % 4 == 0) swap(n, m), s = 1;
|
||||
vector a(n, vector<int>(m));
|
||||
int use = 0;
|
||||
for (int i = 0; i < n; i += 4) {
|
||||
for (int j = 0; j < m; ++j) {
|
||||
use += 1;
|
||||
for (int k = i; k < i + 4; ++k) {
|
||||
a[k][j] = use;
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << "YES\n";
|
||||
if (s) {
|
||||
for (int i = 0; i < m; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
cout << a[j][i] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < m; ++j) {
|
||||
cout << a[i][j] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
}
|
||||
} else if (n % 2 or m % 2 or max(n, m) == 2) {
|
||||
cout << "NO\n";
|
||||
} else {
|
||||
int s = 0;
|
||||
if (n > m) swap(n, m), s = 1;
|
||||
vector a(n, vector<int>(m));
|
||||
int use = 0;
|
||||
for (int i = 0; i + 3 < n; i += 4) {
|
||||
for (int j = 0; j < m; ++j) {
|
||||
use += 1;
|
||||
for (int k = i; k < i + 4; ++k) {
|
||||
a[k][j] = use;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < m - 6; j += 4) {
|
||||
for (int i = n - 2; i < n; ++i) {
|
||||
use += 1;
|
||||
for (int k = j; k < j + 4; ++k) {
|
||||
a[i][k] = use;
|
||||
}
|
||||
}
|
||||
}
|
||||
use += 1;
|
||||
a[n - 2][m - 6] = a[n - 1][m - 6] = a[n - 1][m - 5] = a[n - 1][m - 4] = use;
|
||||
use += 1;
|
||||
a[n - 2][m - 5] = a[n - 2][m - 4] = a[n - 2][m - 3] = a[n - 2][m - 2] = use;
|
||||
use += 1;
|
||||
a[n - 2][m - 1] = a[n - 1][m - 3] = a[n - 1][m - 2] = a[n - 1][m - 1] = use;
|
||||
cout << "YES\n";
|
||||
if (s) {
|
||||
for (int i = 0; i < m; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
cout << a[j][i] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < m; ++j) {
|
||||
cout << a[i][j] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
177
src/bin/g.cc
177
src/bin/g.cc
|
@ -1,5 +1,5 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
// #pragma GCC optimize("Ofast,unroll-loops")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
@ -527,7 +527,7 @@ constexpr std::array<T, N> __initarray(const T& value) {
|
|||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
|
@ -538,160 +538,43 @@ void dump_ignore() {}
|
|||
void prep() {
|
||||
}
|
||||
|
||||
template<typename Addable_Info_t, typename Tag_t, typename Sequence = std::vector<Addable_Info_t>> class segtree {
|
||||
private:
|
||||
using size_type = uint64_t;
|
||||
using info_type = Addable_Info_t;
|
||||
using tag_type = Tag_t;
|
||||
size_type _max;
|
||||
vector<info_type> d;
|
||||
vector<tag_type> b;
|
||||
|
||||
void pull(size_type p) {
|
||||
d[p] = d[p * 2] + d[p * 2 + 1];
|
||||
}
|
||||
|
||||
void push(size_type p, size_type left_len, size_type right_len) {
|
||||
d[p * 2].apply(b[p], left_len), d[p * 2 + 1].apply(b[p], right_len);
|
||||
b[p * 2].apply(b[p]), b[p * 2 + 1].apply(b[p]);
|
||||
b[p] = tag_type();
|
||||
}
|
||||
|
||||
void set(size_type s, size_type t, size_type p, size_type x, const info_type& c) {
|
||||
if (s == t) {
|
||||
d[p] = c;
|
||||
return;
|
||||
}
|
||||
size_type m = s + (t - s >> 1);
|
||||
if (s != t) push(p, m - s + 1, t - m);
|
||||
if (x <= m) set(s, m, p * 2, x, c);
|
||||
else set(m + 1, t, p * 2 + 1, x, c);
|
||||
pull(p);
|
||||
}
|
||||
|
||||
void range_apply(size_type s, size_type t, size_type p, size_type l, size_type r, const tag_type& c) {
|
||||
if (l <= s && t <= r) {
|
||||
d[p].apply(c, t - s + 1);
|
||||
b[p].apply(c);
|
||||
return;
|
||||
}
|
||||
size_type m = s + (t - s >> 1);
|
||||
push(p, m - s + 1, t - m);
|
||||
if (l <= m) range_apply(s, m, p * 2, l, r, c);
|
||||
if (r > m) range_apply(m + 1, t, p * 2 + 1, l, r, c);
|
||||
pull(p);
|
||||
}
|
||||
|
||||
info_type range_query(size_type s, size_type t, size_type p, size_type l, size_type r) {
|
||||
if (l <= s && t <= r) {
|
||||
return d[p];
|
||||
}
|
||||
size_type m = s + (t - s >> 1);
|
||||
info_type res = {};
|
||||
push(p, m - s + 1, t - m);
|
||||
if (l <= m) res = res + range_query(s, m, p * 2, l, r);
|
||||
if (r > m) res = res + range_query(m + 1, t, p * 2 + 1, l, r);
|
||||
return res;
|
||||
}
|
||||
|
||||
void build(const Sequence& a, size_type s, size_type t, size_type p) {
|
||||
if (s == t) {
|
||||
d[p] = a[s];
|
||||
return;
|
||||
}
|
||||
int m = s + (t - s >> 1);
|
||||
build(a, s, m, p * 2);
|
||||
build(a, m + 1, t, p * 2 + 1);
|
||||
pull(p);
|
||||
}
|
||||
public:
|
||||
segtree(size_type __max) : d(4 * __max), b(4 * __max), _max(__max - 1) {}
|
||||
segtree(const Sequence& a) : segtree(a.size()) {
|
||||
build(a, {}, _max, 1);
|
||||
}
|
||||
|
||||
void set(size_type i, const info_type& c) {
|
||||
set({}, _max, 1, i, c);
|
||||
}
|
||||
|
||||
void range_apply(size_type l, size_type r, const tag_type& c) {
|
||||
range_apply({}, _max, 1, l, r, c);
|
||||
}
|
||||
|
||||
void apply(size_type i, const tag_type& c) {
|
||||
range_apply(i, i, c);
|
||||
}
|
||||
|
||||
info_type range_query(size_type l, size_type r) {
|
||||
return range_query({}, _max, 1, l, r);
|
||||
}
|
||||
|
||||
info_type query(size_type i) {
|
||||
return range_query(i, i);
|
||||
}
|
||||
|
||||
Sequence serialize() {
|
||||
Sequence res = {};
|
||||
for (size_type i = 0; i <= _max; ++i) {
|
||||
res.push_back(query(i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
const vector<info_type>& get_d() {
|
||||
return d;
|
||||
}
|
||||
};
|
||||
|
||||
struct Tag {
|
||||
void apply(const Tag& rhs) { }
|
||||
};
|
||||
|
||||
struct Info {
|
||||
int val = 0;
|
||||
void apply(const Tag& rhs, size_t len) { }
|
||||
};
|
||||
|
||||
Info operator+(const Info &a, const Info &b) {
|
||||
return {a.val + b.val};
|
||||
}
|
||||
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n, k);
|
||||
readvec(int, a, n);
|
||||
vector<int> del(n + 1, 1);
|
||||
for (int i = 0; i < k; ++i) {
|
||||
read(int, x);
|
||||
del[x] = 0;
|
||||
}
|
||||
vector<int> rev(n + 1);
|
||||
vector<pii> oc(n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
rev[a[i]] = i;
|
||||
oc[i] = {a[i], i};
|
||||
}
|
||||
set<int> tr;
|
||||
segtree<Info, Tag> cnt(n);
|
||||
sort_by_key(oc.begin(), oc.end(), expr(pii(p.first, -p.second), auto&& p));
|
||||
set<int> empty;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
empty.emplace(i);
|
||||
}
|
||||
vector<int> stack;
|
||||
int pre = -1;
|
||||
ll res = 0;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
if (del[i]) {
|
||||
int left = 0, right = n - 1;
|
||||
auto it = tr.lower_bound(rev[i]);
|
||||
if (it != tr.end()) {
|
||||
right = *it - 1;
|
||||
for (auto&& [val, pos] : oc) {
|
||||
if (val != pre) stack = {};
|
||||
pre = val;
|
||||
stack.emplace_back(pos);
|
||||
empty.erase(pos);
|
||||
int ll = 0, lr = pos, rl = pos, rr = n - 1;
|
||||
{
|
||||
auto it = empty.lower_bound(pos);
|
||||
if (it != empty.begin()) {
|
||||
ll = *prev(it) + 1;
|
||||
}
|
||||
if (it != tr.begin()) {
|
||||
left = *prev(it) + 1;
|
||||
}
|
||||
// deb(cnt.range_query(left, right).val);
|
||||
// if (res > INT_MAX - right - left + 1 - cnt.range_query(left, right).val) {
|
||||
// debug(res);
|
||||
// }
|
||||
res += right - left + 1 - cnt.range_query(left, right).val;
|
||||
cnt.set(rev[i], { 1 });
|
||||
}
|
||||
if (not del[i]) {
|
||||
tr.emplace(rev[i]);
|
||||
if (stack.size() >= k) {
|
||||
rl = stack[stack.size() - k];
|
||||
auto it = empty.lower_bound(lr);
|
||||
if (it != empty.end()) {
|
||||
rr = *it - 1;
|
||||
}
|
||||
if (rl <= rr) {
|
||||
res += 1LL * (lr - ll + 1) * (rr - rl + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << res << '\n';
|
||||
|
|
|
@ -0,0 +1,592 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
int n;
|
||||
unordered_set<string> oc;
|
||||
while (1) {
|
||||
read(string, s);
|
||||
if (s[0] >= '0' and s[0] <= '9') {
|
||||
n = stoi(s);
|
||||
break;
|
||||
}
|
||||
string nw;
|
||||
for (auto&& c : s) {
|
||||
if (c >= 'A' and c <= 'Z') {
|
||||
nw += c - 'A' + 'a';
|
||||
} else if (c >= 'a' and c <= 'z') {
|
||||
nw += c;
|
||||
}
|
||||
}
|
||||
oc.emplace(nw);
|
||||
}
|
||||
while (n--) {
|
||||
read(string, s);
|
||||
oc.erase(s);
|
||||
}
|
||||
cout << oc.size() << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,572 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(string, s);
|
||||
string s1(s.rbegin(), s.rend());
|
||||
cout << s1 << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,611 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n);
|
||||
vector<int> a(n + 1);
|
||||
a[0] = 2200;
|
||||
adj(ch, n);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
read(int, f, x);
|
||||
edge(ch, i, f);
|
||||
a[i] = x;
|
||||
}
|
||||
vector<ll> ps(n + 1);
|
||||
vector<bool> valid(n + 1);
|
||||
{
|
||||
auto dfs = [&] (auto dfs, int v, int pa) -> void {
|
||||
int son = 0;
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
son += 1;
|
||||
dfs(dfs, u, v);
|
||||
ps[v] += ps[u];
|
||||
}
|
||||
if (not son) {
|
||||
ps[v] = a[v];
|
||||
} else {
|
||||
valid[v] = 1;
|
||||
}
|
||||
};
|
||||
dfs(dfs, 0, 0);
|
||||
}
|
||||
if (ps[0] > a[0]) {
|
||||
cout << "NO\n";
|
||||
return;
|
||||
}
|
||||
|
||||
sort(a.begin() + 1, a.end());
|
||||
sort(ps.begin() + 1, ps.end());
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
if (ps[i] > a[i]) {
|
||||
cout << "NO\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
cout << "YES\n";
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,673 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
template <typename T> struct point {
|
||||
T x, y;
|
||||
point() : x(), y() {}
|
||||
point(const pair<T, T>& a) : x(a.first), y(a.second) {}
|
||||
point(const T& x, const T& y) : x(x), y(y) {}
|
||||
|
||||
inline T square() const { return x * x + y * y; }
|
||||
inline ld norm() const { return sqrt((long double)(square())); }
|
||||
|
||||
inline point operator+(const point& rhs) const { return point(x + rhs.x, y + rhs.y); }
|
||||
inline point operator-(const point& rhs) const { return point(x - rhs.x, y - rhs.y); }
|
||||
inline point operator+() const { return *this; }
|
||||
inline point operator-() const { return point(-x, -y); }
|
||||
inline point operator*(const T& a) const { return point(x * a, y * a); }
|
||||
inline T operator*(const point& rhs) const { return x * rhs.y - y * rhs.x; }
|
||||
inline point operator/(const T& a) const { return point(x / a, y / a); }
|
||||
inline point& operator+=(const point& rhs) { x += rhs.x, y += rhs.y; return *this; }
|
||||
inline point& operator-=(const point& rhs) { x -= rhs.x, y -= rhs.y; return *this; }
|
||||
inline point& operator*=(const T& a) { x *= a, y *= a; return *this; }
|
||||
inline point& operator/=(const T& a) { x /= a, y /= a; return *this; }
|
||||
|
||||
inline bool operator==(const point& rhs) const { return x == rhs.x and y == rhs.y; }
|
||||
inline bool operator!=(const point& rhs) const { return not (*this == rhs); }
|
||||
inline bool operator<(const point& rhs) const { return pair(x, y) < pair(rhs.x, rhs.y); }
|
||||
inline bool operator<=(const point& rhs) const { return *this < rhs or *this == rhs; }
|
||||
inline bool operator>(const point& rhs) const { return not (*this <= rhs); }
|
||||
inline bool operator>=(const point& rhs) const { return not (*this < rhs); }
|
||||
|
||||
static inline ld slope(const point& a, const point& b) {
|
||||
if (a.x == b.x) return INFLL;
|
||||
return ld(a.y - b.y) / (a.x - b.x);
|
||||
}
|
||||
|
||||
// distance from point `a` to line `l--r`
|
||||
static inline ld dist(const point& a, const point& l, const point& r) {
|
||||
return area(a, l, r) * 2 / (l - r).norm();
|
||||
}
|
||||
|
||||
static inline ld area(const point& a, const point& b, const point& c) {
|
||||
return (b - a) * (c - a) / ld(2);
|
||||
}
|
||||
|
||||
friend inline istream& operator>>(istream& in, point& a) {
|
||||
return in >> a.x >> a.y;
|
||||
}
|
||||
|
||||
friend inline ostream& operator<<(ostream& out, const point& a) {
|
||||
return out << a.x << ' ' << a.y;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct convex_hull {
|
||||
int n, m;
|
||||
vector<int> on_hull;
|
||||
// WARN: counter-clockwise
|
||||
vector<int> hull;
|
||||
|
||||
// WARN: sort the points before initializing
|
||||
template <typename T>
|
||||
convex_hull(const vector<point<T>>& a) : n(a.size()), on_hull(n) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
int m = hull.size();
|
||||
// WARN: keeping the points that are on edges
|
||||
while (m > 1 and (a[hull[m - 1]] - a[hull[m - 2]]) * (a[i] - a[hull[m - 2]]) < 0) {
|
||||
on_hull[hull[m - 1]] -= 1;
|
||||
hull.pop_back();
|
||||
m -= 1;
|
||||
}
|
||||
on_hull[i] += 1;
|
||||
hull.emplace_back(i);
|
||||
}
|
||||
int low = hull.size();
|
||||
|
||||
for (int i = n - 2; ~i; --i) {
|
||||
int m = hull.size();
|
||||
while (m > low and (a[hull[m - 1]] - a[hull[m - 2]]) * (a[i] - a[hull[m - 2]]) < 0) {
|
||||
on_hull[hull[m - 1]] -= 1;
|
||||
hull.pop_back();
|
||||
m -= 1;
|
||||
}
|
||||
on_hull[i] += 1;
|
||||
hull.emplace_back(i);
|
||||
}
|
||||
if (n > 1) {
|
||||
hull.pop_back();
|
||||
}
|
||||
m = hull.size();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
using point = point<ll>;
|
||||
read(int, n);
|
||||
readvec(point, a, n);
|
||||
vector<ld> dp(n + 1, -INFLL);
|
||||
dp[0] = 0;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
for (int j = 1; j <= i; ++j) {
|
||||
chmax(dp[i], dp[j - 1] + (a[i - 1] - a[j - 1]).norm());
|
||||
}
|
||||
}
|
||||
cout << fixed << setprecision(50);
|
||||
cout << (long double)dp[n] << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,645 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n, m);
|
||||
if (n % 4 == 0 or m % 4 == 0) {
|
||||
int s = 0;
|
||||
if (m % 4 == 0) swap(n, m), s = 1;
|
||||
vector a(n, vector<int>(m));
|
||||
int use = 0;
|
||||
for (int i = 0; i < n; i += 4) {
|
||||
for (int j = 0; j < m; ++j) {
|
||||
use += 1;
|
||||
for (int k = i; k < i + 4; ++k) {
|
||||
a[k][j] = use;
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << "YES\n";
|
||||
if (s) {
|
||||
for (int i = 0; i < m; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
cout << a[j][i] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < m; ++j) {
|
||||
cout << a[i][j] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
}
|
||||
} else if (n % 2 or m % 2 or max(n, m) == 2) {
|
||||
cout << "NO\n";
|
||||
} else {
|
||||
int s = 0;
|
||||
if (n > m) swap(n, m), s = 1;
|
||||
vector a(n, vector<int>(m));
|
||||
int use = 0;
|
||||
for (int i = 0; i + 3 < n; i += 4) {
|
||||
for (int j = 0; j < m; ++j) {
|
||||
use += 1;
|
||||
for (int k = i; k < i + 4; ++k) {
|
||||
a[k][j] = use;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < m - 6; j += 4) {
|
||||
for (int i = n - 2; i < n; ++i) {
|
||||
use += 1;
|
||||
for (int k = j; k < j + 4; ++k) {
|
||||
a[i][k] = use;
|
||||
}
|
||||
}
|
||||
}
|
||||
use += 1;
|
||||
a[n - 2][m - 6] = a[n - 1][m - 6] = a[n - 1][m - 5] = a[n - 1][m - 4] = use;
|
||||
use += 1;
|
||||
a[n - 2][m - 5] = a[n - 2][m - 4] = a[n - 2][m - 3] = a[n - 2][m - 2] = use;
|
||||
use += 1;
|
||||
a[n - 2][m - 1] = a[n - 1][m - 3] = a[n - 1][m - 2] = a[n - 1][m - 1] = use;
|
||||
cout << "YES\n";
|
||||
if (s) {
|
||||
for (int i = 0; i < m; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
cout << a[j][i] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < m; ++j) {
|
||||
cout << a[i][j] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,607 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n, k);
|
||||
readvec(int, a, n);
|
||||
vector<pii> oc(n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
oc[i] = {a[i], i};
|
||||
}
|
||||
sort_by_key(oc.begin(), oc.end(), expr(pii(p.first, -p.second), auto&& p));
|
||||
set<int> empty;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
empty.emplace(i);
|
||||
}
|
||||
vector<int> stack;
|
||||
int pre = -1;
|
||||
ll res = 0;
|
||||
for (auto&& [val, pos] : oc) {
|
||||
if (val != pre) stack = {};
|
||||
pre = val;
|
||||
stack.emplace_back(pos);
|
||||
empty.erase(pos);
|
||||
int ll = 0, lr = pos, rl = pos, rr = n - 1;
|
||||
{
|
||||
auto it = empty.lower_bound(pos);
|
||||
if (it != empty.begin()) {
|
||||
ll = *prev(it) + 1;
|
||||
}
|
||||
}
|
||||
if (stack.size() >= k) {
|
||||
rl = stack[stack.size() - k];
|
||||
auto it = empty.lower_bound(lr);
|
||||
if (it != empty.end()) {
|
||||
rr = *it - 1;
|
||||
}
|
||||
if (rl <= rr) {
|
||||
res += 1LL * (lr - ll + 1) * (rr - rl + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << res << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,579 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n);
|
||||
read(int, a, b, c);
|
||||
int res = 0;
|
||||
readvec(pii, s, n);
|
||||
read(int, d);
|
||||
for (auto&& [x, y] : s) {
|
||||
if (x + y >= c) continue;
|
||||
if (min(a, x + d) + y >= c) res += 1;
|
||||
}
|
||||
cout << res<< '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,624 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
constexpr int N = 5e5 + 1;
|
||||
constexpr int M = 5e3 + 1;
|
||||
vector<int> d[M << 2];
|
||||
bitset<N> blob[M];
|
||||
vector<int> oc[N];
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n);
|
||||
auto range_apply = [&] (auto range_apply, int s, int t, int p, int l, int r, int x) -> void {
|
||||
if (l <= s and t <= r) {
|
||||
d[p].emplace_back(x);
|
||||
return;
|
||||
}
|
||||
int m = s + (t - s >> 1);
|
||||
if (l <= m) range_apply(range_apply, s, m, p * 2, l, r, x);
|
||||
if (r > m) range_apply(range_apply, m + 1, t, p * 2 + 1, l, r, x);
|
||||
};
|
||||
int tot = 1;
|
||||
vector<int> idx(n << 2, -1);
|
||||
idx[1] = 0;
|
||||
vector<int> res(n);
|
||||
deque<tiii> q;
|
||||
readvec(pii, a, n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
auto [op, x] = a[i];
|
||||
if (op == 1) {
|
||||
oc[x].emplace_back(i);
|
||||
} else {
|
||||
range_apply(range_apply, 0, n - 1, 1, popback(oc[x]), i - 1, x);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < N; ++i) {
|
||||
while (oc[i].size()) {
|
||||
range_apply(range_apply, 0, n - 1, 1, popback(oc[i]), n - 1, i);
|
||||
}
|
||||
}
|
||||
q.emplace_back(0, n - 1, 1);
|
||||
while (q.size()) {
|
||||
auto [s, t, p] = popfront(q);
|
||||
blob[idx[p]][0] = 1;
|
||||
for (auto&& x : d[p]) {
|
||||
blob[idx[p]] |= blob[idx[p]] << x;
|
||||
}
|
||||
if (s == t) {
|
||||
res[s] = blob[idx[p]].count() - 1;
|
||||
continue;
|
||||
}
|
||||
int m = s + (t - s >> 1);
|
||||
idx[p * 2] = idx[p];
|
||||
blob[tot++] = blob[idx[p]];
|
||||
idx[p * 2 + 1] = tot - 1;
|
||||
q.emplace_back(s, m, p * 2);
|
||||
q.emplace_back(m + 1, t, p * 2 + 1);
|
||||
}
|
||||
// debug(tot);
|
||||
putvec_eol(res);
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,596 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(ll, k);
|
||||
|
||||
auto work = [&] (int128 tot) -> int128 {
|
||||
constexpr int N = 10;
|
||||
int128 res = 0;
|
||||
int128 pw = 1;
|
||||
for (int i = 1; i <= N; ++i) {
|
||||
pw *= 100;
|
||||
}
|
||||
for (int i = N; ~i; --i) {
|
||||
res += tot / (4 * pw) - tot / (100 * pw);
|
||||
pw /= 100;
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
int128 l = 2024, r = LLONG_MAX;
|
||||
int128 start = 2024 - work(2024);
|
||||
while (l < r) {
|
||||
int128 mid = l + r >> 1;
|
||||
if ((mid - work(mid)) - start >= k) {
|
||||
r = mid;
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
cout << l << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,801 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
template <typename T> struct argument_type;
|
||||
template <typename T, typename U> struct argument_type<T(U)> { using type = U; };
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = __float128; // up to 1e-9 precision in binary search
|
||||
using pii = pair<int, int>; using pil = pair<int, ll>; using pid = pair<int, ld>;
|
||||
using pli = pair<ll, int>; using pll = pair<ll, ll>; using pld = pair<ll, ld>;
|
||||
using pdi = pair<ld, int>; using pdl = pair<ld, ll>; using pdd = pair<ld, ld>;
|
||||
using tiii = tuple<int, int, int>; using tiil = tuple<int, int, ll>; using tiid = tuple<int, int, ld>;
|
||||
using tili = tuple<int, ll, int>; using till = tuple<int, ll, ll>; using tild = tuple<int, ll, ld>;
|
||||
using tidi = tuple<int, ld, int>; using tidl = tuple<int, ld, ll>; using tidd = tuple<int, ld, ld>;
|
||||
using tlii = tuple<ll, int, int>; using tlil = tuple<ll, int, ll>; using tlid = tuple<ll, int, ld>;
|
||||
using tlli = tuple<ll, ll, int>; using tlll = tuple<ll, ll, ll>; using tlld = tuple<ll, ll, ld>;
|
||||
using tldi = tuple<ll, ld, int>; using tldl = tuple<ll, ld, ll>; using tldd = tuple<ll, ld, ld>;
|
||||
using tdii = tuple<ld, int, int>; using tdil = tuple<ld, int, ll>; using tdid = tuple<ld, int, ld>;
|
||||
using tdli = tuple<ld, ll, int>; using tdll = tuple<ld, ll, ll>; using tdld = tuple<ld, ll, ld>;
|
||||
using tddi = tuple<ld, ld, int>; using tddl = tuple<ld, ld, ll>; using tddd = tuple<ld, ld, ld>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
template <typename T> using oi = ostream_iterator<T>;
|
||||
template <typename T> using ii = istream_iterator<T>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll PRIMELL = 901017227882342239LL;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937_64 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!((var) == (val))) (var) = (val); else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if ((var) == (val)) continue; (var) = (val);)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if ((var) == (val)) break; (var) = (val);)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
safe_hash hasher;
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
auto h = hasher(x);
|
||||
res1 = (res1 + h * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + h * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__), ch[v].emplace_back(u, __VA_ARGS__);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, ...) __AS_PROCEDURE(ch[u].emplace_back(v, __VA_ARGS__);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
|
||||
// add declarations to avoid circular dependency
|
||||
template<typename T, typename U> istream& operator>>(istream&, pair<T, U>&);
|
||||
template<typename T, typename U> ostream& operator<<(ostream&, const pair<T, U>&);
|
||||
template<typename T, size_t N> istream& operator>>(istream&, array<T, N>&);
|
||||
template <typename T, size_t N> ostream& operator<<(ostream&, const array<T, N>&);
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>&, const std::tuple<Args...>&);
|
||||
template<typename T> ostream& operator<<(ostream&, const vector<T>&);
|
||||
std::ostream& operator<<(std::ostream&, const int128&);
|
||||
|
||||
template<typename T, typename U> istream& operator>>(istream& in, pair<T, U>& p) {
|
||||
return in >> p.first >> p.second;
|
||||
}
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename T, size_t N> istream& operator>>(istream& in, array<T, N>& a) {
|
||||
for (size_t i = 0; i < N; ++i) in >> a[i];
|
||||
return in;
|
||||
}
|
||||
template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) {
|
||||
for (auto&& i : a) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(t, ...) __AS_PROCEDURE(argument_type<void(t)>::type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a(n); for (auto& x : (a)) cin >> x;)
|
||||
#define readvec1(t, a, n) __AS_PROCEDURE(vector<argument_type<void(t)>::type> a((n) + 1); copy_n(ii<argument_type<void(t)>::type>(cin), (n), (a).begin() + 1);)
|
||||
#define putvec(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec1(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, " ")); cout << endl;)
|
||||
#define putvec_eol(a) __AS_PROCEDURE(copy((a).begin(), (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define putvec1_eol(a) __AS_PROCEDURE(copy((a).begin() + 1, (a).end(), oi<__as_typeof(a)::value_type>(cout, "\n"));)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : (a)) cerr << x << ' '; cerr << endl;)
|
||||
#define deb(...) debug(make_tuple(__VA_ARGS__))
|
||||
|
||||
/* pops */
|
||||
template <typename Container>
|
||||
inline auto poptop(Container& q) {
|
||||
auto ret = q.top();
|
||||
q.pop();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popback(Container& q) {
|
||||
auto ret = q.back();
|
||||
q.pop_back();
|
||||
return ret;
|
||||
}
|
||||
template <typename Container>
|
||||
inline auto popfront(Container& q) {
|
||||
auto ret = q.front();
|
||||
q.pop_front();
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* math */
|
||||
template <typename return_t>
|
||||
return_t qpow(ll b, ll p) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
return_t half = qpow<return_t>(b, p / 2);
|
||||
if (p % 2 == 1) return half * half * b;
|
||||
else return half * half;
|
||||
}
|
||||
|
||||
// dynamic modulus
|
||||
ll qpow(ll b, ll p, ll mod) {
|
||||
if (b == 0 and p != 0) return 0;
|
||||
if (p == 0) return 1;
|
||||
ll half = qpow(b, p / 2, mod);
|
||||
if (p % 2 == 1) return (int128(half) * half % mod) * b % mod;
|
||||
else return half * half % mod;
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
// Accurately find `i` 'th root of `n` (taking the floor)
|
||||
inline ll root(ll n, ll i) {
|
||||
ll l = 0, r = pow(LLONG_MAX, (long double)(1) / i);
|
||||
while (l < r) {
|
||||
ll mid = l + r + 1 >> 1;
|
||||
if (qpow<int128>(mid, i) <= n) {
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
|
||||
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
|
||||
#define fastcomb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] * factrev[k] * factrev[(n) - (k)])
|
||||
|
||||
__attribute__((target("lzcnt")))
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
template <typename T>
|
||||
T mygcd(T a, T b) { return b == 0 ? a : mygcd(b, a % b); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
// return (factor, count, factor ** count)
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
vector<pii> decompose_prime(int N) {
|
||||
// return (factor, count)
|
||||
vector<pii> result;
|
||||
for (int i = 2; i * i <= N; i++) {
|
||||
if (N % i == 0) {
|
||||
int cnt = 0;
|
||||
while (N % i == 0) N /= i, ++cnt;
|
||||
result.emplace_back(i, cnt);
|
||||
}
|
||||
}
|
||||
if (N != 1) {
|
||||
result.emplace_back(N, 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(const string& s, const string& t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(const string& s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(int128(lhs.val) * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return lhs * mod(inverse(rhs.val, mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
MLL& operator+=(const MLL& rhs) { return *this = *this + rhs; }
|
||||
MLL& operator-=(const MLL& rhs) { return *this = *this - rhs; }
|
||||
MLL& operator*=(const MLL& rhs) { return *this = *this * rhs; }
|
||||
MLL& operator/=(const MLL& rhs) { return *this = *this / rhs; }
|
||||
MLL& operator%=(const MLL& rhs) { return *this = *this % rhs; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
// miscancellous
|
||||
template <typename T, typename U>
|
||||
bool chmax(T& lhs, const U& rhs) {
|
||||
bool ret = lhs < rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template <typename T, typename U>
|
||||
bool chmin(T& lhs, const U& rhs) {
|
||||
bool ret = lhs > rhs;
|
||||
if (ret) {
|
||||
lhs = rhs;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define functor(func) ([&](auto&&... val) \
|
||||
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
|
||||
{return func(std::forward<decltype(val)>(val)...);})
|
||||
#define expr(ret, ...) ([&] (__VA_ARGS__) { return (ret); })
|
||||
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
|
||||
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
|
||||
vector<pair<T, U>> res;
|
||||
auto a_it = a_first;
|
||||
auto b_it = b_first;
|
||||
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
|
||||
res.emplace_back(*a_it, *b_it);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
|
||||
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
|
||||
vector<pair<T, U>> res;
|
||||
if (n > 0) {
|
||||
res.emplace_back(*a_first, *b_first);
|
||||
for (size_t i = 1; i != n; ++i) {
|
||||
res.emplace_back(*++a_first, *++b_first);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
template <typename T>
|
||||
class ArithmeticIterator : bidirectional_iterator_tag {
|
||||
public:
|
||||
using difference_type = ptrdiff_t;
|
||||
using value_type = T;
|
||||
private:
|
||||
value_type value;
|
||||
public:
|
||||
ArithmeticIterator(const T& value) : value(value) {}
|
||||
value_type operator*() const { return value; }
|
||||
ArithmeticIterator<T>& operator++() { ++value; return *this; }
|
||||
ArithmeticIterator<T>& operator--() { --value; return *this; }
|
||||
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
|
||||
};
|
||||
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
|
||||
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
|
||||
}
|
||||
#define initarray(init, N) (__initarray<decay<decltype(init)>::type, (N)>(init))
|
||||
namespace detail {
|
||||
template <typename T, std::size_t...Is>
|
||||
constexpr std::array<T, sizeof...(Is)>
|
||||
make_array(const T& value, std::index_sequence<Is...>) {
|
||||
return {{(static_cast<void>(Is), value)...}};
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
constexpr std::array<T, N> __initarray(const T& value) {
|
||||
return detail::make_array(value, std::make_index_sequence<N>());
|
||||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {
|
||||
}
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n, m, k, t);
|
||||
readvec(int, a, k);
|
||||
vector<int> cnt(m + 1);
|
||||
int l = 0, r = n;
|
||||
vector grid(3, vector<int>(3, -1));
|
||||
int extra = 0;
|
||||
|
||||
vector<vector<int>> mp = {{2, 3, 4}, {5, 1, 6}, {7, 8, 9}};
|
||||
vector<pii> seq = {{1, 1}, {0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};
|
||||
while (1) {
|
||||
int f = 1;
|
||||
// debug(0);
|
||||
// debug(grid);
|
||||
|
||||
int op = 0;
|
||||
|
||||
for (auto&& [i, j] : seq) {
|
||||
// deb(i, j, l, r);
|
||||
if (grid[i][j] == -1 and l < r) {
|
||||
grid[i][j] = a[l++];
|
||||
op = 1;
|
||||
if (grid[i][j] == t) {
|
||||
if (r == k) {
|
||||
extra += 1;
|
||||
} else {
|
||||
r += 1;
|
||||
}
|
||||
} else if (grid[i][j] == 0) {
|
||||
cnt[grid[i][j]] += 1;
|
||||
grid[i][j] = -1;
|
||||
if (r == k) {
|
||||
extra += 1;
|
||||
} else {
|
||||
r += 1;
|
||||
}
|
||||
f = 0;
|
||||
goto fi;
|
||||
}
|
||||
}
|
||||
}
|
||||
// debug(2);
|
||||
// debug(grid);
|
||||
fi:
|
||||
if (f == 0) continue;
|
||||
unordered_set<int> oc;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if (grid[i][j] == -1 or oc.count(grid[i][j])) {
|
||||
f = 0;
|
||||
goto fii;
|
||||
}
|
||||
oc.emplace(grid[i][j]);
|
||||
}
|
||||
}
|
||||
fii:
|
||||
if (f == 1) {
|
||||
cnt[grid[1][1]] += 1;
|
||||
grid[1][1] = -1;
|
||||
continue;
|
||||
}
|
||||
int use4 = 0;
|
||||
|
||||
// debug(1);
|
||||
// debug(grid);
|
||||
while (1) {
|
||||
tiii cand = { INF, INF, INF };
|
||||
vector mx = { INF, INF, INF };
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
int f = 1;
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if (grid[i][j] == -1) {
|
||||
f = 0;
|
||||
break;
|
||||
} else if (grid[i][j] != grid[i][0]) {
|
||||
f = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (f) {
|
||||
vector curr = {mp[i][0], mp[i][1], mp[i][2]};
|
||||
sort(curr.begin(), curr.end());
|
||||
if (chmin(mx, curr)) {
|
||||
cand = tiii {i * 3, i * 3 + 1, i * 3 + 2};
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
int f = 1;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
if (grid[i][j] == -1) {
|
||||
f = 0;
|
||||
break;
|
||||
} else if (grid[i][j] != grid[0][j]) {
|
||||
f = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (f) {
|
||||
vector curr = { mp[0][j], mp[1][j], mp[2][j] };
|
||||
sort(curr.begin(), curr.end());
|
||||
if (chmin(mx, curr)) {
|
||||
cand = tiii {j, 3 + j, 6 + j};
|
||||
}
|
||||
}
|
||||
}
|
||||
if (grid[0][0] == grid[1][1] and grid[0][0] == grid[2][2] and grid[0][0] != -1) {
|
||||
vector curr = {mp[0][0], mp[1][1], mp[2][2]};
|
||||
sort(curr.begin(), curr.end());
|
||||
if (chmin(mx, curr)) {
|
||||
cand = tiii{0, 4, 8};
|
||||
}
|
||||
}
|
||||
if (grid[0][2] == grid[1][1] and grid[0][2] == grid[2][0] and grid[0][2] != -1) {
|
||||
vector curr = {mp[0][2], mp[1][1], mp[0][2]};
|
||||
sort(curr.begin(), curr.end());
|
||||
if (chmin(mx, curr)) {
|
||||
cand = tiii{2, 4, 6};
|
||||
}
|
||||
}
|
||||
if (get<0>(cand) == INF) break;
|
||||
op = 1;
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
if (r == k) {
|
||||
extra += 1;
|
||||
} else {
|
||||
r += 1;
|
||||
}
|
||||
}
|
||||
auto [i, j, k] = cand;
|
||||
// deb(i, j, k);
|
||||
if (i == 4) {
|
||||
use4 = 1;
|
||||
} else {
|
||||
cnt[grid[i / 3][i % 3]] += 1;
|
||||
grid[i / 3][i % 3] = -1;
|
||||
}
|
||||
if (j == 4) {
|
||||
use4 = 1;
|
||||
} else {
|
||||
cnt[grid[j / 3][j % 3]] += 1;
|
||||
grid[j / 3][j % 3] = -1;
|
||||
}
|
||||
if (k == 4) {
|
||||
use4 = 1;
|
||||
} else {
|
||||
cnt[grid[k / 3][k % 3]] += 1;
|
||||
grid[k / 3][k % 3] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
pii cand = { INF, INF };
|
||||
vector mx = { INF, INF };
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if (grid[i][j] == -1) continue;
|
||||
for (int k = 0; k < 3; ++k) {
|
||||
for (int l = 0; l < 3; ++l) {
|
||||
if (k == i and l == j) continue;
|
||||
if (grid[k][l] == grid[i][j]) {
|
||||
vector curr = {mp[k][l], mp[i][j]};
|
||||
sort(curr.begin(), curr.end());
|
||||
if (chmin(mx, curr)) {
|
||||
cand = pii{ i * 3 + j, k * 3 + l };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cand.first == INF) break;
|
||||
auto [i, j] = cand;
|
||||
op = 1;
|
||||
for (int i = 0; i < 1; ++i) {
|
||||
if (r == k) {
|
||||
extra += 1;
|
||||
} else {
|
||||
r += 1;
|
||||
}
|
||||
}
|
||||
if (i == 4) {
|
||||
use4 = 1;
|
||||
} else {
|
||||
cnt[grid[i / 3][i % 3]] += 1;
|
||||
grid[i / 3][i % 3] = -1;
|
||||
}
|
||||
if (j == 4) {
|
||||
use4 = 1;
|
||||
} else {
|
||||
cnt[grid[j / 3][j % 3]] += 1;
|
||||
grid[j / 3][j % 3] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (use4) {
|
||||
cnt[grid[1][1]] += 1;
|
||||
grid[1][1] = -1;
|
||||
}
|
||||
|
||||
if (op == 0) break;
|
||||
|
||||
int has = 0;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if (grid[i][j] != -1) {
|
||||
has = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (has == 0) {
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
if (r == k) {
|
||||
extra += 1;
|
||||
} else {
|
||||
r += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if (grid[i][j] != -1) {
|
||||
cnt[grid[i][j]] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
putvec(cnt);
|
||||
if (extra > 0) {
|
||||
cout << "Unhappy! " << extra << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
|
||||
static_assert(false, "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie;
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t != (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
44
src/bin/j.cc
44
src/bin/j.cc
|
@ -527,7 +527,7 @@ constexpr std::array<T, N> __initarray(const T& value) {
|
|||
}
|
||||
/*******************************************************/
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
|
@ -535,45 +535,21 @@ void dump() {}
|
|||
|
||||
void dump_ignore() {}
|
||||
|
||||
constexpr int N = 2e5 + 10;
|
||||
ll fact[N], factrev[N + 1], s[N + 1];
|
||||
|
||||
void prep() {
|
||||
fact[0] = factrev[0] = 1;
|
||||
for (int i = 1; i < N; ++i) {
|
||||
fact[i] = (fact[i - 1] * i) % MDL;
|
||||
}
|
||||
s[0] = 1;
|
||||
for (int i = 1; i <= N; ++i) {
|
||||
s[i] = s[i - 1] * fact[i - 1] % MDL;
|
||||
}
|
||||
factrev[N] = inverse(s[N], MDL);
|
||||
for (int i = N; i; --i) {
|
||||
factrev[i - 1] = factrev[i] * fact[i - 1] % MDL;
|
||||
}
|
||||
for (int i = 0; i < N; ++i) {
|
||||
factrev[i] = factrev[i + 1] * s[i] % MDL;
|
||||
}
|
||||
}
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n, p, q);
|
||||
ll win = p * inverse(q, MDL) % MDL;
|
||||
ll lose = mod(1 - win, MDL);
|
||||
ll res = 0;
|
||||
ll fixed = qpow(win, n, MDL);
|
||||
auto mycomb = [&] (int n, int m) {
|
||||
if (n < m or m < 0 or n < 0) return 0LL;
|
||||
return (fact[n] * factrev[m] % MDL) * factrev[n - m] % MDL;
|
||||
};
|
||||
ll pw = 1;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
res += (mycomb(n - 1 + i, n - 1) * fixed % MDL) * pw % MDL;
|
||||
res %= MDL;
|
||||
pw = pw * lose % MDL;
|
||||
read(int, n);
|
||||
read(int, a, b, c);
|
||||
int res = 0;
|
||||
readvec(pii, s, n);
|
||||
read(int, d);
|
||||
for (auto&& [x, y] : s) {
|
||||
if (x + y >= c) continue;
|
||||
if (min(a, x + d) + y >= c) res += 1;
|
||||
}
|
||||
cout << res << '\n';
|
||||
cout << res<< '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
69
src/bin/k.cc
69
src/bin/k.cc
|
@ -527,7 +527,7 @@ constexpr std::array<T, N> __initarray(const T& value) {
|
|||
}
|
||||
/*******************************************************/
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
|
@ -538,30 +538,63 @@ void dump_ignore() {}
|
|||
void prep() {
|
||||
}
|
||||
|
||||
constexpr int N = 5e5 + 1;
|
||||
constexpr int M = 5e3 + 1;
|
||||
vector<int> d[M << 2];
|
||||
bitset<N> blob[M];
|
||||
vector<int> oc[N];
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
using mll = MLL<MDL>;
|
||||
read(int, n, m);
|
||||
vector<int> diff(n + 1);
|
||||
vector<int> diff2(n + 1);
|
||||
while (m--) {
|
||||
read(int, op, x);
|
||||
read(int, n);
|
||||
auto range_apply = [&] (auto range_apply, int s, int t, int p, int l, int r, int x) -> void {
|
||||
if (l <= s and t <= r) {
|
||||
d[p].emplace_back(x);
|
||||
return;
|
||||
}
|
||||
int m = s + (t - s >> 1);
|
||||
if (l <= m) range_apply(range_apply, s, m, p * 2, l, r, x);
|
||||
if (r > m) range_apply(range_apply, m + 1, t, p * 2 + 1, l, r, x);
|
||||
};
|
||||
int tot = 1;
|
||||
vector<int> idx(n << 2, -1);
|
||||
idx[1] = 0;
|
||||
vector<int> res(n);
|
||||
deque<tiii> q;
|
||||
readvec(pii, a, n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
auto [op, x] = a[i];
|
||||
if (op == 1) {
|
||||
diff[x] += 1;
|
||||
oc[x].emplace_back(i);
|
||||
} else {
|
||||
diff2[x] += 1;
|
||||
range_apply(range_apply, 0, n - 1, 1, popback(oc[x]), i - 1, x);
|
||||
}
|
||||
}
|
||||
mll res = 0;
|
||||
mll cnt = 0;
|
||||
mll cnt2 = 0;
|
||||
mll sum2 = 0;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
cnt += diff[i], cnt2 += diff2[i];
|
||||
sum2 += cnt2;
|
||||
res += cnt + (2 * sum2 - cnt2);
|
||||
cout << res << " \n"[i == n];
|
||||
for (int i = 0; i < N; ++i) {
|
||||
while (oc[i].size()) {
|
||||
range_apply(range_apply, 0, n - 1, 1, popback(oc[i]), n - 1, i);
|
||||
}
|
||||
}
|
||||
q.emplace_back(0, n - 1, 1);
|
||||
while (q.size()) {
|
||||
auto [s, t, p] = popfront(q);
|
||||
blob[idx[p]][0] = 1;
|
||||
for (auto&& x : d[p]) {
|
||||
blob[idx[p]] |= blob[idx[p]] << x;
|
||||
}
|
||||
if (s == t) {
|
||||
res[s] = blob[idx[p]].count() - 1;
|
||||
continue;
|
||||
}
|
||||
int m = s + (t - s >> 1);
|
||||
idx[p * 2] = idx[p];
|
||||
blob[tot++] = blob[idx[p]];
|
||||
idx[p * 2 + 1] = tot - 1;
|
||||
q.emplace_back(s, m, p * 2);
|
||||
q.emplace_back(m + 1, t, p * 2 + 1);
|
||||
}
|
||||
// debug(tot);
|
||||
putvec_eol(res);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
75
src/bin/l.cc
75
src/bin/l.cc
|
@ -527,7 +527,7 @@ constexpr std::array<T, N> __initarray(const T& value) {
|
|||
}
|
||||
/*******************************************************/
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
|
@ -540,64 +540,33 @@ void prep() {
|
|||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n, m);
|
||||
readvec(int, a, n);
|
||||
readvec(int, b, m);
|
||||
read(ll, k);
|
||||
|
||||
vector res(n, vector<int>(m));
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < m; ++j) {
|
||||
res[i][j] = b[j];
|
||||
auto work = [&] (int128 tot) -> int128 {
|
||||
constexpr int N = 10;
|
||||
int128 res = 0;
|
||||
int128 pw = 1;
|
||||
for (int i = 1; i <= N; ++i) {
|
||||
pw *= 100;
|
||||
}
|
||||
}
|
||||
int g = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
g = g == 0 ? a[i] : gcd(g, a[i]);
|
||||
}
|
||||
ll l = 1;
|
||||
for (int j = 0; j < m; ++j) {
|
||||
l = lcm(l, ll(1) * b[j]);
|
||||
if (l > 1e9) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
for (int i = N; ~i; --i) {
|
||||
res += tot / (4 * pw) - tot / (100 * pw);
|
||||
pw /= 100;
|
||||
}
|
||||
}
|
||||
if (g % l != 0) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
}
|
||||
int col = 0, dir = 1;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (l == a[i]) continue;
|
||||
res[i][col] = a[i];
|
||||
col += dir;
|
||||
if (col == m) {
|
||||
col = max(0, m - 2);
|
||||
dir = -dir;
|
||||
} else if (col == -1) {
|
||||
col = min(m - 1, 1);
|
||||
dir = -dir;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
for (int j = 0; j < m; ++j) {
|
||||
int g = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
g = g == 0 ? res[i][j] : gcd(g, res[i][j]);
|
||||
}
|
||||
if (g != b[j]) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
int128 l = 2024, r = LLONG_MAX;
|
||||
int128 start = 2024 - work(2024);
|
||||
while (l < r) {
|
||||
int128 mid = l + r >> 1;
|
||||
if ((mid - work(mid)) - start >= k) {
|
||||
r = mid;
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Yes\n";
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < m; ++j) {
|
||||
cout << res[i][j] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
cout << l << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
263
src/bin/m.cc
263
src/bin/m.cc
|
@ -1,5 +1,5 @@
|
|||
// #pragma GCC target("popcnt,lzcnt,abm,bmi,bmi2")
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC optimize("Ofast,unroll-loops")
|
||||
/************* This code requires C++17. ***************/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
@ -527,7 +527,7 @@ constexpr std::array<T, N> __initarray(const T& value) {
|
|||
}
|
||||
/*******************************************************/
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
|
@ -535,42 +535,243 @@ void dump() {}
|
|||
|
||||
void dump_ignore() {}
|
||||
|
||||
constexpr int B = 2;
|
||||
constexpr int N = 1e5;
|
||||
constexpr int s = 31623; // sqrt(MDL)
|
||||
int a[N], b[N];
|
||||
|
||||
void prep() {
|
||||
a[0] = b[0] = 1;
|
||||
for(int i = 1; i <= s; i++) a[i] = (ll)a[i - 1] * B % MDL ;
|
||||
for(int i = 1; i <= s; i++) b[i] = (ll)b[i - 1] * a[s] % MDL ;
|
||||
}
|
||||
|
||||
ll lpow(ll power) { return ll(b[power / s]) * a[power % s] % MDL; }
|
||||
// ll lpow(ll power) {
|
||||
// return qpow(2, power, MDL);
|
||||
// }
|
||||
|
||||
inline ll series(int n) {
|
||||
return mod(1 - inverse(lpow(n + 1), MDL), MDL) * inverse(1 - inverse(2, MDL), MDL) % MDL;
|
||||
}
|
||||
|
||||
// __attribute__((target("popcnt")))
|
||||
void solve() {
|
||||
read(int, n);
|
||||
read(int, n, m, k, t);
|
||||
readvec(int, a, k);
|
||||
vector<int> cnt(m + 1);
|
||||
int l = 0, r = n;
|
||||
vector grid(3, vector<int>(3, -1));
|
||||
int extra = 0;
|
||||
|
||||
ll inv2 = inverse(2, MDL);
|
||||
vector<vector<int>> mp = {{2, 3, 4}, {5, 1, 6}, {7, 8, 9}};
|
||||
vector<pii> seq = {{1, 1}, {0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};
|
||||
while (1) {
|
||||
int f = 1;
|
||||
// debug(0);
|
||||
// debug(grid);
|
||||
|
||||
auto calc = [&] (int x) {
|
||||
ll left = ((n - 1) * lpow(n - 2) % MDL) * series(x) % MDL;
|
||||
ll right = lpow(n - 2) * mod(-x * inverse(lpow(x), MDL) % MDL + series(x - 1), MDL) % MDL;
|
||||
ll mid = lpow(n) * series(x) % MDL;
|
||||
// mll mid = 0;
|
||||
// deb(x, mid);
|
||||
return mod(((left - right) % MDL + mid) % MDL - 2 * x, MDL);
|
||||
};
|
||||
// deb(calc(1), calc(3));
|
||||
cout << mod(mod(lpow(n - 1) - 1 - (n - 1), MDL) - mod(calc(n - 1) - calc(max(1, (n + 1) / 2 - 1)), MDL), MDL) << '\n';
|
||||
int op = 0;
|
||||
|
||||
for (auto&& [i, j] : seq) {
|
||||
// deb(i, j, l, r);
|
||||
if (grid[i][j] == -1 and l < r) {
|
||||
grid[i][j] = a[l++];
|
||||
op = 1;
|
||||
if (grid[i][j] == t) {
|
||||
if (r == k) {
|
||||
extra += 1;
|
||||
} else {
|
||||
r += 1;
|
||||
}
|
||||
} else if (grid[i][j] == 0) {
|
||||
cnt[grid[i][j]] += 1;
|
||||
grid[i][j] = -1;
|
||||
if (r == k) {
|
||||
extra += 1;
|
||||
} else {
|
||||
r += 1;
|
||||
}
|
||||
f = 0;
|
||||
goto fi;
|
||||
}
|
||||
}
|
||||
}
|
||||
// debug(2);
|
||||
// debug(grid);
|
||||
fi:
|
||||
if (f == 0) continue;
|
||||
unordered_set<int> oc;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if (grid[i][j] == -1 or oc.count(grid[i][j])) {
|
||||
f = 0;
|
||||
goto fii;
|
||||
}
|
||||
oc.emplace(grid[i][j]);
|
||||
}
|
||||
}
|
||||
fii:
|
||||
if (f == 1) {
|
||||
cnt[grid[1][1]] += 1;
|
||||
grid[1][1] = -1;
|
||||
continue;
|
||||
}
|
||||
int use4 = 0;
|
||||
|
||||
// debug(1);
|
||||
// debug(grid);
|
||||
while (1) {
|
||||
tiii cand = { INF, INF, INF };
|
||||
vector mx = { INF, INF, INF };
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
int f = 1;
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if (grid[i][j] == -1) {
|
||||
f = 0;
|
||||
break;
|
||||
} else if (grid[i][j] != grid[i][0]) {
|
||||
f = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (f) {
|
||||
vector curr = {mp[i][0], mp[i][1], mp[i][2]};
|
||||
sort(curr.begin(), curr.end());
|
||||
if (chmin(mx, curr)) {
|
||||
cand = tiii {i * 3, i * 3 + 1, i * 3 + 2};
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
int f = 1;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
if (grid[i][j] == -1) {
|
||||
f = 0;
|
||||
break;
|
||||
} else if (grid[i][j] != grid[0][j]) {
|
||||
f = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (f) {
|
||||
vector curr = { mp[0][j], mp[1][j], mp[2][j] };
|
||||
sort(curr.begin(), curr.end());
|
||||
if (chmin(mx, curr)) {
|
||||
cand = tiii {j, 3 + j, 6 + j};
|
||||
}
|
||||
}
|
||||
}
|
||||
if (grid[0][0] == grid[1][1] and grid[0][0] == grid[2][2] and grid[0][0] != -1) {
|
||||
vector curr = {mp[0][0], mp[1][1], mp[2][2]};
|
||||
sort(curr.begin(), curr.end());
|
||||
if (chmin(mx, curr)) {
|
||||
cand = tiii{0, 4, 8};
|
||||
}
|
||||
}
|
||||
if (grid[0][2] == grid[1][1] and grid[0][2] == grid[2][0] and grid[0][2] != -1) {
|
||||
vector curr = {mp[0][2], mp[1][1], mp[0][2]};
|
||||
sort(curr.begin(), curr.end());
|
||||
if (chmin(mx, curr)) {
|
||||
cand = tiii{2, 4, 6};
|
||||
}
|
||||
}
|
||||
if (get<0>(cand) == INF) break;
|
||||
op = 1;
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
if (r == k) {
|
||||
extra += 1;
|
||||
} else {
|
||||
r += 1;
|
||||
}
|
||||
}
|
||||
auto [i, j, k] = cand;
|
||||
// deb(i, j, k);
|
||||
if (i == 4) {
|
||||
use4 = 1;
|
||||
} else {
|
||||
cnt[grid[i / 3][i % 3]] += 1;
|
||||
grid[i / 3][i % 3] = -1;
|
||||
}
|
||||
if (j == 4) {
|
||||
use4 = 1;
|
||||
} else {
|
||||
cnt[grid[j / 3][j % 3]] += 1;
|
||||
grid[j / 3][j % 3] = -1;
|
||||
}
|
||||
if (k == 4) {
|
||||
use4 = 1;
|
||||
} else {
|
||||
cnt[grid[k / 3][k % 3]] += 1;
|
||||
grid[k / 3][k % 3] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
pii cand = { INF, INF };
|
||||
vector mx = { INF, INF };
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if (grid[i][j] == -1) continue;
|
||||
for (int k = 0; k < 3; ++k) {
|
||||
for (int l = 0; l < 3; ++l) {
|
||||
if (k == i and l == j) continue;
|
||||
if (grid[k][l] == grid[i][j]) {
|
||||
vector curr = {mp[k][l], mp[i][j]};
|
||||
sort(curr.begin(), curr.end());
|
||||
if (chmin(mx, curr)) {
|
||||
cand = pii{ i * 3 + j, k * 3 + l };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cand.first == INF) break;
|
||||
auto [i, j] = cand;
|
||||
op = 1;
|
||||
for (int i = 0; i < 1; ++i) {
|
||||
if (r == k) {
|
||||
extra += 1;
|
||||
} else {
|
||||
r += 1;
|
||||
}
|
||||
}
|
||||
if (i == 4) {
|
||||
use4 = 1;
|
||||
} else {
|
||||
cnt[grid[i / 3][i % 3]] += 1;
|
||||
grid[i / 3][i % 3] = -1;
|
||||
}
|
||||
if (j == 4) {
|
||||
use4 = 1;
|
||||
} else {
|
||||
cnt[grid[j / 3][j % 3]] += 1;
|
||||
grid[j / 3][j % 3] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (use4) {
|
||||
cnt[grid[1][1]] += 1;
|
||||
grid[1][1] = -1;
|
||||
}
|
||||
|
||||
if (op == 0) break;
|
||||
|
||||
int has = 0;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if (grid[i][j] != -1) {
|
||||
has = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (has == 0) {
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
if (r == k) {
|
||||
extra += 1;
|
||||
} else {
|
||||
r += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if (grid[i][j] != -1) {
|
||||
cnt[grid[i][j]] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
putvec(cnt);
|
||||
if (extra > 0) {
|
||||
cout << "Unhappy! " << extra << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -8,7 +8,10 @@ import io
|
|||
PRIME = 998_244_353
|
||||
|
||||
if __name__ == '__main__':
|
||||
N = 1000000
|
||||
print(N, 0)
|
||||
print(*range(1, N + 1))
|
||||
|
||||
N = 100000
|
||||
M = N // 2
|
||||
print(N, M, N)
|
||||
for i in range(1, M + 1):
|
||||
print(randint(1, 2 * i - 1), 2 * i)
|
||||
for _ in range(N):
|
||||
print(randint(1, N), randint(1, N))
|
||||
|
|
Loading…
Reference in New Issue