diff --git a/src/bin/a.cc b/src/bin/a.cc index d7ecbb5..28bc6a6 100644 --- a/src/bin/a.cc +++ b/src/bin/a.cc @@ -233,7 +233,7 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// -// #define SINGLE_TEST_CASE +#define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} @@ -241,14 +241,16 @@ void dump() {} void prep() {} void solve() { - read(int, n); - read(string, a); - string rev(a.rbegin(), a.rend()); - if (rev < a) { - cout << rev << a << endl; - } else { - cout << a << endl; + read(string, s); + int open = 1; + for (auto&& x : s) { + if (x == '|') { + open ^= 1; + } else if (open) { + cout << x; + } } + cout << endl; } int main() { diff --git a/src/bin/a.out b/src/bin/a.out index edd9e32..8b8be2e 100755 Binary files a/src/bin/a.out and b/src/bin/a.out differ diff --git a/src/bin/abc-344a.cc b/src/bin/abc-344a.cc new file mode 100644 index 0000000..28bc6a6 --- /dev/null +++ b/src/bin/abc-344a.cc @@ -0,0 +1,275 @@ +#pragma GCC optimize("Ofast") +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::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 */ +using ll = int64_t; +using ull = uint64_t; +using pii = pair; +using pil = pair; +using pli = pair; +using pll = pair; + +/* 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; + +/* random */ + +mt19937 rd(chrono::duration_cast(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 +#define ugt std::greater + +#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 + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +/* 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> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map 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 void __read(T& x) { cin >> x; } +template 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 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 ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} + +/* 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); +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector 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 calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair 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 kmp(string s, string t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector 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; +} +///////////////////////////////////////////////////////// + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 512 + +void dump() {} + +void prep() {} + +void solve() { + read(string, s); + int open = 1; + for (auto&& x : s) { + if (x == '|') { + open ^= 1; + } else if (open) { + cout << x; + } + } + cout << endl; +} + +int main() { + 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 (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + solve(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/abc-344b.cc b/src/bin/abc-344b.cc new file mode 100644 index 0000000..0119ff1 --- /dev/null +++ b/src/bin/abc-344b.cc @@ -0,0 +1,273 @@ +#pragma GCC optimize("Ofast") +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::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 */ +using ll = int64_t; +using ull = uint64_t; +using pii = pair; +using pil = pair; +using pli = pair; +using pll = pair; + +/* 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; + +/* random */ + +mt19937 rd(chrono::duration_cast(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 +#define ugt std::greater + +#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 + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +/* 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> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map 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 void __read(T& x) { cin >> x; } +template 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 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 ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} + +/* 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); +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector 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 calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair 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 kmp(string s, string t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector 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; +} +///////////////////////////////////////////////////////// + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 512 + +void dump() {} + +void prep() {} + +void solve() { + vector res; + loop { + read(int, x); + res.push_back(x); + if (x == 0) break; + } + reverse(res.begin(), res.end()); + for (auto&& x : res) cout << x << endl; +} + +int main() { + 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 (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + solve(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/abc-344c.cc b/src/bin/abc-344c.cc new file mode 100644 index 0000000..86e4f80 --- /dev/null +++ b/src/bin/abc-344c.cc @@ -0,0 +1,288 @@ +#pragma GCC optimize("Ofast") +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::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 */ +using ll = int64_t; +using ull = uint64_t; +using pii = pair; +using pil = pair; +using pli = pair; +using pll = pair; + +/* 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; + +/* random */ + +mt19937 rd(chrono::duration_cast(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 +#define ugt std::greater + +#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 + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +/* 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> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map 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 void __read(T& x) { cin >> x; } +template 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 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 ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} + +/* 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); +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector 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 calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair 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 kmp(string s, string t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector 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; +} +///////////////////////////////////////////////////////// + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 512 + +void dump() {} + +void prep() {} + +void solve() { + read(int, n); + readvec(ll, a, n); + read(int, m); + readvec(ll, b, m); + read(int, l); + readvec(ll, c, l); + unordered_set able; + for (auto&& x : a) { + for (auto&& y : b) { + for (auto&& z : c) { + able.insert(x + y + z); + } + } + } + read(int, q); + while (q--) { + read(int, x); + if (able.count(x)) { + cout << "Yes\n"; + } else { + cout << "No\n"; + } + } +} + +int main() { + 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 (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + solve(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/abc-344d.cc b/src/bin/abc-344d.cc new file mode 100644 index 0000000..d2baa0f --- /dev/null +++ b/src/bin/abc-344d.cc @@ -0,0 +1,291 @@ +#pragma GCC optimize("Ofast") +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::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 */ +using ll = int64_t; +using ull = uint64_t; +using pii = pair; +using pil = pair; +using pli = pair; +using pll = pair; + +/* 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; + +/* random */ + +mt19937 rd(chrono::duration_cast(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 +#define ugt std::greater + +#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 + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +/* 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> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map 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 void __read(T& x) { cin >> x; } +template 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 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 ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} + +/* 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); +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector 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 calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair 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 kmp(string s, string t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector 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; +} +///////////////////////////////////////////////////////// + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 512 + +void dump() {} + +void prep() {} + +void solve() { + read(string, t); + int n = t.length(); + read(int, m); + vector> dp(m + 1, vector(n + 1, INF)); + dp[0][0] = 0; + for (int y = 1; y <= m; ++y) { + read(int, k); + readvec(string, s, k); + for (int i = 0; i <= n; ++i) dp[y][i] = dp[y - 1][i]; + for (auto&& x : s) { + int l = x.size(); + for (int i = 0; i + l <= n; ++i) { + int able = 1; + for (int j = 0; j < l; ++j) { + if (t[i + j] != x[j]) { + able = 0; + break; + } + } + if (able) { + dp[y][i + l] = min(dp[y][i + l], dp[y - 1][i] + 1); + } + } + } + } + cout << (dp[m][n] == INF ? -1 : dp[m][n]) << endl; +} + +int main() { + 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 (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + solve(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/abc-344e.cc b/src/bin/abc-344e.cc new file mode 100644 index 0000000..caa2fc9 --- /dev/null +++ b/src/bin/abc-344e.cc @@ -0,0 +1,315 @@ +#pragma GCC optimize("Ofast") +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::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 */ +using ll = int64_t; +using ull = uint64_t; +using pii = pair; +using pil = pair; +using pli = pair; +using pll = pair; + +/* 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; + +/* random */ + +mt19937 rd(chrono::duration_cast(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 +#define ugt std::greater + +#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 + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +/* 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> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map 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 void __read(T& x) { cin >> x; } +template 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 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 ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} + +/* 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); +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector 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 calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair 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 kmp(string s, string t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector 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; +} +///////////////////////////////////////////////////////// + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 512 + +void dump() {} + +void prep() {} + +struct ListNode { + int val; + ListNode* prev; + ListNode* next; +}; + +void solve() { + read(int, n); + ListNode head {0, nullptr, nullptr}, tail {0, nullptr, nullptr}; + head.next = &tail; + tail.prev = &head; + ListNode* curr = &head; + unordered_map mp; + for (int i = 0; i < n; ++i) { + read(int, x); + curr->next = new ListNode{x, curr, &tail}; + curr = curr->next; + mp[x] = curr; + } + read(int, q); + while (q--) { + read(int, op); + if (op == 1) { + read(int, x, y); + ListNode* pre = mp[x]; + ListNode* nxt = pre->next; + ListNode* nw = new ListNode {y, pre, nxt}; + pre->next = nw; + nxt->prev = nw; + mp[y] = nw; + } else { + read(int, x); + ListNode* nw = mp[x]; + ListNode* pre = nw->prev; + ListNode* nxt = nw->next; + pre->next = nxt; + nxt->prev = pre; + mp.erase(x); + delete nw; + } + } + curr = &head; + loop { + curr = curr->next; + if (curr->prev != &head) delete curr->prev; + if (curr == &tail) { + break; + } + cout << curr->val << ' '; + } + cout << endl; +} + +int main() { + 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 (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + solve(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/abc-344f.cc b/src/bin/abc-344f.cc new file mode 100644 index 0000000..5d49194 --- /dev/null +++ b/src/bin/abc-344f.cc @@ -0,0 +1,326 @@ +#pragma GCC optimize("Ofast") +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::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 */ +using ll = int64_t; +using ull = uint64_t; +using pii = pair; +using pil = pair; +using pli = pair; +using pll = pair; + +/* 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; + +/* random */ + +mt19937 rd(chrono::duration_cast(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 +#define ugt std::greater + +#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 + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +/* 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> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map 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 void __read(T& x) { cin >> x; } +template 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 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 ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} + +/* 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); +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector 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 calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair 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 kmp(string s, string t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector 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; +} +///////////////////////////////////////////////////////// + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 512 + +void dump() {} + +void prep() {} + +void solve() { + read(int, n); + vector> cost(n, vector(n)); + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + cin >> cost[i][j]; + } + } + vector> r(n, vector(n - 1)); + vector> d(n - 1, vector(n)); + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n - 1; ++j) { + cin >> r[i][j]; + } + } + for (int i = 0; i < n - 1; ++i) { + for (int j = 0; j < n; ++j) { + cin >> d[i][j]; + } + } + vector> dp1(n, vector(n, INFLL)); + dp1[n - 1][n - 1] = 0; + for (int i = n - 1; ~i; --i) { + for (int j = n - 1; ~j; --j) { + if (j + 1 != n) dp1[i][j] = min(dp1[i][j], dp1[i][j+1] + r[i][j]); + if (i + 1 != n) dp1[i][j] = min(dp1[i][j], dp1[i+1][j] + d[i][j]); + } + } + vector> dp2(n, vector(n, {INFLL, 0})); + dp2[0][0] = {0, 0}; + ll res = INFLL; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + if (i) { + auto [co, sv] = dp2[i-1][j]; + ll new_co = (max(ll(0), d[i-1][j] - sv) + cost[i-1][j] - 1) / cost[i-1][j]; + ll new_sv = sv + new_co * cost[i-1][j] - d[i-1][j]; + dp2[i][j] = min(dp2[i][j], {co + new_co + 1, new_sv}); + } + if (j) { + auto [co, sv] = dp2[i][j-1]; + ll new_co = (max(ll(0), r[i][j-1] - sv) + cost[i][j-1] - 1) / cost[i][j-1]; + ll new_sv = sv + new_co * cost[i][j-1] - r[i][j-1]; + dp2[i][j] = min(dp2[i][j], {co + new_co + 1, new_sv}); + } + ll wait = (max(ll(0), dp1[i][j] - dp2[i][j].second) + cost[i][j] - 1) / cost[i][j]; + res = min(res, dp2[i][j].first + wait + 2 * n - 2 - i - j); + } + } + // for (int i = 0; i < n; ++i) { + // for (int j = 0; j < n; ++j) { + // cout << dp1[i][j] << ' '; + // } + // cout << endl; + // } + // for (int i = 0; i < n; ++i) { + // for (int j = 0; j < n; ++j) { + // cout << dp2[i][j] << ' '; + // } + // cout << endl; + // } + cout << res << endl; +} + +int main() { + 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 (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + solve(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/b.cc b/src/bin/b.cc index b49a6ff..0119ff1 100644 --- a/src/bin/b.cc +++ b/src/bin/b.cc @@ -233,7 +233,7 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// -// #define SINGLE_TEST_CASE +#define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} @@ -241,46 +241,14 @@ void dump() {} void prep() {} void solve() { - read(int, n); - readvec(int, a, n); - vector oc(n); - for (int i = 0; i < n; ++i) { - oc[a[i]] = 1; - } - int cand = -1; - for (int i = 0; i < n; ++i) { - if (!oc[i]) { - cand = i; - break; - } - } - if (cand == -1) { - cout << -1 << endl; - return; - } - unordered_set open; - vector split; - int cnt = 0; - for (int i = 0; i < n; ++i) { - if (a[i] < cand) { - open.insert(a[i]); - } - if (open.size() == cand) { - split.push_back(i); - open.clear(); - } - } - if (split.size() < 2) { - cout << -1 << endl; - } else { - split.back() = n - 1; - cout << split.size() << endl; - int prev = 0; - for (auto&& x : split) { - cout << prev + 1 << " " << x + 1 << endl; - prev = x + 1; - } + vector res; + loop { + read(int, x); + res.push_back(x); + if (x == 0) break; } + reverse(res.begin(), res.end()); + for (auto&& x : res) cout << x << endl; } int main() { diff --git a/src/bin/c.cc b/src/bin/c.cc index 6decbbb..86e4f80 100644 --- a/src/bin/c.cc +++ b/src/bin/c.cc @@ -233,7 +233,7 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// -// #define SINGLE_TEST_CASE +#define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} @@ -241,44 +241,29 @@ void dump() {} void prep() {} void solve() { - read(int, n, l); - vector a(n); - for (int i = 0; i < n; ++i) cin >> a[i].second >> a[i].first; - sort(a.begin(), a.end()); - auto check = [&] (int k) -> bool { - if (k == 0) return true; - if (k == 1) { - int mn = INT_MAX; - for (int i = 0; i < n; ++i) { - mn = min(mn, a[i].second); + read(int, n); + readvec(ll, a, n); + read(int, m); + readvec(ll, b, m); + read(int, l); + readvec(ll, c, l); + unordered_set able; + for (auto&& x : a) { + for (auto&& y : b) { + for (auto&& z : c) { + able.insert(x + y + z); } - return mn <= l; - } - ll res = INFLL; - for (int i = 0; i < n; ++i) { - priority_queue pq; - ll pq_sum = 0; - for (int j = i + 1; j < n; ++j) { - while (pq.size() > k - 2) pq_sum -= pq.top(), pq.pop(); - if (pq.size() == k - 2) { - res = min(res, pq_sum + a[j].second + a[i].second + a[j].first - a[i].first); - } - pq_sum += a[j].second; - pq.push(a[j].second); - } - } - return res <= l; - }; - int left = 0, right = n; - while (left < right) { - int mid = left + right + 1 >> 1; - if (check(mid)) { - left = mid; - } else { - right= mid - 1; } } - cout << left << endl; + read(int, q); + while (q--) { + read(int, x); + if (able.count(x)) { + cout << "Yes\n"; + } else { + cout << "No\n"; + } + } } int main() { diff --git a/src/bin/codefun-1684.cc b/src/bin/codefun-1684.cc new file mode 100644 index 0000000..1a62e5b --- /dev/null +++ b/src/bin/codefun-1684.cc @@ -0,0 +1,338 @@ +#pragma GCC optimize("Ofast") +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +/* macro helpers */ +#define __NARGS(...) std::tuple_size::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 */ +using ll = int64_t; +using ull = uint64_t; +using pii = pair; +using pil = pair; +using pli = pair; +using pll = pair; + +/* 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; + +/* random */ + +mt19937 rd(chrono::duration_cast(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 +#define ugt std::greater + +#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 + size_t operator()(const pair& a) const { + auto hash1 = safe_hash()(a.first); + auto hash2 = safe_hash()(a.second); + if (hash1 != hash2) { + return hash1 ^ hash2; + } + return hash1; + } +}; + +/* 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> ch((n) + 1);) +#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) +#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map 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 void __read(T& x) { cin >> x; } +template 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 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 ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { + for (auto&& i : vec) out << i << ' '; + return out; +} + +/* 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); +} + +/* string algorithms */ +vector calc_next(string t) { // pi function of t + int n = (int)t.length(); + vector 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 calc_z(string t) { // z function of t + int m = t.length(); + vector z; + z.push_back(m); + pair 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 kmp(string s, string t) { // find all t in s + string cur = t + '#' + s; + int sz1 = s.size(), sz2 = t.size(); + vector v; + vector 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; +} +///////////////////////////////////////////////////////// + +#define SINGLE_TEST_CASE +// #define DUMP_TEST_CASE 512 + +void dump() {} + +void prep() {} + +class quick_union { +private: + vector c; +public: + quick_union(size_t n) : c(n) { + iota(c.begin(), c.end(), 0); + } + + size_t query(size_t i) { + if (c[i] != i) c[i] = query(c[i]); + return c[i]; + } + + void merge(size_t i, size_t j) { + c[query(i)] = query(j); + } + + bool connected(size_t i, size_t j) { + return query(i) == query(j); + } +}; + +void solve() { + read(int, n, m, q); + vector add_edge; + vector> events; + set pts; + for (int i = 0; i < m; ++i) { + read(int, u, v); + pts.insert(u), pts.insert(v); + add_edge.emplace_back(u, v); + } + int N = 0; + unordered_map mp; + for (auto&& x : pts) mp[x] = ++N; + vector> ch(N + 1); + for (auto [u, v] : add_edge) { + ch[mp[u]].insert(mp[v]), ch[mp[v]].insert(mp[u]); + } + for (int i = 0; i < q; ++i) { + read(int, op, u, v); + if (op == 1) { + ch[mp[v]].erase(mp[u]); + ch[mp[u]].erase(mp[v]); + } + events.emplace_back(op, u, v); + } + reverse(events.begin(), events.end()); + quick_union qu(N + 1); + for (int i = 1; i <= N; ++i) { + for (auto&& j : ch[i]) { + qu.merge(i, j); + } + } + vector res; + for (auto&& [op, u, v] : events) { + if (op == 1) { + qu.merge(mp[u], mp[v]); + } else { + if (mp.count(u) && mp.count(v) && qu.connected(mp[u], mp[v])) { + res.push_back(1); + } else { + res.push_back(0); + } + } + } + reverse(res.begin(), res.end()); + for (auto&& x : res) { + if (x) { + cout << "Yes\n"; + } else { + cout << "No\n"; + } + } +} + +int main() { + 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 (i + 1 == (DUMP_TEST_CASE)) { + dump(); + } else { + solve(); + } +#else + solve(); +#endif + } +#endif +} diff --git a/src/bin/d.cc b/src/bin/d.cc index 2077ef4..d2baa0f 100644 --- a/src/bin/d.cc +++ b/src/bin/d.cc @@ -233,7 +233,7 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// -// #define SINGLE_TEST_CASE +#define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} @@ -241,36 +241,32 @@ void dump() {} void prep() {} void solve() { - read(int, n, c); - readvec(int, a, n); - ll tot = ll(c + 2) * (c + 1) / 2; - for (int i = 0; i < n; ++i) { - tot -= max(0, (2 * min(a[i], c) - a[i]) / 2 + 1); - tot -= max(0, c - a[i] + 1); - } - vector odd(n + 1), even(n + 1); - for (int i = 1; i <= n; ++i) { - odd[i] = odd[i - 1] + (a[i - 1] % 2 == 1); - even[i] = even[i - 1] + (a[i - 1] % 2 == 0); - } - for (int i = 0; i < n; ++i) { - if (a[i] > 2 * c) break; - int l = i, r = n - 1; - while (l < r) { - int mid = l + r + 1 >> 1; - if (a[mid] + a[i] > 2 * c) { - r = mid - 1; - } else { - l = mid; + read(string, t); + int n = t.length(); + read(int, m); + vector> dp(m + 1, vector(n + 1, INF)); + dp[0][0] = 0; + for (int y = 1; y <= m; ++y) { + read(int, k); + readvec(string, s, k); + for (int i = 0; i <= n; ++i) dp[y][i] = dp[y - 1][i]; + for (auto&& x : s) { + int l = x.size(); + for (int i = 0; i + l <= n; ++i) { + int able = 1; + for (int j = 0; j < l; ++j) { + if (t[i + j] != x[j]) { + able = 0; + break; + } + } + if (able) { + dp[y][i + l] = min(dp[y][i + l], dp[y - 1][i] + 1); + } } } - if (a[i] % 2 == 0) { - tot += even[r + 1] - even[i]; - } else { - tot += odd[r + 1] - odd[i]; - } } - cout << tot << endl; + cout << (dp[m][n] == INF ? -1 : dp[m][n]) << endl; } int main() { diff --git a/src/bin/e.cc b/src/bin/e.cc index c220e86..caa2fc9 100644 --- a/src/bin/e.cc +++ b/src/bin/e.cc @@ -7,10 +7,7 @@ */ #include -#include using namespace std; -using namespace __gnu_cxx; -using namespace __gnu_pbds; /* macro helpers */ #define __NARGS(...) std::tuple_size::value @@ -31,8 +28,8 @@ using pll = pair; /* constants */ constexpr int INF = 0x3f3f3f3f; constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; -constexpr ull MDL = 1e9 + 7; -constexpr ull PRIME = 998'244'353; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; constexpr ll MDL1 = 8784491; constexpr ll MDL2 = PRIME; @@ -118,6 +115,20 @@ struct pair_hash { #define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) #define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) #define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map 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)) @@ -128,7 +139,22 @@ template void __read(T& x, U&... args) { cin >> x; __ #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 ostream& operator<<(ostream& out, vector vec) { +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { for (auto&& i : vec) out << i << ' '; return out; } @@ -207,38 +233,62 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// - -// #define SINGLE_TEST_CASE +#define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} void prep() {} +struct ListNode { + int val; + ListNode* prev; + ListNode* next; +}; + void solve() { read(int, n); - readvec(int, a, n); - vector ps(n + 1); - for (int i = 1; i <= n; ++i) { - ps[i] = ps[i-1] + a[i-1]; + ListNode head {0, nullptr, nullptr}, tail {0, nullptr, nullptr}; + head.next = &tail; + tail.prev = &head; + ListNode* curr = &head; + unordered_map mp; + for (int i = 0; i < n; ++i) { + read(int, x); + curr->next = new ListNode{x, curr, &tail}; + curr = curr->next; + mp[x] = curr; } read(int, q); - auto calc = [] (ll u, int p) -> ll { - return (2 * u + 1 - p) * p / 2; - }; while (q--) { - read(int, s); - read(ll, u); - int l = s, r = n; - while (l < r) { - int mid = l + r + 1 >> 1; - if (mid != s && calc(u, ps[mid] - ps[s-1]) <= calc(u, ps[mid-1] - ps[s-1])) { - r = mid - 1; - } else { - l = mid; - } + read(int, op); + if (op == 1) { + read(int, x, y); + ListNode* pre = mp[x]; + ListNode* nxt = pre->next; + ListNode* nw = new ListNode {y, pre, nxt}; + pre->next = nw; + nxt->prev = nw; + mp[y] = nw; + } else { + read(int, x); + ListNode* nw = mp[x]; + ListNode* pre = nw->prev; + ListNode* nxt = nw->next; + pre->next = nxt; + nxt->prev = pre; + mp.erase(x); + delete nw; } - cout << l << ' '; + } + curr = &head; + loop { + curr = curr->next; + if (curr->prev != &head) delete curr->prev; + if (curr == &tail) { + break; + } + cout << curr->val << ' '; } cout << endl; } @@ -255,7 +305,7 @@ int main() { if (i + 1 == (DUMP_TEST_CASE)) { dump(); } else { - solve(); + solve(); } #else solve(); diff --git a/src/bin/f.cc b/src/bin/f.cc index c43b770..5d49194 100644 --- a/src/bin/f.cc +++ b/src/bin/f.cc @@ -7,10 +7,7 @@ */ #include -#include using namespace std; -using namespace __gnu_cxx; -using namespace __gnu_pbds; /* macro helpers */ #define __NARGS(...) std::tuple_size::value @@ -31,8 +28,8 @@ using pll = pair; /* constants */ constexpr int INF = 0x3f3f3f3f; constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL; -constexpr ull MDL = 1e9 + 7; -constexpr ull PRIME = 998'244'353; +constexpr ll MDL = 1e9 + 7; +constexpr ll PRIME = 998'244'353; constexpr ll MDL1 = 8784491; constexpr ll MDL2 = PRIME; @@ -118,6 +115,20 @@ struct pair_hash { #define adj(ch, n) __AS_PROCEDURE(vector> ch((n) + 1);) #define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);) #define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);) +template pair> discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + map mp; + for (auto&& x : st) mp[x] = ++N; + return {N, mp}; +} +template pair> unordered_discretize(Iterator __first, Iterator __last) { + set st(__first, __last); + size_t N = 0; + unordered_map 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)) @@ -128,7 +139,22 @@ template void __read(T& x, U&... args) { cin >> x; __ #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 ostream& operator<<(ostream& out, vector vec) { +template ostream& operator<<(ostream& out, const pair& p) { + out << "{" << p.first << ", " << p.second << "}"; + return out; +} +template +void print_tuple_impl(std::basic_ostream& os, const Tuple& t, std::index_sequence) { + using swallow = int[]; // guaranties left to right order + (void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get(t)), 0)... }; +} +template +decltype(auto) operator<<(std::basic_ostream& os, const std::tuple& t) { + os << "{"; + print_tuple_impl(os, t, std::index_sequence_for{}); + return os << "}"; +} +template ostream& operator<<(ostream& out, const vector& vec) { for (auto&& i : vec) out << i << ' '; return out; } @@ -207,8 +233,7 @@ int period(string s) { // find the length of shortest recurring period } ///////////////////////////////////////////////////////// - -// #define SINGLE_TEST_CASE +#define SINGLE_TEST_CASE // #define DUMP_TEST_CASE 512 void dump() {} @@ -216,56 +241,67 @@ void dump() {} void prep() {} void solve() { - read(int, n, m); - vector> a(n, vector(m)); + read(int, n); + vector> cost(n, vector(n)); for (int i = 0; i < n; ++i) { - for (int j = 0; j < m; ++j) { - read(char, c); - a[i][j] = c - 48; + for (int j = 0; j < n; ++j) { + cin >> cost[i][j]; } } - auto serialize = [&] (int x, int y) -> int { - return x * m + y; - }; - vector> ch(n * m); + vector> r(n, vector(n - 1)); + vector> d(n - 1, vector(n)); for (int i = 0; i < n; ++i) { - for (int j = 0; j < m; ++j) { - if (a[i][j]) continue; - if (a[mod(i-1, n)][j] == 0 && a[mod(i-2, n)][j] == 0) { - ch[serialize(mod(i-2, n), j)].push_back(serialize(mod(i, n), j)); - } - if (j && a[mod(i-1, n)][j-1] == 0) { - ch[serialize(mod(i-1, n), j-1)].push_back(serialize(mod(i, n), j)); - } + for (int j = 0; j < n - 1; ++j) { + cin >> r[i][j]; } } - vector vis(n * m); - vector dis(n * m, INF); - deque dq; - dq.push_back(serialize(0, 0)); - dis[serialize(0, 0)] = 0; - while (dq.size()) { - int v = dq.front(); dq.pop_front(); - continue_or(vis[v], 1); - for (auto&& u : ch[v]) { - if (dis[v] + 1 < dis[u]) { - dis[u] = dis[v] + 1; - dq.push_back(u); - } + for (int i = 0; i < n - 1; ++i) { + for (int j = 0; j < n; ++j) { + cin >> d[i][j]; } } - int res = INF; + vector> dp1(n, vector(n, INFLL)); + dp1[n - 1][n - 1] = 0; + for (int i = n - 1; ~i; --i) { + for (int j = n - 1; ~j; --j) { + if (j + 1 != n) dp1[i][j] = min(dp1[i][j], dp1[i][j+1] + r[i][j]); + if (i + 1 != n) dp1[i][j] = min(dp1[i][j], dp1[i+1][j] + d[i][j]); + } + } + vector> dp2(n, vector(n, {INFLL, 0})); + dp2[0][0] = {0, 0}; + ll res = INFLL; for (int i = 0; i < n; ++i) { - int d = dis[serialize(i, m - 1)]; - if (d == INF) continue; - int k = (d - i + n - 2) / n; - res = min(res, i + 1 + k * n); - } - if (res == INF) { - cout << -1 << endl; - } else { - cout << res << endl; + for (int j = 0; j < n; ++j) { + if (i) { + auto [co, sv] = dp2[i-1][j]; + ll new_co = (max(ll(0), d[i-1][j] - sv) + cost[i-1][j] - 1) / cost[i-1][j]; + ll new_sv = sv + new_co * cost[i-1][j] - d[i-1][j]; + dp2[i][j] = min(dp2[i][j], {co + new_co + 1, new_sv}); + } + if (j) { + auto [co, sv] = dp2[i][j-1]; + ll new_co = (max(ll(0), r[i][j-1] - sv) + cost[i][j-1] - 1) / cost[i][j-1]; + ll new_sv = sv + new_co * cost[i][j-1] - r[i][j-1]; + dp2[i][j] = min(dp2[i][j], {co + new_co + 1, new_sv}); + } + ll wait = (max(ll(0), dp1[i][j] - dp2[i][j].second) + cost[i][j] - 1) / cost[i][j]; + res = min(res, dp2[i][j].first + wait + 2 * n - 2 - i - j); + } } + // for (int i = 0; i < n; ++i) { + // for (int j = 0; j < n; ++j) { + // cout << dp1[i][j] << ' '; + // } + // cout << endl; + // } + // for (int i = 0; i < n; ++i) { + // for (int j = 0; j < n; ++j) { + // cout << dp2[i][j] << ' '; + // } + // cout << endl; + // } + cout << res << endl; } int main() { @@ -280,7 +316,7 @@ int main() { if (i + 1 == (DUMP_TEST_CASE)) { dump(); } else { - solve(); + solve(); } #else solve(); diff --git a/src/bin/std.in b/src/bin/std.in index 70c4422..d5b9057 100644 --- a/src/bin/std.in +++ b/src/bin/std.in @@ -1,2 +1,10 @@ -500000 1 +3 +1 1 1 +1 1 1 +1 1 1 +1000000000 1000000000 +1000000000 1000000000 +1000000000 1000000000 +1000000000 1000000000 1000000000 +1000000000 1000000000 1000000000