backup
This commit is contained in:
parent
ca2c65a14d
commit
5588d468ed
BIN
src/bin/a.out
BIN
src/bin/a.out
Binary file not shown.
|
@ -0,0 +1,532 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (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
|
||||
|
||||
/* 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;
|
||||
using ld = long double;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = long double;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
using pid = pair<int, ld>;
|
||||
using pdi = pair<ld, int>;
|
||||
using pld = pair<ll, ld>;
|
||||
using pdl = pair<ld, ll>;
|
||||
using pdd = pair<ld, ld>;
|
||||
using tlll = tuple<ll, ll, ll>;
|
||||
using tlld = tuple<ll, ll, ld>;
|
||||
using tlli = tuple<ll, ll, int>;
|
||||
using tldl = tuple<ll, ld, ll>;
|
||||
using tldd = tuple<ll, ld, ld>;
|
||||
using tldi = tuple<ll, ld, int>;
|
||||
using tlil = tuple<ll, int, ll>;
|
||||
using tlid = tuple<ll, int, ld>;
|
||||
using tlii = tuple<ll, int, int>;
|
||||
using tdll = tuple<ld, ll, ll>;
|
||||
using tdld = tuple<ld, ll, ld>;
|
||||
using tdli = tuple<ld, ll, int>;
|
||||
using tddl = tuple<ld, ld, ll>;
|
||||
using tddd = tuple<ld, ld, ld>;
|
||||
using tddi = tuple<ld, ld, int>;
|
||||
using tdil = tuple<ld, int, ll>;
|
||||
using tdid = tuple<ld, int, ld>;
|
||||
using tdii = tuple<ld, int, int>;
|
||||
using till = tuple<int, ll, ll>;
|
||||
using tild = tuple<int, ll, ld>;
|
||||
using tili = tuple<int, ll, int>;
|
||||
using tidl = tuple<int, ld, ll>;
|
||||
using tidd = tuple<int, ld, ld>;
|
||||
using tidi = tuple<int, ld, int>;
|
||||
using tiil = tuple<int, int, ll>;
|
||||
using tiid = tuple<int, int, ld>;
|
||||
using tiii = tuple<int, int, int>;
|
||||
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 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 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 {
|
||||
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) {
|
||||
res1 = (res1 + x * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + x * 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 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
|
||||
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))
|
||||
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(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (auto& x : a) cin >> x;)
|
||||
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<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;)
|
||||
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 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;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
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(string s, 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(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(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), 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; }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
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 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 (; a_it != a_last and 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; }
|
||||
};
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
vector<int> ch(n + 1);
|
||||
vector<vector<int>> hc(n + 1);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
read(int, x);
|
||||
if (i + x <= n and i + x > 0) {
|
||||
ch[i] = i + x;
|
||||
hc[i + x].emplace_back(i);
|
||||
}
|
||||
}
|
||||
vector<int> choice(n + 1, -1);
|
||||
vector<bool> vis(n + 1);
|
||||
auto mark = [&] (auto mark, int v) -> int {
|
||||
if (choice[v] != -1) return choice[v];
|
||||
if (ch[v] == 0) {
|
||||
choice[v] = 1;
|
||||
} else if (vis[ch[v]]) {
|
||||
choice[v] = 0;
|
||||
} else {
|
||||
vis[v] = 1;
|
||||
choice[v] = mark(mark, ch[v]);
|
||||
vis[v] = 0;
|
||||
}
|
||||
return choice[v];
|
||||
};
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
if (choice[i] == -1) {
|
||||
mark(mark, i);
|
||||
}
|
||||
}
|
||||
int choices = count(choice.begin(), choice.end(), 1);
|
||||
vis.assign(n + 1, 0);
|
||||
auto dfs2 = [&] (auto dfs2, int v) -> void {
|
||||
if (not choice[v]) return;
|
||||
choice[v] = 0, --choices;
|
||||
for (auto&& u : hc[v]) {
|
||||
dfs2(dfs2, u);
|
||||
}
|
||||
};
|
||||
ll res = 0;
|
||||
int cnt = 0;
|
||||
// debug(choice);
|
||||
auto dfs = [&] (auto dfs, int v) -> int {
|
||||
if (vis[v]) return false;
|
||||
if (v == 0) return true;
|
||||
dfs2(dfs2, v);
|
||||
// debug(make_tuple(v, choices));
|
||||
res += n + 1 + choices;
|
||||
vis[v] = 1;
|
||||
cnt += 1;
|
||||
return dfs(dfs, ch[v]);
|
||||
};
|
||||
if (dfs(dfs, 1)) {
|
||||
res += ll(1) * (n - cnt) * (2 * n + 1);
|
||||
}
|
||||
cout << res << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
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,521 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (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
|
||||
|
||||
/* 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;
|
||||
using ld = long double;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = long double;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
using pid = pair<int, ld>;
|
||||
using pdi = pair<ld, int>;
|
||||
using pld = pair<ll, ld>;
|
||||
using pdl = pair<ld, ll>;
|
||||
using pdd = pair<ld, ld>;
|
||||
using tlll = tuple<ll, ll, ll>;
|
||||
using tlld = tuple<ll, ll, ld>;
|
||||
using tlli = tuple<ll, ll, int>;
|
||||
using tldl = tuple<ll, ld, ll>;
|
||||
using tldd = tuple<ll, ld, ld>;
|
||||
using tldi = tuple<ll, ld, int>;
|
||||
using tlil = tuple<ll, int, ll>;
|
||||
using tlid = tuple<ll, int, ld>;
|
||||
using tlii = tuple<ll, int, int>;
|
||||
using tdll = tuple<ld, ll, ll>;
|
||||
using tdld = tuple<ld, ll, ld>;
|
||||
using tdli = tuple<ld, ll, int>;
|
||||
using tddl = tuple<ld, ld, ll>;
|
||||
using tddd = tuple<ld, ld, ld>;
|
||||
using tddi = tuple<ld, ld, int>;
|
||||
using tdil = tuple<ld, int, ll>;
|
||||
using tdid = tuple<ld, int, ld>;
|
||||
using tdii = tuple<ld, int, int>;
|
||||
using till = tuple<int, ll, ll>;
|
||||
using tild = tuple<int, ll, ld>;
|
||||
using tili = tuple<int, ll, int>;
|
||||
using tidl = tuple<int, ld, ll>;
|
||||
using tidd = tuple<int, ld, ld>;
|
||||
using tidi = tuple<int, ld, int>;
|
||||
using tiil = tuple<int, int, ll>;
|
||||
using tiid = tuple<int, int, ld>;
|
||||
using tiii = tuple<int, int, int>;
|
||||
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 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 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 {
|
||||
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) {
|
||||
res1 = (res1 + x * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + x * 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 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
|
||||
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))
|
||||
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(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (auto& x : a) cin >> x;)
|
||||
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<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;)
|
||||
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 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;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
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(string s, 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(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(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), 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; }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
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 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 (; a_it != a_last and 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; }
|
||||
};
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n, m);
|
||||
vector<vector<int>> a(n, vector<int>(m + 1));
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 1; j <= m; ++j) {
|
||||
cin >> a[i][j];
|
||||
}
|
||||
}
|
||||
vector<int> res(n);
|
||||
auto rev = [&] (const vector<int>& x) {
|
||||
vector<int> res(m + 1);
|
||||
for (int i = 0; i <= m; ++i) {
|
||||
int curr = x[i];
|
||||
res[curr] = i;
|
||||
}
|
||||
return res;
|
||||
};
|
||||
vector<array<int, 11>> trie(1);
|
||||
auto insert = [&] (const vector<int>& x) -> void {
|
||||
int curr = 0;
|
||||
for (int i = 1; i <= m; ++i) {
|
||||
// debug(i), debug(x[i]);
|
||||
// debug(curr);
|
||||
if (not trie[curr][x[i]]) {
|
||||
trie[curr][x[i]] = trie.size();
|
||||
trie.push_back({});
|
||||
}
|
||||
curr = trie[curr][x[i]];
|
||||
}
|
||||
};
|
||||
auto query = [&] (const vector<int>& x) -> int {
|
||||
int curr = 0;
|
||||
for (int i = 1; i <= m; ++i) {
|
||||
if (not trie[curr][x[i]]) {
|
||||
return i - 1;
|
||||
}
|
||||
curr = trie[curr][x[i]];
|
||||
}
|
||||
return m;
|
||||
};
|
||||
for (int i = 0; i < n; ++i) {
|
||||
insert(rev(a[i]));
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cout << query(a[i]) << " \n"[i + 1 == n];
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
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,497 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (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
|
||||
|
||||
/* 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;
|
||||
using ld = long double;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = long double;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
using pid = pair<int, ld>;
|
||||
using pdi = pair<ld, int>;
|
||||
using pld = pair<ll, ld>;
|
||||
using pdl = pair<ld, ll>;
|
||||
using pdd = pair<ld, ld>;
|
||||
using tlll = tuple<ll, ll, ll>;
|
||||
using tlld = tuple<ll, ll, ld>;
|
||||
using tlli = tuple<ll, ll, int>;
|
||||
using tldl = tuple<ll, ld, ll>;
|
||||
using tldd = tuple<ll, ld, ld>;
|
||||
using tldi = tuple<ll, ld, int>;
|
||||
using tlil = tuple<ll, int, ll>;
|
||||
using tlid = tuple<ll, int, ld>;
|
||||
using tlii = tuple<ll, int, int>;
|
||||
using tdll = tuple<ld, ll, ll>;
|
||||
using tdld = tuple<ld, ll, ld>;
|
||||
using tdli = tuple<ld, ll, int>;
|
||||
using tddl = tuple<ld, ld, ll>;
|
||||
using tddd = tuple<ld, ld, ld>;
|
||||
using tddi = tuple<ld, ld, int>;
|
||||
using tdil = tuple<ld, int, ll>;
|
||||
using tdid = tuple<ld, int, ld>;
|
||||
using tdii = tuple<ld, int, int>;
|
||||
using till = tuple<int, ll, ll>;
|
||||
using tild = tuple<int, ll, ld>;
|
||||
using tili = tuple<int, ll, int>;
|
||||
using tidl = tuple<int, ld, ll>;
|
||||
using tidd = tuple<int, ld, ld>;
|
||||
using tidi = tuple<int, ld, int>;
|
||||
using tiil = tuple<int, int, ll>;
|
||||
using tiid = tuple<int, int, ld>;
|
||||
using tiii = tuple<int, int, int>;
|
||||
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 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 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 {
|
||||
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) {
|
||||
res1 = (res1 + x * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + x * 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 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
|
||||
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))
|
||||
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(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (auto& x : a) cin >> x;)
|
||||
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<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;)
|
||||
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 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;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
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(string s, 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(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(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), 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; }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
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 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 (; a_it != a_last and 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; }
|
||||
};
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
readvec(pll, a, n);
|
||||
sort(a.begin(), a.end());
|
||||
ll res = INFLL;
|
||||
ll max_b = -INFLL;
|
||||
set<pli> b;
|
||||
for (int i = 0; i < n; ++i) b.emplace(a[i].second, i);
|
||||
for (int i = n - 1; ~i; --i) {
|
||||
auto [x, y] = a[i];
|
||||
res = min(res, abs(x - max_b));
|
||||
b.erase({y, i});
|
||||
auto it = b.lower_bound({x, 0});
|
||||
if (it != b.end()) {
|
||||
res = min(res, abs(x - max(max_b, it->first)));
|
||||
}
|
||||
if (it != b.begin()) {
|
||||
res = min(res, abs(x - max(max_b, (--it)->first)));
|
||||
}
|
||||
max_b = max(max_b, y);
|
||||
}
|
||||
cout << res << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
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,534 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (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
|
||||
|
||||
/* 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;
|
||||
using ld = long double;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = long double;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
using pid = pair<int, ld>;
|
||||
using pdi = pair<ld, int>;
|
||||
using pld = pair<ll, ld>;
|
||||
using pdl = pair<ld, ll>;
|
||||
using pdd = pair<ld, ld>;
|
||||
using tlll = tuple<ll, ll, ll>;
|
||||
using tlld = tuple<ll, ll, ld>;
|
||||
using tlli = tuple<ll, ll, int>;
|
||||
using tldl = tuple<ll, ld, ll>;
|
||||
using tldd = tuple<ll, ld, ld>;
|
||||
using tldi = tuple<ll, ld, int>;
|
||||
using tlil = tuple<ll, int, ll>;
|
||||
using tlid = tuple<ll, int, ld>;
|
||||
using tlii = tuple<ll, int, int>;
|
||||
using tdll = tuple<ld, ll, ll>;
|
||||
using tdld = tuple<ld, ll, ld>;
|
||||
using tdli = tuple<ld, ll, int>;
|
||||
using tddl = tuple<ld, ld, ll>;
|
||||
using tddd = tuple<ld, ld, ld>;
|
||||
using tddi = tuple<ld, ld, int>;
|
||||
using tdil = tuple<ld, int, ll>;
|
||||
using tdid = tuple<ld, int, ld>;
|
||||
using tdii = tuple<ld, int, int>;
|
||||
using till = tuple<int, ll, ll>;
|
||||
using tild = tuple<int, ll, ld>;
|
||||
using tili = tuple<int, ll, int>;
|
||||
using tidl = tuple<int, ld, ll>;
|
||||
using tidd = tuple<int, ld, ld>;
|
||||
using tidi = tuple<int, ld, int>;
|
||||
using tiil = tuple<int, int, ll>;
|
||||
using tiid = tuple<int, int, ld>;
|
||||
using tiii = tuple<int, int, int>;
|
||||
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 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 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 {
|
||||
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) {
|
||||
res1 = (res1 + x * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + x * 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 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
|
||||
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))
|
||||
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(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); copy_n(ii<type>(cin), (n), a.begin());)
|
||||
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<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;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
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;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
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(string s, 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(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(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), 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; }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
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 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 (; a_it != a_last and 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; }
|
||||
};
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
adj(ch, n);
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
read(int, u, v);
|
||||
edge(ch, u, v);
|
||||
}
|
||||
vector<int> max_dist(n + 1), max_depth(n + 1);
|
||||
auto get = [&] (auto get, int v, int pa) -> int {
|
||||
int curr = 0;
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
curr = max(curr, get(get, u, v) + 1);
|
||||
}
|
||||
max_depth[v] = curr;
|
||||
return curr;
|
||||
};
|
||||
get(get, 1, 0);
|
||||
// debug(max_depth);
|
||||
auto dfs = [&] (auto dfs, int v, int pa, int up) -> void {
|
||||
// debug(make_tuple(v, up));
|
||||
max_dist[v] = max(max_depth[v], up);
|
||||
int max_depth1 = -1, max_depth_u1 = -1;
|
||||
int max_depth2 = -1, max_depth_u2 = -1;
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
int curr_depth = max_depth[u];
|
||||
// debug(make_tuple(u, curr_depth));
|
||||
if (curr_depth > max_depth1) {
|
||||
max_depth2 = max_depth1, max_depth_u2 = max_depth_u1;
|
||||
max_depth1 = curr_depth, max_depth_u1 = u;
|
||||
} else if (curr_depth > max_depth2) {
|
||||
max_depth2 = curr_depth, max_depth_u2 = u;
|
||||
}
|
||||
}
|
||||
// debug(make_tuple(max_depth1, max_depth2));
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
if (u == max_depth_u1) {
|
||||
dfs(dfs, u, v, max(up, max_depth2 + 1) + 1);
|
||||
} else {
|
||||
dfs(dfs, u, v, max(up, max_depth1 + 1) + 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
dfs(dfs, 1, 0, 0);
|
||||
// debug(max_dist);
|
||||
vector<int> diff(n + 2);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
diff[max_dist[i] + 1] += 1;
|
||||
}
|
||||
vector<int> res;
|
||||
int curr = 0;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
curr += diff[i];
|
||||
if (curr < n) {
|
||||
res.emplace_back(1 + curr);
|
||||
} else {
|
||||
res.emplace_back(curr);
|
||||
}
|
||||
}
|
||||
putvec(res);
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
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,523 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (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
|
||||
|
||||
/* 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;
|
||||
using ld = long double;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = long double;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
using pid = pair<int, ld>;
|
||||
using pdi = pair<ld, int>;
|
||||
using pld = pair<ll, ld>;
|
||||
using pdl = pair<ld, ll>;
|
||||
using pdd = pair<ld, ld>;
|
||||
using tlll = tuple<ll, ll, ll>;
|
||||
using tlld = tuple<ll, ll, ld>;
|
||||
using tlli = tuple<ll, ll, int>;
|
||||
using tldl = tuple<ll, ld, ll>;
|
||||
using tldd = tuple<ll, ld, ld>;
|
||||
using tldi = tuple<ll, ld, int>;
|
||||
using tlil = tuple<ll, int, ll>;
|
||||
using tlid = tuple<ll, int, ld>;
|
||||
using tlii = tuple<ll, int, int>;
|
||||
using tdll = tuple<ld, ll, ll>;
|
||||
using tdld = tuple<ld, ll, ld>;
|
||||
using tdli = tuple<ld, ll, int>;
|
||||
using tddl = tuple<ld, ld, ll>;
|
||||
using tddd = tuple<ld, ld, ld>;
|
||||
using tddi = tuple<ld, ld, int>;
|
||||
using tdil = tuple<ld, int, ll>;
|
||||
using tdid = tuple<ld, int, ld>;
|
||||
using tdii = tuple<ld, int, int>;
|
||||
using till = tuple<int, ll, ll>;
|
||||
using tild = tuple<int, ll, ld>;
|
||||
using tili = tuple<int, ll, int>;
|
||||
using tidl = tuple<int, ld, ll>;
|
||||
using tidd = tuple<int, ld, ld>;
|
||||
using tidi = tuple<int, ld, int>;
|
||||
using tiil = tuple<int, int, ll>;
|
||||
using tiid = tuple<int, int, ld>;
|
||||
using tiii = tuple<int, int, int>;
|
||||
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 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 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 {
|
||||
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) {
|
||||
res1 = (res1 + x * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + x * 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 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
|
||||
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))
|
||||
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(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); copy_n(ii<type>(cin), (n), a.begin());)
|
||||
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<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;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
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;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
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(string s, 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(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(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), 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; }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
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 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 (; a_it != a_last and 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; }
|
||||
};
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
#define DUMP_TEST_CASE 1393
|
||||
#define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {
|
||||
read(int, n);
|
||||
read(ll, k);
|
||||
readvec(ll, left_vec, n);
|
||||
readvec(ll, right_vec, n);
|
||||
cout << n << ' ' << k << endl;
|
||||
putvec(left_vec);
|
||||
putvec(right_vec);
|
||||
}
|
||||
|
||||
void dump_ignore() {
|
||||
read(int, n);
|
||||
read(ll, k);
|
||||
readvec(ll, left_vec, n);
|
||||
readvec(ll, right_vec, n);
|
||||
}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
read(ll, k);
|
||||
readvec(ll, left_vec, n);
|
||||
readvec(ll, right_vec, n);
|
||||
vector<pll> a = zip<ll, ll>(left_vec.begin(), left_vec.end(), right_vec.begin(), right_vec.end());
|
||||
ll left = 0, right = -1;
|
||||
vector<ll> length, lf, rf;
|
||||
for (auto&& [l, r] : a) {
|
||||
if (l > right + 1) {
|
||||
if (right - left + 1 != 0) length.emplace_back(right - left + 1);
|
||||
lf.emplace_back(l);
|
||||
rf.emplace_back(r);
|
||||
left = l;
|
||||
right = r;
|
||||
} else {
|
||||
right = r;
|
||||
}
|
||||
}
|
||||
length.emplace_back(right - left + 1);
|
||||
int m = length.size();
|
||||
ll res = INFLL;
|
||||
int r = 0;
|
||||
ll curr = 0;
|
||||
ll cnt1 = 0;
|
||||
for (int i = 0; i < m; ++i) {
|
||||
while (r < m and curr < k) {
|
||||
curr += length[r];
|
||||
if (length[r] == 1) cnt1 += 1;
|
||||
++r;
|
||||
}
|
||||
if (curr < k) break;
|
||||
ll use = max(ll(0), min(curr - k, cnt1));
|
||||
res = min(res, rf[r - 1] - (curr - k) + use + (r - i - use) * 2);
|
||||
curr -= length[i];
|
||||
if (length[i] == 1) cnt1 -= 1;
|
||||
}
|
||||
cout << (res == INFLL ? -1 : res) << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
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
|
||||
}
|
|
@ -15,7 +15,7 @@ using namespace std;
|
|||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
|
@ -68,6 +68,7 @@ using tiii = tuple<int, int, int>;
|
|||
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;
|
||||
|
@ -202,10 +203,17 @@ template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, s
|
|||
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(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (auto& x : a) cin >> x;)
|
||||
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<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;)
|
||||
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;
|
||||
|
@ -391,10 +399,46 @@ template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, Ran
|
|||
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 (; a_it != a_last and 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; }
|
||||
};
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
|
@ -403,43 +447,51 @@ void dump_ignore() {}
|
|||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
readvec(int, a, n);
|
||||
int sq = sqrt(n);
|
||||
read(int, q);
|
||||
vector<ll> res(q);
|
||||
vector<vector<pii>> req(n + 1);
|
||||
for (int t = 0; t < q; ++t) {
|
||||
read(int, i, s);
|
||||
--i;
|
||||
if (s < sq) {
|
||||
req[s].emplace_back(i, t);
|
||||
} else {
|
||||
ll curr = 0;
|
||||
for (int j = i; j < n; j += s) {
|
||||
curr += a[j];
|
||||
}
|
||||
res[t] = curr;
|
||||
read(int, n, m);
|
||||
vector<vector<int>> a(n, vector<int>(m + 1));
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 1; j <= m; ++j) {
|
||||
cin >> a[i][j];
|
||||
}
|
||||
}
|
||||
vector<ll> ss(n);
|
||||
for (int s = 1; s < sq; ++s) {
|
||||
sort(req[s].begin(), req[s].end(), greater());
|
||||
int ptr = 0;
|
||||
ss.assign(n, 0);
|
||||
for (int i = n - 1; ~i; --i) {
|
||||
if (i + s >= n) {
|
||||
ss[i] = a[i];
|
||||
} else {
|
||||
ss[i] = a[i] + ss[i + s];
|
||||
}
|
||||
while (ptr < req[s].size() and req[s][ptr].first == i) {
|
||||
res[req[s][ptr].second] = ss[i];
|
||||
++ptr;
|
||||
}
|
||||
vector<int> res(n);
|
||||
auto rev = [&] (const vector<int>& x) {
|
||||
vector<int> res(m + 1);
|
||||
for (int i = 0; i <= m; ++i) {
|
||||
int curr = x[i];
|
||||
res[curr] = i;
|
||||
}
|
||||
return res;
|
||||
};
|
||||
vector<array<int, 11>> trie(1);
|
||||
auto insert = [&] (const vector<int>& x) -> void {
|
||||
int curr = 0;
|
||||
for (int i = 1; i <= m; ++i) {
|
||||
// debug(i), debug(x[i]);
|
||||
// debug(curr);
|
||||
if (not trie[curr][x[i]]) {
|
||||
trie[curr][x[i]] = trie.size();
|
||||
trie.push_back({});
|
||||
}
|
||||
curr = trie[curr][x[i]];
|
||||
}
|
||||
};
|
||||
auto query = [&] (const vector<int>& x) -> int {
|
||||
int curr = 0;
|
||||
for (int i = 1; i <= m; ++i) {
|
||||
if (not trie[curr][x[i]]) {
|
||||
return i - 1;
|
||||
}
|
||||
curr = trie[curr][x[i]];
|
||||
}
|
||||
return m;
|
||||
};
|
||||
for (int i = 0; i < n; ++i) {
|
||||
insert(rev(a[i]));
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cout << query(a[i]) << " \n"[i + 1 == n];
|
||||
}
|
||||
copy(res.begin(), res.end(), oi<ll>(cout, "\n"));
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
@ -454,7 +506,7 @@ int main() {
|
|||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t < (DUMP_TEST_CASE)) {
|
||||
if (t < (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
|
|
|
@ -1,7 +1,18 @@
|
|||
4
|
||||
2 3 5 7
|
||||
3
|
||||
1 3
|
||||
2 3
|
||||
3 4
|
||||
2 4 1 3
|
||||
1 2 4 3
|
||||
2 1 3 4
|
||||
2 2
|
||||
1 2
|
||||
2 1
|
||||
8 10
|
||||
3 4 9 6 10 2 7 8 1 5
|
||||
3 9 1 8 5 7 4 10 2 6
|
||||
3 10 1 7 5 9 6 4 2 8
|
||||
1 2 3 4 8 6 10 7 9 5
|
||||
1 2 3 4 10 6 8 5 7 9
|
||||
9 6 1 2 10 4 7 8 3 5
|
||||
7 9 3 2 5 6 4 8 1 10
|
||||
9 4 3 7 5 6 1 10 8 2
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
1 1 3 5 5
|
|
@ -15,7 +15,7 @@ using namespace std;
|
|||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
#define __as_typeof(container) remove_reference<decltype(container)>::type
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
|
@ -68,6 +68,7 @@ using tiii = tuple<int, int, int>;
|
|||
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;
|
||||
|
@ -202,10 +203,17 @@ template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, s
|
|||
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(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (auto& x : a) cin >> x;)
|
||||
#define readvec1(type, a, n) __AS_PROCEDURE(vector<type> a((n) + 1); copy_n(ii<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;)
|
||||
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;
|
||||
|
@ -391,10 +399,46 @@ template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, Ran
|
|||
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 (; a_it != a_last and 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; }
|
||||
};
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
// #define TOT_TEST_CASE 10000
|
||||
|
||||
void dump() {}
|
||||
|
||||
|
@ -417,7 +461,7 @@ int main() {
|
|||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t < (DUMP_TEST_CASE)) {
|
||||
if (t < (TOT_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
|
|
482
src/bin/test.cc
482
src/bin/test.cc
|
@ -1,474 +1,16 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
#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) decltype(container)::value_type
|
||||
|
||||
/* 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;
|
||||
using ld = long double;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using ld = long double;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
using pid = pair<int, ld>;
|
||||
using pdi = pair<ld, int>;
|
||||
using pld = pair<ll, ld>;
|
||||
using pdl = pair<ld, ll>;
|
||||
using pdd = pair<ld, ld>;
|
||||
using tlll = tuple<ll, ll, ll>;
|
||||
using tlld = tuple<ll, ll, ld>;
|
||||
using tlli = tuple<ll, ll, int>;
|
||||
using tldl = tuple<ll, ld, ll>;
|
||||
using tldd = tuple<ll, ld, ld>;
|
||||
using tldi = tuple<ll, ld, int>;
|
||||
using tlil = tuple<ll, int, ll>;
|
||||
using tlid = tuple<ll, int, ld>;
|
||||
using tlii = tuple<ll, int, int>;
|
||||
using tdll = tuple<ld, ll, ll>;
|
||||
using tdld = tuple<ld, ll, ld>;
|
||||
using tdli = tuple<ld, ll, int>;
|
||||
using tddl = tuple<ld, ld, ll>;
|
||||
using tddd = tuple<ld, ld, ld>;
|
||||
using tddi = tuple<ld, ld, int>;
|
||||
using tdil = tuple<ld, int, ll>;
|
||||
using tdid = tuple<ld, int, ld>;
|
||||
using tdii = tuple<ld, int, int>;
|
||||
using till = tuple<int, ll, ll>;
|
||||
using tild = tuple<int, ll, ld>;
|
||||
using tili = tuple<int, ll, int>;
|
||||
using tidl = tuple<int, ld, ll>;
|
||||
using tidd = tuple<int, ld, ld>;
|
||||
using tidi = tuple<int, ld, int>;
|
||||
using tiil = tuple<int, int, ll>;
|
||||
using tiid = tuple<int, int, ld>;
|
||||
using tiii = tuple<int, int, int>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
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 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 {
|
||||
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) {
|
||||
res1 = (res1 + x * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + x * 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 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 pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#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, w) __AS_PROCEDURE(ch[u].emplace_back(v, w), ch[v].emplace_back(u, w);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
#define Edgew(ch, u, v, w) __AS_PROCEDURE(ch[u].emplace_back(v, w);)
|
||||
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))
|
||||
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(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
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;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
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(string s, 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(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(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), 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; }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
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 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)); });
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
vector<vector<vector<pair<int, pair<ll,vector<int>>>>>> dp(1 << 14, vector<vector<pair<int, pair<ll, vector<int>>>>>(14, vector<pair<int, pair<ll, vector<int>>>>(14, {INF, {}})));
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> findPermutation(vector<int>& nums) {
|
||||
int n = nums.size();
|
||||
for (int i = 0; i < (1 << n); ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
for (int k = 0; k < n; ++k) {
|
||||
dp[i][j][k].first = INF;
|
||||
dp[i][j][k].second.first = 0;
|
||||
dp[i][j][k].second.second.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
vector<pii> a;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
a.emplace_back(nums[i], i);
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
dp[1 << i][a[i].first][a[i].second] = {abs(a[i].first - a[i].second), {a[i].second, {a[i].second}}};
|
||||
}
|
||||
for (int i = 2; i <= n; ++i) {
|
||||
for (int mask = 0; mask < (1 << n); ++mask) {
|
||||
if (popcount(mask) != i) continue;
|
||||
for (int b = 0; b < n; ++b) {
|
||||
if (((mask >> b) & 1) == 0) {
|
||||
continue;
|
||||
}
|
||||
// use b
|
||||
int prev_mask = mask ^ (1 << b);
|
||||
|
||||
for (int j = 0; j < n; ++j) {
|
||||
int use = 0; int cost = dp[mask][j][a[b].second].first;
|
||||
for (int k = 0; k < n; ++k) {
|
||||
int delta = -abs(j - k) + abs(k - a[b].first) + abs(j - a[b].second);
|
||||
if (dp[prev_mask][j][k].first + delta < cost or dp[prev_mask][j][k].first + delta == cost and dp[prev_mask][j][k].second.first < dp[prev_mask][j][use].second.first) {
|
||||
cost = dp[prev_mask][j][k].first + delta;
|
||||
use = k;
|
||||
}
|
||||
}
|
||||
if (cost != INF) {
|
||||
vector<int> new_vec = dp[prev_mask][j][use].second.second;
|
||||
ll new_hash = dp[prev_mask][j][use].second.first;
|
||||
new_vec.emplace_back(a[b].second);
|
||||
new_hash = new_hash * 14 + a[b].second;
|
||||
if (cost < dp[mask][j][a[b].second].first or cost == dp[mask][j][a[b].second].first and new_hash < dp[mask][j][a[b].second].second.first) {
|
||||
dp[mask][j][a[b].second].second.second = new_vec;
|
||||
dp[mask][j][a[b].second].second.first = new_hash;
|
||||
dp[mask][j][a[b].second].first = cost;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int cost = INF;
|
||||
ll hs = INFLL;
|
||||
vector<int> res;
|
||||
for (int j = 0; j < n; ++j) {
|
||||
for (int k = 0; k < n; ++k) {
|
||||
if (dp[(1 << n) - 1][j][k].first < INF and (dp[(1 << n) - 1][j][k].first < cost or dp[(1 << n) - 1][j][k].first == cost and dp[(1 << n) - 1][j][k].second.first < hs)) {
|
||||
res = dp[(1 << n) - 1][j][k].second.second;
|
||||
hs = dp[(1 << n) - 1][j][k].second.first;
|
||||
// putvec(res);
|
||||
|
||||
cost = dp[(1 << n) - 1][j][k].first;
|
||||
}
|
||||
}
|
||||
}
|
||||
// cout << cost << endl;
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
read(int, n);
|
||||
readvec(int, a, n);
|
||||
cout << Solution().findPermutation(a) << endl;
|
||||
int m = 4;
|
||||
auto rev = [&] (const vector<int>& x) {
|
||||
vector<int> res(m + 1);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
int curr = x[i];
|
||||
res[curr] = i + 1;
|
||||
}
|
||||
return res;
|
||||
};
|
||||
auto res = rev({2, 1, 3, 4});
|
||||
for (auto&& x : res)cout << x << endl;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,21 @@
|
|||
n = 300000
|
||||
print(n)
|
||||
print('1 ' * n)
|
||||
print(n)
|
||||
print(f'1 {n}\n' * n)
|
||||
from random import randint
|
||||
from os import system
|
||||
|
||||
while 1:
|
||||
n = 5
|
||||
ipt = str(n) + '\n'
|
||||
for i in range(2, n + 1):
|
||||
ipt += f'{randint(1, i - 1)} {i}\n'
|
||||
with open('std.in', 'w') as f:
|
||||
f.write(ipt)
|
||||
system('./bad < std.in > std.out')
|
||||
with open('std.out') as f:
|
||||
bad = f.read().strip()
|
||||
system('./good < std.in > std.out')
|
||||
with open('std.out') as f:
|
||||
good = f.read().strip()
|
||||
if bad != good:
|
||||
print('input:', ipt)
|
||||
print('bad:', bad)
|
||||
print('good:', good)
|
||||
break
|
||||
|
|
Loading…
Reference in New Issue