1
0
Fork 0
cp-templates/include.hh

202 lines
6.3 KiB
C++
Raw Normal View History

2024-01-30 19:04:04 +08:00
#pragma GCC optimize("Ofast")
2024-01-16 16:05:34 +08:00
/////////////////////////////////////////////////////////
/**
* Useful Macros
* by subcrip
* (requires C++17)
*/
2024-01-15 14:51:18 +08:00
#include<bits/stdc++.h>
2024-01-30 19:04:04 +08:00
#include<bits/extc++.h>
2024-01-15 14:51:18 +08:00
using namespace std;
2024-01-30 19:04:04 +08:00
using namespace __gnu_cxx;
using namespace __gnu_pbds;
2024-01-15 14:51:18 +08:00
2024-01-16 16:05:34 +08:00
/* macro helpers */
2024-01-15 16:40:44 +08:00
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
2024-01-16 14:49:34 +08:00
#define __DECOMPOSE_S(a, x) auto x = a;
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
constexpr void __() {}
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
2024-01-16 15:52:21 +08:00
#define __as_typeof(container) decltype(container)::value_type
2024-01-15 16:40:44 +08:00
2024-01-16 16:05:34 +08:00
/* type aliases */
2024-01-15 14:51:18 +08:00
using ll = int64_t;
using ull = uint64_t;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
2024-01-16 16:05:34 +08:00
/* constants */
2024-01-16 16:53:00 +08:00
constexpr int INF = 0x3f3f3f3f;
2024-01-16 14:49:34 +08:00
constexpr ull MDL = 1e9 + 7;
2024-01-16 16:53:00 +08:00
constexpr ull PRIME = 998'244'353;
2024-02-11 12:36:42 +08:00
constexpr ll MDL1 = 825;
constexpr ll MDL2 = PRIME;
2024-01-16 14:49:34 +08:00
2024-01-16 16:05:34 +08:00
/* bit-wise operations */
2024-01-15 14:51:18 +08:00
#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)))
2024-01-16 19:01:25 +08:00
/* arithmetic operations */
2024-02-11 12:34:13 +08:00
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
2024-01-16 19:01:25 +08:00
2024-01-16 16:05:34 +08:00
/* fast pairs */
2024-01-15 14:51:18 +08:00
#define upair ull
2024-01-30 19:04:04 +08:00
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
2024-01-15 14:51:18 +08:00
#define u1(p) ((p) >> 32)
#define u2(p) ((p) & ((1ULL << 32) - 1))
#define ult std::less<upair>
#define ugt std::greater<upair>
#define ipair ull
#define imake(x, y) (umake(x, y))
2024-01-30 19:04:04 +08:00
#define i1(p) (int(u1(ll(p))))
#define i2(p) (ll(u2(p) << 32) >> 32)
2024-01-15 14:51:18 +08:00
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);
}
};
2024-01-15 14:55:45 +08:00
2024-01-16 16:05:34 +08:00
/* conditions */
#define loop while (1)
2024-01-16 16:53:00 +08:00
#define if_or(var, val) if (!(var == val)) var = val; else
2024-01-16 16:05:34 +08:00
#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;)
2024-01-30 19:04:04 +08:00
/* 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);
}
};
2024-02-05 00:04:12 +08:00
struct pair_hash {
template <typename T, typename U>
size_t operator()(const pair<T, U>& a) const {
auto hash1 = safe_hash()(a.first);
auto hash2 = safe_hash()(a.second);
if (hash1 != hash2) {
return hash1 ^ hash2;
}
return hash1;
}
};
2024-01-16 16:05:34 +08:00
/* build data structures */
2024-01-30 19:04:04 +08:00
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
2024-01-16 15:52:21 +08:00
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
2024-01-16 16:05:34 +08:00
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
/* io */
2024-01-16 14:49:34 +08:00
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
2024-01-15 16:40:44 +08:00
template<typename T> void __read(T& x) { cin >> x; }
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
2024-01-16 14:49:34 +08:00
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
2024-01-19 17:21:51 +08:00
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
2024-01-16 14:49:34 +08:00
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
2024-01-19 17:21:51 +08:00
template<typename T> ostream& operator<<(ostream& out, vector<T> vec) {
for (auto&& i : vec) out << i << ' ';
return out;
}
2024-01-16 14:49:34 +08:00
2024-01-16 16:05:34 +08:00
/* pops */
2024-01-16 14:49:34 +08:00
#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();)
2024-02-10 22:57:51 +08:00
/* math */
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);
}
2024-01-18 12:36:04 +08:00
2024-02-10 22:57:51 +08:00
/* string algorithms */
2024-01-17 21:09:19 +08:00
vector<int> calc_next(string t) { // pi function of t
2024-01-18 12:36:04 +08:00
int n = (int)t.length();
vector<int> pi(n);
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
if (t[i] == t[j]) j++;
pi[i] = j;
}
return pi;
2024-01-17 21:09:19 +08:00
}
vector<int> calc_z(string t) { // z function of t
int m = t.length();
vector<int> z;
z.push_back(m);
pair<int, int> prev = {1, -1};
for (int i = 1; i < m; ++i) {
if (z[i - prev.first] + i <= prev.second) {
z.push_back(z[i - prev.first]);
} else {
int j = max(i, prev.second + 1);
while (j < m && t[j] == t[j - i]) ++j;
z.push_back(j - i);
prev = {i, j - 1};
}
}
return z;
}
vector<int> kmp(string s, string t) { // find all t in s
2024-01-18 12:36:04 +08:00
string cur = t + '#' + s;
int sz1 = s.size(), sz2 = t.size();
vector<int> v;
vector<int> lps = calc_next(cur);
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
}
return v;
2024-01-16 14:49:34 +08:00
}
2024-01-18 14:34:46 +08:00
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;
}
2024-01-16 16:05:34 +08:00
/////////////////////////////////////////////////////////