This commit is contained in:
subcrip 2024-06-02 19:58:11 +08:00
parent d82e0e4b5c
commit 9008247a12
Signed by: subcrip
SSH Key Fingerprint: SHA256:dFPFi68d8C87YkFkEBU4TkcrYRySWpekRR1hbnDWUCw
9 changed files with 2328 additions and 242 deletions

Binary file not shown.

679
src/bin/cf-1616e.cc Normal file
View File

@ -0,0 +1,679 @@
/**
* Author: subcrip
* Created: 2024-06-02 19:04:46
* Modified: 2024-06-02 19:52:13
* Elapsed: 47 minutes
*/
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with 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 faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
#define 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 */
template <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(b, p / 2);
if (p % 2 == 1) return half * half * b;
else return half * half;
}
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
#define functor(func) [&](auto&&... val) \
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
}
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
}
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> res;
auto a_it = a_first;
auto b_it = b_first;
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
res.emplace_back(*a_it, *b_it);
}
return res;
}
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> res;
if (n > 0) {
res.emplace_back(*a_first, *b_first);
for (size_t i = 1; i != n; ++i) {
res.emplace_back(*++a_first, *++b_first);
}
}
return res;
}
template <typename T>
class ArithmeticIterator : bidirectional_iterator_tag {
public:
using difference_type = ptrdiff_t;
using value_type = T;
private:
value_type value;
public:
ArithmeticIterator(const T& value) : value(value) {}
value_type operator*() const { return value; }
ArithmeticIterator<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
/////////////////////////////////////////////////////////
// #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
void dump() {}
void dump_ignore() {}
void prep() {
}
template<typename Addable_Info_t, typename Tag_t, typename Sequence = std::vector<Addable_Info_t>> class segtree {
private:
using size_type = uint64_t;
using info_type = Addable_Info_t;
using tag_type = Tag_t;
size_type _max;
vector<info_type> d;
vector<tag_type> b;
void pull(size_type p) {
d[p] = d[p * 2] + d[p * 2 + 1];
}
void push(size_type p, size_type left_len, size_type right_len) {
d[p * 2].apply(b[p], left_len), d[p * 2 + 1].apply(b[p], right_len);
b[p * 2].apply(b[p]), b[p * 2 + 1].apply(b[p]);
b[p] = tag_type();
}
void set(size_type s, size_type t, size_type p, size_type x, const info_type& c) {
if (s == t) {
d[p] = c;
return;
}
size_type m = s + (t - s >> 1);
if (s != t) push(p, m - s + 1, t - m);
if (x <= m) set(s, m, p * 2, x, c);
else set(m + 1, t, p * 2 + 1, x, c);
pull(p);
}
void range_apply(size_type s, size_type t, size_type p, size_type l, size_type r, const tag_type& c) {
if (l <= s && t <= r) {
d[p].apply(c, t - s + 1);
b[p].apply(c);
return;
}
size_type m = s + (t - s >> 1);
push(p, m - s + 1, t - m);
if (l <= m) range_apply(s, m, p * 2, l, r, c);
if (r > m) range_apply(m + 1, t, p * 2 + 1, l, r, c);
pull(p);
}
info_type range_query(size_type s, size_type t, size_type p, size_type l, size_type r) {
if (l <= s && t <= r) {
return d[p];
}
size_type m = s + (t - s >> 1);
info_type res = {};
push(p, m - s + 1, t - m);
if (l <= m) res = res + range_query(s, m, p * 2, l, r);
if (r > m) res = res + range_query(m + 1, t, p * 2 + 1, l, r);
return res;
}
void build(const Sequence& a, size_type s, size_type t, size_type p) {
if (s == t) {
d[p] = a[s];
return;
}
int m = s + (t - s >> 1);
build(a, s, m, p * 2);
build(a, m + 1, t, p * 2 + 1);
pull(p);
}
public:
segtree(size_type __max) : d(4 * __max), b(4 * __max), _max(__max - 1) {}
segtree(const Sequence& a) : segtree(a.size()) {
build(a, {}, _max, 1);
}
void set(size_type i, const info_type& c) {
set({}, _max, 1, i, c);
}
void range_apply(size_type l, size_type r, const tag_type& c) {
range_apply({}, _max, 1, l, r, c);
}
void apply(size_type i, const tag_type& c) {
range_apply(i, i, c);
}
info_type range_query(size_type l, size_type r) {
return range_query({}, _max, 1, l, r);
}
info_type query(size_type i) {
return range_query(i, i);
}
Sequence serialize() {
Sequence res = {};
for (size_type i = 0; i <= _max; ++i) {
res.push_back(query(i));
}
return res;
}
const vector<info_type>& get_d() {
return d;
}
};
struct Tag {
int val = 0;
void apply(const Tag& rhs) {
val = rhs.val;
}
};
struct Info {
int val = 0;
void apply(const Tag& rhs, size_t len) {
val += rhs.val * len;
}
};
Info operator+(const Info &a, const Info &b) {
return {a.val + b.val};
}
void solve() {
read(int, n);
read(string, s, t);
array<vector<int>, 26> bk;
for (int i = n - 1; ~i; --i) {
bk[s[i] - 'a'].emplace_back(i);
}
ll res = INFLL;
ll cnt = 0;
vector<int> mark(n);
segtree<Info, Tag> tr(n);
auto get = [&] (int i) -> int {
return i + (i + 1 > n - 1 ? 0 : tr.range_query(i + 1, n - 1).val);
};
int ptr = 0;
for (int i = 0; i < n; ++i) {
while (mark[ptr]) ++ptr;
assert(ptr < n);
int x = s[ptr] - 'a', y = t[i] - 'a';
if (x < y) {
res = min(res, cnt);
}
int f = 0;
for (int j = 0; j < 26; ++j) {
while (bk[j].size() and (mark[bk[j].back()] or get(bk[j].back()) < i)) {
bk[j].pop_back();
}
if (bk[j].size()) {
if (j < y) {
res = min(res, cnt + get(bk[j].back()) - i);
} else if (j == y) {
f = 1;
mark[bk[j].back()] = 1;
tr.set(bk[j].back(), {1});
cnt += get(bk[j].back()) - i;
}
}
}
if (f == 0) break;
}
if (res == INFLL) {
cout << -1 << '\n';
} else {
cout << res << '\n';
}
}
int main() {
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
assert(false && "incompatible compiler variant detected.");
#endif
untie;
prep();
#ifdef SINGLE_TEST_CASE
solve();
#else
read(int, t);
for (int i = 0; i < t; ++i) {
#ifdef DUMP_TEST_CASE
if (t != (TOT_TEST_CASE)) {
solve();
} else if (i + 1 == (DUMP_TEST_CASE)) {
dump();
} else {
dump_ignore();
}
#else
solve();
#endif
}
#endif
}

659
src/bin/cf-1621e.cc Normal file
View File

@ -0,0 +1,659 @@
/**
* Author: subcrip
* Created: 2024-06-02 16:23:11
* Modified: 2024-06-02 18:09:02
* Elapsed: 105 minutes
*/
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with 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 faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
#define 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 */
template <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(b, p / 2);
if (p % 2 == 1) return half * half * b;
else return half * half;
}
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
#define functor(func) [&](auto&&... val) \
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
}
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
}
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> res;
auto a_it = a_first;
auto b_it = b_first;
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
res.emplace_back(*a_it, *b_it);
}
return res;
}
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> res;
if (n > 0) {
res.emplace_back(*a_first, *b_first);
for (size_t i = 1; i != n; ++i) {
res.emplace_back(*++a_first, *++b_first);
}
}
return res;
}
template <typename T>
class ArithmeticIterator : bidirectional_iterator_tag {
public:
using difference_type = ptrdiff_t;
using value_type = T;
private:
value_type value;
public:
ArithmeticIterator(const T& value) : value(value) {}
value_type operator*() const { return value; }
ArithmeticIterator<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
/////////////////////////////////////////////////////////
// #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
void dump() {}
void dump_ignore() {}
void prep() {
}
template<typename T>
struct BIT {
int n;
vector<T> c;
BIT(size_t n) : n(n), c(n + 1) {}
void add(size_t i, const T& k) {
while (i <= n) {
c[i] += k;
i += lowbit(i);
}
}
T getsum(size_t i) {
T res = {};
while (i) {
res += c[i];
i -= lowbit(i);
}
return res;
}
};
template<typename _Tp, typename _Op = function<_Tp(const _Tp&, const _Tp&)>> struct sparse_table {
_Op op;
vector<vector<_Tp>> st;
template <typename ReverseIterator>
sparse_table(ReverseIterator __first, ReverseIterator __last, _Op&& __operation) {
op = __operation;
int n = distance(__first, __last);
st = vector<vector<_Tp>>(n, vector<_Tp>(int(log2(n) + 1)));
int i = n - 1;
for (auto it = __first; it != __last; ++it) {
st[i][0] = *it;
for (int j = 1; i + (1 << j) <= n; ++j) {
st[i][j] = op(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
i -= 1;
}
}
_Tp query(size_t __start, size_t __end) {
int s = lg2(__end - __start + 1);
return op(st[__start][s], st[__end - (1 << s) + 1][s]);
}
};
void solve() {
read(int, n, m);
readvec(int, a, n);
int max_prof = *max_element(a.begin(), a.end());
BIT<int> prof(max_prof);
for (auto&& x : a) prof.add(x, 1);
vector<tuple<int, ll, vector<int>>> b(m);
for (int i = 0; i < m; ++i) {
read(int, k);
ll sum = 0;
for (int j = 0; j < k; ++j) {
read(int, x);
sum += x;
get<2>(b[i]).emplace_back(x);
}
get<1>(b[i]) = sum;
get<0>(b[i]) = i;
}
auto get = [] (const tuple<int, ll, vector<int>>& x) -> int {
return (std::get<1>(x) + std::get<2>(x).size() - 1) / std::get<2>(x).size();
};
sort_by_key(b.begin(), b.end(), get);
// for (auto&& x : b) {
// cerr << get(x) << ' ';
// }
// cerr << endl;
vector<int> c(m);
for (int i = 0; i < m; ++i) {
int avg = get(b[i]);
c[i] = prof.getsum(max_prof) - prof.getsum(min(max_prof, avg - 1)) - (m - i);
}
// debug(c);
sparse_table<int> st(c.rbegin(), c.rend(), functor(min));
vector<vector<int>> res(m);
for (int i = 0; i < m; ++i) {
// debug(i);
auto&& [idx, s, v] = b[i];
// debug(get(b[i]));
for (auto&& x : v) {
int nw_avg = v.size() == 1 ? 0 : (s - x + v.size() - 2) / (v.size() - 1);
// debug(nw_avg);
int pos;
{
int l = 0, r = m - 1;
while (l < r) {
int mid = l + r + 1 >> 1;
if (get(b[mid]) <= nw_avg) {
l = mid;
} else {
r = mid - 1;
}
}
pos = l;
}
if (get(b[pos]) <= nw_avg) ++pos;
// debug(pos);
if (pos > i) {
// debug(make_tuple(prof.getsum(max_prof) - prof.getsum(min(max_prof, max(0, nw_avg - 1))), m - pos + 1));
// debug(st.query(pos, m - 1));
// debug(true);
if ((i + 1 > pos - 1 or st.query(i + 1, pos - 1) >= 1) and (0 > i - 1 or st.query(0, i - 1) >= 0)
and (pos > m - 1 or st.query(pos, m - 1) >= 0)
and prof.getsum(max_prof) - prof.getsum(min(max_prof, max(0, nw_avg - 1))) >= m - pos + 1) {
// debug("asdf");
res[idx].emplace_back(1);
} else {
res[idx].emplace_back(0);
}
} else {
// debug(false);
if ((0 > pos - 1 or st.query(0, pos - 1) >= 0) and (pos > i - 1 or st.query(pos, i - 1) >= -1)
and (i + 1 > m - 1 or st.query(i + 1, m - 1) >= 0)
and prof.getsum(max_prof) - prof.getsum(min(max_prof, max(0, nw_avg - 1))) >= m - pos) {
res[idx].emplace_back(1);
} else {
res[idx].emplace_back(0);
}
}
}
}
for (auto&& v : res) {
for (auto&& x : v) {
if (x) {
cout << 1;
} else {
cout << 0;
}
}
}
cout << '\n';
}
int main() {
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
assert(false && "incompatible compiler variant detected.");
#endif
untie;
prep();
#ifdef SINGLE_TEST_CASE
solve();
#else
read(int, t);
for (int i = 0; i < t; ++i) {
#ifdef DUMP_TEST_CASE
if (t != (TOT_TEST_CASE)) {
solve();
} else if (i + 1 == (DUMP_TEST_CASE)) {
dump();
} else {
dump_ignore();
}
#else
solve();
#endif
}
#endif
}

605
src/bin/cf-1625d.cc Normal file
View File

@ -0,0 +1,605 @@
/**
* Author: subcrip
* Created: 2024-06-02 15:06:25
* Modified: 2024-06-02 16:02:11
* Elapsed: 55 minutes
*/
#pragma GCC optimize("Ofast")
/////////////////////////////////////////////////////////
/**
* This code should require C++14.
* However, it's only been tested with 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 faster(um) __AS_PROCEDURE((um).reserve(1024); (um).max_load_factor(0.25);)
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
#define 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 */
template <typename return_t>
return_t qpow(ll b, ll p) {
if (b == 0 and p != 0) return 0;
if (p == 0) return 1;
return_t half = qpow<return_t>(b, p / 2);
if (p % 2 == 1) return half * half * b;
else return half * half;
}
#define comb(n, k) ((n) < 0 or (k) < 0 or (n) < (k) ? 0 : fact[n] / fact[k] / fact[(n) - (k)])
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; }
};
struct MLLd {
ll val, mdl;
MLLd(ll mdl, ll v = 0) : mdl(mdl), val(mod(v, mdl)) {}
MLLd(const MLLd& other) : mdl(other.mdl), val(other.val) {}
friend MLLd operator+(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val + rhs.val, lhs.mdl)); }
friend MLLd operator-(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - rhs.val, lhs.mdl)); }
friend MLLd operator*(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * rhs.val, lhs.mdl)); }
friend MLLd operator/(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val * mod(inverse(rhs.val, lhs.mdl), lhs.mdl), lhs.mdl)); }
friend MLLd operator%(const MLLd& lhs, const MLLd& rhs) { return MLLd(lhs.mdl, mod(lhs.val - (lhs / rhs).val, lhs.mdl)); }
friend bool operator==(const MLLd& lhs, const MLLd& rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const MLLd& lhs, const MLLd& rhs) { return lhs.val != rhs.val; }
void operator+=(const MLLd& rhs) { val = (*this + rhs).val; }
void operator-=(const MLLd& rhs) { val = (*this - rhs).val; }
void operator*=(const MLLd& rhs) { val = (*this * rhs).val; }
void operator/=(const MLLd& rhs) { val = (*this / rhs).val; }
void operator%=(const MLLd& rhs) { val = (*this % rhs).val; }
};
template <ll mdl>
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
return out << num.val;
}
ostream& operator<<(ostream& out, const MLLd& num) {
return out << num.val;
}
template <ll mdl>
istream& operator>>(istream& in, MLL<mdl>& num) {
return in >> num.val;
}
istream& operator>>(istream& in, MLLd& num) {
return in >> num.val;
}
// miscancellous
#define functor(func) [&](auto&&... val) \
noexcept(noexcept(func(std::forward<decltype(val)>(val)...))) -> decltype(auto) \
{return func(std::forward<decltype(val)>(val)...);}
template <typename Func, typename RandomIt> void sort_by_key(RandomIt first, RandomIt last, Func extractor) {
std::sort(first, last, [&] (auto&& a, auto&& b) { return std::less<>()(extractor(a), extractor(b)); });
}
template <typename Func, typename RandomIt, typename Compare> void sort_by_key(RandomIt first, RandomIt last, Func extractor, Compare comp) {
std::sort(first, last, [&] (auto&& a, auto&& b) { return comp(extractor(a), extractor(b)); });
}
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip(Iterator_T a_first, Iterator_T a_last, Iterator_U b_first, Iterator_U b_last) {
vector<pair<T, U>> res;
auto a_it = a_first;
auto b_it = b_first;
for (; not (a_it == a_last) and not (b_it == b_last); ++a_it, ++b_it) {
res.emplace_back(*a_it, *b_it);
}
return res;
}
template <typename T, typename U, typename Iterator_T, typename Iterator_U>
vector<pair<T, U>> zip_n(Iterator_T a_first, Iterator_U b_first, size_t n) {
vector<pair<T, U>> res;
if (n > 0) {
res.emplace_back(*a_first, *b_first);
for (size_t i = 1; i != n; ++i) {
res.emplace_back(*++a_first, *++b_first);
}
}
return res;
}
template <typename T>
class ArithmeticIterator : bidirectional_iterator_tag {
public:
using difference_type = ptrdiff_t;
using value_type = T;
private:
value_type value;
public:
ArithmeticIterator(const T& value) : value(value) {}
value_type operator*() const { return value; }
ArithmeticIterator<T>& operator++() { ++value; return *this; }
ArithmeticIterator<T>& operator--() { --value; return *this; }
bool operator==(const ArithmeticIterator<T>& rhs) const { return value == rhs.value; }
};
template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container) {
return zip<int, T>(ArithmeticIterator<int>(0), ArithmeticIterator<int>(INT_MAX), container.begin(), container.end());
}
/////////////////////////////////////////////////////////
#define 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, k);
readvec(int, a, n);
if (k == 0) {
cout << n << '\n';
for (int i = 1; i <= n; ++i) {
cout << i << " \n"[i == n];
}
return;
}
int b = msp(k);
unordered_map<int, vector<pii>, safe_hash> bk;
for (int i = 0; i < n; ++i) {
bk[a[i] >> b + 1].emplace_back(i, a[i] & ((1 << b + 1) - 1));
}
vector<int> res;
for (auto&& [head, v] : bk) {
int one = -1, zero = -1, both = 0;
vector<pair<array<int, 2>, int>> trie = {{array<int, 2>(), -1}};
auto insert = [&] (int x, int mark) -> void {
int ptr = 0;
for (int i = 30; ~i; --i) {
int bit = x >> i & 1;
if (not trie[ptr].first[bit]) {
trie[ptr].first[bit] = trie.size();
trie.emplace_back(array<int, 2>(), -1);
}
ptr = trie[ptr].first[bit];
}
trie[ptr].second = mark;
};
auto query_max = [&] (int x) -> pii {
int ptr = 0;
int num = 0;
for (int i = 30; ~i; --i) {
int bit = x >> i & 1;
if (trie[ptr].first[1 ^ bit]) {
num |= 1 << i;
ptr = trie[ptr].first[1 ^ bit];
} else {
ptr = trie[ptr].first[bit];
}
}
return {trie[ptr].second, num};
};
for (auto&& [i, x] : v) {
if ((x & 1 << b) == 0) {
insert(x, i);
zero = i;
} else {
one = i;
}
}
for (auto&& [i, x] : v) {
if (x & 1 << b) {
auto [idx, num] = query_max(x);
// debug(make_tuple(idx, num));
if (idx != -1 and num >= k) {
both = 1;
zero = idx;
one = i;
break;
}
}
}
if (both) {
res.emplace_back(one), res.emplace_back(zero);
} else if (one != -1) {
res.emplace_back(one);
} else if (zero != -1) {
res.emplace_back(zero);
}
}
if (res.size() < 2) {
cout << -1 << '\n';
return;
}
cout << res.size() << '\n';
for (auto&& i : res) cout << i + 1 << ' ';
cout << endl;
}
int main() {
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
assert(false && "incompatible compiler variant detected.");
#endif
untie;
prep();
#ifdef SINGLE_TEST_CASE
solve();
#else
read(int, t);
for (int i = 0; i < t; ++i) {
#ifdef DUMP_TEST_CASE
if (t != (TOT_TEST_CASE)) {
solve();
} else if (i + 1 == (DUMP_TEST_CASE)) {
dump();
} else {
dump_ignore();
}
#else
solve();
#endif
}
#endif
}

View File

@ -1,8 +1,8 @@
/**
* Author: subcrip
* Created: 2024-06-01 13:45:49
* Modified: 2024-06-01 15:48:33
* Elapsed: 122 minutes
* Created: 2024-06-02 19:04:46
* Modified: 2024-06-02 19:52:13
* Elapsed: 47 minutes
*/
#pragma GCC optimize("Ofast")
@ -485,7 +485,7 @@ template <typename T> vector<pair<int, T>> enumerate(const vector<T>& container)
}
/////////////////////////////////////////////////////////
#define SINGLE_TEST_CASE
// #define SINGLE_TEST_CASE
// #define DUMP_TEST_CASE 7219
// #define TOT_TEST_CASE 10000
@ -496,128 +496,167 @@ void dump_ignore() {}
void prep() {
}
void solve() {
read(int, n, m, k);
vector<vector<short>> grid(n + 1, vector<short>(m + 1));
vector<vector<short>> x_diff(n + 1, vector<short>(m + 2)), y_diff(m + 1, vector<short>(n + 2));
while (k--) {
read(short, x1, y1, x2, y2);
if (x1 == x2) {
if (y1 > y2) swap(y1, y2);
x_diff[x1][y1] += 1;
x_diff[x1][y2 + 1] -= 1;
} else if (y1 == y2) {
if (x1 > x2) swap(x1, x2);
y_diff[y1][x1] += 1;
y_diff[y1][x2 + 1] -= 1;
template<typename Addable_Info_t, typename Tag_t, typename Sequence = std::vector<Addable_Info_t>> class segtree {
private:
using size_type = uint64_t;
using info_type = Addable_Info_t;
using tag_type = Tag_t;
size_type _max;
vector<info_type> d;
vector<tag_type> b;
void pull(size_type p) {
d[p] = d[p * 2] + d[p * 2 + 1];
}
void push(size_type p, size_type left_len, size_type right_len) {
d[p * 2].apply(b[p], left_len), d[p * 2 + 1].apply(b[p], right_len);
b[p * 2].apply(b[p]), b[p * 2 + 1].apply(b[p]);
b[p] = tag_type();
}
for (int i = 1; i <= n; ++i) {
int curr = 0;
for (int j = 1; j <= m; ++j) {
curr += x_diff[i][j];
if (curr) grid[i][j] = 1;
void set(size_type s, size_type t, size_type p, size_type x, const info_type& c) {
if (s == t) {
d[p] = c;
return;
}
}
for (int i = 1; i <= m; ++i) {
int curr = 0;
for (int j = 1; j <= n; ++j) {
curr += y_diff[i][j];
if (curr) grid[j][i] = 1;
}
}
// for (int i = 1; i <= n; ++i) {
// for (int j = 1; j <= m; ++j) {
// cerr << grid[i][j] << " \n"[j == m];
// }
// }
// cerr << endl;
read(short, sx, sy);
grid[sx][sy] = 2;
deque<pair<short, short>> q, nq;
q.emplace_back(sx, sy);
vector<vector<short>> t(n + 1, vector<short>(m + 1));
vector<vector<bool>> v(n + 1, vector<bool>(m + 1));
while (1) {
int f = 0;
// debug(endl);
while (q.size()) {
popfront(q, x, y);
// debug(make_tuple(x, y));
grid[x][y] = 2;
if (x - 1 >= 1) {
if (v[x - 1][y] == 0) {
if (grid[x - 1][y] == 0) {
q.emplace_back(x - 1, y);
} else if (grid[x - 1][y] == 1) {
nq.emplace_back(x - 1, y);
}
v[x - 1][y] = 1;
}
} else {
f = 1;
}
if (x + 1 <= n) {
if (v[x + 1][y] == 0) {
if (grid[x + 1][y] == 0) {
q.emplace_back(x + 1, y);
} else if (grid[x + 1][y] == 1) {
nq.emplace_back(x + 1, y);
}
v[x + 1][y] = 1;
}
} else {
f = 1;
}
if (y + 1 <= m) {
if (v[x][y + 1] == 0) {
if (grid[x][y + 1] == 0) {
q.emplace_back(x, y + 1);
} else if (grid[x][y + 1] == 1) {
nq.emplace_back(x, y + 1);
}
v[x][y + 1] = 1;
}
} else {
f = 1;
}
if (y - 1 >= 1) {
if (v[x][y - 1] == 0) {
if (grid[x][y - 1] == 0) {
q.emplace_back(x, y - 1);
} else if (grid[x][y - 1] == 1) {
nq.emplace_back(x, y - 1);
}
v[x][y - 1] = 1;
}
} else {
f = 1;
size_type m = s + (t - s >> 1);
if (s != t) push(p, m - s + 1, t - m);
if (x <= m) set(s, m, p * 2, x, c);
else set(m + 1, t, p * 2 + 1, x, c);
pull(p);
}
void range_apply(size_type s, size_type t, size_type p, size_type l, size_type r, const tag_type& c) {
if (l <= s && t <= r) {
d[p].apply(c, t - s + 1);
b[p].apply(c);
return;
}
if (f) {
break;
size_type m = s + (t - s >> 1);
push(p, m - s + 1, t - m);
if (l <= m) range_apply(s, m, p * 2, l, r, c);
if (r > m) range_apply(m + 1, t, p * 2 + 1, l, r, c);
pull(p);
}
info_type range_query(size_type s, size_type t, size_type p, size_type l, size_type r) {
if (l <= s && t <= r) {
return d[p];
}
size_type m = s + (t - s >> 1);
info_type res = {};
push(p, m - s + 1, t - m);
if (l <= m) res = res + range_query(s, m, p * 2, l, r);
if (r > m) res = res + range_query(m + 1, t, p * 2 + 1, l, r);
return res;
}
void build(const Sequence& a, size_type s, size_type t, size_type p) {
if (s == t) {
d[p] = a[s];
return;
}
int m = s + (t - s >> 1);
build(a, s, m, p * 2);
build(a, m + 1, t, p * 2 + 1);
pull(p);
}
public:
segtree(size_type __max) : d(4 * __max), b(4 * __max), _max(__max - 1) {}
segtree(const Sequence& a) : segtree(a.size()) {
build(a, {}, _max, 1);
}
void set(size_type i, const info_type& c) {
set({}, _max, 1, i, c);
}
void range_apply(size_type l, size_type r, const tag_type& c) {
range_apply({}, _max, 1, l, r, c);
}
void apply(size_type i, const tag_type& c) {
range_apply(i, i, c);
}
info_type range_query(size_type l, size_type r) {
return range_query({}, _max, 1, l, r);
}
info_type query(size_type i) {
return range_query(i, i);
}
Sequence serialize() {
Sequence res = {};
for (size_type i = 0; i <= _max; ++i) {
res.push_back(query(i));
}
return res;
}
const vector<info_type>& get_d() {
return d;
}
};
struct Tag {
int val = 0;
void apply(const Tag& rhs) {
val = rhs.val;
}
};
struct Info {
int val = 0;
void apply(const Tag& rhs, size_t len) {
val += rhs.val * len;
}
};
Info operator+(const Info &a, const Info &b) {
return {a.val + b.val};
}
void solve() {
read(int, n);
read(string, s, t);
array<vector<int>, 26> bk;
for (int i = n - 1; ~i; --i) {
bk[s[i] - 'a'].emplace_back(i);
}
ll res = INFLL;
ll cnt = 0;
vector<int> mark(n);
segtree<Info, Tag> tr(n);
auto get = [&] (int i) -> int {
return i + (i + 1 > n - 1 ? 0 : tr.range_query(i + 1, n - 1).val);
};
int ptr = 0;
for (int i = 0; i < n; ++i) {
while (mark[ptr]) ++ptr;
assert(ptr < n);
int x = s[ptr] - 'a', y = t[i] - 'a';
if (x < y) {
res = min(res, cnt);
}
int f = 0;
for (int j = 0; j < 26; ++j) {
while (bk[j].size() and (mark[bk[j].back()] or get(bk[j].back()) < i)) {
bk[j].pop_back();
}
if (bk[j].size()) {
if (j < y) {
res = min(res, cnt + get(bk[j].back()) - i);
} else if (j == y) {
f = 1;
mark[bk[j].back()] = 1;
tr.set(bk[j].back(), {1});
cnt += get(bk[j].back()) - i;
}
}
}
if (f == 0) break;
}
if (res == INFLL) {
cout << -1 << '\n';
} else {
swap(q, nq);
}
}
int res = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
// cerr << grid[i][j] << " \n"[j == m];
if (grid[i][j] == 0) {
res += 1;
}
}
}
cout << res << '\n';
}
}
int main() {
#if __cplusplus < 201402L or defined(_MSC_VER) and not defined(__clang__)
assert(false && "incompatible compiler variant detected.");
#endif
untie, cout.tie(NULL);
untie;
prep();
#ifdef SINGLE_TEST_CASE
solve();

View File

@ -1,16 +1,13 @@
17 17 14
2 2 2 13
2 2 7 2
2 7 9 7
7 2 7 7
9 7 9 13
2 13 9 13
11 2 11 7
11 2 16 2
16 2 16 7
16 7 11 7
4 15 4 17
4 15 12 15
12 15 12 9
12 9 17 9
8 9
4
1
a
a
3
rll
rrr
3
caa
aca
5
ababa
aabba

View File

@ -1,8 +1,8 @@
/**
* Author: subcrip
* Created: 2024-06-01 13:45:49
* Modified: 2024-06-01 15:48:33
* Elapsed: 122 minutes
* Modified: 2024-06-02 12:33:56
* Elapsed: 1368 minutes
*/
#pragma GCC optimize("Ofast")
@ -496,121 +496,185 @@ void dump_ignore() {}
void prep() {
}
void solve() {
read(int, n, m, k);
vector<vector<short>> grid(n + 1, vector<short>(m + 1));
vector<vector<short>> x_diff(n + 1, vector<short>(m + 2)), y_diff(m + 1, vector<short>(n + 2));
while (k--) {
read(short, x1, y1, x2, y2);
if (x1 == x2) {
if (y1 > y2) swap(y1, y2);
x_diff[x1][y1] += 1;
x_diff[x1][y2 + 1] -= 1;
} else if (y1 == y2) {
if (x1 > x2) swap(x1, x2);
y_diff[y1][x1] += 1;
y_diff[y1][x2 + 1] -= 1;
constexpr int MAXN = 5010;
int low[MAXN], dfn[MAXN], dfs_clock;
bool isbridge[MAXN];
vector<int> ch[MAXN];
int cnt_bridge;
int father[MAXN];
void tarjan(int u, int fa) {
father[u] = fa;
low[u] = dfn[u] = ++dfs_clock;
for (int i = 0; i < ch[u].size(); i++) {
int v = ch[u][i];
if (!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] > dfn[u]) {
isbridge[v] = true;
++cnt_bridge;
}
} else if (dfn[v] < dfn[u] && v != fa) {
low[u] = min(low[u], dfn[v]);
}
}
for (int i = 1; i <= n; ++i) {
int curr = 0;
for (int j = 1; j <= m; ++j) {
curr += x_diff[i][j];
if (curr) grid[i][j] = 1;
}
}
for (int i = 1; i <= m; ++i) {
int curr = 0;
for (int j = 1; j <= n; ++j) {
curr += y_diff[i][j];
if (curr) grid[j][i] = 1;
}
}
// for (int i = 1; i <= n; ++i) {
// for (int j = 1; j <= m; ++j) {
// cerr << grid[i][j] << " \n"[j == m];
// }
// }
// cerr << endl;
read(short, sx, sy);
grid[sx][sy] = 2;
deque<pair<short, short>> q, nq;
q.emplace_back(sx, sy);
vector<vector<short>> t(n + 1, vector<short>(m + 1));
vector<vector<bool>> v(n + 1, vector<bool>(m + 1));
while (1) {
int f = 0;
// debug(endl);
while (q.size()) {
popfront(q, x, y);
// debug(make_tuple(x, y));
grid[x][y] = 2;
if (x - 1 >= 1) {
if (v[x - 1][y] == 0) {
if (grid[x - 1][y] == 0) {
q.emplace_back(x - 1, y);
} else if (grid[x - 1][y] == 1) {
nq.emplace_back(x - 1, y);
}
v[x - 1][y] = 1;
}
} else {
f = 1;
}
if (x + 1 <= n) {
if (v[x + 1][y] == 0) {
if (grid[x + 1][y] == 0) {
q.emplace_back(x + 1, y);
} else if (grid[x + 1][y] == 1) {
nq.emplace_back(x + 1, y);
}
v[x + 1][y] = 1;
}
} else {
f = 1;
}
if (y + 1 <= m) {
if (v[x][y + 1] == 0) {
if (grid[x][y + 1] == 0) {
q.emplace_back(x, y + 1);
} else if (grid[x][y + 1] == 1) {
nq.emplace_back(x, y + 1);
}
v[x][y + 1] = 1;
}
} else {
f = 1;
}
if (y - 1 >= 1) {
if (v[x][y - 1] == 0) {
if (grid[x][y - 1] == 0) {
q.emplace_back(x, y - 1);
} else if (grid[x][y - 1] == 1) {
nq.emplace_back(x, y - 1);
}
v[x][y - 1] = 1;
}
} else {
f = 1;
}
struct LCA {
vector<int> depth;
vector<vector<int>> pa;
LCA(const vector<vector<tiii>>& g, int root = 1) {
int n = g.size() - 1;
int m = 32 - __builtin_clz(n);
depth.resize(n + 1);
pa.resize(n + 1, vector<int>(m, -1));
function<void(int, int)> dfs = [&](int x, int fa) {
pa[x][0] = fa;
for (auto&& [y, _, ___] : g[x]) {
if (y != fa) {
depth[y] = depth[x] + 1;
dfs(y, x);
}
if (f) {
}
};
dfs(root, 0);
for (int i = 0; i < m - 1; i++)
for (int x = 1; x <= n; x++)
if (int p = pa[x][i]; p != -1)
pa[x][i + 1] = pa[p][i];
}
int get_kth_ancestor(int node, int k) {
for (; k; k &= k - 1)
node = pa[node][__builtin_ctz(k)];
return node;
}
int query(int x, int y) {
if (depth[x] > depth[y])
swap(x, y);
y = get_kth_ancestor(y, depth[y] - depth[x]);
if (y == x)
return x;
for (int i = pa[x].size() - 1; i >= 0; i--) {
int px = pa[x][i], py = pa[y][i];
if (px != py) {
x = px;
y = py;
}
}
return pa[x][0];
}
};
void solve() {
read(int, n, m);
vector<pii> edges;
unordered_set<pii, pair_hash> oc;
while (m--){
read(int, x, y);
if (x == y or oc.count(minmax(x, y))) continue;
oc.insert(minmax(x, y));
edges.emplace_back(x, y);
edge(ch, x, y);
}
tarjan(1, 0);
// for (int i = 1; i <= n; ++i) {
// debug(isbridge[i]);
// }
adj(sp, n);
for (auto&& [u, v] : edges) {
if (isbridge[u] and minmax(u, v) == minmax(father[u], u)) continue;
if (isbridge[v] and minmax(u, v) == minmax(father[v], v)) continue;
// debug(make_tuple(u, v));
edge(sp, u, v);
}
// debugvec(sp);
vector<bool> vis(n + 1);
int N = 0;
vector<int> col(n + 1);
vector<int> sz(1);
vector<int> no(n + 1);
auto dfs = [&] (auto dfs, int v) -> void {
if (vis[v]) return;
vis[v] = 1;
col[v] = N;
no[v] = sz[N];
sz[N] += 1;
for (auto&& u : sp[v]) {
if (vis[u]) continue;
dfs(dfs, u);
break;
} else {
swap(q, nq);
}
}
int res = 0;
};
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
// cerr << grid[i][j] << " \n"[j == m];
if (grid[i][j] == 0) {
res += 1;
if (vis[i]) continue;
++N;
sz.emplace_back(0);
dfs(dfs, i);
}
// debug(col);
vector<vector<tiii>> nch(N + 1);
for (auto&& [u, v] : edges) {
if (col[u] == col[v]) continue;
nch[col[u]].emplace_back(col[v], u, v);
nch[col[v]].emplace_back(col[u], v, u);
}
vector<vector<int>> branch(N + 1, vector<int>(N + 1));
vector<int> branch_up(N + 1);
auto dfs2 = [&] (auto dfs2, int v, int pa) -> vector<int> {
vector<int> chs = {v};
for (auto&& [u, from, to] : nch[v]) {
if (u == pa) continue;
branch_up[u] = to;
// debug(make_tuple(u, to));
vector<int> curr = dfs2(dfs2, u, v);
chs.insert(chs.end(), curr.begin(), curr.end());
for (auto&& x : curr) {
branch[v][x] = from;
}
}
return chs;
};
dfs2(dfs2, 1, 0);
LCA lca(nch);
auto query_dist = [&] (int v, int u) -> int {
// debug(make_tuple(u, v));
assert(col[v] == col[u]);
if (no[v] > no[u]) swap(u, v);
// debug(min(no[u] - no[v], no[v] - no[u] + sz[col[v]]));
return min(no[u] - no[v], no[v] - no[u] + sz[col[v]]);
};
auto query = [&] (int v, int u) -> int {
if (col[v] == col[u]) {
return query_dist(u, v);
}
int p = lca.query(col[u], col[v]);
// debug(make_tuple(v, u));
if (p == col[u]) {
// debug(1);
// return lca.depth[col[v]] - lca.depth[col[u]] + query_dist(u, branch[col[u]][col[v]]) + query_dist(v, branch_up[col[v]]);
return lca.depth[col[v]] - lca.depth[col[u]] + query_dist(v, branch_up[col[v]]);
} else if (p == col[v]) {
// return lca.depth[col[u]] - lca.depth[col[v]] + query_dist(v, branch[col[v]][col[u]]) + query_dist(u, branch_up[col[u]]);
return lca.depth[col[u]] - lca.depth[col[v]] + query_dist(v, branch[col[v]][col[u]]);
}else {
// debug(make_tuple(p, col[v], col[u]));
// debug(lca.depth[col[u]] - lca.depth[p]);
return lca.depth[col[v]] - lca.depth[p] + lca.depth[col[u]] - lca.depth[p]
+ query_dist(v, branch_up[col[v]]);// + query_dist(u, branch_up[col[u]]);
}
};
read(int, t);
int prev = 1;
while (t--) {
read(int, v);
cout << query(prev, v) << " \n"[t == 0];
prev = v;
}
cout << res << '\n';
}
int main() {

View File

@ -4,12 +4,8 @@ from os import system
import io
if __name__ == '__main__':
n = 8
m = 8
st = set()
for i in range(1, n + 1):
for j in range(1, m + 1):
print(i ** j, end='\t')
st.add(i ** j)
print()
print(len(st))
n = 5000
m = 10000
t = 200000
print(n, m)
for _ in range(m):

View File

@ -0,0 +1,47 @@
/**
* Author: subcrip
* Created: 2024-06-02 11:52:54
* Modified: 2024-06-02 11:55:42
* Elapsed: 2 minutes
*/
#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 50010;
int low[MAXN], dfn[MAXN], dfs_clock;
bool isbridge[MAXN];
vector<int> ch[MAXN];
int cnt_bridge;
int father[MAXN];
void tarjan(int u, int fa) {
father[u] = fa;
low[u] = dfn[u] = ++dfs_clock;
for (int i = 0; i < ch[u].size(); i++) {
int v = ch[u][i];
if (!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] > dfn[u]) {
isbridge[v] = true;
++cnt_bridge;
}
} else if (dfn[v] < dfn[u] && v != fa) {
low[u] = min(low[u], dfn[v]);
}
}
}
int main() {
int n, m; cin >> n >> m;
while (m--) {
int u, v; cin >> u >> v;
ch[u].emplace_back(v);
ch[v].emplace_back(u);
}
for (int i = 1; i <= n; ++i) {
if (not dfn[i]) tarjan(i, 0);
}
cout << count(begin(isbridge), end(isbridge), 1) << '\n';
}