diff --git a/include.hh b/include.hh index 0ba61a4..f573afe 100644 --- a/include.hh +++ b/include.hh @@ -2,8 +2,10 @@ using namespace std; #define __NARGS(...) std::tuple_size::value -#define __decompose_s(a, x) auto x = a; -#define __decompose_n(a, ...) auto [__VA_ARGS__] = a; +#define __DECOMPOSE_S(a, x) auto x = a; +#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a; +constexpr void __() {} +#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __() using ll = int64_t; using ull = uint64_t; @@ -12,6 +14,11 @@ using pil = pair; using pli = pair; using pll = pair; +constexpr ull MDL = 1e9 + 7; +constexpr ull PRIME = 998244353; +constexpr ull MDL1 = 825; +constexpr ull MDL2 = 87825; + #define lowbit(x) ((x) & -(x)) #define popcount(x) (__builtin_popcountll(ll(x))) #define parity(x) (__builtin_parityll(ll(x))) @@ -42,22 +49,42 @@ struct igt { } }; -#define untie ios_base::sync_with_stdio(0), cin.tie(NULL) +#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, ...) type __VA_ARGS__; __read(__VA_ARGS__); -#define readvec(type, a, n) vector a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; -#define putvec(a) for (auto&& x : a) cout << x << ' '; cout << endl; -#define debug(x) cerr << #x" = " << x << endl; -#define debugvec(a) cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl; +#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;) -#define pa(a) __typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x); -#define sa(a) __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 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 poptop(q, ...) auto [__VA_ARGS__] = q.top(); q.pop(); -#define popback(q, ...) auto [__VA_ARGS__] = q.back(); q.pop_back(); -#define popfront(q, ...) auto [__VA_ARGS__] = q.front();q.pop_front(); +#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();) -#define adj(ch, n) vector> ch((n) + 1); -#define edge(ch, u, v) ch[u].push_back(v), ch[v].push_back(u); -#define Edge(ch, u, v) ch[u].push_back(v); +#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);) + +vector kmp(string s, string t) { // find all t in s + int n = s.length(), m = t.length(); + vector next; next.push_back(-1); + int j = -1, i = 0; + while (i < m) + if (j == -1 || t[i] == t[j]) { + ++i, ++j; + if (i != m && t[i] == t[j]) next.push_back(next[j]); + else next.push_back(j); + } else j = next[j]; + vector res; + i = 0, j = 0; + while (i < n && j < m) + if (j == -1 || s[i] == t[j]) { + ++i, ++j; + if (j == m) res.push_back(i - j), j = next[j]; + } else j = next[j]; + return res; +}