[模板] 代码库

· · 个人记录

XiaoYiiiの奇妙模板库

时间跨度较长,因此马蜂前后差异极大,可能造成不适

1 语言基础

快读快写

inline char gc()
{
    static char BB[1000000], * S = BB, * T = BB;
    return (S == T) && (T = (S = BB) + fread(BB, 1, 1000000, stdin), S == T) ? EOF : *S++;
}

template <typename T>
T read(){
    T x = 0, f = 1;
    char ch = gc();
    while(ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = gc();}
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = gc();}
    return x * f;
}

template <> double read<double> (){
    double x = 0;
    int f = 1;
    char ch = gc();
    while(ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = gc();}
    while(ch >= '0' && ch <= '9'){x = x * double(10) + double(ch - '0'); ch = gc();}
    if(ch == '.'){
        ch = gc();
        double now = 1;
        while(ch >= '0' && ch <= '9'){
            now /= double(10);
            x = x + now * double(ch - '0');
            ch = gc();
        }
    }
    return x * f;
}

template <typename T>
void read(T & t){
    T x = 0, f = 1;
    char ch = gc();
    while(ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = gc();}
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = gc();}
    t = x * f;
}

template <> void read<double> (double & t){
    double x = 0;
    int f = 1;
    char ch = gc();
    while(ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = gc();}
    while(ch >= '0' && ch <= '9'){x = x * double(10) + double(ch - '0'); ch = gc();}
    if(ch == '.'){
        ch = gc();
        double now = 1;
        while(ch >= '0' && ch <= '9'){
            now /= double(10);
            x = x + now * double(ch - '0');
            ch = gc();
        }
    }
    t = x * f;
}

template <typename T, typename...Args>
void read(T & value, Args & ... args){
    read(value);
    read(args...);
}


template <typename T>
void sprint(T x, int w = 0){
    if(x < 0){putchar('-'); x = -x;}
    char ch[20];
    int t = 0;
    while(x){
        ch[++t] = x % 10 + '0';
        x /= 10;
    }
    for(int i = t; i >= 1; i--){
        putchar(ch[i]);
    }
    if(t == 0) putchar('0');
    switch(w){
        case 1:{
            putchar(' ');
            break;
        }
        case 2:{
            putchar('\n');
            break;
        }
        default:{
            break;
        }
    }
}

template<typename T>
void mprint(const T & value){
    T x = value;
    sprint(x, 2);
}

template<typename T, typename ...Args>
void mprint(const T & value, const Args & ... args){
    T x = value;
    sprint(x, 1);
    mprint(args...);
}

高精度

const int SIZE = 3002;
struct Big{
    int data[SIZE], len, f;
    Big(){
        len = 1;
        memset(data, 0, sizeof(data));
        data[0] = 0;
        f = 1;
    }
    Big(const int);
    Big(const Big &);
    Big& operator=(const Big&);
    Big operator+(const Big&) const;
    Big operator*(const Big&) const;
    Big operator*(const int) const;
    bool operator<(const Big&) const;
    bool operator>(const Big&) const;
    bool operator==(const Big&) const;
    const int operator[](const int pos){ return data[pos];}
    void print();
    void read();
    void mul(const int);
};

Big::Big(const int id){
    memset(data, 0, sizeof(data));
    len = 0;
    if(id == 0){
        len = 1;
        return;
    }
    int tmp = id;
    if(id < 0){
        f = -1;
        tmp = -tmp;
    }
    while(tmp){
        data[len++] = tmp % 10;
        tmp /= 10;
    }
}

Big::Big(const Big & T) : len(T.len), f(T.f){
    memset(data, 0, sizeof(data));
    for(int i = 0; i < len; ++i) data[i] = T.data[i];
}

Big& Big::operator=(const Big & T){
    memset(data, 0, sizeof(data));
    len = T.len;
    f = T.f;
    for(int i = 0; i < len; ++i) data[i] = T.data[i];
    return *this;
}

Big Big::operator+(const Big& T) const {
    Big t;
    t.len = max(len, T.len);
    for(int i = 0; i < t.len; ++i){
        t.data[i] = t.data[i] + data[i] + T.data[i];
        if(t.data[i] >= 10){
            t.data[i + 1] += t.data[i] / 10;
            t.data[i] %= 10;
        }
    }
    if(t.data[t.len]) ++t.len;
    return t;
}

Big Big::operator*(const Big & T) const{
    Big t;
    for(int i = 0; i < len + T.len; ++i){
        int be = max(i - T.len + 1, 0), ed = min(i, len - 1);
        for(int j = be; j <= ed; ++j){
            t.data[i] += data[j] * T.data[i - j];
        }
    }
    for(int i = 0; i < len + T.len; ++i){
        if(t.data[i] >= 10){
            t.data[i + 1] += t.data[i] / 10;
            t.data[i] %= 10;
        }
    }
    if(t.data[len + T.len]){
        t.len = len + T.len + 1;
    }
    else{
        t.len = len + T.len;
        while(t.len >= 1 && t.data[t.len - 1] == 0) --t.len;
    }
    return t;
}

Big Big::operator*(const int x) const{
    Big t(*this);
    for(int i = 0; i < t.len; ++i){
        t.data[i] = t.data[i] * x;
    }
    for(int i = 0; i < t.len; ++i){
        if(t.data[i] >= 10){
            t.data[i + 1] += t.data[i] / 10;
            t.data[i] %= 10;
        }
    }

    while(t.data[t.len] >= 10){
        t.data[t.len + 1] = t.data[t.len] / 10;
        t.data[t.len++] %= 10;
    }

    if(t.data[t.len]) ++t.len;
    return t;
}

bool Big::operator<(const Big & T) const{
    if(T.len != len) return len < T.len;
    for(int i = len - 1; i >= 0; i--){
        if(data[i] < T.data[i]) return 1;
        if(data[i] > T.data[i]) return 0;
    }
    return 0;
}

bool Big::operator>(const Big & T) const{
    if(T.len != len) return len > T.len;
    for(int i = len - 1; i >= 0; i--){
        if(data[i] > T.data[i]) return 1;
        if(data[i] < T.data[i]) return 0;
    }
    return 0;
}

bool Big::operator==(const Big & T) const{
    if(T.len != len) return 0;
    for(int i = len - 1; i >= 0; i--){
        if(data[i] != T.data[i]) return 0;
    }
    return 1;
}

void Big::print(){
    for(int i = len - 1; i >= 0; --i){
        printf("%d", data[i]);
    }
    putchar('\n');
}

void Big::read(){
    f = 1;
    memset(data, 0, sizeof(data));
    len = 0;
    int tmp[SIZE];
    char ch = getchar();
    while (ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') {tmp[len++] = ch - '0'; ch = getchar();}
    for(int i = 0; i < len; ++i){
        data[i] = tmp[len - i - 1];
    }
}

void Big::mul(const int x) {
    for(int i = 0; i < len; ++i){
        data[i] = data[i] * x;
    }
    for(int i = 0; i < len; ++i){
        if(data[i] >= 10){
            data[i + 1] += data[i] / 10;
            data[i] %= 10;
        }
    }
    while(data[len] >= 10){
        data[len + 1] = data[len] / 10;
        data[len++] %= 10;
    }
    if(data[len]) ++len;
}

2 树上模板

最近公共祖先

[倍增]

const int N = 5 * 1e5 + 10;
struct edge{
    int v, next;
}e[2 * N];
int h[N], etot, up[N][23], dep[N], n, s;
void add_edge(int u, int v){
    e[etot].v = v;
    e[etot].next = h[u];
    h[u] = etot++;
}
void dfs(int u, int fa){
    up[u][0] = fa;
    dep[u] = dep[fa] + 1;
    for(int i = h[u]; i != -1; i = e[i].next){
        int v = e[i].v;
        if(v == fa) continue;
        dfs(v, u);
    }
}
void init(){
    for(int j = 1; (1 << j) <= n; j++){
        for(int i = 1; i <= n; i++){
            up[i][j] = up[up[i][j - 1]][j - 1];
        }

    }
}
int lca(int u, int v){
    if(dep[u] < dep[v]) swap(u, v);
    if(dep[u] != dep[v]){
        for(int j = 21; j >= 0; j--){
            if(dep[up[u][j]] >= dep[v]){
                u = up[u][j];
            }
        }
    }
    if(u == v) return u;
    for(int j = 21; j >= 0; j--){
        if(up[u][j] != up[v][j]){
            u = up[u][j];
            v = up[v][j];
        }
    }
    return up[u][0];
}

[Euler + RMQ]

这里采用ST表求解RMQ的方法。

const int N = 5 * 1e5 + 10, K = 21;
struct edge{
    int v, next;
}e[2 * N];
int h[N], etot, dep[N], fir[N], LOG[N * K], tot, n, s;
void add_edge(int u, int v){
    e[etot].v = v;
    e[etot].next = h[u];
    h[u] = etot++;
}
struct node{
    int val, u;
}st[2 * N][K];
node min(node A, node B){
    return (A.val < B.val) ? A : B;
}
void dfs(int u, int fa){
    fir[u] = ++tot;
    dep[u] = dep[fa] + 1;
    st[tot][0] = (node){dep[u], u};
    for(int i = h[u]; i != -1; i = e[i].next){
        int v = e[i].v;
        if(v == fa) continue;
        dfs(v, u);
        st[++tot][0] = (node){dep[u], u};
    }
}
void init(){
    LOG[1] = 0;
    LOG[2] = 1;
    for(int i = 3; i <= tot; i++){
        LOG[i] = LOG[i >> 1] + 1;
    }
    for(int j = 1; (1 << j) <= tot; j++){
        for(int i = 1; i + (1 << j) - 1 <= tot; i++){
            int k = i + (1 << (j - 1));
            st[i][j] = min(st[i][j - 1], st[k][j - 1]);
        }
    }
}
int lca(int u, int v){
    int x = fir[u], y = fir[v];
    if(x > y) swap(x, y);
    int j = LOG[y - x + 1], z = y - (1 << j) + 1;
    node ret = min(st[x][j], st[z][j]);
    return ret.u;
}

[Tarjan(offline)]

const int N = 5 * 1e5 + 10;
int n, m, s, h[N], q[N], etot, qtot, ans[N], f[N];
bool vis[N];
struct edge{
    int v, next;
}e[N << 1];
void add_edge(int u, int v){
    e[etot] = (edge){v, h[u]};
    h[u] = etot++;
}
struct query{
    int v, next, id;
}Q[N << 1];
void add_query(int u, int v, int id){
    Q[qtot] = (query){v, q[u], id};
    q[u] = qtot++;
}
int find_set(int x){
    return (f[x] == x) ? x : (f[x] = find_set(f[x]));
}
void Tarjan(int u, int fa){
    f[u] = u;
    for(int i = h[u]; ~i; i = e[i].next){
        int v = e[i].v;
        if(v == fa) continue;
        Tarjan(v, u);
    }
    for(int i = q[u]; ~i; i = Q[i].next){
        int v = Q[i].v;
        if(!vis[v]) continue;
        ans[Q[i].id] = find_set(v);
    }
    f[u] = fa;
    vis[u] = 1;
}

树链剖分

其余部分见下方

int lca(int u, int v){
    while(top[u] != top[v]){
        if(dep[top[u]] < dep[top[v]]) swap(u, v);
        u = f[top[u]];
    }
    return (dep[u] < dep[v]) ? u : v;
}

树链剖分

重链剖分

const int N = 1e5 + 10;
int n, m, r, h[N], siz[N], top[N], dfn[N], id[N], dep[N], f[N], son[N], etot, cnt;
ll p, sum[N << 3], tag[N << 3], len[N << 3], a[N];
struct edge{
    int v, next;
}e[N << 1];
void add_edge(int u, int v){
    e[etot] = (edge){v, h[u]};
    h[u] = etot++;
}
void dfs1(int u){
    siz[u] = 1;
    int tmp = 0;
    for(int i = h[u]; i != -1; i = e[i].next){
        int v = e[i].v;
        if(v == f[u]) continue;
        dep[v] = dep[u] + 1;
        f[v] = u;
        dfs1(v);
        if(siz[v] > tmp){
            son[u] = v;
            tmp = siz[v];
        }
        siz[u] += siz[v];
    }
}
void dfs2(int u, int tp){
    dfn[u] = ++cnt;
    id[cnt] = u;
    top[u] = tp;
    if(!son[u]) return; 
    dfs2(son[u], tp);
    for(int i = h[u]; i != -1; i = e[i].next){
        int v = e[i].v;
        if(v == f[u] || v == son[u]) continue;
        dfs2(v, v);
    }
}

点分治

一般:

const int N = 1e4 + 10;
struct edge{
    int v, next, w;
}e[N << 1];
int h[N], etot, siz[N], msiz[N], n, rt, sum;
bool vis[N];
void add_edge(int u, int v, int w){
    e[etot] = (edge){v, h[u], w};
    h[u] = etot++;
}
void getrt(int u, int fa){
    siz[u] = 1;
    msiz[u] = 0;
    for(int i = h[u]; i != -1; i = e[i].next){
        int v = e[i].v;
        if(v == fa || vis[v]) continue;
        siz[u] += siz[v];
        msiz[u] = max(msiz[u], siz[v]);
    }
    msiz[u] = max(msiz[u], sum - siz[u]);
    if(msiz[u] < msiz[rt]) rt = u;
}
void calc(int u){}
void solve(int u){
    vis[u] = 1;
    calc(u);
    for(int i = h[u]; i != -1; i = e[i].next){
        int v = e[i].v;
        if(vis[v]) continue;
        sum = siz[v];
        msiz[rt = 0] = n + 1;
        getrt(v, u);
        getrt(rt, 0);
        solve(rt);
    }
}

该题:

const int N = 1e4 + 10, M = 105, K = 1e7 + 10;
struct edge{
    int v, next, w;
}e[N << 1];
int h[N], etot, dis[N], siz[N], msiz[N], n, m, rt, sum;
int q[N], cnt1, p[N], cnt2;
struct query{
    int k;
    bool ans;
}Q[M];
bool judge[K], vis[N];
void add_edge(int u, int v, int w){
    e[etot] = (edge){v, h[u], w};
    h[u] = etot++; 
}
void getrt(int u, int fa){
    siz[u] = 1;
    msiz[u] = 0;
    for(int i = h[u]; ~i; i = e[i].next){
        int v = e[i].v;
        if(v == fa || vis[v]) continue;
        getrt(v, u);
        siz[u] += siz[v];
        msiz[u] = max(msiz[u], siz[v]);
    }
    msiz[u] = max(msiz[u], sum - siz[u]);
    if(msiz[u] < msiz[rt]) rt = u;
}
void getdis(int u, int fa){
    p[++cnt2] = dis[u];
    for(int i = h[u]; ~i; i = e[i].next){
        int v = e[i].v;
        if(v == fa || vis[v]) continue;
        dis[v] = dis[u] + e[i].w;
        if(dis[v] > (int) 1e7) continue;
        getdis(v, u); 
    }
}
void calc(int u){
    cnt1 = 0;
    for(int i = h[u]; ~i; i = e[i].next){
        int v = e[i].v;
        if(vis[v]) continue;
        cnt2 = 0;
        dis[v] = e[i].w;
        getdis(v, u);
        for(int j = 1; j <= cnt2; j++){
            for(int k = 1; k <= m; k++){
                if(Q[k].k >= p[j]) Q[k].ans |= judge[Q[k].k - p[j]];
            }
        }
        for(int j = 1; j <= cnt2; j++){
            q[++cnt1] = p[j];
            judge[p[j]] = 1;
        }
    }
    for(int i = 1; i <= cnt1; i++) judge[q[i]] = 0;
}
void solve(int u){
    vis[u] = judge[0] = 1;
    calc(u);
    for(int i = h[u]; ~i; i = e[i].next){
        int v = e[i].v;
        if(vis[v]) continue;
        sum = siz[v];
        msiz[rt = 0] = n + 1;
        getrt(v, u);
        getrt(rt, 0);
        solve(rt);
    }
}
int main(){
    memset(h, -1, sizeof(h));
    scanf("%d%d", &n, &m);
    int u, v, w;
    for(int i = 1; i <= n - 1; i++){
        scanf("%d%d%d", &u, &v, &w);
        add_edge(u, v, w);
        add_edge(v, u, w);
    }
    for(int i = 1; i <= m; i++) scanf("%d", &Q[i].k);
    msiz[rt = 0] = sum = n + 1;
    getrt(1, 0);
    getrt(rt, 0);
    solve(rt);
    for(int i = 1; i <= m; i++) puts((Q[i].ans) ? "AYE" : "NAY");
    return 0;
}

3 图上模版

最短路/-

dijkstra堆优化

const int N = 1e5 + 10, M = 2 * 1e5 + 10;
int h[N], etot, n, m, s;
struct edge{
    int v, next;
    ll w;
}e[M << 1];
void add_edge(int u, int v, ll w){
    e[etot] = (edge){v, h[u], w};
    h[u] = etot++;
}
ll dis[N];
set<pair<ll, int>> heap;
void dijkstra(int u0){
    memset(dis, 0x7f, sizeof(dis));
    dis[u0] = 0;
    heap.insert(make_pair(dis[u0] = 0, u0));
    while(!heap.empty()){
        int u = heap.begin()->second;
        ll d = heap.begin()->first;
        heap.erase(heap.begin());
        if(d > dis[u]) continue;
        for(int i = h[u]; i != -1; i = e[i].next){
            int v = e[i].v;
            if(dis[v] > e[i].w + dis[u]){
                dis[v] = e[i].w + dis[u];
                heap.insert(make_pair(dis[v], v));
            }
        }
    }
}

Bellman_Ford

bool BL(int u0){
    memset(dis, 0x3f, sizeof(dis));
    dis[u0] = 0;
    int now = 0;
    for(int k = 1; k <= n - 1; k++){
        for(int u = 0; u <= n; u++){
            for(int i = h[u]; ~i; i = e[i].next){
                if(dis[e[i].v] > dis[u] + e[i].w){
                    dis[e[i].v] = dis[u] + e[i].w;
                    ++now;
                }
            }
        }
        if(!now) break;
        now = 0;
    }
    for(int u = 0; u <= n; u++){
        for(int i = h[u]; ~i; i = e[i].next){
            if(dis[e[i].v] > dis[u] + e[i].w) return 0;
        }
    }
    return 1;
}

同余最短路

#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
const int N = 1e5 + 10;
int h[N], etot, x, y, z;
struct edge{
    int v, next;
    ll w;
}e[N << 1];
ll d[N], H;
void add_edge(int u, int v, ll w){
    e[etot] = (edge){v, h[u], w};
    h[u] = etot++;
}
set<pair<ll, int> > heap; 
void dijkstra(){
    memset(d, 0xff, sizeof(d));
    heap.insert(make_pair(d[0] = 0, 0));
    while(!heap.empty()){
        ll dd = heap.begin()->first;
        int u = heap.begin()->second;
        heap.erase(heap.begin());
        if(dd > d[u]) continue;
        for(int i = h[u]; ~i; i = e[i].next){
            int v = e[i].v;
            if(d[v] > d[u] + e[i].w){
                heap.insert(make_pair(d[v] = d[u] + e[i].w, v));
            }
        }
    }
}
int main(){
    memset(h, -1, sizeof(h));
    scanf("%llu%d%d%d", &H, &x, &y, &z);
    --H;
    if(x > y) swap(x, y);
    if(x > z) swap(x, z);
    for(int i = 0; i <= x - 1; i++){
        add_edge(i, (i + y) % x, (ll)y);
        add_edge(i, (i + z) % x, (ll)z);
    }
    dijkstra();
    ll ans = 0;
    for(int i = 0; i <= x - 1; i++){
        if(d[i] <= H) ans += (H - d[i]) / (ll)x + (ll)1;
    }
    printf("%llu\n", ans);
    return 0;
}

最小生成树

prim

const int N = 5 * 1e3 + 2, M = 2 * 1e5 + 2;
int h[N], etot, tot, ans, n, m, dis[N];
bool vis[N];
struct edge{
    int v, next, w;
}e[2 * M];
void add_edge(int u, int v, int w){
    e[etot] = (edge) {v, h[u], w};
    h[u] = etot++;
}
bool prim(){
    memset(dis, 0x3f, sizeof(dis));
    dis[1] = 0;
    ans = 0;
    for(int i = 1; i <= n; i++){
        int tmp = 0;
        for(int j = 1; j <= n; j++) if(!vis[j] && dis[j] < dis[tmp]) tmp = j;
        if(!tmp) return 0;
        vis[tmp] = 1;
        ans += dis[tmp];
        for(int j = h[tmp]; j != -1; j = e[j].next) if(!vis[e[j].v]) dis[e[j].v] = min(dis[e[j].v], e[j].w);
    }
    return 1;
}

prim(堆优化)

const int N = 5 * 1e3 + 2, M = 2 * 1e5 + 2;
int h[N], etot, tot, ans, n, m, dis[N];
bool vis[N];
set<pair<int, int>> heap;
struct edge{
    int v, next, w;
}e[2 * M];
void add_edge(int u, int v, int w){
    e[etot] = (edge) {v, h[u], w};
    h[u] = etot++;
}
bool prim(){
    memset(dis, 0x3f, sizeof(dis));
    dis[1] = 0;
    tot = ans = 0;
    heap.insert(make_pair(dis[1], 1));
    while(!heap.empty()){
        int d = heap.begin() -> first;
        int u = heap.begin() -> second;
        heap.erase(heap.begin());
        if(d > dis[u]) continue;
        ++tot;
        vis[u] = 1;
        ans += d;
        for(int i = h[u]; i != -1; i = e[i].next){
            dis[e[i].v] = min(dis[e[i].v], e[i].w);
            if(!vis[e[i].v]) heap.insert(make_pair(dis[e[i].v], e[i].v));
        }
    }
    if(tot < n) return 0;
    else return 1;
}

kruaskal

const int N = 5 * 1e3 + 10, M = 2 * 1e5 + 10;
int n, m, fa[N], rk[N], h[N], etot, tot, ans;
bool vis[N];
void make_set(int x){
    fa[x] = x;
    rk[x] = 0;
}
int find_set(int x){
    return (fa[x] == x) ? fa[x] : find_set(fa[x]);
}
bool Union(int x, int y){
    x = find_set(x);
    y = find_set(y);
    if(x == y) return 0;
    if(rk[x] >= rk[y]){
        if(rk[x] == rk[y]) ++rk[x];
        fa[y] = x;
    }
    else fa[x] = y;
    return 1;
}
struct edge{
    int u, v, w;
    bool operator<(const edge & B){
        return w < B.w;
    }
}e[M];
bool kruaskal(){
    sort(e + 1, e + m + 1);
    for(int i = 1; i <= n; i++) make_set(i);
    for(int i = 1; i <= m; i++){
        if(Union(e[i].u, e[i].v)){
            ans += e[i].w;
            if(++tot == n - 1) return 1;
        }
    }
    return 0;
}

最小树形图

const int N = 1e2 + 10, M = 1e4 + 10;
const ll INF = LONG_LONG_MAX >> 2;
int etot, n, m, r, scc[N], pre[N], top[N], cnt;
ll inn[N], ans;
struct edge{
    int u, v;
    ll w;
}e[M];
void add_edge(int u, int v, ll w){
    e[etot] = (edge){u, v, w};
}
void init(){
    for(int i = 1; i <= n; i++){
        inn[i] = INF;
        top[i] = scc[i] = 0;
    }
    inn[r] = 0;
    cnt = 0;
}
bool solve(){
    ans = 0;
    while(1){
        init();
        for(int i = 1; i <= m; i++){
            int u = e[i].u, v = e[i].v;
            if(u != v && e[i].w < inn[v]){
                inn[v] = e[i].w;
                pre[v] = u;
            } 
        }
        for(int i = 1; i <= n; i++) if(inn[i] == INF) return 0;
        for(int i = 1; i <= n; i++){
            ans += inn[i];
            int u = i;
            while(u != r && !top[u] && !scc[u]){top[u] = i; u = pre[u];}
            if(u != r && top[u] == i){
                scc[u] = ++cnt;
                int v = pre[u];
                while(v != u){scc[v] = cnt; v = pre[v];}
            }
        }
        if(!cnt) return 1;
        for(int i = 1; i <= n; i++) scc[i] = (scc[i]) ? scc[i] : ++cnt;
        for(int i = 1; i <= m; i++){
            if(scc[e[i].u] != scc[e[i].v]) e[i].w -= inn[e[i].v];
            e[i].u = scc[e[i].u];
            e[i].v = scc[e[i].v];
        }
        n = cnt;
        r = scc[r];
    }
    return 0;
}

割点

Tarjan

void Tarjan(int u){
    dfn[u] = low[u] = ++cnt;
    int flag = 0;
    for(int i = h[u]; i != -1; i = e[i].next){
        int v = e[i].v;
        if(!dfn[v]){
            Tarjan(v);
            low[u] = min(low[u], low[v]);
            if(dfn[u] <= low[v]){
                ++flag;
                if(flag >= 2 || u != rt){
                    ans += cut[u] ^ 1;
                    cut[u] = 1; 
                }
            }
        }
        else low[u] = min(low[u], dfn[v]);
    }
}

点双联通分量

Tarjan

void Tarjan(int u){
    dfn[u] = low[u] = ++cnt;
    st.push(u);
    if(h[u] == -1 && u == rt){
        col[++colcnt].push_back(u);
        return;
    }
    int flag = 0;
    for(int i = h[u]; i != -1; i = e[i].next){
        int v = e[i].v;
        if(!dfn[v]){
            Tarjan(v);
            low[u] = min(low[u], low[v]);
            if(dfn[u] <= low[v]){
                ++flag;
                if(flag >= 2 || u != rt){
                    ans += cut[u] ^ 1;
                    cut[u] = 1; 
                }
                ++colcnt;
                while(st.top() != v){
                    col[colcnt].push_back(st.top());
                    st.pop();
                }
                col[colcnt].push_back(st.top());
                st.pop();
                col[colcnt].push_back(u);
            }
        }
        else low[u] = min(low[u], dfn[v]);
    }
}

边双联通分量

Tarjan

void Tarjan(int u, int fa){
    ins[u] = 1;
    dfn[u] = low[u] = ++cnt;
    st.push(u);
    for(int i = h[u]; i != -1; i = e[i].next){
        int v = e[i].v;
        if(!dfn[v]){
            Tarjan(v, i);
            low[u] = min(low[u], low[v]);
        }
        else if (i != (fa ^ 1)){
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]){
        ++colcnt;
        while(st.top() != u){
            col[colcnt].push_back(st.top());
            ins[st.top()] = 0;
            st.pop();
        }
        col[colcnt].push_back(st.top());
        ins[st.top()] = 0;
        st.pop();
    }
}

割边

Tarjan

void Tarjan(int u, int fa){
    dfn[u] = low[u] = ++cnt;
    for(int i = h[u]; ~i; i = e[i].next){
        int v = e[i].v;
        if(!dfn[v]){
            Tarjan(v, i);
            low[u] = min(low[u], low[v]);
            if(low[v] > dfn[u]){
                if(u < v) ans.push_back((node){u, v});
                else ans.push_back((node){v, u});
            }
        } 
        else if(i != (fa ^ 1)){
            low[u] = min(low[u], dfn[v]);
        }
    }
}

强连通分量/2-SAT

void Tarjan(int u){
    ins[u] = 1;
    dfn[u] = low[u] = ++cnt;
    st.push(u);
    for(int i = 0; i < G[u].size(); i++){
        int v = G[u][i];
        if(!dfn[v]){
            Tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if (ins[v]){
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]){
        ++colcnt;
        while(st.top() != u){
            whe[st.top()] = colcnt;
            ins[st.top()] = 0;
            st.pop();
        }
        whe[st.top()] = colcnt;
        ins[st.top()] = 0;
        st.pop();
    }

SCC缩点

void Tarjan(int u){
    ins[u] = 1;
    dfn[u] = low[u] = ++cnt;
    st.push(u);
    for(int i = 0; i < G[u].size(); i++){
        int v = G[u][i];
        if(!dfn[v]){
            Tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if (ins[v]){
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u]){
        ++colcnt;
        while(st.top() != u){
            col[colcnt].push_back(st.top());
            whe[st.top()] = colcnt;
            a[colcnt + n] += a[st.top()];
            ins[st.top()] = 0;
            st.pop();
        }
        col[colcnt].push_back(st.top());
        a[colcnt + n] += a[st.top()];
        whe[st.top()] = colcnt;
        ins[st.top()] = 0;
        st.pop();
    }
}
void dfs(int u){
    ins[u] = 1;
    for(int i = 0; i < G[u].size(); i++){
        int v = G[u][i];
        if(whe[u] != whe[v]) D[whe[u] + n].push_back(whe[v] + n);
        if(!ins[v]) dfs(v);
    }
}
for(int i = 1; i <= n; i++){
    if(!dfn[i]) Tarjan(i);
}
for(int i = 1; i <= n; i++){
    if(!ins[i]) dfs(i);
}
for(int i = n + 1; i <= n + colcnt; i++){
    sort(D[i].begin(), D[i].end());
    D[i].erase(unique(D[i].begin(), D[i].end()), D[i].end());
}

网络最大流

Edmonds-Karp

const int M = 10005, N = 205;
const long long INF = LONG_LONG_MAX;
struct egde{
    int to, next;
    long long cap, flow;
}e[M];
int h[N], etot, pre[N], flag[N][N], n, m, s, t;
long long mf[N], maxflow;
bool vis[N];
void add_edge(int u, int v, long long w){
    e[etot].to = v;
    e[etot].cap = w;
    e[etot].flow = 0;
    e[etot].next = h[u];
    h[u] = etot++;
    e[etot].to = u;
    e[etot].cap = 0;
    e[etot].flow = 0;
    e[etot].next = h[v];
    h[v] = etot++;
}
void update(int x){
    long long upp = mf[x];
    while(x != s){
        e[pre[x]].flow += upp;
        e[pre[x] ^ 1].flow -= upp;
        x = e[pre[x] ^ 1].to;
    }
}
bool bfs(){
    memset(mf, 0, sizeof(mf));
    memset(vis, 0, sizeof(vis));
    queue <int> Q;
    Q.push(s);
    vis[s] = 1;
    mf[s] = INF;
    while(!Q.empty()){
        int u = Q.front();
        Q.pop();
        for(int i = h[u]; i != -1; i = e[i].next){
            int v = e[i].to;
            if(vis[v] || e[i].cap <= e[i].flow) continue;
            vis[v] = 1;
            pre[v] = i;
            mf[v] = min(mf[u], e[i].cap - e[i].flow);
            Q.push(v);
            if(v == t) return 1;
        }
        if(mf[t]) return 1;
    }
    return 0;
}
void Maxflow(){
    while(bfs()){
        maxflow += mf[t];
        update(t);

    }
}

Dinic

const int M = 10005, N = 205;
const long long INF = 0x3f3f3f3f3f3f3f3f;
struct egde{
    int to, next;
    long long cap, flow;
}e[M];
int h[N], etot, pre[N], flag[N][N], n, m, s, t, now[N];
long long mf[N], maxflow, dis[N];
bool vis[N];
void add_edge(int u, int v, long long w){
    e[etot].to = v;
    e[etot].cap = w;
    e[etot].flow = 0;
    e[etot].next = h[u];
    h[u] = etot++;
    e[etot].to = u;
    e[etot].cap = 0;
    e[etot].flow = 0;
    e[etot].next = h[v];
    h[v] = etot++;
}
bool bfs(){
    memset(dis, 63, sizeof(dis));
    queue <int> Q;
    Q.push(s);
    dis[s] = 0;
    now[s] = h[s];
    while(!Q.empty()){
        int u = Q.front();
        Q.pop();
        for(int i = h[u]; i != -1; i = e[i].next){
            int v = e[i].to;
            if(dis[v] != INF || e[i].cap <= e[i].flow) continue;
            dis[v] = dis[u] + 1;
            now[v] = h[v];
            Q.push(v);
            if(v == t) return 1;
        }
    }
    return 0;
}
long long dfs(int u, long long sum){
    if(u == t) return sum;
    long long tmp, ret = 0;
    for(int i = now[u]; i != -1 && sum > 0; i = e[i].next){
        now[u] = i;
        int v = e[i].to;
        if(dis[v] != dis[u] + 1 || e[i].cap <= e[i].flow) continue;
        tmp = dfs(v, min(e[i].cap - e[i].flow, sum));
        if(!tmp) dis[v] = INF;
        e[i].flow += tmp;
        ret += tmp;
        e[i ^ 1].flow -= tmp;
        sum -= tmp;
    }
    return ret;
}
void Dinic(){
    while(bfs()){
        maxflow += dfs(s, INF);
    }
}

ISAP

const int M = 10005, N = 205;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct egde{
    int to, next;
    ll cap, flow;
}e[M];
int h[N], etot, pre[N], flag[N][N], n, m, s, t, now[N], dis[N], gap[N];
ll mf[N], maxflow;
bool vis[N];
void add_edge(int u, int v, ll w){
    e[etot].to = v;
    e[etot].cap = w;
    e[etot].flow = 0;
    e[etot].next = h[u];
    h[u] = etot++;
    e[etot].to = u;
    e[etot].cap = 0;
    e[etot].flow = 0;
    e[etot].next = h[v];
    h[v] = etot++;
}
bool bfs(){
    memset(dis, -1, sizeof(dis));
    memset(gap, 0, sizeof(gap));
    queue <int> Q;
    Q.push(t);
    dis[t] = 0;
    gap[0] = 1;
    while(!Q.empty()){
        int u = Q.front();
        Q.pop();
        for(int i = h[u]; i != -1; i = e[i].next){
            int v = e[i].to;
            if(~dis[v]) continue;
            dis[v] = dis[u] + 1;
            ++gap[dis[v]];
            Q.push(v);
        }
    }
    return 0;
}
ll dfs(int u, ll sum){
    if(u == t) return sum;
    ll tmp, ret = 0;
    for(int i = now[u]; i != -1; i = e[i].next){
        now[u] = i;
        int v = e[i].to;
        if(dis[v] + 1 != dis[u] || e[i].cap <= e[i].flow) continue;
        tmp = dfs(v, min(e[i].cap - e[i].flow, sum - ret));
        if(!tmp) continue;
        e[i].flow += tmp;
        ret += tmp;
        e[i ^ 1].flow -= tmp;
        if(sum == ret) return ret;
    }

    if(--gap[dis[u]] == 0) dis[s] = n + 1;
    ++gap[++dis[u]];
    return ret;
}
void ISAP(){
    maxflow = 0;
    bfs();
    while(dis[s] < n){
        for(int i = 1; i <= n; i++) now[i] = h[i];
        maxflow += dfs(s, INF);
    }
}

二分图最大匹配

匈牙利算法(dfs)

const int N = 505, M = 5 * 1e4 + 5;
int h[N], match[N], etot, n, m, E;
struct edge{
    int v, next;
}e[M << 1];
void add_edge(int u, int v){
    e[etot] = (edge){v, h[u]};
    h[u] =  etot++;
}
bool vis[N];
bool dfs(int u){
    for(int i = h[u]; ~i; i = e[i].next){
        int v = e[i].v;
        if(!vis[v]){
            vis[v] = 1;
            if(!match[v] || dfs(match[v])){
                match[v] = u;
                return 1;
            }
        }
    }
    return 0;
}
int solve(){
    int ret = 0;
    memset(match, 0, sizeof(match));
    for(int i = 1; i <= n; i++){
        memset(vis, 0, sizeof(vis));
        if(dfs(i)) ++ret;
    }   
    return ret;
}

bfs

const int N = 505, M = 5 * 1e4 + 5;
int h[N], match[2][N], etot, n, m, E, pre[N];
struct edge{
    int v, next;
}e[M << 1];
void add_edge(int u, int v){
    e[etot] = (edge){v, h[u]};
    h[u] =  etot++;
}
bool vis[2][N];
void upd(int v){
    while(v){
        int u = match[0][match[1][v] = pre[v]];
        match[0][match[1][v]] = v;
        v = u;
    }
}
bool bfs(int s){
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[0][u] = 1;
        for(int i = h[u]; ~i; i = e[i].next){
            int v = e[i].v;
            if(vis[1][v]) continue;
            vis[1][v] = 1;
            pre[v] = u;
            if(!match[1][v]){
                upd(v);
                return 1;
            }
            else q.push(match[1][v]);
        }
    }
    return 0;
}
int solve(){
    int ret = 0;
    memset(match, 0, sizeof(match));
    for(int i = 1; i <= n; i++){
        memset(vis, 0, sizeof(vis));
        if(bfs(i)) ++ret;
    }   
    return ret;
}
int main(){
    memset(h, -1, sizeof(h));
    scanf("%d%d%d", &n, &m, &E);
    int u, v;
    for(int i = 1; i <= E; i++){
        scanf("%d%d", &u, &v);
        add_edge(u, v);
    }
    printf("%d\n", solve());
    return 0;
}

二分图最大权完美匹配

KM算法

const int N = 505, M = 5 * 1e4 + 5;
const ll INF = 0x6f6f6f6f6f6f6f6f;
int match[2][N], n, m, pre[N];
ll slack[N], val[2][N], e[N][N];
bool vis[2][N];
void upd(int v){
    while(v) swap(v, match[0][match[1][v] = pre[v]]);
}
void bfs(int s){
    queue<int> q;
    while(!q.empty()) q.pop();
    q.push(s);
    vis[0][s] = 1;
    while(1){
        while(!q.empty()){
            int u = q.front();
            q.pop();
            for(int v = 1; v <= n; v++){
                if(e[u][v] == -INF || vis[1][v]) continue;
                ll delta = val[0][u] + val[1][v] - e[u][v];
                if(delta <= slack[v]){
                    slack[v] = delta;
                    pre[v] = u;
                    if(slack[v] == 0){
                        vis[1][v] = 1;
                        if(!match[1][v]){
                            upd(v);
                            return;
                        }
                        else if(!vis[0][match[1][v]]){
                            q.push(match[1][v]);
                            vis[0][match[1][v]] = 1;
                        }
                    }
                }
            }
        }
        ll d = INF;
        for(int i = 1; i <= n; i++) if(!vis[1][i]) d = min(d, slack[i]);
        //printf("***%lld\n", d);
        if(d == INF) break;
        for(int i = 1; i <= n; i++){
            if(vis[0][i]) val[0][i] -= d;
            if(vis[1][i]) val[1][i] += d;
            else slack[i] -= d;
        }
        for(int i = 1; i <= n; i++){
            if(!vis[1][i] && !slack[i]){
                vis[1][i] = 1;
                if(!match[1][i]){
                    upd(i);
                    return;
                }
                else if(!vis[0][match[1][i]]){
                    q.push(match[1][i]);
                    vis[0][match[1][i]] = 1;
                }
            }
        }
    }
    return;
}
void solve(){
    memset(match, 0, sizeof(match));
    for(int i = 1; i <= n; i++){
        memset(vis, 0, sizeof(vis));
        memset(slack, 0x6f, sizeof(slack));
        bfs(i);
        /*
        for(int i = 1; i <= n; i++) printf("%lld ", slack[i]);
        putchar('\n');
        for(int i = 1; i <= n; i++) printf("%d ", pre[i]);
        putchar('\n');
        for(int i = 0; i <= n; i++) printf("%d ", match[0][i]);
        putchar('\n');
        for(int i = 0; i <= n; i++) printf("%d ", match[1][i]);
        putchar('\n');
        */
    }   
}
int main(){
    scanf("%d%d", &n, &m);
    int u, v;
    ll w;
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n; j++){
            e[i][j] = e[j][i] = -INF;
        }
        val[0][i] = -INF;
    }
    //printf("%lld\n", INF);
    for(int i = 1; i <= m; i++){
        scanf("%d%d%lld", &u, &v, &w);
        e[u][v] = w;
        val[0][u] = max(val[0][u], w);
    }
    solve();
    ll ans = 0ll;
    for(int i = 1; i <= n; i++) ans += val[0][i] + val[1][i];
    printf("%lld\n", ans);
    for(int i = 1; i <= n; i++) printf("%d ", match[1][i]);
    putchar('\n');
    return 0;
}

线段树分治

const int N = 1e5 + 10;
pair<int, int> edge[N << 1];
vector<int> seg[N << 2];
stack<pair<int, int>> S;
int fa[N << 1], rk[N << 1], n, m, k;
bool ans[N];
void make_set(int x){
    fa[x] = x;
    rk[x] = 1;
}
int find_set(int x){
    return (fa[x] == x) ? x : find_set(fa[x]);
}
void Union(int x, int y){
    x = find_set(x);
    y = find_set(y);
    if(x == y) return;
    if(rk[x] > rk[y]) swap(x, y);
    S.push(make_pair(x, rk[x] == rk[y]));
    fa[x] = y;
    if(rk[x] == rk[y]) ++rk[y];
}
void reset(pair<int, int> & t){
    rk[fa[t.first]] -= t.second;
    fa[t.first] = t.first; 
}
void add(int A, int B, int e, int l, int r, int x){
    if(A <= l && r <= B){
        seg[x].push_back(e);
        return;
    }
    int mid = (l + r) >> 1;
    if(A <= mid) add(A, B, e, l, mid, ls);
    if(mid + 1 <= B) add(A, B, e, mid + 1, r, rs);
}
void solve(int l, int r, int x){
    int pre = S.size();
    bool now = 0;
    for(int i = 0; i < seg[x].size(); i++){
        now |= (find_set(edge[seg[x][i]].first) == find_set(edge[seg[x][i]].second));
        if(now) break;
        Union(edge[seg[x][i]].first, edge[seg[x][i]].second + n);
        Union(edge[seg[x][i]].first + n, edge[seg[x][i]].second);
    }
    if(now){
        for(int i = l; i <= r; i++) ans[i] = 0;
    }
    else{
        if(l == r){
            ans[l] = 1;
        }
        else{
            int mid = (l + r) >> 1;
            solve(l, mid, ls);
            solve(mid + 1, r, rs);
        }
    }
    while(S.size() != pre){
        reset(S.top());
        S.pop();
    }
}

4 数学模板

矩阵快速幂

struct mymartix{
    ll data[M][M];
    int r, c;
    mymartix(int R, int C):r(R), c(C) { memset(data, 0, sizeof (data)); }
    mymartix operator*(const mymartix& B) const {  
        mymartix ret(r, B.c);
        for(int i = 1; i <= r; i++){
            for(int j = 1; j <= B.c; j++){
                for(int k = 1; k <= c; k++){
                    ret.data[i][j] = (ret.data[i][j] + (data[i][k] * B.data[k][j]) % p) % p;
                }
            }
        }
        return ret;
    }

    void print(){
        for(int i = 1; i <= r; i++){
            for(int j = 1; j <= c; j++){
                printf("%lld ", data[i][j]);
            }
            putchar('\n');
        }
    }
};

线性筛

int prime[N], cnt, n;
bool p[N];
void init() {
    for (int i = 2; i <= n; i++) {
        if (!p[i]) {
            prime[++cnt] = i;
        }
        for (int j = 1; j <= cnt && i * prime[j] <= n; ++j) {
            p[i * prime[j]] = 1;
            if (i % prime[j] == 0) {
                break;
            }
        }
    }
}

欧拉函数

线性筛

int phi[N], prime[N], cnt, n;
void ora(){
    phi[1] = 1;
    for(int i = 2; i <= n; ++i){
        if(!phi[i]){
            prime[++cnt] = i;
            phi[i] = i - 1;
        }
        for(int j = 1; j <= cnt && i * prime[j] <= n; ++j){
            if(i % prime[j] == 0){
                phi[i * prime[j]] = phi[i] * prime[j]; 
                break;
            }
            phi[i * prime[j]] = phi[i] * (prime[j] - 1);
        }
    }
}

O(\sqrt V)求值

ll find_phi(ll x){
    ll ret = x;
    for(ll i = 2; i * i <= x; ++i){
        if(x % i) continue;
        ret = ret / i * (i - 1ll);
        while(x % i == 0ll) x /= i;
    }
    if(x != 1ll) ret = ret / x * (x - 1ll);
    return ret;
}

gcd

递归写法

int gcd(int a, int b){
    return (b) ? gcd(b, a % b) : a;
}

递推写法

inline int gcd(int a,int b){
    if(!b) return a;
    while(b ^= a ^= b ^= a %= b);
    return a;
}

exgcd

const int N = 3 * 1e6 + 10;
inline int exgcd(int a, int b, int& x, int& y){
    if(!b){
        x = 1;
        y = 0;
        return a;
    }
    int ret = exgcd(b, a % b, y, x);
    y = y - a / b * x;
    return ret;
}
int main(){
    int n, p;
    scanf("%d%d", &n, &p);
    int x = 0, y = 0;
    int t = p / exgcd(n, p, x, y);
    x = (x % t + t) % t;
    printf("%d", x);
    return 0;
}

线性求逆元

const int N = 3 * 1e6 + 10;
long long inv[N]; 
int main(){
    inv[1] = 1ll;
    printf("%lld\n", inv[1]);
    int n, p;
    scanf("%d%d", &n, &p);
    for(int i = 2; i <= n; i++){
        inv[i] = (p - p / i) * 1ll * inv[p % i] % p;
        printf("%lld\n", inv[i]);
    }
    return 0;
}

卢卡斯定理

const int MAXN = 1e5 + 10;
ll fact[MAXN], p;
ll qpow(ll a, int b){
    ll ret = 1ll;
    while(b){
        if(b & 1) ret = ret * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return ret;
}
ll C(int n, int m){
    if(m > n) return 0;
    return fact[n] * qpow(fact[m], p - 2) % p * qpow(fact[n - m], p - 2) % p;
}
ll lucas(int n, int m){
    if(!m) return 1ll;
    return C(n % p, m % p) * lucas(n / p, m / p) % p;
}

高斯消元

#include<bits/stdc++.h>
using namespace std;
const int N = 105;
const double eps = 1e-6;
int n;
double a[N][N];
int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n + 1; j++){
            scanf("%lf", &a[i][j]);
        }
    }
    for(int i = 1, r; i <= n; i++){
        r = i;
        for(int j = i; j <= n; j++){
            if(fabs(a[j][i]) > fabs(a[r][i])) {
                r = j;
            }
        }
        if(fabs(a[r][i]) < eps){
            puts("No Solution");
            return 0;
        }
        swap(a[r], a[i]);
        for(int j = 1; j <= n; j++){
            if(j == i) continue;
            double div = a[j][i] / a[i][i];
            for(int k = 1; k <= n + 1; k++){
                a[j][k] -= div * a[i][k];
            }
        }
        for(int j = n + 1; j >= i; j--){
            a[i][j] /= a[i][i];
        }
    }
    for(int i = 1; i <= n; i++){
        printf("%.2lf\n", a[i][n + 1]);
    }
    return 0;
}

拉格朗日插值

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e3 + 10;
const ll P = 998244353;
int n;
ll x[N], y[N], k, ans;
ll qpow(ll a, ll b){
    ll ret = 1ll;
    while(b){
        if(b & 1) ret = ret * a % P;
        a = a * a % P;
        b >>= 1; 
    }
    return ret;
}
int main(){
    scanf("%d%lld", &n, &k);
    for(int i = 1; i <= n; i++){
        scanf("%lld%lld", &x[i], &y[i]);
    }
    for(int i = 1; i <= n; i++){
        ll tmp = y[i];
        for(int j = 1; j <= n; j++){
            if(j == i) continue;
            tmp = tmp * (k - x[j] + P) % P * qpow((x[i] - x[j] + P) % P, P - 2) % P;
        }
        ans = (ans + tmp) % P;
    }
    printf("%lld\n", ans);
    return 0;
}

中国剩余定理

const int N = 11;
ll a[N], m[N], mul = 1, ans;
int n;
ll exgcd(ll A, ll B, ll &x, ll &y){
    if(B == 0){
        x = 1;
        y = 0;
        return A;
    }
    ll ret = exgcd(B, A % B, y, x);
    y = y - A / B * x;
    return ret;
}
void rd(){
    n = qread();
    for(int i = 1; i <= n; i++){
        m[i] = qread();
        a[i] = qread();
        mul *= m[i];
    }
}
int main(){
    rd();
    for(int i = 1; i <= n; i++){
        ll M = mul / m[i], t, tmp;
        exgcd(M, m[i], t, tmp);
        t = ((t < 0) ? t + m[i] : t) % m[i];
        M = M * t % mul;
        for(int j = 1; j <= a[i]; j++){
            ans = (ans + M) % mul;
        }
    }
    qprint(ans, 2);
    return 0;
}

excrt


ll excrt(int n, ll *a, ll *b) {
    ll r = a[1], m = b[1];

    for (int i = 2; i <= n; i++) {
        ll r2 = a[i], m2 = b[i], p, q;
        ll g = exgcd(m, m2, p, q);
        ll d = r2 - r;

        if (d % g != 0) return -1;

        ll md = m2 / g;
        ll t = (ll)((__int128)(d / g) * p % md);
        if (t < 0) t += md;

        ll lcm = (ll)((__int128)m / g * m2);
        r = (ll)((r + (__int128)m * t) % lcm);
        r = (r % lcm + lcm) % lcm;
        m = lcm;
    }

    return r;
}

5 数据结构

[单调队列]()

struct node{
    int head, tail, q[N];
    node(){
        head = 0;
        tail = -1;
    }
    void update(int id){
        while(head <= tail && q[head] < id) ++head;
    }
    void insert(int id, bool (*cmp)(int, int)){
        while(head <= tail && cmp(q[tail], id)) --tail;
        q[++tail] = id;
    }
    int top() {return q[head];}
    bool empty() {return head > tail;}
    void pop() {++head;}
    void print(){
        for(int i = head; i <= tail; i++){
            printf("%d ", q[i]);
        }
        putchar('\n');
    }
};

树状数组

const int N = 5 * 1e5 + 10;
ll d[2][N], a[N], b[N];
int n, m;
int lowbit(int x){
    return x & -x;
}
void ad(int x, ll v){
    ll vv = (ll)x * v;
    while(x <= n){
        d[0][x] += v;
        d[1][x] += vv;
        x += lowbit(x);
    }
}
void add(int x, int y, ll v){
    ad(x, v);
    ad(y + 1, -v);
}
ll qq(int x, int op){
    ll ret = 0ll;
    while(x){
        ret += d[op][x];
        x -= lowbit(x);
    }
    return ret;
}
ll query(int x, int y){
    return (ll)(y + 1) * qq(y, 0) - (ll)x * qq(x - 1, 0) - (qq(y, 1) - qq(x - 1, 1));
}
void init(){
    for(int i = 1; i <= n; i++) b[i] = i * 1ll * (a[i] - a[i - 1]);
    for(int i = 1; i <= n; i++) b[i] += b[i - 1];
    for(int i = 1; i <= n; i++){
        d[0][i] = a[i] - a[i - lowbit(i)]; 
        d[1][i] = b[i] - b[i - lowbit(i)];
    }
}
int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++){
        scanf("%lld", &a[i]);
        //add(i, i, a[i]);
    } 
    init();
    int op, l, r;
    ll v;
    while(m--){
        scanf("%d", &op);
        if(op == 1){
            scanf("%d%d%lld", &l, &r, &v);
            add(l, r, v);
        }
        else{
            scanf("%d%d", &l, &r);
            printf("%lld\n", query(l, r));
        }
    } 
    return 0;
}

左偏树

#include<bits/stdc++.h>
#define ll long long
const int N = 1e5 + 10;
namespace LST{
    struct node{
        int id, val, ch[2], d, fa;
        node(){
            id = val = ch[0] = ch[1] = fa = 0;
            d = -1;
        }
        node(int _id, int _val = 0):id(_id), val(_val){
            d = ch[0] = ch[1] = fa = 0;
        }
        bool operator<(const node & B)const {
            return (val == B.val) ? id < B.id : val < B.val;
        }
    };
    struct Heap{
        int rt[N];
        node tr[N];
        void make_set(int x){rt[x] = x;}
        int find_set(int x){return (rt[x] == x) ? x : rt[x] = find_set(rt[x]);}
        bool Union(int x, int y){
            x = find_set(x);
            y = find_set(y);
            if(x == y) return 0;
            rt[x] = rt[y] = merge(x, y);
            return 1; 
        }
        int merge(int x, int y){
            if(!x || !y) return x + y;
            if(tr[y] < tr[x]) x ^= y ^= x ^= y;
            tr[x].ch[1] = merge(tr[x].ch[1], y);
            tr[tr[x].ch[1]].fa = x;
            if(tr[tr[x].ch[0]].d < tr[tr[x].ch[1]].d) tr[x].ch[0] ^= tr[x].ch[1] ^= tr[x].ch[0] ^= tr[x].ch[1];
            tr[x].d = tr[tr[x].ch[1]].d + 1;
            return x;
        }
        int pop(int x){
            rt[tr[x].ch[0]] = rt[tr[x].ch[1]] = rt[x] = merge(tr[x].ch[0], tr[x].ch[1]);
            tr[x].ch[0] = tr[x].ch[1] = tr[x].d = 0;
            return tr[x].val;
        }
        void push_up(int x){
            if(!x) return;
            if(tr[x].d != tr[tr[x].ch[1]].d + 1){
                tr[x].d = tr[tr[x].ch[1]].d + 1;
                push_up(tr[x].fa);
            }
        }
        void erase(int x){
            int y = merge(tr[x].ch[0], tr[x].ch[1]);
            tr[y].fa = tr[x].fa;
            if(tr[tr[x].fa].ch[0] == x) tr[tr[x].fa].ch[0] = y;
            else if(tr[tr[x].fa].ch[1] == x) tr[tr[x].fa].ch[1] = y;
            push_up(tr[y].fa);
        }
    };
}

平衡树

替罪羊树

const int N = 1e6 + 10;

namespace scapegoat{
    struct Node{
        Node * ls, * rs; 
        int v, tot, size, ok;
        Node(){
            ls = rs = nullptr;
            size = tot = ok = 1;
        }
        Node(int val) : v(val){
            ls = rs = nullptr;
            size = tot = ok = 1;
        }
        void update(){
            size = ((ls) ? ls->size : 0) + ((rs) ? rs->size : 0) + 1;
            tot = ((ls) ? ls->tot : 0) + ((rs) ? rs->tot : 0) + 1;
        }
        void clear(){
            ls = rs = nullptr;
            size = tot = ok = 1;
        }
    };

    struct Tree{
        Node * root;
        Node * order[N];
        int cnt;
        double alpha;
        Tree(double w);
        void inorder(Node * & u);
        Node* build(int l, int r);
        void rebuild(Node * & u);
        bool bad(Node * & u){
            return (double) u->size * alpha <= (double) max(((u->ls) ? u->ls->size : 0), ((u->rs) ? u->rs->size : 0));
        }
        void insert(Node * & u, int val);
        int rk(Node * u, int val);
        int kth(Node * u, int k);
        void del_rk(Node * & u, int k);
        void del(int val);
        void print(Node * u);
        int pren(int val);
        int sucn(int val);
    };

    Tree::Tree(double w = 0.75){
        alpha = w;
        root = nullptr;
    }

    void Tree::inorder(Node * & u){
        if(!u) return;
        inorder(u->ls);
        if(u->ok) order[++cnt] = u;
        inorder(u->rs);
        if(!u->ok) {
            delete u;
            u = nullptr;
        }
    }

    Node* Tree::build(int l, int r){
        int mid = (l + r) >> 1;
        Node * u = order[mid];
        if(l == r){
            u->clear();
            return u;
        }
        u->ls = (l < mid) ? build(l, mid - 1) : nullptr;
        u->rs = build(mid + 1, r);
        u->update();
        return u;
    }

    void Tree::rebuild(Node * & u){
        cnt = 0;
        inorder(u);
        if(cnt) u = build(1, cnt);
    }

    void Tree::insert(Node * & u, int val){
        if(!u){
            u = new Node(val);
            return;
        }
        ++u->size;
        ++u->tot;
        if(u->v >= val) insert(u->ls, val);
        else insert(u->rs, val);
        if(bad(u)) rebuild(u);
    }

    int Tree::rk(Node * u, int val){
        int ret = 1;
        while(u){
            if(u->v >= val) u = u->ls;
            else{
                ret += u->ok + ((u->ls) ? u->ls->size : 0);
                u = u->rs;
            }
        }
        return ret;
    }

    int Tree::kth(Node * u, int k){
        while(u){
            if(u->ok && ((u->ls) ? u->ls->size : 0) + 1 == k) return u->v;
            else if(((u->ls) ? u->ls->size : 0) >= k) u = u->ls;
            else{
                k -= u->ok + ((u->ls) ? u->ls->size : 0);
                u = u->rs;
            }
        }
        return 0;
    }

    void Tree::del_rk(Node * & u, int k){
        if(!u) return;
        --u->size;
        if(u->ok && ((u->ls) ? u->ls->size : 0) + 1 == k){
            u->ok = 0;
            return;
        }
        if(((u->ls) ? u->ls->size : 0) + u->ok >= k) del_rk(u->ls, k);
        else del_rk(u->rs, k - ((u->ls) ? u->ls->size : 0) - u->ok);
    }

    void Tree::del(int val){
        if(!root) return;
        int k = rk(root, val);
        del_rk(root, k);
        if(!root) return;
        //printf("%d %d\n", root->tot, root->size);
        if(root->tot * alpha >= root->size) rebuild(root);
    }

    void Tree::print(Node * u){
        if(!u) return;
        printf("v = %d, l = %d, r = %d, ok = %d\n", u->v, ((u->ls) ? u->ls->v : 0), ((u->rs) ? u->rs->v : 0), u->ok);
        print(u->ls);
        print(u->rs);
    }

    int Tree::pren(int val){
        if(!root) return 0;
        int k = rk(root, val) - 1;
        return kth(root, k);
    }

    int Tree::sucn(int val){
        if(!root) return 0;
        int k = rk(root, val + 1);
        return kth(root, k);
    }
}

Splay

const int N = 1e5 + 10;
namespace Splay{
    struct node{
        int p, s[2], val, siz;
        void init(int _val, int _p){
            val = _val;
            p = _p;
        }
    };
    struct Tree{
        node tr[N];
        int root, tot;
        Tree(){
            root = tot = 0;
        }
        void update(int u){
            tr[u].siz = tr[tr[u].s[0]].siz + 1 + tr[tr[u].s[1]].siz;
        }
        void rotate(int);
        void splay(int, int);
        void insert(int);
        int fid(int);
        int rk(int);
        int kth(int);
        int pren(int);
        int sucn(int);
        int merge(int, int);
        void del(int, int &, int &);
        void del(int);
        void print(int);
    };
    void Tree::rotate(int x){
        int y = tr[x].p, z = tr[y].p, k = tr[y].s[1] == x;
        if(z) tr[z].s[tr[z].s[1] == y] = x;
        tr[x].p = z;
        tr[y].s[k] = tr[x].s[k ^ 1];
        if(tr[x].s[k ^ 1]) tr[tr[x].s[k ^ 1]].p = y;
        tr[x].s[k ^ 1] = y;
        tr[y].p = x;
        update(y);
        update(x);
    }
    void Tree::splay(int x, int k){
        while(tr[x].p != k){
            int y = tr[x].p, z = tr[y].p;
            if(z != k){
                if((tr[y].s[1] == x) ^ (tr[z].s[1] == y)) rotate(x);
                else rotate(y);
            }
            rotate(x);
        }
        if(!k) root = x;
    }
    void Tree::insert(int val){
        int u = root, p = 0;
        while(u) p = u, u = tr[u].s[val > tr[u].val];
        u = ++tot;
        if(p) tr[p].s[val > tr[p].val] = u;
        tr[u].init(val, p);
        splay(u, 0);
    }
    int Tree::fid(int val){
        if(!root) return -1;
        int u = root, p = 0;
        while(u){
            if(tr[u].val == val){
                splay(u, 0);
                return u;
            }
            u = tr[p = u].s[val > tr[u].val];
        }
        return -1;  
    }
    int Tree::rk(int val){
        int u = root, p = 0, ret = 1;
        while(u){
            p = u;
            if(val > tr[u].val) ret += tr[tr[u].s[0]].siz + 1;
            u = tr[u].s[val > tr[u].val];
        }
        splay(p, 0);
        return ret;
    }
    int Tree::kth(int k){
        int u = root;
        while(u){
            if(tr[tr[u].s[0]].siz >= k) u = tr[u].s[0];
            else if(tr[tr[u].s[0]].siz + 1 == k) return u;
            else k -= tr[tr[u].s[0]].siz + 1, u = tr[u].s[1];
        }
        return -1;
    }
    int Tree::pren(int val){
        return (root) ? kth(rk(val) - 1) : -1;
    }
    int Tree::sucn(int val){
        return (root) ? kth(rk(val + 1)) : -1;
    }
    int Tree::merge(int A, int B){
        if(!A | !B) return A | B;
        root = A;
        splay(root = A = kth(tr[A].siz), 0);
        tr[A].s[1] = B;
        tr[B].p = A;
        update(A); 
        return A;
    }
    void Tree::del(int val, int & A, int & B){
        if(!root){
            A = B = 0;
            return;
        }
        int u = fid(val);
        if(~u){
            A = tr[u].s[0];
            B = tr[u].s[1];
            tr[u].s[0] = tr[u].s[1] = 0;
            tr[A].p = tr[B].p = 0;
            root = 0;
        }
        else A = B = 0;
    }
    void Tree::del(int val){
        int A, B;
        del(val, A, B);
        root = merge(A, B);
    }
    void Tree::print(int u){
        if(!u) return;
        print(tr[u].s[0]);
        printf("u = %d val = %d l = %d r = %d siz = %d\n", u, tr[u].val, tr[u].s[0], tr[u].s[1], tr[u].siz);
        print(tr[u].s[1]);
    }
}

可持久化线段树

const int N = 1e6 + 1000;
int a[N], n, m, tot, lc[50 * N], rc[50 * N], root[10 * N];
long long sum[50 * N];
void update(int x){
    sum[x] = sum[lc[x]] + sum[rc[x]];
}
void build(int l, int r, int &x){
    if(!x) x = ++tot;
    if(l == r){sum[x] = a[l] * 1ll; return;}
    int mid = (l + r) >> 1;
    build(l, mid, lc[x]);
    build(mid + 1, r, rc[x]);
    update(x);
}
void modify(int p, int v, int l, int r, int &x, int last){
    if(!x) x = ++tot;
    if(l == r){ sum[x] = v * 1ll; return;}
    int mid = (l + r) >> 1;
    if(p <= mid){
        rc[x] = rc[last];
        modify(p, v, l, mid, lc[x], lc[last]);
    }
    else{
        lc[x] = lc[last];
        modify(p, v, mid + 1, r, rc[x], rc[last]);
    }
    update(x);
}
long long query(int p, int l, int r, int &x){
    if(!x) x = ++tot;
    if(l == r) return sum[x];
    int mid = (l + r) >> 1;
    if(p <= mid) return query(p, l, mid, lc[x]);
    else return query(p, mid + 1, r, rc[x]);
}
int main(){
    n = qread();
    m = qread();
    for(int i = 1; i <= n; i++) a[i] = qread();
    build(1, n, root[0]);
    int v, loc, val, op;
    for(int i = 1; i <= m; i++){
        v = qread();
        op = qread();
        loc = qread();
        if(op == 1){
            val = qread();
            modify(loc, val, 1, n, root[i], root[v]);
        }
        else{
            root[i] = root[v];
            qprint(query(loc, 1, n, root[v]));
            putchar('\n');
        }
    }
    return 0;
}

可持久化字典树

#include<bits/stdc++.h>
using namespace std;
const int N = 6 * 1e5 + 10;
int tr[N * 32][2], ver[N * 32], rt[N], tot;
void insert(int v, int last, int x){
    for(int i = 1 << 24; i; i >>= 1){
        ver[x] = ver[last] + 1;
        bool op = v & i;
        if(!tr[x][op]) tr[x][op] = ++tot;
        tr[x][!op] = tr[last][!op];
        x = tr[x][op];
        last = tr[last][op];
    }
    ver[x] = ver[last] + 1;
}
int query(int v, int x, int y){
    int ret = 0;
    for(int i = 1 << 24; i; i >>= 1){
        bool t = v & i;
        if(ver[tr[x][!t]] - ver[tr[y][!t]]){
            ret += i;
            x = tr[x][!t];
            y = tr[y][!t];
        }
        else{
            x = tr[x][t];
            y = tr[y][t];
        }
    }
    return ret;
}
int n, m, a[N], pre[N];
int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
        pre[i] = pre[i - 1] ^ a[i];
        rt[i] = ++tot;
        insert(pre[i], rt[i - 1], rt[i]);
    }
    char op[5];
    int val, l, r;
    while(m--){
        scanf("%s", op);
        if(op[0] == 'A'){
            ++n;
            scanf("%d", &a[n]);
            pre[n] = pre[n - 1] ^ a[n]; 
            rt[n] = ++tot;
            insert(pre[n], rt[n - 1], rt[n]);
        }
        else{
            scanf("%d%d%d", &l, &r, &val);
            --l;
            --r;
            int ret = 0;
            if(!l) ret = max(pre[n] ^ val, query(pre[n] ^ val, rt[r], rt[0]));
            else ret = query(pre[n] ^ val, rt[r], rt[l - 1]);
            printf("%d\n", ret);
        }
    }
    return 0;
}

块状链表

const int sqn = 3;
namespace Sqrt_list{
    struct node{
        node * nxt;
        int siz;
        int val[(sqn << 1) + 5];
        node(){
            siz = 0;
            nxt = nullptr;
        }  
        void add(int v){val[++siz] = v;}
        void split();
        void check();
        void insert(int pos, int v);
    };
    void node::split(){
        node * q = new node;
        for(int i = sqn + 1; i <= siz; i++){
           q->add(val[i]);
        }
        siz = sqn;
        q->nxt = nxt;
        nxt = q;
    }
    void node::check(){
        if(siz >= (sqn << 1)) split();
    }
    void node::insert(int pos, int v){//插在 pos 前
        for(int i = siz; i >= pos; i--){
            val[i + 1] = val[i];
        }
        ++siz;
        val[pos] = v;
        check();
    }
    struct List{
        node * head;
        int len;
        List(){
            head = new node;
            len = 0;
        }
        List(const int * a, int n):len(n){
            head = new node;
            node * p = head;
            for(int i = 1, j = 0; i <= n; i++){
                if(i - j > sqn) p->nxt = new node, p = p->nxt, j = i - 1;
                p->add(a[i]);
            }
        }
        node * fid(int & pos);
        void insert(int pos, int v);
        int query(int pos);
        void print();
    };
    node * List::fid(int & pos){
        int tot;
        node * p = head;
        for(tot = head->siz; p != nullptr && tot < pos; p = p->nxt, tot += p->siz);
        tot -= p->siz;
        pos -= tot;
        return p;
    }
    void List::insert(int pos, int v){  
        if(pos >= ++len){
            node * p = head;
            while(p->nxt != nullptr) p = p->nxt;
            p->add(v);
            p->check();
            return;
        }
        node * p = fid(pos);
        p->insert(pos, v);
    }
    int List::query(int pos){
        node * p = fid(pos);
        return p->val[pos];
    }
    void List::print(){
        node * p = head;
        for(; p != nullptr; p = p->nxt){
            putchar('{');
            for(int i = 1; i <= p->siz; i++) printf("%d ", p->val[i]);
            putchar('}');
        }
        putchar('\n');
    }
}

区间改+加等差数列+单点插+区间查

#include<bits/stdc++.h>
#define ll long long
const int sqn = 420;
const int N = 1e5 + 10;
namespace Sqrt_list{
    struct node{
        node * nxt;
        int siz;
        ll val[(sqn << 1) + 5], tag, sum, d, fir;
        node(){
            siz = 0;
            sum = d = fir = 0;
            tag = -1;
            nxt = nullptr;
        }  
        void pb(ll v){val[++siz] = v; sum += v;}
        void split();
        void check();
        void insert(int pos, ll v);
        void down1();
        void down2();
        void down();
        void add(int l, int r, ll st, ll ad);
        void chg(int l, int r, ll v);
        ll query(int l, int r);
    };
    void node::split(){
        node * q = new node;
        for(int i = sqn + 1; i <= siz; i++){
           q->pb(val[i]);
           sum -= val[i];
           q->sum += val[i];
        }
        siz = sqn;
        q->nxt = nxt;
        nxt = q;
    }
    void node::check(){
        if(siz >= (sqn << 1)) split();
    }
    void node::insert(int pos, ll v){//插在 pos 前
        down();
        for(int i = siz; i >= pos; i--){
            val[i + 1] = val[i];
        }
        ++siz;
        val[pos] = v;
        sum += v;
        check();
    }
    void node::down1(){
        if(~tag){
            for(int i = 1; i <= siz; i++) val[i] = tag;
            tag = -1;
        }
    }
    void node::down2(){
        if(fir || d){
            ll now = fir;
            for(int i = 1; i <= siz; i++){
                val[i] += now; 
                now += d;
            }
            fir = 0;
            d = 0;
        }
    }
    void node::down(){
        down1();
        down2();
    }
    void node::add(int l, int r, ll st, ll ad){
        if(l == 1 && r == siz){
            printf("%lld\n", sum);
            sum += (st * 2ll + (ll)(siz - 1) * ad) * (ll)siz / 2ll;
            printf("%lld\n", sum);
            fir += st;
            d += ad;
            return;
        }
        down1();
        ll now = st;
        for(int i = l; i <= r; i++){
            sum -= val[i];
            val[i] += now;
            now += ad;
            sum += val[i];
        }
    }
    void node::chg(int l, int r, ll v){
        if(l == 1 && r == siz){
            sum = (ll)siz * v;
            fir = 0;
            d = 0;
            tag = v;
            return;
        }
        down();
        for(int i = l; i <= r; i++){
            sum -= val[i];
            val[i] = v;
            sum += val[i];
        }
    }
    ll node::query(int l, int r){
        if(l == 1 && r == siz) return sum;
        down();
        ll ret = 0ll;
        for(int i = l; i <= r; i++) ret += val[i];
        return ret;
    }
    struct List{
        node * head;
        int len;
        List(){
            head = new node;
            len = 0;
        }
        List(const ll * a, int n):len(n){
            head = new node;
            node * p = head;
            for(int i = 1, j = 0; i <= n; i++){
                if(i - j > sqn) p->nxt = new node, p = p->nxt, j = i - 1;
                p->pb(a[i]);
            }
        }
        node * fid(int & pos);
        void insert(int pos, ll v);
        ll query(int pos);
        void add(int A, int B, ll st, ll ad);
        void chg(int A, int B, ll v);
        ll query(int A, int B);
        void print();
    };
    node * List::fid(int & pos){
        int tot;
        node * p = head;
        for(tot = head->siz; p != nullptr && tot < pos; p = p->nxt, tot += p->siz);
        tot -= p->siz;
        pos -= tot;
        return p;
    }
    void List::insert(int pos, ll v){  
        if(pos >= ++len){
            node * p = head;
            while(p->nxt != nullptr) p = p->nxt;
            p->down();
            p->pb(v);
            p->check();
            return;
        }
        node * p = fid(pos);
        p->insert(pos, v);
    }
    ll List::query(int pos){
        node * p = fid(pos);
        return p->val[pos];
    }
    void List::print(){
        node * p = head;
        for(; p != nullptr; p = p->nxt){
            putchar('{');
            for(int i = 1; i <= p->siz; i++) printf("%lld ", p->val[i]);
            putchar('}');
        }
        putchar('\n');
    }
    void List::chg(int A, int B, ll v){
        node * l = fid(A);
        node * r = fid(B);
        if(l == r){
            l->chg(A, B, v);
            return;
        }
        l->chg(A, l->siz, v);
        r->chg(1, B, v);
        for(node* p = l->nxt; p != nullptr && p != r; p = p->nxt) p->chg(1, p->siz, v);
    }
    void List::add(int A, int B, ll st, ll ad){
        node * l = fid(A);
        node * r = fid(B);
        if(l == r){
            l->add(A, B, st, ad);
            return;
        }
        ll now = st;
        l->add(A, l->siz, now, ad);
        now += (ll)(l->siz - A + 1) * ad;
        for(node * p = l->nxt; p != nullptr && p != r; p = p->nxt){
            p->add(1, p->siz, now, ad);
            now += (ll)p->siz * ad;
        }
        r->add(1, B, now, ad);
    }
    ll List::query(int A, int B){
        node * l = fid(A);
        node * r = fid(B);
        if(l == r) return l->query(A, B);
        ll ret = l->query(A, l->siz) + r->query(B, r->siz);
        for(node * p = l->nxt; p != nullptr && p != r; p = p->nxt) ret += p->query(1, p->siz);
        return ret;
    }
}

6 字符串模版

KMP

const int N = 1e6 + 10;
int nxt[N], n, m;
char s[N], t[N];
int main(){
    scanf("%s%s", s + 1, t + 1);
    n = strlen(s + 1);
    m = strlen(t + 1);
    nxt[1] = 0;
    for(int i = 2, k = 0; i <= m; i++){
        while(k && t[k + 1] != t[i]) k = nxt[k];
        if(t[k + 1] == t[i]) ++k;
        nxt[i] = k;
    }
    for(int i = 1, j = 0; i <= n; i++){
        while(j && t[j + 1] != s[i]) j = nxt[j];
        if(t[j + 1] == s[i]) ++j;
        if(j == m) printf("%d\n", i - m + 1), j = nxt[j];
    }
    for(int i = 1; i <= m; i++) printf("%d ", nxt[i]);
    putchar('\n');
    return 0;
}

AC自动机Ⅰ

const int N = 2 * 1e6;
int fail[N], go[N][26], ed[N], tot, n;
void insert(const char * s){
    int len = strlen(s + 1), now = 0;
    for(int i = 1; i <= len; i++){
        int op = s[i] - 'a';
        if(!go[now][op]) go[now][op] = ++tot;
        now = go[now][op];
    }
    ++ed[now];
}
void init(){
    queue <int> Q;
    for(int i = 0; i <= 25; i++) if(go[0][i]) Q.push(go[0][i]);
    while(!Q.empty()){
        int u = Q.front();
        Q.pop();
        for(int i = 0; i <= 25; i++){
            if(go[u][i]){
                fail[go[u][i]] = go[fail[u]][i];
                Q.push(go[u][i]);
            }
            else{
                go[u][i] = go[fail[u]][i];
            }
        }
    }
}
int fid(const char * t){
    int ret = 0, len = strlen(t + 1), now = 0;
    for(int i = 1; i <= len; i++){
        now = go[now][t[i] - 'a'];
        for(int j = now; j && ~ed[j]; j = fail[j]){
            ret += ed[j];
            ed[j] = -1;
        }
    }
    return ret;
}

后缀数组

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, M = 200;
char s[N];
int sa[N], rk[N], cnt[M], x[N << 1], y[N << 1], n, m;
void get_sa(){
    memset(cnt + 1, 0, m * sizeof(int));
    for(int i = 1; i <= n; i++) ++cnt[x[i] = s[i]];
    for(int i = 2; i <= m; i++) cnt[i] += cnt[i - 1];
    for(int i = n; i; i--) sa[cnt[x[i]]--] = i;

    for(int k = 1; k <= n; k <<= 1){
        int num = 0;
        for(int i = n - k + 1; i <= n; i++) y[++num] = i;
        for(int i = 1; i <= n; i++){
            if(sa[i] > k) y[++num] = sa[i] - k;
        }
        memset(cnt + 1, 0, m * sizeof(int));
        for(int i = 1; i <= n; i++) ++cnt[x[i]];
        for(int i = 2; i <= m; i++) cnt[i] += cnt[i - 1];
        for(int i = n; i; i--) sa[cnt[x[y[i]]]--] = y[i], y[i] = 0;
        swap(x, y);
        x[sa[1]] = 1, num = 1;
        for(int i = 2; i <= n; i++){
            x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++num;
        }
        if(num == n) break;
        m = num;
    }
}
int main(){
    scanf("%s", s + 1);
    n = strlen(s + 1);
    m = 122;
    get_sa();
    for(int i = 1; i <= n; i++) printf("%d ", sa[i]);
    putchar('\n');
    return 0;
}