上次更新时间:2026/2/25 记得点右上角的更新按钮更新 awa。
本帖会不断更新&分类
SegTree
namespace SegTree {
#define inf 1e18
const int maxn = /*your size*/;
struct Tree {
int tr[maxn * 4], lz[maxn * 4], a[maxn];
void push_up(int x) {
tr[x] = tr[x * 2] + tr[x * 2 + 1];
}
void push_down(int x, int l, int r) {
if (!lz[x]) return;
int mid = (l + r) >> 1;
tr[x * 2] += lz[x] * (mid - l + 1);
lz[x * 2] += lz[x];
tr[x * 2 + 1] += lz[x] * (r - mid);
lz[x * 2 + 1] += lz[x];
lz[x] = 0;
}
void build(int x, int l, int r) {
lz[x] = 0; // 初始化懒惰标记
if (l == r) {
tr[x] = a[l];
return;
}
int mid = (l + r) >> 1;
build(x * 2, l, mid);
build(x * 2 + 1, mid + 1, r);
push_up(x);
}
int query(int x, int l, int r, int ql, int qr) {
if (l > qr || ql > r) return 0; // 无交集返回0
if (l >= ql && r <= qr) return tr[x];
int mid = (l + r) >> 1;
push_down(x, l, r);
return query(x * 2, l, mid, ql, qr) +
query(x * 2 + 1, mid + 1, r, ql, qr);
}
void modify(int x, int l, int r, int ql, int qr, int add) {
if (l >= ql && r <= qr) {
tr[x] += (r - l + 1) * add;
lz[x] += add;
return;
}
if (l > qr || ql > r) return;
int mid = (l + r) >> 1;
push_down(x, l, r);
modify(x * 2, l, mid, ql, qr, add);
modify(x * 2 + 1, mid + 1, r, ql, qr, add);
push_up(x);
}
};
}
using namespace SegTree;BitTree
namespace BitTree {
const int maxn = /*your size*/;
int lowbit(int x) {
return x & -x;
}
struct tree {
int c[maxn];
void add(int idx, int x) {
for (; idx <= maxn; idx += lowbit(idx)) c[idx] += x;
}
int query(int idx) {
int res = 0;
for (; idx; idx -= lowbit(idx)) res += c[idx];
return res;
}
};
}
using namespace BitTree;uBlock Origin
Block
namespace Block {
const int maxn = /*your size*/;
struct block {
int st[maxn_], ed[maxn_], sum[maxn_], add[maxn_];
int pos[maxn], a[maxn];
int n;
void init() {
int block = sqrt(n), t = n / block;
if (n % block) t++;
for (int i = 1; i <= t; i++) {
st[i] = (i - 1) * block + 1;
ed[i] = i * block;
}
for (int i = 1; i <= n; i++) pos[i] = (i - 1) / block + 1;
for (int i = 1; i <= t; i++) {
for (int j = st[i]; j <= ed[i]; j++) sum[i] += a[j];
}
}
void modify(int l, int r, int k) {
int p = pos[l], q = pos[r];
if (p == q) {
for (int i = l; i <= r; i++) a[i] += k;
sum[p] += k * (r - l + 1);
} else {
for (int i = p + 1; i <= q - 1; i++) add[i] += k;
for (int i = l; i <= ed[p]; i++) a[i] += k;
for (int i = st[q]; i <= r; i++) a[i] += k;
sum[p] += k * (ed[p] - l + 1), sum[q] += k * (r - st[q] + 1);
}
}
int query(int l, int r) {
int p = pos[l], q = pos[r], res = 0;
if (p == q) {
for (int i = l; i <= r; i++) res += a[i];
res += add[p] * (r - l + 1);
} else {
for (int i = p + 1; i <= q - 1; i++) res += sum[i] +
add[i] * (ed[i] - st[i] + 1);
for (int i = l; i <= ed[p]; i++) res += a[i];
for (int i = st[q]; i <= r; i++) res += a[i];
res += add[p] * (ed[p] - l + 1) + add[q] * (r - st[q] + 1);
}
return res;
}
};
}
using namespace Block;LCA
namespace LCA {
const int maxn = /*your size*/;
int dep[maxn], fa[maxn][20];
void dfs(int u) {
if (u == 0) {
dep[u] = 0;
return;
}
if (dep[u]) return;
int f = fa[u][0];
if (!dep[f])
dfs(f);
dep[u] = dep[f] + 1;
}
void init() {
for (int j = 1; j < 20; j++) {
for (int i = 1; i <= n; i++) {
fa[i][j] = fa[fa[i][j - 1]][j - 1];
}
}
for (int i = 1; i <= n; i++) if (!dep[i]) dfs(i);
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int j = 19; j >= 0; j--) {
if (dep[fa[x][j]] >= dep[y]) {
x = fa[x][j];
}
}
if (x == y) return x;
for (int j = 19; j >= 0; j--) {
if (fa[x][j] != fa[y][j]) {
x = fa[x][j];
y = fa[y][j];
}
}
return fa[x][0];
}
}
using namespace LCA;矩阵快速幂
namespace Matrix {
const int maxn = /*your size*/, mod = 1e9 + 7;
struct matrix {
int a[maxn][maxn];
void clear() {
memset(a, 0, sizeof(a));
}
matrix operator *(const matrix &tmp) const {
matrix ret;
ret.clear();
for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= 1; j++) {
for (int k = 0; k <= 1; k++) {
ret.a[i][j] = (ret.a[i][j] + a[i][k] * tmp.a[k][j] % mod) % mod;
}
}
}
return ret;
}
void operator *=(const matrix &tmp) {
*this = *this * tmp;
}
};
matrix fpow(matrix x, int p) {
matrix ret;
ret.clear();
for (int i = 0; i <= 1; i++) ret.a[i][i] = 1;
while (p) {
if (p & 1) ret *= x;
p >>= 1;
x *= x;
}
return ret;
}
}
using namesapce Matrix;FastIO
namespace fast_rw {
inline char nc() {
static char buf[1000010], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000010, stdin), p1 == p2) ? EOF : *p1++;
}
inline void read(char *s) {
char c = nc();
while (c != ' ' && c != '\n' && c != '\r') *(++s) = c, c = nc();
*(s + 1) = 0;
}
inline void read(char &c) {
do c = nc();
while (c == ' ' || c == '\n' || c == '\r');
}
template<typename T> inline void read(T &x, int f = 1, char c = nc()) {
x = 0;
while (c > '9' || c < '0') f = (c == '-') ? -1 : 1, c = nc();
while (c <= '9' && c >= '0') x = x * 10 + (c ^ 48), c = nc();
x *= f;
}
template<typename T> inline void write(T x) {
short _wr[40];
if (x < 0) putchar('-'), x = -x;
int _top = 0;
do _wr[++_top] = x % 10, x /= 10;
while (x);
while (_top) putchar(48 | _wr[_top--]);
}
inline void write(char c) {
putchar(c);
}
inline void write(char *s) {
int pl = 1;
while (s[pl]) putchar(s[pl++]);
}
inline void write(const char *s) {
int pl = 0;
while (s[pl]) putchar(s[pl++]);
}
template<typename T, typename ...Args> inline void write(T x, Args... Rest) {
write(x), write(Rest...);
}
}
using namespace fast_rw;高精度
class bigint {
#define VC vector<char>
#define pb push_back
#define bt bigint
#define f(i, a, b) for(int i=(a);i<=(b);++i)
#define rf(i, a, b) for(int i=(a);i>=(b);--i)
public:
int symbol = 1;
std::VC _a;
int size() const {
return _a.size();
}
char &operator[](int i) {
return _a[i];
}
static bt add(bt A, bt B) {
if (A.size() < B.size())return add(B, A);
std::VC C;
int t = 0;
f(i, 0, A.size() - 1) {
t += A[i];
if (i < B.size()) t += B[i];
C.pb(t % 10);
t /= 10;
}
if (t) C.pb(t);
return {C};
}
void resize(int size) {
_a.reserve(size);
}
static bigint plu(bigint A, bigint B) {
if (A.symbol == -1 && B.symbol == -1) {
auto t = add(A, B);
t.symbol = -1;
return t;
}
if (A.symbol == -1 && B.symbol == 1) {
return sub(B, A);
}
if (A.symbol == 1 && B.symbol == -1) {
return sub(A, B);
}
return add(A, B);
}
template<class T, class Y>friend bt operator+(T A, Y B) {
return plu(A, B);
}
static bt sub(bt A, bt B) {
A.symbol = 1, B.symbol = 1;
std::VC C;
if (A < B) {
bt a;
a = sub(B, A);
a.symbol = -1;
return a;
}
int t = 0;
f(i, 0, A.size() - 1) {
t = A[i] - t;
if (i < B.size()) t -= B[i];
C.pb((t + 10) % 10);
if (t < 0) t = 1;
else t = 0;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return bigint(C);
}
static bt SUB(bt A, bt B) {
if (A.symbol == -1 && B.symbol == -1) {
bigint t;
t = sub(A, B);
t.symbol *= -1;
return t;
}
if (A.symbol == 1 && B.symbol == -1) {
return add(A, B);
}
if (A.symbol == -1 && B.symbol == 1) {
bigint a = add(A, B);
a.symbol = -1;
return a;
}
return sub(A, B);
}
template<class T, class Y>
friend bt operator-(T A, Y B) {
return SUB(A, B);
}
static bigint mul(bt A, bt B) {
int len1 = A.size(), len2 = B.size(), cnt = len1 + len2;
VC x;
long long *c = new long long[len1 + len2 + 10], t = 0;
f(i, 0, len1 + len2 + 8)c[i] = 0;
f(i, 0, len1 - 1)f(j, 0, len2 - 1)c[i + j] += A[i] * B[j];
f(i, 0, cnt - 1) {
t += c[i];
c[i] = t % 10;
t /= 10;
}
while (t) {
c[++cnt] = t % 10;
t /= 10;
}
while (cnt >= 1 && c[cnt] == 0) cnt--;
x.assign(c, c + cnt + 1);
bigint ans(x);
delete[] c;
return {x};
}
friend bigint MUL(bt A, bt B) {
if (A.symbol * B.symbol == 1)return mul(A, B);
else {
bt t = mul(A, B);
t.symbol = -1;
return t;
}
}
template<class T, class Y>
friend bt operator*(T A, Y B) {
return MUL(A, B);
}
static bigint smilediv(bt A, int b) {
char *cha = new char[A.size()], *ans = new char[A.size()];
int l = A.size(), m = l - 1, x = 0;
std::copy(A._a.begin(), A._a.end(), cha);
rf(i, l - 1, 0) {
ans[i] = (x * 10 + cha[i]) / b;
x = (cha[i] + x * 10) % b;
}
while (!ans[m] && m >= 1)m--;
bigint z(0);
z._a.assign(ans, ans + m + 1);
delete []cha;
delete[]ans;
return z;
}
static bigint div(bt A, bt B) {
if (A < B)return {0};
bt l = 1, r = A;
while (l <= r) {
if (r.symbol == -1)exit(1);
bt mid = smilediv(l + r, 2);
if (A < mid * B)r = mid - 1;
else l = mid + 1;
}
return r;
}
friend bigint DIV(bt A, bt B) {
bt t = div(A, B);
if (A.symbol * B.symbol == -1)t.symbol = -1;
return t;
}
template<class T, class Y>friend bt operator/(T A, Y B) {
return DIV(A, B);
}
static bigint mod(bt A, bt B) {
bt x = A / B;
return A - x * B;
}
friend bigint MOD(bt A, bt B) {
return mod(mod(A, B) + B, B);
}
template<class T, class Y>
friend bt operator%(T A, Y B) {
return MOD(A, B);
}
bigint(VC c) : _a(std::move(c)) { }
bigint() {
_a.assign({0});
}
template<class T>bigint(T z) {
_a.reserve(50);
_a.clear();
string s;
if (typeid(z) != typeid(const char *) && typeid(z) != typeid(string))s = to_string((long long) z);
else s = z;
if (s[0] != '-') {
rf(i, s.size() - 1, 0)_a.pb(s[i] - '0');
} else {
rf(i, s.size() - 1, 1)_a.pb(s[i] - '0');
symbol = -1;
}
}
friend std::ostream &operator<<(std::ostream &os, bt cc) {
if (cc.symbol == -1 && !(cc._a.size() == 1 && cc[0] == 0))os << '-';
rf(i, cc.size() - 1, 0)os << int(cc[i]);
return os;
}
friend std::istream &operator>>(std::istream &is, bt &cc) {
cc._a.clear();
std::string x;
is >> x;
if (x[0] == '-') {
cc.symbol = -1;
rf(i, x.size() - 1, 1)cc._a.pb(x[i] - '0');
} else rf(i, x.size() - 1, 0)cc._a.pb(x[i] - '0');
return is;
}
template<class T>bool operator<(T A) {
bigint other = A;
if (this->symbol == -1 && other.symbol == 1 && !(*(this) == other))return 1;
if (this->size() < other.size())return 1;
else if (this->size() > other.size())return 0;
else if (this->size() == other.size())rf(i, this->size() - 1, 0) {
if ((*this)[i] < other[i])return 1;
}
return 0;
}
bool operator<(bigint A) {
bigint other = A;
if (this->symbol == -1 && other.symbol == 1 && !(*(this) == other))return 1;
if (this->size() < other.size())return 1;;
if (this->size() > other.size())return 0;
if (this->size() == other.size())rf(i, this->size() - 1, 0) {
if ((*this)[i] < other[i])return 1;
else if ((*this)[i] > other[i])return 0;
}
return 0;
}
template<class T>bool operator==(T A) {
bigint other(A);
if (other._a == this->_a && other.symbol == this->symbol || (this->size() == 1 && this->symbol == -1 && other.symbol == 1 && other.size() == 1 && (*this)[0] == 0 && other[0] == 0))return 1;
return 0;
}
template<class T>bool operator>(T A) {
bigint other(A);
if (*this < other == 0 && (*this == other) == 0)return true;
return false;
}
template<class T>bool operator<=(T A) {
bigint other(A);
if (*(this) < other || *this == other)return 1;
return 0;
}
template<class T>bool operator>=(T A) {
bigint other(A);
if (*(this) > other || *this == other)return 1;
return 0;
}
bigint operator-() {
auto t = *this;
t.symbol *= -1;
return t;
}
bigint operator+=(bt other) {
*this = *this + other;
return *this;
}
bigint operator-=(bt other) {
*this = *this - other;
return *this;
}
bigint operator*=(bt other) {
*this = *this * other;
return *this;
}
bigint operator/=(bt other) {
*this = *this / other;
return *this;
}
bigint operator%=(bt other) {
*this = *this % other;
return *this;
}
bigint operator++(int) {
bt t = *this;
*this = *this + 1;
return t;
}
bigint operator++() {
*this = *this + 1;
return *this;
}
bigint operator--(int) {
bt t = *this;
*this = *this - 1;
return t;
}
bigint operator--() {
*this = *this - 1;
return *this;
}
operator int() const {
int t = 0;
for (int i = _a.size() - 1; i >= 0; i--) {
t = t * 10 + _a[i];
}
return t;
}
bigint fact() {
bigint res = 1;
for (bigint i = 1; i <= *this; i++) res *= i;
return res;
}
};