版本比较
板子
正文
增加 251 行删除 14 行
195 行未改动全部展开196197198199200200201202203204128 行未改动全部展开3333343353363373383393403413423413433443453463473483493 行未改动3533543553563533573583593603613583593603613623633643653663673623633643653663673683693703713723733743753763773786 行未改动38538638738838238939039139239321 行未改动415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648195 行未改动全部展开```cppstruct Barrett{ ll m, p; void init(int pp){m = ((__int128)1 << 64) / pp;p = pp;} ll operator()(ll x){x = x - ((__int128(x) * m) >> 64) * p;if(x >= p) x -= p;return x;} ll operator()(ll x){x = x - ((__int128(x) * m) >> 64) * p;((x >= p) && (x -= p));return x;}};```
::::128 行未改动全部展开::::
::::success[多项式乘法(NTT)]```cpp
const int N = 3e6 + 7, mod = 998244353, G = 3, Gi = 332748118;int n, m;int rev[N];inline int getn(int n){int _n = 1;while(_n < n) _n <<= 1;return _n;}inline void NTT(int lim, vz& a, int type){ up(0, lim - 1, i) if(i < rev[i]) swap(a[i], a[rev[i]]); if(lim == 1) return; a.resize(lim); up(0, lim - 1, i){rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (ctz(lim) - 1));if(i < rev[i]) swap(a[i], a[rev[i]]);} for(int mid = 1; mid < lim; mid <<= 1){ Z wn = ksm(type == 1 ? G : Gi, (mod - 1) / (mid << 1), mod); for(int j = 0; j < lim; j += (mid << 1)){ Z w = 1;3 行未改动 } } } if(type == -1){ int inv = ksm(lim, mod - 2, mod); const int inv = ksm(lim, mod - 2, mod); up(0, lim - 1, i) a[i] *= inv; }}inline vz convolution(vz a, vz b){ int n = a.size(), m = b.size(); int lim = 1 << (__lg(n + m) + 1); a.resize(lim); b.resize(lim); up(0, lim - 1, i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (__lg(n + m))); NTT(lim, a, 1); NTT(lim, b, 1); up(0, lim - 1, i) a[i] *= b[i]; NTT(lim, a, -1); a.resize(n + m - 1); int n = _sz(a), m = _sz(b); if(min(n, m) <= 32){ vz c(n + m - 1); up(0, n - 1, i) up(0, m - 1, j) c[i + j] += a[i] * b[j]; return c; } n = n + m - 1; int _n = getn(n); NTT(_n, a, 1); NTT(_n, b, 1); up(0, _n - 1, i) a[i] *= b[i]; NTT(_n, a, -1); a.resize(n); return a;}```
6 行未改动 multiset<int> s1, s2; int sum = 0; SET(){sum = 0;s1.clear();s2.clear();} inline void insert(int x){ if(s1.size() <= m){sum += x;s1.insert(x);return;} if(s1.size() < m){sum += x;s1.insert(x);return;} if(*s1.begin() < x){ sum -= *s1.begin(); sum += x; s2.insert(*s1.begin());21 行未改动};```
::::
::::success[OR,AND,XOR]```cppinline void Or(vz& f, Z x = 1) { int n = f.size(); for(int o = 2, k = 1; o <= n; o <<= 1, k <<= 1) for(int i = 0; i < n; i += o) up(0, k - 1, j) f[i + j + k] += f[i + j] * x;}inline vz OR(vz a, vz b){ int n = a.size();vz c(n); Or(a);Or(b);up(0, n - 1, i) c[i] = a[i] * b[i];Or(c, mod - 1); return c;}inline void And(vz& f, Z x = 1) { int n = f.size(); for(int o = 2, k = 1; o <= n; o <<= 1, k <<= 1) for(int i = 0; i < n; i += o) up(0, k - 1, j) f[i + j] += f[i + j + k] * x;}inline vz AND(vz a, vz b){ int n = a.size();vz c(n); And(a);And(b);up(0, n - 1, i) c[i] = a[i] * b[i];And(c, mod - 1); return c;}inline void Xor(vz& f, Z x = 1) { int n = f.size(); for(int o = 2, k = 1; o <= n; o <<= 1, k <<= 1) for(int i = 0; i < n; i += o) up(0, k - 1, j) f[i + j] += f[i + j + k], f[i + j + k] = f[i + j] - f[i + j + k] - f[i + j + k], f[i + j] *= x, f[i + j + k] *= x;}inline vz XOR(vz a, vz b){ int n = a.size();vz c(n); Xor(a);Xor(b);up(0, n - 1, i) c[i] = a[i] * b[i];Xor(c, (mod + 1) / 2); return c;}::::
::::success[光速幂]```cppstruct{ int pw[2][65536], p; void init(int x, int p){ this->p = p; up(0, 1, b){ int y = exchange(x, 1); up(0, 65535, i) pw[b][i] = exchange(x, 1ll * x * y % p); } } int operator()(int x) const{return 1ll * pw[0][x & 65535] * pw[1][x >> 16] % p;}}gpw;```
::::::::success[O(1) set]```cpp
template <int N>class tiny_set {private: static_assert(N > 0, "N must be positive"); static_assert(N <= 262144, "N must be <= 262144"); ull a; ull b[(N-1>>12)+1]; ull c[(N-1>>6)+1]; int cc;public: tiny_set() { clear(); } void insert(int x) { if (count(x)) return; ++cc; c[x >> 6] |= 1ull << (x & 63); b[x >> 12] |= 1ull << ((x >> 6) & 63); a |= 1ull << (x >> 12); } void erase(int x) { if (!count(x)) return; --cc; if (!(c[x >> 6] &= ~(1ull << (x & 63)))) if (!(b[x >> 12] &= ~(1ull << ((x >> 6) & 63)))) a &= ~(1ull << (x >> 12)); } bool count(int x) const { return (c[x >> 6] >> (x & 63)) & 1; } int getmin() const { int A = __builtin_ctzll(a); int B = __builtin_ctzll(b[A]); return (A << 12) | (B << 6) | __builtin_ctzll(c[(A << 6) | B]); } int getmax() const { if (cc == 0) return -1; int A = 63 - __builtin_clzll(a); int B = 63 - __builtin_clzll(b[A]); return (A << 12) | (B << 6) | (63 - __builtin_clzll(c[(A << 6) | B])); } void clear() { a = 0; cc = 0; memset(b, 0, sizeof b); memset(c, 0, sizeof c); } int size() const { return cc; } bool empty() const { return cc == 0; }};```
::::
::::success[双哈希]```cppstruct Hash{ string s; vi h1, h2; void init(string _s){ s = _s;int n = _sz(s); h1.resize(n);h2.resize(n); h1[0] = s[0];h2[0] = s[0]; up(1, n - 1, i) h1[i] = h1[i - 1] * 233 % mod, h2[i] = h2[i - 1] * 23151 % mod; } pii get(int l, int r){ if(!l) return mkp(h1[r], h2[r]); return mkp((h1[r] + mod - h1[l - 1] * pw1[r - l + 1] % mod) % mod, (h2[r] + mod - h2[l - 1] * pw2[r - l + 1] % mod) % mod); }}h[100007];signed main() { pw1[0] = pw2[0] = 1; up(0, N - 1, i) pw1[i] = pw1[i - 1] * 233 % mod, pw2[i] = pw2[i - 1] * 23151 % mod;
```
::::
::::success[O(n)-O(1) rmq]```cppstruct RMQ{ int n, m; int a[N], pre[N], suf[N]; int st[16][N >> 6], sta[65]; ull ps[N]; int oo = -inf; inline int cmp(int x, int y){return x > y;} inline int cp(int x, int y){return x > y ? x : y;} inline void ck(int &x, int y){x = cp(x, y);} inline void init(){ int top = 0; a[0] = pre[0] = suf[n + 1] = st[0][0] = oo; up(1, n, i){ if(!(i & 63)) top = 0, pre[i] = a[i], st[0][i >> 6] = a[i]; else ps[i] = ps[i - 1], pre[i] = cp(pre[i - 1], a[i]), ck(st[0][i >> 6], a[i]); while(top && cmp(a[i], a[sta[top] + (i ^ (i & 63))])) ps[i] ^= 1ull << sta[top--]; sta[++top] = i & 63, ps[i] |= 1ull << (i & 63); } dn(n, 1, i){if((i & 63) == 63) suf[i] = a[i];else suf[i] = cp(suf[i + 1], a[i]);} m = n >> 6; up(1, __lg(m + 1), j) up(0, m - (1 << j) + 1, i) st[j][i] = cp(st[j - 1][i], st[j - 1][i + (1 << j - 1)]); } inline int query(int l, int r){ int L = l >> 6, R = r >> 6; if(L == R) return a[l + ctzll(ps[r] >> (l & 63))]; int res = cp(suf[l], pre[r]); if(L + 1 <= R - 1){ int k = __lg(R - L - 1); res = cp(res, cp(st[k][L + 1], st[k][R - (1 << k)])); } return res; }}rmq;```
::::::::success[fhq有交合并]```cpp#define lc tr[p].ls#define rc tr[p].rsstruct node{int ls, rs, val, x, tag, sz;ull pr;}tr[N];int rt[N];inline void newnode(int p, ll v){lc = rc = 0;tr[p].val = v;tr[p].sz = 1;tr[p].tag = 0;}inline void ad(int p, int x){ckmax(tr[p].tag, x);ckmax(tr[p].x, x);}inline void pd(int p){if(p && tr[p].tag) ad(lc, tr[p].tag), ad(rc, tr[p].tag), tr[p].tag = 0;}inline void pu(int p){tr[p].sz = tr[lc].sz + tr[rc].sz + 1;}inline void split(int p, int k, int &l, int &r){ if(!p){l = r = 0;return;} pd(p); if(p <= k) l = p, split(rc, k, rc, r), pu(p); else r = p, split(lc, k, l, lc), pu(p);}inline int merge(int p, int q){ if(!p || !q) return p | q; if(tr[p].pr < tr[q].pr) swap(p, q); pd(p);pd(q);int l, r;split(q, p, l, r); lc = merge(lc, l);rc = merge(rc, r);pu(p); return p;}inline int ask(int p, int k){ if(k == tr[p].val) return tr[p].x; assert(p); pd(p); if(k < tr[p].val) return ask(lc, k); return ask(rc, k);}```
::::::::success[矩阵快速幂]```cppstruct Mat{ int a[105][105]; Mat(int p = 0){ up(0, n, i) up(0, n, j) a[i][j] = (i == j) * p; } int* operator [](int i){return a[i];} Mat friend operator*(Mat x, Mat y){ Mat z; up(0, n, k) up(0, n, i) up(0, n, j) z[i][j] = (z[i][j] + x[i][k] * y[k][j]) % mod; return z; }}A;Mat ksm(Mat a, int m){ Mat ans(1); while(m){ if(m & 1) ans = ans * a; a = a * a; m >>= 1; } return ans;}```
::::