Slope Trick学习笔记
算法介绍
凸函数:
定义函数
关键性质:
- 两个凸函数相加仍然是凸函数。
- 凸函数的前缀最小值/后缀最小值/区间最小值仍是凸函数。
分段线性函数:
若函数
定义断点表示斜率发生变化的地方,并记
Slope Trick 的核心思想
Slope Trick 是通过维护断点信息和斜率增量来表示整个凸函数的。只要已知最左侧的斜率和断点位置以及对应的斜率增量,就可以表示出任意位置的斜率
考虑用一个堆来维护断点信息,为了方便,可以将一个断点的增量拆成若干个单位增量加入集合。比如加入一个新的断点
批量修改堆中元素时,比如将一段连续的地方斜率加
例题
P4597 序列 sequence
模板题,优化前缀最小值的 DP,Slope Trick 最基础的运用。
给定一个序列
首先考虑朴素 DP,设
设
算法流程:
从小到大枚举
绝对值函数:
加上
前缀最小值函数:
可以形象的理解为将函数图像上斜率大于
在这道题中,因为加入绝对值函数后,最右侧的斜率恰好是 for 或 if。
怎么计算对答案的贡献呢?设
所以可以对每个
又因为
完整处理:
最后放一张 ChatGPT 老师的图片,用一个例子更好的理解 Slope Trick。
:::success[代码]
#include <bits/stdc++.h>
// #define MEMORY
using namespace std;
using LL = long long;
bool Mbe;
LL n, ans;
priority_queue<LL> q;
bool Med;
int main() {
#ifdef MEMORY
cout << (&Med - &Mbe) / 1048576.0 << "\n";
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (LL i = 1, x; i <= n; i++) {
cin >> x, q.push(x), q.push(x), ans += q.top() - x, q.pop();
}
cout << ans;
return 0;
}
:::
虽然代码很短,但是思维难度还是非常高的。
AT_arc070_c [ARC070E] NarrowRectangles
Slope Trick 优化区间最小值的 DP。
首先考虑朴素 DP,令
设
将之前的前缀最小值变为了区间最小值,同样的,可以设
然后就是怎么求区间最小值,设左侧最大点为
:::success[代码]
#include <bits/stdc++.h>
// #define MEMORY
using namespace std;
using LL = long long;
bool Mbe;
const int N = 1e5 + 5;
LL n, adl, adr, ans;
priority_queue<LL> ql;
priority_queue<LL, vector<LL>, greater<LL>> qr;
bool Med;
int main() {
#ifdef MEMORY
cout << (&Med - &Mbe) / 1048576.0 << "\n";
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n, ql.push(0), qr.push(1e18);
for (LL i = 1, l, r, w, lst = 0; i <= n; i++) {
cin >> l >> r, w = r - l;
lst && (adl -= w, adr += lst), lst = w;
LL x = adl + ql.top(), y = adr + qr.top();
if (l < x) {
ans += x - l;
ql.push(l - adl), ql.push(l - adl), ql.pop(), qr.push(x - adr);
} else if (l > y) {
ans += l - y;
qr.push(l - adr), qr.push(l - adr), qr.pop(), ql.push(y - adl);
} else {
ql.push(l - adl), qr.push(l - adr);
}
}
cout << ans;
return 0;
}
:::
练习
放了很多题,多做做加深理解。
P4331 [BalticOI 2004] Sequence (Day1)
前缀最小值 DP + 输出方案。
和第一道例题差不多一样的,不过需要求出改变的方案。先将严格递增转化为非降,可以将每一个数都减去
设第
:::success[代码]
#include <bits/stdc++.h>
// #define MEMORY
using namespace std;
using LL = long long;
bool Mbe;
const int N = 1e6 + 5;
LL n, ans, a[N];
priority_queue<LL> q;
bool Med;
int main() {
#ifdef MEMORY
cout << (&Med - &Mbe) / 1048576.0 << "\n";
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (LL i = 1, x; i <= n; i++) {
cin >> x, x -= i;
q.push(x), q.push(x), a[i] = q.top(), ans += q.top() - x, q.pop();
}
cout << ans << "\n";
for (int i = n - 1; i; i--) a[i] = min(a[i + 1], a[i]);
for (int i = 1; i <= n; i++) cout << a[i] + i << "\n";
return 0;
}
:::
AT_abc217_h [ABC217H] Snuketoon
一道区间最小值的拓展题。
和第二道例题很像,也是维护区间最小值。设
-
$$f_{i, x} = \begin{cases} X_i - x + g_{i - 1, x}, & x < X_i \\ g_{i - 1, x}, & x \ge X_i \end{cases}$$ -
$$f_{i, x} = \begin{cases} x - X_i + g_{i - 1, x}, & x > X_i \\ g_{i - 1, x}, & x \le X_i \end{cases}$$
这样就很套路了,技巧一样的,也是维护两个堆,稍微改一下形式就可以了。
需要注意一点,这道题要求从起点
:::success[代码]
#include <bits/stdc++.h>
// #define MEMORY
using namespace std;
using LL = long long;
bool Mbe;
const LL Inf = 1e18;
LL n, adl, adr, ans;
priority_queue<LL> ql;
priority_queue<LL, vector<LL>, greater<LL>> qr;
bool Med;
int main() {
#ifdef MEMORY
cout << (&Med - &Mbe) / 1048576.0 << "\n";
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) ql.push(0), qr.push(0);
for (LL i = 1, t, op, x, l, lst = 0; i <= n; i++) {
cin >> t >> op >> x;
l = t - lst, lst = t;
adl -= l, adr += l;
LL a = adl + ql.top(), b = adr + qr.top();
if (!op) {
if (x <= b) {
ql.push(x - adl);
} else {
ans += x - b;
qr.push(x - adr), qr.pop(), ql.push(b - adl);
}
} else {
if (x >= a) {
qr.push(x - adr);
} else {
ans += a - x;
ql.push(x - adl), ql.pop(), qr.push(a - adr);
}
}
}
cout << ans;
return 0;
}
:::
P4272 [CTSC2009] 序列变换
另一道区间最小值拓展题。
也是一道区间最小值的题目,和 ARC070E 很像,DP 部分基本上是相同的。设
前面的 DP 部分是一样的,关键在于怎么处理
时,不能满足条件,将
:::success[代码]
#include <bits/stdc++.h>
// #define MEMORY
using namespace std;
using LL = long long;
bool Mbe;
const int N = 5e5 + 5;
LL n, m, a, b, adl, adr, ans;
priority_queue<LL> ql;
priority_queue<LL, vector<LL>, greater<LL>> qr;
bool Med;
int main() {
#ifdef MEMORY
cout << (&Med - &Mbe) / 1048576.0 << "\n";
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> a >> b;
ql.push(-1e18), qr.push(1e18);
for (LL i = 1, x; i <= n; i++) {
cin >> x, adl += a, adr += b;
LL l = 1 + (i - 1) * a, r = m - (n - i) * a;
LL y = min(r, max(l, x));
ans += abs(x - y), x = y;
LL u = adl + ql.top(), v = adr + qr.top();
if (x < u) {
ans += u - x;
ql.push(x - adl), ql.push(x - adl), ql.pop(), qr.push(u - adr);
} else if (x > v) {
ans += x - v;
qr.push(x - adr), qr.push(x - adr), qr.pop(), ql.push(v - adl);
} else {
ql.push(x - adl), qr.push(x - adr);
}
}
cout << ans;
return 0;
}
:::
P3642 [APIO2016] 烟花表演
Slope Trick 优化树形 DP,和一些其他形式的转移(分类讨论)。
一道稍微复杂一点的题目,但也没有太难,多推一下可以自己弄出来的。首先可以树形 DP,设
设
这一步就是最关键的了,一样的维护一下断点信息,斜率变化量就行了,这是 trival 的。最后就是怎么计算答案的贡献了,会发现,所有
:::success[代码]
#include <bits/stdc++.h>
#include <bits/extc++.h>
// #define MEMORY
using namespace std;
using LL = long long;
bool Mbe;
const int N = 3e5 + 5;
const LL Inf = 1e18;
LL n, m, ans;
vector<pair<LL, LL>> e[N];
__gnu_pbds::priority_queue<LL> ql[N];
__gnu_pbds::priority_queue<LL, greater<LL>> qr[N];
void Dfs(int u) {
for (auto [v, w] : e[u]) {
Dfs(v);
LL a = ql[v].top(), b = qr[v].top();
ql[v].pop(), qr[v].clear();
ql[v].push(a + w), qr[v].push(b + w);
ql[u].join(ql[v]), qr[u].join(qr[v]);
}
for (; ql[u].size() && qr[u].size();) {
LL a = ql[u].top(), b = qr[u].top();
if (a <= b) break;
ans += a - b, ql[u].pop(), qr[u].pop();
ql[u].push(b), qr[u].push(a);
}
}
bool Med;
int main() {
#ifdef MEMORY
cout << (&Med - &Mbe) / 1048576.0 << "\n";
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (LL i = 2, fa, w; i <= n + m; i++) {
cin >> fa >> w, e[fa].push_back({i, w});
}
for (int i = n + 1; i <= n + m; i++) ql[i].push(0), qr[i].push(0);
Dfs(1), cout << ans;
return 0;
}
:::
其实这道题并不需要维护两个堆,因为只需要知道谷底的信息,而且谷底只有两个断点,对应的就是堆中的最大值和次大值。
P12479 [集训队互测 2024] 长野原龙势流星群
Slope Trick 优化树形 DP,和其他技巧。
稍微有一点技巧性的题。设
定义
这道题和前面的题不太一样,不是纯推公式,需要一些思考和技巧,值得做一下的。
:::success[代码]
#include <bits/stdc++.h>
#include <bits/extc++.h>
// #define MEMORY
using namespace std;
using LL = long long;
using Pii = pair<LL, LL>;
bool Mbe;
const int N = 2e5 + 5;
LL n, a[N]; double ans[N];
vector<int> e[N];
struct V {
LL sum, cnt;
friend bool operator<(V x, V y) {
return x.sum * 1.0 / x.cnt < y.sum * 1.0 / y.cnt;
}
};
__gnu_pbds::priority_queue<V> q[N];
void Dfs(int u) {
for (int v : e[u]) {
Dfs(v), q[u].join(q[v]);
}
LL sum = a[u], cnt = 1;
for (; q[u].size(); q[u].pop()) {
auto [s, c] = q[u].top();
if (s * 1.0 / c <= sum * 1.0 / cnt) break;
sum += s, cnt += c;
}
ans[u] = sum * 1.0 / cnt, q[u].push({sum, cnt});
}
bool Med;
int main() {
#ifdef MEMORY
cout << (&Med - &Mbe) / 1048576.0 << "\n";
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 2, fa; i <= n; i++) {
cin >> fa, e[fa].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> a[i];
Dfs(1);
for (int i = 1; i <= n; i++) {
cout << fixed << setprecision(6) << ans[i] << "\n";
}
return 0;
}
:::
P11678 [USACO25JAN] Watering the Plants P
一道不太一样的运用,维护每一条直线的信息+坐标反转。
挺好玩的一道题,没有之前的那些题那么套路。首先 DP,设
这个式子已经很经典了,一看到就知道是 Slope Trick。设
将
-
首先需要用
g_{i - 1} 求出h_x = g_{i - 1, w_i - x} ,因为没有往中间插入断点的操作,所以可以用一个双端队列,记录一个反转标记。和之前的题不太一样,这道题不是维护端点,而是维护断点分出来的每一条直线的信息。 -
然后需要加一个一次函数求出
f_i ,维护全局懒标记\mathrm{add}_k ,\mathrm{add_k} \gets \mathrm{add}_k + c_i 。 -
接下来就是求
g_i ,首先需要求后缀最小值。和最开始的那道例题一样,需要将左侧斜率小于0 的部分推平,也就是弹出队列。最后在最开始补上一段斜率为0 的线段即可。 -
将范围限制在恰好
[0, w_i] ,如果\mathrm{len} > w_i ,那么把右边超过w_i 的部分全部吃了;否则需要在[\mathrm{len}, w_i] 的地方补一段斜率为c_i 的直线。
注意在这些操作中都需要更新
:::success[代码]
#include <bits/stdc++.h>
// #define MEMORY
using namespace std;
using LL = long long;
using Pii = pair<LL, LL>;
bool Mbe;
const int N = 5e5 + 5;
LL n, m, w[N], c[N], len, bs, ts, adk;
deque<Pii> q;
bool rev;
Pii Get(Pii p) {
rev && (p.first *= -1), p.first += adk;
return p;
}
Pii Top(bool op) {
return Get((rev ^ op) ? q.back() : q.front());
}
void Push(LL k, LL l, bool op) {
if (!l) return;
Pii w = {(rev ? -1 : 1) * (k - adk), l};
(rev ^ op) ? q.push_back(w) : q.push_front(w);
len += l, ts += k * l;
}
void Pop(bool op) {
(rev ^ op) ? q.pop_back() : q.pop_front();
}
bool Med;
int main() {
#ifdef MEMORY
cerr << (&Med - &Mbe) / 1048576.0 << "\n";
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> w[i];
for (int i = 2; i <= n; i++) cin >> c[i];
bs = w[1] * c[2];
Push(0, min(w[1], w[2]), 1), Push(c[2], max(0ll, w[2] - w[1]), 1);
cout << bs + ts << "\n";
for (int i = 3; i <= n; i++) {
rev ^= 1;
bs += ts, ts = -ts + len * c[i], adk = -adk + c[i];
LL cnt = 0;
for (; q.size() && Top(0).first < 0; Pop(0)) {
auto [k, l] = Top(0);
len -= l, bs += k * l, ts -= k * l, cnt += l;
}
Push(0, cnt, 0);
for (; len > w[i];) {
auto [k, l] = Top(1);
Pop(1), Push(k, l - min(l, len - w[i]), 1), len -= l, ts -= k * l;
}
len < w[i] && (Push(c[i], w[i] - len, 1), 1);
cout << bs + ts << "\n";
}
return 0;
}
:::
P13954 [ICPC 2023 Nanjing R] 红黑树
维护斜率和对应位置相加的运用。
看上去就是 P3642 [APIO2016] 烟花表演 的一道双倍经验,但发现它只能将边权改为 0/1,所以之前的方法似乎不再适用。来重新分析这道问题:
设
所以可以发现,当
因为答案要求
这道题的价值就在于是利用图像来解题的,有时候数形结合会使许多问题更加简单。
:::success[代码]
#include <bits/stdc++.h>
// #define MEMORY
using namespace std;
using LL = long long;
bool Mbe;
const int N = 1e5 + 5;
int t, n, m, fa[N], bs[N], ts[N];
string s;
priority_queue<int, vector<int>, greater<int>> q[N];
bool Med;
int main() {
#ifdef MEMORY
cerr << abs(&Mbe - &Med) / 1048576.0 << "\n";
#endif
cin.tie(0)->sync_with_stdio(0);
for (cin >> t; t; t--) {
cin >> n >> s, s = " " + s;
for (int i = 1; i <= n; i++) {
q[i] = q[0], bs[i] = ts[i] = 0;
}
for (int i = 2; i <= n; i++) cin >> fa[i];
for (int v = n; v; v--) {
s[v] == '0' ? q[v].push(1) : (bs[v]++, ts[v]--, q[v].push(-1));
int u = fa[v];
if (!u) continue;
bs[u] += bs[v], ts[u] = 0;
if (q[u].empty()) {
swap(q[u], q[v]), ts[u] = ts[v];
continue;
}
auto tmp = q[0];
for (; q[u].size() && q[v].size(); q[u].pop(), q[v].pop()) {
int w = q[u].top() + q[v].top();
tmp.push(w), w < 0 && (ts[u] += w);
}
swap(q[u], tmp);
}
for (int i = 1; i <= n; i++) {
cout << bs[i] + ts[i] << " \n"[i == n];
}
}
return 0;
}
:::