跳到正文

题解

P1253 扶苏的问题 题解

huxint约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年10月25日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 2 分钟

版本

  1. v1当前

写的时候不知道怎么来维护点的属性,受到别人的启发,想到使用矩阵来推导

添加数 xx

(v1)×(10x1)=(v+x1) \begin{pmatrix} v & 1 \end{pmatrix} \times \begin{pmatrix} 1 & 0\\ x & 1 \end{pmatrix} = \begin{pmatrix} v + x & 1 \end{pmatrix}

设置为 xx

(v1)×(00x1)=(x1) \begin{pmatrix} v & 1 \end{pmatrix} \times \begin{pmatrix} 0 & 0\\ x & 1 \end{pmatrix} = \begin{pmatrix} x & 1 \end{pmatrix}

注意是右乘矩阵

主要的设计代码如下

C++
using Info = std::array<i64, 2>;
using Function = std::array<i64, 4>;
auto mapping = [&](Function f, Info x) -> Info {
    return Info{x[0] * f[0] + x[1] * f[2], x[0] * f[1] + x[1] * f[3]};
};
auto composition = [&](Function f, Function g) -> Function {
    return Function{g[0] * f[0] + g[1] * f[2], g[0] * f[1] + g[1] * f[3], g[2] * f[0] + g[3] * f[2], g[2] * f[1] + g[3] * f[3]};
};
auto op = [&](Info lhs, Info rhs) -> Info {
    return Info{std::max(lhs[0], rhs[0]), 1};
};
auto e = [&]() -> Info {
    return Info{-inf64, 0};
};
auto id = [&]() -> Function {
    return Function{1, 0, 0, 1};
};
int n, q;
std::cin >> n >> q;
SegmentTree::LazySegTree<Info, Function, mapping, composition, op, e, id> seg(n, [&](auto ...) {
    Info x;
    std::cin >> x[0];
    x[1] = 1;
    return x;
});
原文