跳到正文

休闲·娱乐

A+B冷门解法

EXR_FAL约 46 分钟
原文
文章信息与版本

文章信息

发布
2025年6月20日
收录
2026年4月15日
更新
2026年11月10日
篇幅
约 46 分钟

版本

  1. v2当前
  2. v1

前言

你说什么?问我为什么要写这依托东西?就是纯闲的

此处给出了题解区的没提到过的做法,而且亲测每种做法都能过,不能过的话踢我()

ODT

思路如下:

  1. 将a和b作为区间的值:创建两个区间,一个代表a,一个代表b。
    • 例如,区间[0, 1)的值为a,区间[1, 2)的值为b。
  2. 合并区间:将这两个区间合并,计算它们的值的和。
    • 例如,将区间[0, 2)的值设置为a + b。
  3. 输出结果:直接输出合并后的区间的值。

代码如下:

C++
// Ciallo~(∠・ω< )⌒★

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

inline ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * f;
}

inline void write(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
inline void out(ll x) {
    write(x);
    putchar('\n');
}

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const int N = 1e5 + 6;

#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define ls(x) ((x) << 1)
#define rs(x) ((x) << 1 | 1)
#define lowbit(x) ((x) & (-(x)))
#define debug(x) cout << #x << " = " << (x) << endl

struct Node {
    int l, r;
    mutable int v;
    Node(int l, int r = 0, int v = 0) : l(l), r(r), v(v) {}
    bool operator<(const Node &o) const { return l < o.l; }
};

set<Node> odt;

inline auto split(int pos) {
    auto it = odt.lower_bound(Node(pos));
    if (it != odt.end() && it->l == pos) return it;
    --it;
    int l = it->l, r = it->r, v = it->v;
    odt.erase(it);
    odt.insert(Node(l, pos - 1, v));
    return odt.insert(Node(pos, r, v)).first;
}

inline void assign(int l, int r, int v) {
    auto itr = split(r + 1), itl = split(l);
    odt.erase(itl, itr);
    odt.insert(Node(l, r, v));
}

int main() {
    int a = read(), b = read();

    odt.insert(Node(0, 1e9, 0));

    assign(0, 0, a);
    assign(1, 1, b);
    assign(0, 1, a + b);

    auto it = odt.lower_bound(Node(0));
    out(it->v);

    return 0;
}

动态DP

为了更符合动态 DP(Dynamic Dynamic Programming)的特点,我们可以将 A+B 问题建模为序列上的线性递推问题,并通过矩阵乘法来表示状态转移。虽然 A+B 问题本身极其简单,但我们可以强行构造一个矩阵转移的形式,以展示动态 DP 的核心思想。


动态 DP 通常用于解决带修改的动态规划问题,其核心思想是:

  1. 将 DP 转移表示为线性变换(通常是矩阵乘法)。
  2. 用数据结构(如线段树)维护转移矩阵,从而支持快速修改和查询。

虽然 A+B 问题不涉及修改,但我们仍然可以构造一个矩阵转移模型。


设 dp[i] 表示前 i 个数的和。对于 A + B,我们可以定义:

  • dp[0] = 0(初始状态)
  • dp[1] = dp[0] + A = A(第一步)
  • dp[2] = dp[1] + B = A + B(第二步)

我们可以将递推关系写成矩阵形式:

  • 初始向量: [dp[0]1]=[01]\begin{bmatrix} dp[0] \\ 1 \end{bmatrix} = \begin{bmatrix} 0 \\ 1 \end{bmatrix}
  • 转移矩阵(用于加上 A): MA=[1A01]M_A = \begin{bmatrix} 1 & A \\ 0 & 1 \end{bmatrix} 作用: [1A01]×[dp[i−1]1]=[dp[i−1]+A1]\begin{bmatrix} 1 & A \\ 0 & 1 \end{bmatrix} \times \begin{bmatrix} dp[i-1] \\ 1 \end{bmatrix} = \begin{bmatrix} dp[i-1] + A \\ 1 \end{bmatrix}
  • 转移矩阵(用于加上 B): MB=[1B01]M_B = \begin{bmatrix} 1 & B \\ 0 & 1 \end{bmatrix} 作用: [1B01]×[dp[i−1]1]=[dp[i−1]+B1]\begin{bmatrix} 1 & B \\ 0 & 1 \end{bmatrix} \times \begin{bmatrix} dp[i-1] \\ 1 \end{bmatrix} = \begin{bmatrix} dp[i-1] + B \\ 1 \end{bmatrix}

最终的 dp[2] 可以通过矩阵乘法计算:

[dp[2]1]=MB×MA×[dp[0]1]\begin{bmatrix} dp[2] \\ 1 \end{bmatrix} = M_B \times M_A \times \begin{bmatrix} dp[0] \\ 1 \end{bmatrix}

计算过程:

  1. 先乘 M_A: MA×[01]=[0+A1]=[A1]M_A \times \begin{bmatrix} 0 \\ 1 \end{bmatrix} = \begin{bmatrix} 0 + A \\ 1 \end{bmatrix} = \begin{bmatrix} A \\ 1 \end{bmatrix}
  2. 再乘 M_B: MB×[A1]=[A+B1]M_B \times \begin{bmatrix} A \\ 1 \end{bmatrix} = \begin{bmatrix} A + B \\ 1 \end{bmatrix}

最终得到 dp[2] = A + B。


代码如下:

C++
// Ciallo~(∠・ω< )⌒★

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

inline ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * f;
}

inline void write(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
inline void out(ll x) {
    write(x);
    putchar('\n');
}

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const int N = 1e5 + 6;

#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define ls(x) ((x) << 1)
#define rs(x) ((x) << 1 | 1)
#define lowbit(x) ((x) & (-(x)))
#define debug(x) cout << #x << " = " << (x) << endl

struct Matrix {
    int a, b, c, d;
    Matrix(int a = 1, int b = 0, int c = 0, int d = 1) : a(a), b(b), c(c), d(d) {}

    inline Matrix operator*(const Matrix &other) const {
        return Matrix(
            a * other.a + b * other.c,
            a * other.b + b * other.d,
            c * other.a + d * other.c,
            c * other.b + d * other.d
        );
    }
};

int main() {
    int A = read(), B = read();

    Matrix MA(1, A, 0, 1);
    Matrix MB(1, B, 0, 1);

    constexpr int dp0 = 0, one = 1;
    int dp1 = MA.a * dp0 + MA.b * one;
    int dp2 = MB.a * dp1 + MB.b * one;

    out(dp2);

    return 0;
}

K-D Tree(平面最近点对

思路如下:

  1. 将输入的a和b视为二维空间中的点(a,b)
  2. 构建一个2D树存储这些点
  3. 查询所有点并计算它们的和

代码如下:

C++
// Ciallo~(∠・ω< )⌒★

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

inline ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * f;
}

inline void write(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
inline void out(ll x) {
    write(x);
    putchar('\n');
}

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const int N = 5e5 + 6;

#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define ls(x) ((x) << 1)
#define rs(x) ((x) << 1 | 1)
#define lowbit(x) ((x) & (-(x)))
#define debug(x) cout << #x << " = " << (x) << endl

struct Point {
    int x[2], w;
} pt[N];

int n, idx, rt;
int buf[N], tot;

struct Node {
    int l, r;
    Point p;
    int L[2], R[2];
    int sum, sz;
} tr[N];

inline int new_node() {
    return tot ? buf[tot--] : ++idx;
}

inline void push_up(int u) {
    auto &L = tr[tr[u].l], &R = tr[tr[u].r];
    tr[u].sum = tr[u].p.w + L.sum + R.sum;
    tr[u].sz = L.sz + R.sz + 1;
    for (int i = 0; i < 2; i++) {
        tr[u].L[i] = min({tr[u].p.x[i], L.L[i], R.L[i]});
        tr[u].R[i] = max({tr[u].p.x[i], L.R[i], R.R[i]});
    }
}

const double Alpha = 0.72;

void flatten(int u, int cnt) {
    if (tr[u].l) flatten(tr[u].l, cnt);
    buf[++tot] = u;
    pt[tr[tr[u].l].sz + 1 + cnt] = tr[u].p;
    if (tr[u].r) flatten(tr[u].r, cnt + tr[tr[u].l].sz + 1);
}

int rebuild(int l, int r, int dim) {
    if (l > r) return 0;
    int mid = (l + r) >> 1, u = new_node();
    nth_element(pt + l, pt + mid, pt + r + 1,
        [dim](const Point &a, const Point &b) {
            return a.x[dim] < b.x[dim];
        });
    tr[u].p = pt[mid];
    tr[u].l = rebuild(l, mid - 1, dim ^ 1);
    tr[u].r = rebuild(mid + 1, r, dim ^ 1);
    push_up(u);
    return u;
}

inline void maintain(int &u, int dim) {
    if (tr[u].sz * Alpha < max(tr[tr[u].l].sz, tr[tr[u].r].sz)) {
        flatten(u, 0);
        u = rebuild(1, tot, dim);
    }
}

void insert(int &u, const Point &p, int dim) {
    if (!u) {
        u = new_node();
        tr[u].l = tr[u].r = 0;
        tr[u].p = p;
        push_up(u);
        return;
    }
    if (p.x[dim] <= tr[u].p.x[dim])
        insert(tr[u].l, p, dim ^ 1);
    else
        insert(tr[u].r, p, dim ^ 1);
    push_up(u);
    maintain(u, dim);
}

inline bool in_range(const Node &t, int x1, int y1, int x2, int y2) {
    return t.L[0] >= x1 && t.R[0] <= x2 &&
           t.L[1] >= y1 && t.R[1] <= y2;
}

inline bool point_in(const Point &t, int x1, int y1, int x2, int y2) {
    return t.x[0] >= x1 && t.x[0] <= x2 &&
           t.x[1] >= y1 && t.x[1] <= y2;
}

inline bool out_range(const Node &t, int x1, int y1, int x2, int y2) {
    return t.R[0] < x1 || t.L[0] > x2 ||
           t.R[1] < y1 || t.L[1] > y2;
}

int query(int u, int x1, int y1, int x2, int y2) {
    if (!u) return 0;
    if (in_range(tr[u], x1, y1, x2, y2)) return tr[u].sum;
    if (out_range(tr[u], x1, y1, x2, y2)) return 0;
    int res = 0;
    if (point_in(tr[u].p, x1, y1, x2, y2))
        res += tr[u].p.w;
    res += query(tr[u].l, x1, y1, x2, y2);
    res += query(tr[u].r, x1, y1, x2, y2);
    return res;
}

int main() {
    tr[0].L[0] = tr[0].L[1] = INF;
    tr[0].R[0] = tr[0].R[1] = -INF;

    int a = read(), b = read();

    insert(rt, {a, 0, a}, 0);
    insert(rt, {0, b, b}, 0);

    int sum = query(rt, -INF, -INF, INF, INF);
    out(sum);

    return 0;
}

分治(平面最近点对

其实想到了KDT后自然就有分治了()

代码如下:

C++
// Ciallo~(∠・ω< )⌒★

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

inline ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * f;
}

inline void write(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
inline void out(ll x) {
    write(x);
    putchar('\n');
}

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const int N = 1e5 + 6;

#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define ls(x) ((x) << 1)
#define rs(x) ((x) << 1 | 1)
#define lowbit(x) ((x) & (-(x)))
#define debug(x) cout << #x << " = " << (x) << endl

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) {}
};

inline bool compareX(const Point& a, const Point& b) {
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}

inline int distSq(const Point& p1, const Point& p2) {
    int dx = p1.x - p2.x, dy = p1.y - p2.y;
    return dx * dx + dy * dy;
}

inline int bruteForce(const vector<Point>& points, int left, int right) {
    int minDist = INF;
    for (int i = left; i <= right; ++i) {
        for (int j = i + 1; j <= right; ++j) {
            minDist = min(minDist, distSq(points[i], points[j]));
        }
    }
    return minDist;
}

inline int stripClosest(vector<Point>& strip, int d) {
    int minDist = d;
    sort(all(strip), [](const Point& a, const Point& b) {
        return a.y < b.y;
    });

    for (int i = 0; i < strip.size(); ++i) {
        for (int j = i + 1; j < strip.size() && (strip[j].y - strip[i].y) < minDist; ++j) {
            minDist = min(minDist, distSq(strip[i], strip[j]));
        }
    }
    return minDist;
}

int closestUtil(vector<Point>& points, int left, int right) {
    if (right - left <= 3) {
        return bruteForce(points, left, right);
    }

    int mid = (left + right) >> 1;
    Point midPoint = points[mid];

    int dl = closestUtil(points, left, mid);
    int dr = closestUtil(points, mid + 1, right);
    int d = min(dl, dr);

    vector<Point> strip;
    for (int i = left; i <= right; ++i) {
        int dx = points[i].x - midPoint.x;
        if (dx * dx < d) {
            strip.push_back(points[i]);
        }
    }

    return min(d, stripClosest(strip, d));
}

inline int closestPair(vector<Point>& points) {
    sort(all(points), compareX);
    return closestUtil(points, 0, points.size() - 1);
}

int main() {
    int a = read(), b = read();

    vector<Point> points;
    points.emplace_back(a, 0);
    points.emplace_back(-b, 0);

    int minDistSq = closestPair(points);
    int sum = sqrt(minDistSq);

    out(sum);

    return 0;
}

数值积分(辛普森法

思路如下: 可以将加法看作是在区间[0,1]上对常数函数(a+b)进行积分,然后使用辛普森法来”计算”这个积分值。

代码如下:

C++
// Ciallo~(∠・ω< )⌒★

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

inline ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * f;
}

inline void write(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
inline void out(ll x) {
    write(x);
    putchar('\n');
}

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1.0);
const int N = 1e5 + 6;

#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define ls(x) ((x) << 1)
#define rs(x) ((x) << 1 | 1)
#define lowbit(x) ((x) & (-(x)))
#define debug(x) cout << #x << " = " << (x) << endl

inline double calc(const function<double(double)>& f, double a, double b, int n = 1000) {
    double h = (b - a) / n;
    double sum = f(a) + f(b);

    for (int i = 1; i < n; i += 2) {
        sum += 4.0 * f(a + i * h);
    }
    for (int i = 2; i < n; i += 2) {
        sum += 2.0 * f(a + i * h);
    }

    return sum * h / 3.0;
}

int main() {
    int a = read(), b = read();

    auto func = [a, b](double x) {
        return static_cast<double>(a + b);
    };

    double res = calc(func, 0.0, 1.0);
    int rres = static_cast<int>(round(res));

    out(rres);

    return 0;
}

线性规划(单纯形法

思路如下:

设变量为x和y,表示a和b的值。我们需要最大化x+y,同时约束x=a和y=b。

将A+B问题建模为线性规划:

最大化 x + y 约束: x = a, y = b 转换为标准形式: 最大化 x + y 约束: x ≤ a, -x ≤ -a y ≤ b, -y ≤ -b

代码如下:

C++
// Ciallo~(∠・ω< )⌒★

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

inline ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * f;
}

inline void write(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
inline void out(ll x) {
    write(x);
    putchar('\n');
}

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-6;
const int N = 1e5 + 6;

#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define ls(x) ((x) << 1)
#define rs(x) ((x) << 1 | 1)
#define lowbit(x) ((x) & (-(x)))
#define debug(x) cout << #x << " = " << (x) << endl

class SimplexSolver {
private:
    vector<vector<double>> table;
    int m, n;

    inline void pivot(int p, int q) {
        double pivot_val = table[p][q];
        for (int j = 0; j <= n + m; ++j) {
            table[p][j] /= pivot_val;
        }

        for (int i = 0; i <= m; ++i) {
            if (i != p && abs(table[i][q]) > EPS) {
                double factor = table[i][q];
                for (int j = 0; j <= n + m; ++j) {
                    table[i][j] -= factor * table[p][j];
                }
            }
        }
    }

public:
    SimplexSolver(const vector<vector<double>>& A, const vector<double>& b, const vector<double>& c) {
        m = b.size();
        n = c.size();
        table.resize(m + 1, vector<double>(n + m + 1, 0.0));

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                table[i][j] = A[i][j];
            }
            table[i][n + i] = 1.0;
            table[i][n + m] = b[i];
        }

        for (int j = 0; j < n; ++j) {
            table[m][j] = c[j];
        }
    }

    void solve() {
        while (true) {
            int q = 0;
            for (int j = 1; j < n + m; ++j) {
                if (table[m][j] > table[m][q]) {
                    q = j;
                }
            }

            if (table[m][q] <= EPS) break;

            int p = -1;
            double min_ratio = numeric_limits<double>::max();
            for (int i = 0; i < m; ++i) {
                if (table[i][q] > EPS) {
                    double ratio = table[i][n + m] / table[i][q];
                    if (ratio < min_ratio) {
                        min_ratio = ratio;
                        p = i;
                    }
                }
            }

            if (p == -1) return;
            pivot(p, q);
        }
    }

    inline double getObjectiveValue() const {
        return -table[m][n + m];
    }

    vector<double> getSolution() const {
        vector<double> x(n, 0.0);
        for (int j = 0; j < n; ++j) {
            bool is_basic = false;
            int p = -1;
            for (int i = 0; i < m; ++i) {
                if (abs(table[i][j] - 1.0) < EPS) {
                    if (p == -1) p = i;
                    else { is_basic = false; break; }
                } else if (abs(table[i][j]) > EPS) {
                    is_basic = false;
                    break;
                }
            }
            if (p != -1) x[j] = table[p][n + m];
        }
        return x;
    }
};

int main() {
    int a = read(), b = read();

    vector<vector<double>> A = {
        {1.0, 0.0},  // x ≤ a
        {-1.0, 0.0}, // -x ≤ -a
        {0.0, 1.0},  // y ≤ b
        {0.0, -1.0}  // -y ≤ -b
    };

    vector<double> B = {static_cast<double>(a), static_cast<double>(-a),
                        static_cast<double>(b), static_cast<double>(-b)};
    vector<double> C = {1.0, 1.0};

    SimplexSolver solver(A, B, C);
    solver.solve();

    out(static_cast<int>(round(solver.getObjectiveValue())));

    return 0;
}

生成函数

思路如下:

将整数a和b分别表示为生成函数:G₁(x) = a·x⁰, G₂(x) = b·x⁰

将两个生成函数相加:G(x) = G₁(x) + G₂(x) = (a+b)·x⁰

提取x⁰项的系数,即为a+b的结果

代码如下:

C++
// Ciallo~(∠・ω< )⌒★

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

inline ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * f;
}

inline void write(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
inline void out(ll x) {
    write(x);
    putchar('\n');
}

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const int N = 1e5 + 6;

#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define ls(x) ((x) << 1)
#define rs(x) ((x) << 1 | 1)
#define lowbit(x) ((x) & (-(x)))
#define debug(x) cout << #x << " = " << (x) << endl

class GeFunc {
private:
    vi vec;
public:
    explicit GeFunc(int value = 0) {
        vec.push_back(value);
    }

    GeFunc operator+(const GeFunc& other) const {
        GeFunc result;
        result.vec[0] = this->vec[0] + other.vec[0];
        return result;
    }

    inline int get() const {
        return vec.empty() ? 0 : vec.front();
    }
};

int main() {
    int a = read(), b = read();

    GeFunc gf1(a);
    GeFunc gf2(b);

    GeFunc sum = gf1 + gf2;

    out(sum.get());

    return 0;
}

析合树

思路如下:

  1. 将输入的两个数a和b视为一个长度为2的数组

  2. 对这个数组构建析合树

  3. 在析合树中,我们将”合节点”定义为子节点值的和

  4. 最终根节点的值就是a+b的结果

代码如下:

C++
// Ciallo~(∠・ω< )⌒★

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

inline ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch - '0');
        ch = getchar();
    }
    return x * f;
}

inline void write(ll x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
inline void out(ll x) {
    write(x);
    putchar('\n');
}

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const int N = 1e5 + 6;

#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define all(x) (x).begin(), (x).end()
#define ls(x) ((x) << 1)
#define rs(x) ((x) << 1 | 1)
#define lowbit(x) ((x) & (-(x)))
#define debug(x) cout << #x << " = " << (x) << endl

struct DCTNode {
    int l, r;
    int val;
    bool is;
    DCTNode *left, *right;
    DCTNode(int l, int r, int val, bool is)
        : l(l), r(r), val(val), is(is), left(nullptr), right(nullptr) {}
};

class DecompositionTree {
private:
    DCTNode* root;
    vi data;

    DCTNode* build(int l, int r) {
        if (l == r) {
            return new DCTNode(l, r, data[l], false);
        }

        int mid = (l + r) >> 1;
        DCTNode* left = build(l, mid);
        DCTNode* right = build(mid + 1, r);

        int sum = left->val + right->val;
        DCTNode* node = new DCTNode(l, r, sum, true);
        node->left = left;
        node->right = right;

        return node;
    }

    void destroy(DCTNode* node) {
        if (!node) return;
        destroy(node->left);
        destroy(node->right);
        delete node;
    }

public:
    explicit DecompositionTree(const vi& arr) : data(arr) {
        root = arr.empty() ? nullptr : build(0, (int)arr.size() - 1);
    }

    ~DecompositionTree() {
        destroy(root);
    }

    inline int get_sum() const {
        return root ? root->val : 0;
    }

    void print_tr(DCTNode* node, int indent = 0) const {
        if (!node) return;

        for (int i = 0; i < indent; ++i) putchar(' ');
        printf("[%d,%d]: %d", node->l, node->r, node->val);
        if (node->is) printf(" (Merge)");
        putchar('\n');

        print_tr(node->left, indent + 4);
        print_tr(node->right, indent + 4);
    }

    inline DCTNode* get_root() const { return root; }
};

int main() {
    int a = read(), b = read();

    vi arr = {a, b};
    DecompositionTree dt(arr);

    out(dt.get_sum());

    return 0;
}
原文