FAc 算法模版
FlowerAccepted · · 算法·理论
缺省源
#include "bits/stdc++.h"
using namespace std;
#define inlfc __attribute__((always_inline)) inline
#define isz(x) ((int)x.size())
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
return 0;
}
栈
注:Python 的栈执行 pop() 会同时返回栈顶,我们沿用这一方便的特性。
template<typename Tp>
class Stack {
public:
vector<Tp> a;
size_t sz = 0;
inlfc Stack(int n) : a(n + 1) {};
inlfc Stack() : a(1e5 + 5) {};
inlfc void push(const Tp &x) { a[++ sz] = x; }
inlfc Tp& top() { return a[sz]; }
inlfc Tp pop() { return a[sz ? sz -- : sz]; }
inlfc size_t size() { return sz; }
inlfc void clear() { sz = 0; }
inlfc Tp& operator [] (size_t i) { return a[i]; }
};
单调队列专用队列
GPT 胡一个。
template<typename T, int N>
struct FastDeque {
T q[N];
int l, r;
inlfc void clear() { l = 1, r = 0; }
inlfc bool empty() { return l > r; }
inlfc int size() { return r - l + 1; }
inlfc T front() { return q[l]; }
inlfc T back() { return q[r]; }
inlfc void pop_front() { ++l; }
inlfc void pop_back() { --r; }
inlfc void push_back(T x) { q[++r] = x; }
};
双端队列用 std::list 就得了,不需要肥猪 deque。
并查集
struct Dsu {
int f[MAXN], n;
ll s[MAXN];
inlfc void init(int _n) {
n = _n;
for (int i = 1; i <= n; i ++) {
f[i] = i;
s[i] = 1;
}
}
inlfc int find(int x) {
while (x != f[x]) {
f[x] = f[f[x]];
x = f[x];
}
return x;
}
inlfc void merge(int x, int y) {
x = find(x);
y = find(y);
if (s[x] < s[y]) swap(x, y);
f[y] = x;
s[x] += s[y];
}
inlfc bool check(int x, int y) {
return find(x) == find(y);
}
};
带取模的整数
ull P;
template <typename Tp>
class mulint {
public:
Tp v;
inlfc mulint() : v(0) {}
inlfc mulint(Tp x) : v(x % P) {}
inlfc mulint& operator = (Tp x) { v = x % P; return *this; }
inlfc mulint& operator = (mulint x) { v = x.v; return *this; }
inlfc mulint& operator += (mulint x) { (v += x.v) %= P; return *this; }
inlfc mulint& operator *= (mulint x) { (v *= x.v) %= P; return *this; }
inlfc mulint operator + (mulint x) const { return mulint((v + x.v) % P); }
inlfc mulint operator * (mulint x) const { return mulint((v * x.v) % P); }
};
线段树(单点、区间加、乘、查询)
当前节点永远可信 子节点只有在 pushdown 后才可信
template <typename Tp>
class SgT {
public:
static const size_t MAXN = 5e5 + 5;
struct {
size_t l, r;
Tp s, lazyadd, lazymul;
} a[MAXN << 2];
#define lson (i << 1)
#define rson (i << 1 | 1)
// 当前节点永远可信
// 子节点只有在 pushdown 后才可信
inlfc void pushup(size_t i) {
a[i].s = a[lson].s + a[rson].s;
}
inlfc void pushdown(size_t i) {
Tp &mul = a[i].lazymul, &add = a[i].lazyadd;
a[lson].s = a[lson].s * mul + add * (a[lson].r - a[lson].l + 1);
a[rson].s = a[rson].s * mul + add * (a[rson].r - a[rson].l + 1);
a[lson].lazymul *= mul;
a[rson].lazymul *= mul;
a[lson].lazyadd = a[lson].lazyadd * mul + add;
a[rson].lazyadd = a[rson].lazyadd * mul + add;
add = 0;
mul = 1;
}
void build(size_t i, size_t l, size_t r, const vector<Tp> &A) {
a[i] = {l, r, 0, 0, 1};
if (l == r) { a[i].s = A[l]; return; }
const size_t mid = (l + r) >> 1;
build(lson, l , mid, A);
build(rson, mid + 1, r , A);
pushup(i);
}
void access_modify_add(size_t i, const size_t &pos, const Tp &x) {
pushdown(i);
if (a[i].l == a[i].r) { a[i].s += x; return; }
if (pos <= a[lson].r)
access_modify_add(lson, pos, x);
else access_modify_add(rson, pos, x);
pushup(i);
}
void access_modify_mul(size_t i, const size_t &pos, const Tp &x) {
pushdown(i);
if (a[i].l == a[i].r) { a[i].s *= x; return; }
if (pos <= a[lson].r)
access_modify_mul(lson, pos, x);
else access_modify_mul(rson, pos, x);
pushup(i);
}
void range_modify_add(size_t i, const size_t &l, const size_t &r, const Tp &x) {
if (l <= a[i].l && a[i].r <= r) {
a[i].s += x * (a[i].r - a[i].l + 1);
a[i].lazyadd += x;
return;
}
pushdown(i);
if (a[lson].r >= l) range_modify_add(lson, l, r, x);
if (a[rson].l <= r) range_modify_add(rson, l, r, x);
pushup(i);
}
void range_modify_mul(size_t i, const size_t &l, const size_t &r, const Tp &x) {
if (l <= a[i].l && a[i].r <= r) {
a[i].s *= x;
a[i].lazymul *= x;
a[i].lazyadd *= x;
return;
}
pushdown(i);
if (a[lson].r >= l) range_modify_mul(lson, l, r, x);
if (a[rson].l <= r) range_modify_mul(rson, l, r, x);
pushup(i);
}
Tp access_query(size_t i, const size_t &pos) {
pushdown(i);
if (a[i].l == a[i].r) { return a[i].s; }
const size_t mid = (a[i].l + a[i].r) >> 1;
if (pos <= mid)
return access_query(lson, pos);
else return access_query(rson, pos);
}
Tp range_query(size_t i, const size_t &l, const size_t &r) {
if (l <= a[i].l && a[i].r <= r) return a[i].s;
if (a[i].r < l || r < a[i].l) return 0;
pushdown(i);
Tp res = 0;
if (a[lson].r >= l) res += range_query(lson, l, r);
if (a[rson].l <= r) res += range_query(rson, l, r);
return res;
}
#undef lson
#undef rson
};