diff --git a/src/bin/cf-1920d.cc b/src/bin/cf-1920d.cc new file mode 100644 index 0000000..3c6fcd1 --- /dev/null +++ b/src/bin/cf-1920d.cc @@ -0,0 +1,215 @@ +///////////////////////////////////////////////////////// +/** + * 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 ull MDL = 1e9 + 7; +constexpr ull PRIME = 998'244'353; +constexpr ull MDL1 = 825; +constexpr ull MDL2 = 87825; + +/* 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)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | ull(y)) +#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(p))) +#define i2(p) (int(u2(p))) +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;) + +/* build data structures */ +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t> 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);) + +/* 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, 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();) + +/* 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; +} +///////////////////////////////////////////////////////// + +void solve() { + read(int, n, q); + // for periodic prefixes, last denotes the position of its last period. + // for other prefixes, last denotes the position of its period part. + vector last(n); + vector per(n); + vector length(n); + vector num(n); // if the current op is append, then it stores the added number. + for (int i =0; i < n; ++i) { + read(ll, b, x); + if (b == 1) { + length[i] = {i ? (length[i - 1].first + 1) : 1, i}; + num[i] = x; + if (i) { + if (per[i-1]) last[i] = i - 1; + else last[i] = last[i-1]; + } else { + last[i] = i; + } + per[i] = 0; + } else { + ll prev_len = length[i-1].first; + if (x >= LLONG_MAX / prev_len) { + length[i] = {1e18, i}; + } else { + length[i] = {min(ll(1e18), prev_len * (x + 1)), i}; + } + if (per[i-1]) last[i] = last[i-1]; + else last[i] = i - 1; + per[i] = 1; + } + } + while (q--) { + read(ll, k); + int i = lower_bound(length.begin(), length.end(), make_pair(k, 0))->second; + loop { + if (i == 0) { + cout << num[0] << ' '; + break; + } + ll curr_len = length[last[i]].first; + if (per[i]) { + k = (k - 1) % curr_len + 1; + i = last[i]; + } else { + if (k > curr_len) { + cout << num[last[i] + k - curr_len] << ' '; + break; + } else { + i = last[i]; + } + } + } + } + cout << endl; +} + +int main() { + read(int, t); + while (t--) solve(); +} \ No newline at end of file diff --git a/src/bin/cf-1922e.cc b/src/bin/cf-1922e.cc new file mode 100644 index 0000000..58e6b29 --- /dev/null +++ b/src/bin/cf-1922e.cc @@ -0,0 +1,191 @@ +///////////////////////////////////////////////////////// +/** + * 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 ull MDL = 1e9 + 7; +constexpr ull PRIME = 998'244'353; +constexpr ull MDL1 = 825; +constexpr ull MDL2 = 87825; + +/* 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)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | ull(y)) +#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(p))) +#define i2(p) (int(u2(p))) +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;) + +/* build data structures */ +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t> 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);) + +/* 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;) + +/* 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();) + +/* 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; +} +///////////////////////////////////////////////////////// + +inline unsigned mask(int k) { + return (1 << k) - 1; +} + +void solve() { + read(ull, x); + x -=1 ; + int prev_bit = 0; + int combo = 0; + vectorred(61); + int len = 0, tot = 0; + int curr = 0; + while (x >> curr) { + ++len; ++curr; + int bit = (x >> (curr - 1)) & 1; + if (bit == 0) { + combo = 1; + } else { + if (combo) { + len = curr - 1; + red[prev_bit] = 1; // TODO + tot += len - prev_bit; + x ^= mask(len) - mask(prev_bit); + combo = 1; + } + tot += 1; + prev_bit = len; + } + } + cout << tot << endl; + for (int i = len - 1; i >= 0; --i) { + cout << len - i << ' '; + if (red[i]) cout << 0 << ' '; + } + cout << endl; +} + +int main() { + read(int, t); + while (t--) solve(); +} \ No newline at end of file diff --git a/src/bin/lc-1032.cc b/src/bin/lc-1032.cc new file mode 100644 index 0000000..2e0559a --- /dev/null +++ b/src/bin/lc-1032.cc @@ -0,0 +1,224 @@ +///////////////////////////////////////////////////////// +/** + * 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 ull MDL = 1e9 + 7; +constexpr ull PRIME = 998'244'353; +constexpr ull MDL1 = 825; +constexpr ull MDL2 = 87825; + +/* 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)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | ull(y)) +#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(p))) +#define i2(p) (int(u2(p))) +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;) + +/* build data structures */ +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t> 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);) + +/* 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, 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();) + +/* 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; +} +///////////////////////////////////////////////////////// + + + +class StreamChecker { +private: + vector> trie; + vector tag; + int state; +public: + StreamChecker(vector& ss) { + state = 0; + int n = ss.size(); + trie.push_back(vector(26)); + tag.push_back(0); + int last = 0; + // build trie for ss + for (int i = 0; i < n; ++i) { + auto&& s = ss[i]; + int curr = 0; + for (auto&& c : s) { + int x = c - 'a'; + if (!trie[curr][x]) + trie[curr][x] = ++last, + tag.push_back(0), + trie.push_back(vector(26)); + curr = trie[curr][x]; + } + // cout << "state " << curr << " is for " << s << endl; + tag[curr] = 1; + } + + + // build fail + vector fail(last + 1); + vector> suc(last + 1); + deque dq; + for (int i = 0; i < 26; ++i) { + if (trie[0][i]) dq.push_back(trie[0][i]), suc[0].push_back(trie[0][i]); + } + while (dq.size()) { + int c = dq.front(); dq.pop_front(); + for (int i = 0; i < 26; ++i) { + if (trie[c][i]) { + fail[trie[c][i]] = trie[fail[c]][i]; + suc[trie[fail[c]][i]].push_back(trie[c][i]); + dq.push_back(trie[c][i]); + } else { + trie[c][i] = trie[fail[c]][i]; + } + } + } + auto dfs = [&] (auto dfs, int curr, int prev_tag) -> void { + if (prev_tag | tag[curr]) { + tag[curr] = 1; + } + for (auto&& c : suc[curr]) dfs(dfs, c, prev_tag | tag[curr]); + }; + dfs(dfs, 0, 0); + } + + bool query(char letter) { + state = trie[state][letter - 'a']; + return tag[state]; + } +}; + +/** + * Your StreamChecker object will be instantiated and called as such: + * StreamChecker* obj = new StreamChecker(words); + * bool param_1 = obj->query(letter); + */ \ No newline at end of file diff --git a/src/bin/lg-2852.cc b/src/bin/lg-2852.cc new file mode 100644 index 0000000..e755a5c --- /dev/null +++ b/src/bin/lg-2852.cc @@ -0,0 +1,263 @@ +///////////////////////////////////////////////////////// +/** + * 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 ull MDL = 1e9 + 7; +constexpr ull PRIME = 998'244'353; +constexpr ull MDL1 = 825; +constexpr ull MDL2 = 87825; + +/* 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)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | ull(y)) +#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(p))) +#define i2(p) (int(u2(p))) +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;) + +/* build data structures */ +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t> 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);) + +/* 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, 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();) + +/* 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; +} +///////////////////////////////////////////////////////// + +constexpr int N = 1e6 + 10; + +ll s[N]; +ll n, sa[N], rk[N], oldrk[N << 1], id[N], key1[N], cnt[N], height[N]; + +bool cmp(ll x, ll y, ll w) { + return oldrk[x] == oldrk[y] && oldrk[x + w] == oldrk[y + w]; +} + +void calc_sa() { + ll i, m = N - 1, p, w; + for (i = 1; i <= n; ++i) ++cnt[rk[i] = s[i]]; + for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1]; + for (i = n; i >= 1; --i) sa[cnt[rk[i]]--] = i; + + for (w = 1;; w <<= 1, m = p) { + for (p = 0, i = n; i > n - w; --i) id[++p] = i; + for (i = 1; i <= n; ++i) + if (sa[i] > w) id[++p] = sa[i] - w; + + memset(cnt, 0, sizeof(cnt)); + for (i = 1; i <= n; ++i) ++cnt[key1[i] = rk[id[i]]]; + + for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1]; + for (i = n; i >= 1; --i) sa[cnt[key1[i]]--] = id[i]; + memcpy(oldrk + 1, rk + 1, n * sizeof(ll)); + for (p = 0, i = 1; i <= n; ++i) + rk[sa[i]] = cmp(sa[i], sa[i - 1], w) ? p : ++p; + if (p == n) { + break; + } + } +} + +void calc_height() { + int i, k; + for (i = 1, k = 0; i <= n; ++i) { + if (rk[i] == 0) continue; + if (k) --k; + while (s[i + k] == s[sa[rk[i] - 1] + k]) ++k; + height[rk[i]] = k; + } +} + +ll d[4*N]; + +void build(int s,int t,int p){ // root p, range [s,t] + // debug(s), debug(t), debug(p); + if(s==t){ + d[p]=height[s]; + return; + } + int m=s+(t-s>>1); + build(s,m,p*2),build(m+1,t,p*2+1); + d[p]=min(d[p*2], d[p*2+1]); +} + +ll getmin(int s,int t,int p,int l,int r){ + if (l > r) return LLONG_MAX; + if(l<=s&&t<=r)return d[p]; + int m=s+(t-s>>1); + ll sum=LLONG_MAX; + if(l<=m)sum=min(sum, getmin(s,m,p*2,l,r)); + if(r>m) sum=min(sum, getmin(m+1,t,p*2+1,l,r)); + return sum; +} + +int main() { + untie; + cin >> n; + read(int, k); + for (int i = 1; i <= n; ++i) cin >> s[i]; + calc_sa(), calc_height(); + build(1, n, 1); + int l = 0, r = n; + auto binary_search = [&] (int start, int length) { + int l = start, r = n; + while (l < r) { + int mid = l + r + 1 >> 1; + if (getmin(1, n, 1, start + 1, mid) < length) { + r = mid - 1; + } else { + l = mid; + } + } + return l; + }; + // l = 2, r = 4; + // for (int i = 1; i <= n; ++i) cerr << height[i] << " \n"[i == n]; + // debug(rk[2]), debug(rk[4]); + // for (int start = 1; start <= n; ++start) + // for (int i = start; i <= n; ++i) cerr << getmin(1, n, 1, start, i) << " \n"[i == n]; + while (l < r) { + int curr_len = l + r + 1 >> 1; + ll match = 0; + for (int i = 1; i <= n; ++i) { + ll idx = binary_search(i, curr_len); + match = max(match, idx - i + 1); + } + // debug(curr_len), debug(match); + if (match < k) { + // cerr << curr_len << " not ok\n"; + r = curr_len - 1; + } else { + l = curr_len; + } + } + cout << l << endl; +} \ No newline at end of file diff --git a/src/bin/lg-3808.cc b/src/bin/lg-3808.cc new file mode 100644 index 0000000..c23cb05 --- /dev/null +++ b/src/bin/lg-3808.cc @@ -0,0 +1,224 @@ +///////////////////////////////////////////////////////// +/** + * 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 ull MDL = 1e9 + 7; +constexpr ull PRIME = 998'244'353; +constexpr ull MDL1 = 825; +constexpr ull MDL2 = 87825; + +/* 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)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | ull(y)) +#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(p))) +#define i2(p) (int(u2(p))) +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;) + +/* build data structures */ +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t> 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);) + +/* 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, 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();) + +/* 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; +} +///////////////////////////////////////////////////////// + + +void solve(vector ss, string t) { + vector> trie(1, vector(26)); + vector tag(1); + int last = 0; + int n = ss.size(); + for (int i = 0; i < n; ++i) { + auto&& s = ss[i]; + int curr = 0; + for (auto&& c : s) { + int x = c - 'a'; + if (!trie[curr][x]) + trie[curr][x] = ++last, + tag.push_back({}), + trie.push_back(vector(26)); + curr = trie[curr][x]; + } + tag[curr] += 1; + } + vector fail(last + 1); + vector> suc(last + 1); + deque> dq; + for (int i = 0; i < 26; ++i) { + if (trie[0][i]) dq.emplace_back(trie[0][i], trie[0][i]), suc[0].push_back(trie[0][i]); + } + while (dq.size()) { + popfront(dq, c, rt); + for (int i = 0; i < 26; ++i) { + if (trie[c][i]) { + fail[trie[c][i]] = trie[fail[c]][i]; + suc[trie[fail[c]][i]].push_back(trie[c][i]); + dq.emplace_back(trie[c][i], rt); + } else { + trie[c][i] = trie[fail[c]][i]; + } + } + } + vector oc(last + 1); + int curr = 0; + for (auto&& c : t) { + int x = c - 'a'; + if (!trie[curr][x]) curr = fail[curr]; + curr = trie[curr][x]; + if (tag[curr]) { + oc[curr] += 1; + } + } + int res = 0; + // debug(oc); + auto dfs = [&] (auto dfs, int cur) -> bool { + int has = oc[cur]; + for (auto&& c : suc[cur]) { + has |= dfs(dfs, c); + } + if (has) { + res += tag[cur]; + } + // cerr << "cur = " << cur << ", has = " << has << endl; + return has && tag[cur]; + }; + int rtq = dfs(dfs, 0); + cout << res - rtq << endl; +} + +int main() { + read(int, n); + readvec(string, ss, n); + read(string, t); + solve(ss, t); +} \ No newline at end of file diff --git a/src/bin/lg-5357.cc b/src/bin/lg-5357.cc new file mode 100644 index 0000000..b5d5b90 --- /dev/null +++ b/src/bin/lg-5357.cc @@ -0,0 +1,239 @@ +///////////////////////////////////////////////////////// +/** + * 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 ull MDL = 1e9 + 7; +constexpr ull PRIME = 998'244'353; +constexpr ull MDL1 = 825; +constexpr ull MDL2 = 87825; + +/* 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)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | ull(y)) +#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(p))) +#define i2(p) (int(u2(p))) +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;) + +/* build data structures */ +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t> 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);) + +/* 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, 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();) + +/* 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; +} +///////////////////////////////////////////////////////// + + +void solve(vector ss, string t) { + int n = ss.size(); + vector> trie(1, vector(26)); + vector tag(1); + vector id(1); + vector alias(n, -1); + int last = 0; + // build trie for ss + for (int i = 0; i < n; ++i) { + auto&& s = ss[i]; + int curr = 0; + for (auto&& c : s) { + int x = c - 'a'; + if (!trie[curr][x]) + trie[curr][x] = ++last, + tag.push_back({}), + id.push_back({}), + trie.push_back(vector(26)); + curr = trie[curr][x]; + } + if (!tag[curr]) id[curr] = i; + else alias[i] = id[curr]; + tag[curr] = 1; + } + + + // build fail + vector fail(last + 1); + vector> suc(last + 1); + deque> dq; + for (int i = 0; i < 26; ++i) { + if (trie[0][i]) dq.emplace_back(trie[0][i], trie[0][i]), suc[0].push_back(trie[0][i]); + } + while (dq.size()) { + popfront(dq, c, rt); + for (int i = 0; i < 26; ++i) { + if (trie[c][i]) { + fail[trie[c][i]] = trie[fail[c]][i]; + suc[trie[fail[c]][i]].push_back(trie[c][i]); + dq.emplace_back(trie[c][i], rt); + } else { + trie[c][i] = trie[fail[c]][i]; + } + } + } + + + // match t + vector oc(last + 1); + int curr = 0; + for (auto&& c : t) { + int x = c - 'a'; + if (!trie[curr][x]) curr = fail[curr]; + curr = trie[curr][x]; + oc[curr] += 1; + } + + + // collect result + vector res(n); + auto dfs = [&] (auto dfs, int cur) -> int { + int cnt = oc[cur]; + for (auto&& c : suc[cur]) { + cnt += dfs(dfs, c); + } + if (cnt && tag[cur]) { + res[id[cur]] += cnt; + } + return cnt; + }; + dfs(dfs, 0); + for (int i = 0; i < n; ++i) { + if (alias[i] != -1) cout << res[alias[i]]; + else cout << res[i]; + cout << endl; + } +} + +int main() { + read(int, n); + readvec(string, ss, n); + read(string, t); + solve(ss, t); +} \ No newline at end of file diff --git a/src/bin/test.cc b/src/bin/test.cc index 55828d7..e755a5c 100644 --- a/src/bin/test.cc +++ b/src/bin/test.cc @@ -88,8 +88,12 @@ template void __read(T& x, U&... args) { cin >> x; __ #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 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) { + for (auto&& i : vec) out << i << ' '; + return out; +} /* pops */ #define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();) @@ -136,25 +140,124 @@ vector kmp(string s, string t) { // find all t in s } return v; } -///////////////////////////////////////////////////////// - -int main() { - read(string, s); +int period(string s) { // find the length of shortest recurring period int n = s.length(); - string rev(s.rbegin(), s.rend()); - auto translate = [&] (int i) {return n - i - 1;}; auto z = calc_z(s); - auto pi = calc_next(rev); - int res = 0; - for (int i = 1; i < n - 1; ++i) { - int suf = pi[translate(i)]; - if (suf > 0 && suf <= z[i]) { - res = max(res, suf); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; } } - if (res) { - cout << s.substr(0, res); - } else { - cout << "Just a legend"; + return n; +} +///////////////////////////////////////////////////////// + +constexpr int N = 1e6 + 10; + +ll s[N]; +ll n, sa[N], rk[N], oldrk[N << 1], id[N], key1[N], cnt[N], height[N]; + +bool cmp(ll x, ll y, ll w) { + return oldrk[x] == oldrk[y] && oldrk[x + w] == oldrk[y + w]; +} + +void calc_sa() { + ll i, m = N - 1, p, w; + for (i = 1; i <= n; ++i) ++cnt[rk[i] = s[i]]; + for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1]; + for (i = n; i >= 1; --i) sa[cnt[rk[i]]--] = i; + + for (w = 1;; w <<= 1, m = p) { + for (p = 0, i = n; i > n - w; --i) id[++p] = i; + for (i = 1; i <= n; ++i) + if (sa[i] > w) id[++p] = sa[i] - w; + + memset(cnt, 0, sizeof(cnt)); + for (i = 1; i <= n; ++i) ++cnt[key1[i] = rk[id[i]]]; + + for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1]; + for (i = n; i >= 1; --i) sa[cnt[key1[i]]--] = id[i]; + memcpy(oldrk + 1, rk + 1, n * sizeof(ll)); + for (p = 0, i = 1; i <= n; ++i) + rk[sa[i]] = cmp(sa[i], sa[i - 1], w) ? p : ++p; + if (p == n) { + break; + } + } +} + +void calc_height() { + int i, k; + for (i = 1, k = 0; i <= n; ++i) { + if (rk[i] == 0) continue; + if (k) --k; + while (s[i + k] == s[sa[rk[i] - 1] + k]) ++k; + height[rk[i]] = k; + } +} + +ll d[4*N]; + +void build(int s,int t,int p){ // root p, range [s,t] + // debug(s), debug(t), debug(p); + if(s==t){ + d[p]=height[s]; + return; } + int m=s+(t-s>>1); + build(s,m,p*2),build(m+1,t,p*2+1); + d[p]=min(d[p*2], d[p*2+1]); +} + +ll getmin(int s,int t,int p,int l,int r){ + if (l > r) return LLONG_MAX; + if(l<=s&&t<=r)return d[p]; + int m=s+(t-s>>1); + ll sum=LLONG_MAX; + if(l<=m)sum=min(sum, getmin(s,m,p*2,l,r)); + if(r>m) sum=min(sum, getmin(m+1,t,p*2+1,l,r)); + return sum; +} + +int main() { + untie; + cin >> n; + read(int, k); + for (int i = 1; i <= n; ++i) cin >> s[i]; + calc_sa(), calc_height(); + build(1, n, 1); + int l = 0, r = n; + auto binary_search = [&] (int start, int length) { + int l = start, r = n; + while (l < r) { + int mid = l + r + 1 >> 1; + if (getmin(1, n, 1, start + 1, mid) < length) { + r = mid - 1; + } else { + l = mid; + } + } + return l; + }; + // l = 2, r = 4; + // for (int i = 1; i <= n; ++i) cerr << height[i] << " \n"[i == n]; + // debug(rk[2]), debug(rk[4]); + // for (int start = 1; start <= n; ++start) + // for (int i = start; i <= n; ++i) cerr << getmin(1, n, 1, start, i) << " \n"[i == n]; + while (l < r) { + int curr_len = l + r + 1 >> 1; + ll match = 0; + for (int i = 1; i <= n; ++i) { + ll idx = binary_search(i, curr_len); + match = max(match, idx - i + 1); + } + // debug(curr_len), debug(match); + if (match < k) { + // cerr << curr_len << " not ok\n"; + r = curr_len - 1; + } else { + l = curr_len; + } + } + cout << l << endl; } \ No newline at end of file diff --git a/src/bin/test1.cc b/src/bin/test1.cc new file mode 100644 index 0000000..9439c51 --- /dev/null +++ b/src/bin/test1.cc @@ -0,0 +1,40 @@ +#include + +int main() { + int n; + scanf("%d", &n); + printf("%d\n", n - 1); + fflush(stdout); + for (int i = 0; i < n - 1; ++i) { + printf("%d ", n - 1 - i); + for (int j = i + 1; j <= n - 1; ++j) { + printf("%d", j); + if (j == n - 1) printf("\n"), fflush(stdout); + else printf(" "); + } + + } + char* s = new char[n - 1]; + scanf("%s", s); + for (int i = 0; i < n - 1; ++i) { + if (i == n - 2) { + if (s[i] == '1') { + delete[] s; + printf("%d\n", i + 1); + fflush(stdout); + return 0; + } + } else { + if (s[i] == '1' && s[i+1] == '0') { + delete[] s; + printf("%d\n", i + 1); + fflush(stdout); + return 0; + } + } + } + delete[] s; + printf("%d\n", n); + fflush(stdout); + return 0; +} \ No newline at end of file diff --git a/src/bin/testget.cc b/src/bin/testget.cc new file mode 100644 index 0000000..a5b04f5 --- /dev/null +++ b/src/bin/testget.cc @@ -0,0 +1,157 @@ +///////////////////////////////////////////////////////// +/** + * 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 ull MDL = 1e9 + 7; +constexpr ull PRIME = 998'244'353; +constexpr ull MDL1 = 825; +constexpr ull MDL2 = 87825; + +/* 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)) + +/* fast pairs */ +#define upair ull +#define umake(x, y) (ull(x) << 32 | ull(y)) +#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(p))) +#define i2(p) (int(u2(p))) +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;) + +/* build data structures */ +#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t> 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);) + +/* 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;) + +/* 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();) + +/* 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; +} +///////////////////////////////////////////////////////// + +int main() { + constexpr int n = 3e5; + cout << 1 << ' ' << n << endl; + for (int i = 0; i < n; ++i) { + cout << 1 << ' '; + } +} \ No newline at end of file diff --git a/src/bin/uva-455.cc b/src/bin/uva-455.cc new file mode 100644 index 0000000..f03a162 --- /dev/null +++ b/src/bin/uva-455.cc @@ -0,0 +1,51 @@ +///////////////////////////////////////////////////////// +/** + * Useful Macros + * by subcrip + * (requires C++17) + */ + +#include +using namespace std; + +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; +} + +///////////////////////////////////////////////////////// + +int q = 0; + +void solve() { + if (q) cout << endl; + q = 1; + string s; cin >> s; + 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) { + cout << i << endl; + return; + } + } + cout << n << endl; +} + +int main() { + int t; cin >> t; + while (t--) solve(); +} \ No newline at end of file