CSP-J 可爱的模板们

· · 算法·理论

1. 队列

#include <bits/stdc++.h>

using namespace std;

int Q;
int q[1000050];
int front = 1, rear = 0;

int main() {
    scanf("%d", &Q);
    for (int i = 1; i <= Q; i++) {
        int op, x;
        scanf("%d", &op);
        if (op == 1) scanf("%d", &x), q[++rear] = x; // 入队 O(1)
        else if (op == 2) ++front; // 出队 O(1)
        else if (op == 3) printf("%d\n", q[front]); // 访问队首 O(1)
        else if (op == 4) printf("%d\n", q[rear]); // 访问队尾 O(1)
        else if (op == 5) 
            scanf("%d", &x), printf("%d\n", x > rear - front + 1 ? -1 : q[front + x - 1]); // 第 x 个元素(手写独有)O(1)
    }
    return 0;
}
// 总时间复杂度 O(Q)

2. 栈

#include <bits/stdc++.h>

using namespace std;

int q;
int s[1000050];
int top = 0;

int main() {
    scanf("%d", &q);
    while (q--) {
        int op, x;
        scanf("%d", &op);
        if (op == 1) scanf("%d", &x), s[++top] = x; // 入栈 O(1)
        else if (op == 2) --top; // 弹栈 O(1)
        else if (op == 3) printf("%d\n", s[top]); // 访问栈顶 O(1)
        else if (op == 4) scanf("%d", &x), printf("%d\n", s[x]); // 访问栈里第 x 个元素(手写独有)O(1)
    }
    return 0;
}
// 总时间复杂度 O(q)

3. 高精度加/减/乘

前方高能!!

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 50;

struct Bigint {
    char s[N];
    Bigint() {}
    Bigint(char a[]) {
        int len = strlen(a + 1);
        memset(s, 0, sizeof(s));
        for (int i = 1; i <= len; i++) s[i] = a[i];
    }
    ////////////////////////////////////////////////////////////////
    friend bool operator == (Bigint x, Bigint y) {
        int len1 = strlen(x.s + 1), len2 = strlen(y.s + 1);
        if (len1 != len2) return 0;
        for (int i = 1; i <= len1; i++) 
            if (x.s[i] != y.s[i]) 
                return 0;
        return 1;
    }
    friend bool operator != (Bigint x, Bigint y) {
        return !(x == y);
    }
    friend bool operator < (Bigint x, Bigint y) {
        int len1 = strlen(x.s + 1), len2 = strlen(y.s + 1);
        if (len1 != len2) return len1 < len2;
        for (int i = 1; i <= len1; i++) 
            if (x.s[i] != y.s[i]) 
                return x.s[i] < y.s[i];
        return 0;
    }
    friend bool operator <= (Bigint x, Bigint y) {
        return (x < y) || (x == y);
    }
    friend bool operator > (Bigint x, Bigint y) {
        return !(x <= y);
    }
    friend bool operator >= (Bigint x, Bigint y) {
        return !(x < y);
    }
    //////////////////////////////////////////////////////////////// 似乎没有什么用的东西呢
    friend Bigint operator + (Bigint x, Bigint y) { // O(lena+lenb)
        int a[N], b[N], c[N];
        char ans[N];
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        memset(c, 0, sizeof(c));
        memset(ans, 0, sizeof(ans));
        int lena = strlen(x.s + 1), lenb = strlen(y.s + 1), lenc = max(lena, lenb);
        for (int i = 1; i <= lena; i++) a[i] = x.s[lena - i + 1] - '0';
        for (int i = 1; i <= lenb; i++) b[i] = y.s[lenb - i + 1] - '0';
        for (int i = 1; i <= lenc; i++) {
            c[i] += a[i] + b[i];
            c[i + 1] += c[i] / 10;
            c[i] %= 10;
        }
        if (c[lenc + 1]) ++lenc;
        for (int i = 1; i <= lenc; i++) ans[i] = c[lenc - i + 1] + '0';
        Bigint res = Bigint(ans);
        return res;
    }
    friend Bigint operator - (Bigint x, Bigint y) { // O(lena+lenb)
        if (x < y) swap(x, y), putchar('-');
        int a[N], b[N], c[N];
        char ans[N];
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        memset(c, 0, sizeof(c));
        memset(ans, 0, sizeof(ans));
        int lena = strlen(x.s + 1), lenb = strlen(y.s + 1), lenc = max(lena, lenb);
        for (int i = 1; i <= lena; i++) a[i] = x.s[lena - i + 1] - '0';
        for (int i = 1; i <= lenb; i++) b[i] = y.s[lenb - i + 1] - '0';
        for (int i = 1; i <= lenc; i++) {
            if (a[i] < b[i]) a[i] += 10, --a[i + 1];
            c[i] = a[i] - b[i];
        }
        while (!c[lenc] && lenc > 1) --lenc;
        for (int i = 1; i <= lenc; i++) ans[i] = c[lenc - i + 1] + '0';
        Bigint res = Bigint(ans);
        return res;
    }
    friend Bigint operator * (Bigint x, Bigint y) { // O(lenalenb) (不会 FFT)
        int a[N], b[N], c[N];
        char ans[N];
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        memset(c, 0, sizeof(c));
        memset(ans, 0, sizeof(ans));
        int lena = strlen(x.s + 1), lenb = strlen(y.s + 1), lenc = lena + lenb;
        for (int i = 1; i <= lena; i++) a[i] = x.s[lena - i + 1] - '0';
        for (int i = 1; i <= lenb; i++) b[i] = y.s[lenb - i + 1] - '0';
        for (int i = 1; i <= lena; i++) 
            for (int j = 1; j <= lenb; j++) 
                c[i + j - 1] += a[i] * b[j];
        for (int i = 1; i <= lenc; i++) c[i + 1] += c[i] / 10, c[i] %= 10;
        if (!c[lenc]) --lenc;
        for (int i = 1; i <= lenc; i++) ans[i] = c[lenc - i + 1] + '0';
        Bigint res = Bigint(ans);
        return res;
    }
} a, b;

Bigint read() {
    char s[N];
    memset(s, 0, sizeof(s));
    scanf("%s", s + 1);
    Bigint res = Bigint(s);
    return res;
}

void write(Bigint x) {
    printf("%s\n", x.s + 1);
}

int main() {
    a = read(), b = read();
    write(a + b);
    //write(a - b);
    //write(a * b);
    return 0;
}

4. 一维前缀和

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 50;

int n, q;
long long a[N], s[N];

int main() {
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for (int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i]; // 预处理 O(n)
    while (q--) {
        int x, y;
        scanf("%d%d", &x, &y);
        printf("%lld\n", s[y] - s[x - 1]); // O(1) 询问
    }
    return 0;
}
// 总时间复杂度 O(n + q)

5. 一维差分

#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 50;

int n, q;
long long a[N], c[N], s[N];

int main() {
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); // O(n) 读入
    for (int i = 1; i <= q; i++) {
        int x, y;
        long long z;
        scanf("%d%d%lld", &x, &y, &z);
        c[x] += z, c[y + 1] -= z;
    } // O(q) 差分
    for (int i = 1; i <= n; i++) s[i] = s[i - 1] + c[i];
    for (int i = 1; i <= n; i++) a[i] += s[i]; // 前缀和 O(n)
    for (int i = 1; i <= n; i++) printf("%lld ", a[i]); // O(1) 回答
    return 0;
}
// O(n + q)

6. 二维前缀和

#include <bits/stdc++.h>

using namespace std;

const int N = 1e3 + 50;

int n, m, q;
long long a[N][N], s[N][N];

int main() {
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) 
            scanf("%lld", &a[i][j]);
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) 
            s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
    while (q--) {
        int a, b, c, d;
        scanf("%d%d%d%d", &a, &b, &c, &d);
        printf("%lld\n", s[c][d] - s[c][b - 1] - s[a - 1][d] + s[a - 1][b - 1]);
    }
    return 0;
}
// 总时间复杂度 O(nm + q);

7. 二维差分

#include <bits/stdc++.h>

using namespace std;

const int N = 1e3 + 50;

int n, m, q;
long long v[N][N], e[N][N], s[N][N];

int main() {
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) 
            scanf("%lld", &v[i][j]);
    while (q--) {
        int a, b, c, d;
        long long z;
        scanf("%d%d%d%d%lld", &a, &b, &c, &d, &z);
        e[a][b] += z;
        e[a][d + 1] -= z;
        e[c + 1][b] -= z;
        e[c + 1][d + 1] += z;
    } // 差分 O(q)
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) 
            s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + e[i][j]; // 前缀和 O(nm)
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) 
            printf("%lld ", v[i][j] + s[i][j]);
        printf("\n");
    }
    return 0;
}
// 总时间复杂度 O(nm + q);

8. 01 背包

#include <bits/stdc++.h>

using namespace std;

const int N = 1e3 + 50;

int n, m, v[N], w[N], f[N];
string g[N];

int main() {
    scanf("%d%d", &m, &n);
    for (int i = 1; i <= n; i++) scanf("%d%d", &v[i], &w[i]);
    for (int i = 1; i <= n; i++) 
        for (int j = m; j >= v[i]; j--) 
            if (f[j - v[i]] + w[i] > f[j]) 
                f[j] = f[j - v[i]] + w[i], g[j] = g[j - v[i]] + char(i + '0'); // 01 背包模板
    printf("%d\n", f[m]);
    //int len = g[m].size();
    //for (int i = 0; i < len; i++) printf("%d ", g[m][i] - '0'); // 记录路径
    return 0;
}
// 时间 O(nm) 空间 O(nm)

9. 完全背包

#include <bits/stdc++.h>

using namespace std;

const int N = 1e3 + 50;

int n, m, v[N], w[N], f[N];

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%d%d", &v[i], &w[i]);
    for (int i = 1; i <= n; i++) 
        for (int j = v[i]; j <= m; j++) 
            f[j] = max(f[j], f[j - v[i]] + w[i]);
    printf("%d", f[m]);
    return 0;
}
// O(nm)

10. 多重背包

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 50;

int n, m, v[N], w[N], a[N], c[35], f[N];

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%d%d%d", &v[i], &w[i], &a[i]);
    for (int i = 1; i <= n; i++) {
        int x = 1, tot = 0;
        while (a[i]) {
            if (a[i] >= x) c[++tot] = x, a[i] -= x, x <<= 1;
            else c[++tot] = a[i], a[i] = 0;
        } // 二进制优化
        for (int j = 1; j <= tot; j++) 
            for (int k = m; k >= v[i] * c[j]; k--) 
                f[k] = max(f[k], f[k - v[i] * c[j]] + w[i] * c[j]);
    }
    printf("%d", f[m]);
    return 0;
}
// O(nmloga)

11. BFS

#include<bits/stdc++.h>

using namespace std;

const int maxn = 500;

int ans[maxn][maxn];
int n, m;

struct node {
    int x;
    int y;
};

int dx[8] = {1, 2, 2, 1, -1, -2, -2, -1}, dy[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
queue <node> q;

int main() {
    int x0, y0;
    cin >> n >> m >> x0 >> y0;
    memset(ans, -1, sizeof(ans));
    ans[x0][y0] = 0;
    node tp = {x0, y0};
    q.push(tp);
    // 初始化
    while (!q.empty()) {
        node u = q.front(); // 出队
        q.pop();
        for (int i = 0; i < 8; i++) { // 找邻居
            node s;
            s.x = u.x + dx[i];
            s.y = u.y + dy[i];
            if (s.x >= 1 && s.x <= n && s.y >= 1 && s.y <= m && ans[s.x][s.y] == -1) {
                ans[s.x][s.y] = ans[u.x][u.y] + 1;
                q.push(s); // 入队
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cout << ans[i][j] << "    ";
        }
        cout << endl;
    }
    return 0;
}
// P1443
// O(n + m)

12. DFS

#include <bits/stdc++.h>

using namespace std;

const int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};

struct node {
    int x;
    int y;
} ;

int n, m, X1, Y1, X2, Y2, mp[20][20], cnt = 0;

bool vis[20][20], flag = false;

node ans[200];

inline void dfs(int x,int y) { // 深搜,不想做解释
    if (x == X2 && y == Y2) {
        for (int i = 1; i <= cnt; i++) {
            printf("(%d,%d)", ans[i].x, ans[i].y);
            if (i != cnt) printf("->");
        }
        flag = true;
        printf("\n");
        return;
    }
    for (int i = 0; i < 4; i++) {
        int tx = x + dx[i], ty = y + dy[i];
        if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && mp[tx][ty] == 1 && vis[tx][ty] == false) {
            cnt++;
            ans[cnt] = {tx, ty};
            vis[tx][ty] = true;
            dfs(tx, ty);
            vis[tx][ty] = false;
            ans[cnt] = {0, 0};
            cnt--;  
        }
    }
    return;
}
int main(){
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) 
            scanf("%d", &mp[i][j]);
    scanf("%d%d%d%d", &X1, &Y1, &X2, &Y2);
    vis[X1][Y1] = true;
    cnt++;
    ans[cnt] = {X1, Y1};
    dfs(X1, Y1);
    if (!flag) printf("-1");
    return 0;
}
// P1238
// O(n + m)

13. 树的遍历 + 倍增 LCA

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 50;

int n, q;
vector <int> e[N];
int d[N];
int f[N][25];

inline void dfs(int x) {
    for (auto y : e[x]) 
        d[y] = d[x] + 1, dfs(y);
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i < n; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        e[x].push_back(y);
        f[y][0] = x;
    }
    d[1] = 1;
    dfs(1);
    for (int j = 1; j <= 20; j++) 
        for (int i = 1; i <= n; i++) 
            f[i][j] = f[f[i][j - 1]][j - 1];
    scanf("%d", &q);
    while (q--) {
        int x, y;
        scanf("%d%d", &x, &y);
        if (d[x] < d[y]) swap(x, y);
        for (int i = 0, c = d[x] - d[y]; c; i++) 
            if (c & (1 << i)) 
                x = f[x][i], c -= 1 << i;
        if (x == y) {
            printf("%d\n", x);
            continue;
        }
        for (int i = 20; i >= 0; i--) 
            if (f[x][i] != f[y][i]) 
                x = f[x][i], y = f[y][i];
        printf("%d\n", f[x][0]);
    }
    return 0;
}

如果有遗失或错误请在下方评论区回复我,我会更新。