版本比较
板子
正文
增加 558 行删除 1 行
1234567810 行未改动192021222123242526272829303132333435363738394041424344454647484950515253545556575858 行未改动117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648::::success[区间min]```cpp#define lc p << 1#define rc p << 1 | 1struct sgt{ int tr[N << 2], tag[N << 2]; sgt(){mems(tr, 0x3f);} inline void update(int p, int l, int r, int x, int k){10 行未改动 int mid = l + r >> 1; tr[lc] += tag[p], tr[rc] += tag[p], tag[lc] += tag[p], tag[rc] += tag[p], tag[p] = 0; return min(query(lc, l, mid, L, R), query(rc, mid + 1, r, L, R)); }}};```
::::
::::success[区间修改]```cpp#define lc p << 1#define rc p << 1 | 1struct sgt{ int tr[N << 2], tag[N << 2]; sgt(){mems(tr, 0x3f);} inline void update(int p, int l, int r, int L, int R, int k){ if(L <= l && r <= R){tr[p] = min(tr[p], k);return;} int mid = l + r >> 1; tr[lc] += tag[p], tr[rc] += tag[p], tag[lc] += tag[p], tag[rc] += tag[p], tag[p] = 0; if(L <= mid) update(lc, l, mid, L, R, k); if(R > mid) update(rc, mid + 1, r, L, R, k); tr[p] = min(tr[lc], tr[rc]); } inline int query(int p, int l, int r, int L, int R){ if(L > r || R < l) return inf; if(L <= l && r <= R) return tr[p]; int mid = l + r >> 1; tr[lc] += tag[p], tr[rc] += tag[p], tag[lc] += tag[p], tag[rc] += tag[p], tag[p] = 0; return min(query(lc, l, mid, L, R), query(rc, mid + 1, r, L, R)); }};```
::::
::::success[颜色段均摊]```cppstruct Chtholly{ struct node_t{58 行未改动}```
::::::::success[树状数组单点加区间和]```cppint c[N];inline void add(int x, int k){for(; x <= n; x += lowbit(x)) c[x] += k;}inline int ask(int x){int res = 0;for(; x > 0; x -= lowbit(x)) res += c[x];return res;}inline int ask(int l, int r){return ask(r) - ask(l - 1);}
```
::::
::::success[树状数组区间加区间和]```cppint tr[N], tr1[N];inline void add(int* tree, int x, int k){for(; x <= n; x += lowbit(x)) tree[x] += k;}inline int ask(int* tree, int x){int sum = 0;for(; x > 0; x -= lowbit(x)) sum += tree[x];return sum;}inline void update(int x, int y, int k){add(tr, y + 1, -k);add(tr, x, k);add(tr1, x, k * (x - 1));add(tr1, y + 1, -k * y);}inline int query(int x, int y){return y * ask(tr, y) - ask(tr1, y) - ((x - 1) * ask(tr, x - 1) - ask(tr1, x - 1));}
```::::
::::success[重链剖分]```cppvector<int> G[N];int w[N];int fa[N], dep[N], son[N], sz[N], top[N];int dfn[N], nfd[N], nw[N], cnt;inline void dfs1(int u, int f){ fa[u] = f, dep[u] = dep[f] + 1, sz[u] = 1; for(int v : G[u]){ if(v == f) ctn; dfs1(v, u); sz[u] += sz[v]; if(sz[son[u]] < sz[v]) son[u] = v; }}inline void dfs2(int u, int t){ top[u] = t, dfn[u] = ++cnt, nw[cnt] = w[u], nfd[cnt] = u; if(!son[u]) return; dfs2(son[u], t); for(int v : G[u]){ if(v == fa[u] || v == son[u]) ctn; dfs2(v, v); }}int tr[N], tr1[N];inline void add(int* tree, int x, int k){for(; x <= n; x += lowbit(x)) tree[x] += k;}inline int ask(int* tree, int x){int sum = 0;for(; x > 0; x -= lowbit(x)) sum += tree[x];return sum;}inline void update(int x, int y, int k){add(tr, y + 1, -k);add(tr, x, k);add(tr1, x, k * (x - 1));add(tr1, y + 1, -k * y);}inline int query(int x, int y){return y * ask(tr, y) - ask(tr1, y) - ((x - 1) * ask(tr, x - 1) - ask(tr1, x - 1));}inline void update_path(int u, int v, int k){ while(top[u] != top[v]){ if(dep[top[u]] < dep[top[v]]) swap(u, v); update(dfn[top[u]], dfn[u], k); u = fa[top[u]]; } if(dep[u] < dep[v]) swap(u, v); update(dfn[v], dfn[u], k);}inline int query_path(int u, int v){ int res = 0; while(top[u] != top[v]){ if(dep[top[u]] < dep[top[v]]) swap(u, v); res += query(dfn[top[u]], dfn[u]); u = fa[top[u]]; } if(dep[u] < dep[v]) swap(u, v); res += query(dfn[v], dfn[u]); return res;}```
::::::::success[Barrett]```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;((x >= p) && (x -= p));return x;}};```
::::
::::success[区间ckmin区间求和区间求max]```cpp#define lc p << 1#define rc p << 1 | 1int a[N];struct node{ int sum, len, mx[2], tag; inline void T(int t){ if(mx[0] <= t) return; sum += (t - mx[0]) * len; mx[0] = tag = t; }}tr[N << 2];inline void pushup(node &A, node B, node C){ A.sum = B.sum + C.sum; if(B.mx[0] == C.mx[0]){A.mx[0] = B.mx[0];A.mx[1] = max(B.mx[1], C.mx[1]);A.len = B.len + C.len;} else if(B.mx[0] > C.mx[0]){A.mx[0] = B.mx[0];A.mx[1] = max(B.mx[1], C.mx[0]);A.len = B.len;} else{A.mx[0] = C.mx[0];A.mx[1] = max(B.mx[0], C.mx[1]);A.len = C.len;}}inline void pushup(int p){pushup(tr[p], tr[lc], tr[rc]);}inline void build(int p, int l, int r){ tr[p].len = 1;tr[p].sum = a[l];tr[p].tag = -1;tr[p].mx[0] = a[l];tr[p].mx[1] = -1; if(l == r){return;} int mid = l + r >> 1; build(lc, l, mid); build(rc, mid + 1, r); pushup(p);}inline void pushdown(int p){if(~tr[p].tag){tr[lc].T(tr[p].tag);tr[rc].T(tr[p].tag);tr[p].tag = -1;}}inline void update(int p, int l, int r, int L, int R, int k){ if(tr[p].mx[0] <= k) return; if(L <= l && r <= R && tr[p].mx[1] < k){tr[p].T(k);return;} int mid = l + r >> 1; pushdown(p); if(L <= mid) update(lc, l, mid, L, R, k); if(R > mid) update(rc, mid + 1, r, L, R, k); pushup(p);}inline node query(int p, int l, int r, int L, int R){ if(L <= l && r <= R) return tr[p]; int mid = l + r >> 1; pushdown(p); if(R <= mid) return query(lc, l, mid, L, R); if(L > mid) return query(rc, mid + 1, r, L, R); node res;pushup(res, query(lc, l, mid, L, R), query(rc, mid + 1, r, L, R)); return res;}```
::::
::::success[区间ckmin区间ckmax区间加区间求和区间求max区间求min]```cpp#define lc p << 1#define rc p << 1 | 1int a[N];struct node{ int sum, cmx, cmn, mx[2], mn[2], tmx, tmn, tad, len; inline void T(int t, int op){ if(op == 0){ if(mx[0] <= t) return; sum += (t - mx[0]) * cmx; if(mn[1] == mx[0]) mn[1] = t; if(mn[0] == mx[0]) mn[0] = t; if(tmx > t) tmx = t; mx[0] = tmn = t; } if(op == 1){ if(mn[0] > t) return; sum += (t - mn[0]) * cmn; if(mx[1] == mn[0]) mx[1] = t; if(mx[0] == mn[0]) mx[0] = t; if(tmn < t) tmn = t; mn[0] = tmx = t; } if(op == 2){ sum += len * t; if(tmx != -infll) tmx += t; if(tmn != infll) tmn += t; mx[0] += t, mn[0] += t, mx[1] += t, mn[1] += t; tad += t; } }}tr[N << 2];inline void pushup(node &A, node B, node C){ A.sum = B.sum + C.sum; A.len = B.len + C.len; if(B.mx[0] == C.mx[0]){A.mx[0] = B.mx[0];A.mx[1] = max(B.mx[1], C.mx[1]);A.cmx = B.cmx + C.cmx;} else if(B.mx[0] > C.mx[0]){A.mx[0] = B.mx[0];A.mx[1] = max(B.mx[1], C.mx[0]);A.cmx = B.cmx;} else{A.mx[0] = C.mx[0];A.mx[1] = max(B.mx[0], C.mx[1]);A.cmx = C.cmx;} if(B.mn[0] == C.mn[0]){A.mn[0] = B.mn[0];A.mn[1] = min(B.mn[1], C.mn[1]);A.cmn = B.cmn + C.cmn;} else if(B.mn[0] < C.mn[0]){A.mn[0] = B.mn[0];A.mn[1] = min(B.mn[1], C.mn[0]);A.cmn = B.cmn;} else{A.mn[0] = C.mn[0];A.mn[1] = min(B.mn[0], C.mn[1]);A.cmn = C.cmn;}}inline void pushup(int p){pushup(tr[p], tr[lc], tr[rc]);}inline void build(int p, int l, int r){ tr[p].sum = a[l];tr[p].cmx = tr[p].cmn = 1;tr[p].tmx = -infll;tr[p].tmn = infll;tr[p].mx[0] = a[l];tr[p].mx[1] = -infll;tr[p].mn[0] = a[l];tr[p].mn[1] = infll;tr[p].tad = 0;tr[p].len = 1; if(l == r){return;} int mid = l + r >> 1; build(lc, l, mid); build(rc, mid + 1, r); pushup(p);}inline void pushdown(int p){ if(tr[p].tad){tr[lc].T(tr[p].tad, 2);tr[rc].T(tr[p].tad, 2);tr[p].tad = 0;} if(tr[p].tmx != -infll){tr[lc].T(tr[p].tmx, 1);tr[rc].T(tr[p].tmx, 1);tr[p].tmx = -infll;} if(tr[p].tmn != infll){tr[lc].T(tr[p].tmn, 0);tr[rc].T(tr[p].tmn, 0);tr[p].tmn = infll;}}inline void update(int p, int l, int r, int L, int R, int k, int op){ if(op == 0 && tr[p].mx[0] <= k) return; if(op == 1 && tr[p].mn[0] >= k) return; if(L <= l && r <= R && (op == 0 ? tr[p].mx[1] < k : op == 1 ? tr[p].mn[1] > k : 1)){tr[p].T(k, op);return;} int mid = l + r >> 1; pushdown(p); if(L <= mid) update(lc, l, mid, L, R, k, op); if(R > mid) update(rc, mid + 1, r, L, R, k, op); pushup(p);}inline node query(int p, int l, int r, int L, int R){ if(L <= l && r <= R) return tr[p]; int mid = l + r >> 1; pushdown(p); if(R <= mid) return query(lc, l, mid, L, R); if(L > mid) return query(rc, mid + 1, r, L, R); node res;pushup(res, query(lc, l, mid, L, R), query(rc, mid + 1, r, L, R)); return res;}```
::::
::::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){ 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; for(int i = 0; i < mid; i++, w = w * wn){ Z x = a[j + i], y = w * a[j + mid + i]; a[j + i] = x + y, a[j + mid + i] = x - y; } } } if(type == -1){ 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 = _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;}```
::::
::::success[插入删除前m大的和]
```cppstruct SET{ 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.begin() < x){ sum -= *s1.begin(); sum += x; s2.insert(*s1.begin()); s1.erase(s1.begin()); s1.insert(x); } else{ s2.insert(x); } } inline void erase(int x){ if(s1.find(x) != s1.end()){ sum -= x; if(!s2.empty()){ sum += *s2.rbegin(); s1.insert(*s2.rbegin()); s2.erase((--s2.end())); } s1.erase(s1.find(x)); } else{ s2.erase(s2.find(x)); } }};```
::::
::::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;}```
::::