李超线段树
Grace2022
·
·
算法·理论
作用
在平面直角坐标系里的 n 条线段或直线中,找到与 x = p 相交的纵坐标最大 / 最小的线。支持动态插入查询,时间复杂度是 \log 或 \log ^2 级别的。
李超线段树的实现
我们由 P4097 【模板】李超线段树 / [HEOI2013] Segment 引入李超线段树。
::::info[P4097 题意]
要求在平面直角坐标系下维护两个操作:
- 在平面上加入一条线段,线段的端点分别为 (x_0, y_0)、(x_1, y_1)。记第 i 条被插入的线段的标号为 i。
- 给定一个数 k,询问与直线 x = k 相交的线段中,交点纵坐标最大的线段的编号。若不存在任何一条线段与查询直线有交,则输出 0;若有多条线段与查询直线的交点纵坐标都是最大的,则输出编号最小的线段。
强制在线。
不保证 $x_0 \neq x_1$。对于一条 $x_0' = x_1'$ 的线段,认为其与 $x = x_0'$ 的交点为其两端点中纵坐标较大的端点。
::::
### 实现
观察到以下性质:
- 由于不涉及删除 / 撤销 / 修改等操作,若一条线段 $i$ 在横坐标区间 $[l, r]$ 不优于线段 $j$,那么在后续的插入查询过程中 $i$ 一定不会成为答案。
- 在横坐标都覆盖了 $[l, r]$ 的前提下,线段 $i$ 与线段 $j$ 只可能有两种情况:
- 不相交。此时一定有一条线段完全优于另一条线段。
- 相交。除非线段竖直,此时一定有交点左边和交点右边的优劣情况不同。
由于插入线段涉及到横坐标上的区间修改,查询类似单点查询,我们使用线段树解决。
一个线段树区间分为三个特殊点:$l$、$r$、$mid$。考虑在线段树区间上记录与 $x = mid$ 交点最高的线段编号,如果没有则记为 $0$。
设线段树区间的根节点为 $rt$,其所记录的线段编号为 $tree_{rt}$。
现在新加入了一条线段 $i$,我们找到 $i$ 覆盖的区间 $[l, r]$。
假设 $i$ 在区间 $[l, r]$ 上与 $x = mid$ 的交点劣于 $tree_{rt}$(如果不劣,交换两者)。我们需要找到 $i$ 与 $tree_{rt}$ 的交点,这样才能找到 $i$ 优的部分(如果有交点,这一部分一定包含了 $l$ 或 $r$)。
考虑上述两条线段的两种情况。判断 $i$ 与 $tree_{rt}$ 和 $x = l$ 与 $x = r$ 的优劣关系,至多有一边优。
如果有一边优,这里设 $i$ 与 $x = l$ 更优,$x = r$ 同理。那么线段 $i$ 在 $[l, mid]$ 的范围内与 $tree_{rt}$ 产生了交点,递归 $[l, mid]$。
如果两边都劣,说明 $i$ 永远不可能成为答案,停止递归。
在上述修改找交点的过程中,由于只会向一边递归,时间复杂度 $O(\log n)$:
```cpp
inline double calc(int id, int x){
return x * p[id].k + p[id].b;
}
void upd(int rt, int l, int r, int id){
if(tree[rt] == 0){
tree[rt] = id;
return;
}
int mid = l + r >> 1;
if(calc(tree[rt], mid) < calc(id, mid)) swap(tree[rt], id); //id是该区间内不优的
if(calc(tree[rt], l) < calc(id, l)) upd(rt << 1, l, mid, id);
if(calc(tree[rt], r) < calc(id, r)) upd(rt << 1 | 1, mid + 1, r, id);
}
```
但是,我们还需要找到线段覆盖的所有区间,所以还会再乘一个 $O(\log n)$:
```cpp
void Update(int L, int R, int C, int rt, int l, int r){
if(L <= l && r <= R){ //只递归被线段覆盖的部分
upd(rt, l, r, C);
return;
}
int mid = l + r >> 1;
if(L <= mid) Update(L, R, C, rt << 1, l, mid);
if(mid < R) Update(L, R, C, rt << 1 | 1, mid + 1, r);
}
```
对于查询,本身是一个很普通的单点查询。但是由于我们用到了**标记永久化**的思想(区间修改没有懒标记,不下传不上传),我们需要对递归到的每一个线段树区间取 $\min$ 作为答案。因为由观察到的第一点性质,我们在修改时只会递归到第一个“最优区间”并更新这个线段树节点,但是其对其以下的节点也有可能是更优的(比如,一条线段凌驾于所有区间之上,就会更新于最长的那个区间),所以需要对所有包含 $x$ 的线段树区间取 $\min$。
单点修改时间复杂度 $O(\log n)$:
```cpp
#define PDI pair<double, int> //最大纵坐标,线段编号
#define eps 1e-9
inline PDI pmax(PDI x, PDI y){ //由于本题在取等的情况下还要判断大小,避免精度问题需要另写一个函数
if(fabs(x.first - y.first) <= eps){
if(x.second < y.second) return x;
return y;
}
return max(x, y);
}
PDI Query(int L, int rt, int l, int r){
if(l == r) return (PDI){calc(tree[rt], L), tree[rt]};
int mid = l + r >> 1;
PDI Ans = {calc(tree[rt], L), tree[rt]}; //标记永久化要一路取min
if(L <= mid) Ans = pmax(Ans, Query(L, rt << 1, l, mid));
else Ans = pmax(Ans, Query(L, rt << 1 | 1, mid + 1, r));
return Ans;
}
```
以上就是李超线段树的实现了,插入线段的时间复杂度是 $O(\log ^2 n)$,查询是 $O(\log n)$。
若插入直线,相当于一条覆盖完整个区间的线段,就不需要外层的那一个 ```Update``` 函数找线段覆盖的区间了,插入和查询时间复杂度都是 $O(\log n)$。
::::success[P4097 AC代码]
本题小细节:
- 竖直线段的斜率和截距怎么算?这相当于 $y = 0x + \max(y_0, y_1)$。
- 精度问题。
- 插入的线段编号需要另算。
```cpp
#include<bits/stdc++.h>
using namespace std;
#define PDI pair<double, int>
#define LL long long
#define y0 ushafoaertiopisgjd
#define y1 sdfgnlisjergnsifjg
#define eps 1e-9
const int Mod1 = 39989, Mod2 = 1e9, N = 1e5 + 5;
struct line{
double k, b;
}p[N];
int tree[N << 2];
inline double calc(int id, int x){
return x * p[id].k + p[id].b;
}
void upd(int rt, int l, int r, int id){
if(tree[rt] == 0){
tree[rt] = id;
return;
}
int mid = l + r >> 1;
if(calc(tree[rt], mid) < calc(id, mid) + eps) swap(tree[rt], id);
if(calc(tree[rt], l) < calc(id, l) + eps) upd(rt << 1, l, mid, id);
if(calc(tree[rt], r) < calc(id, r) + eps) upd(rt << 1 | 1, mid + 1, r, id);
}
void Update(int L, int R, int C, int rt, int l, int r){
if(L <= l && r <= R){
upd(rt, l, r, C);
return;
}
int mid = l + r >> 1;
if(L <= mid) Update(L, R, C, rt << 1, l, mid);
if(mid < R) Update(L, R, C, rt << 1 | 1, mid + 1, r);
}
inline PDI pmax(PDI x, PDI y){
if(fabs(x.first - y.first) <= eps){
if(x.second < y.second) return x;
return y;
}
return max(x, y);
}
PDI Query(int L, int rt, int l, int r){
if(l == r) return (PDI){calc(tree[rt], L), tree[rt]};
int mid = l + r >> 1;
PDI Ans = {calc(tree[rt], L), tree[rt]};
if(L <= mid) Ans = pmax(Ans, Query(L, rt << 1, l, mid));
else Ans = pmax(Ans, Query(L, rt << 1 | 1, mid + 1, r));
return Ans;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, lasans = 0, cnt = 0;
cin >> n;
for(int i = 1, o; i <= n; ++i){
cin >> o;
if(o == 0){
int k; cin >> k;
lasans = Query((k + lasans - 1) % Mod1 + 1, 1, 1, Mod1).second;
cout << lasans << '\n';
}
else{
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
x0 = (x0 + lasans - 1) % Mod1 + 1, x1 = (x1 + lasans - 1) % Mod1 + 1;
y0 = (y0 + lasans - 1) % Mod2 + 1, y1 = (y1 + lasans - 1) % Mod2 + 1;
if(x0 > x1) swap(x0, x1), swap(y0, y1);
if(x0 == x1){
p[++cnt].k = 0; p[cnt].b = max(y0, y1);
}
else{
p[++cnt].k = 1.0 * (y1 - y0) / (x1 - x0);
p[cnt].b = y0 - p[cnt].k * x0;
}
Update(x0, x1, cnt, 1, 1, Mod1);
}
}
return 0;
}
```
::::
---
### 练习
请完成:
- [P4097 【模板】李超线段树 / [HEOI2013] Segment](https://www.luogu.com.cn/problem/P4097),模板。
- [P4254 [JSOI2008] Blue Mary 开公司](https://www.luogu.com.cn/problem/P4254),换壳模板。
- [P3081 [USACO13MAR] Hill Walk G](https://www.luogu.com.cn/problem/P3081),需简单转化,动态开点李超线段树,[题解](https://www.luogu.com.cn/article/hslhvko8)。
## 李超线段树的合并
按照常规的[线段树合并](https://www.luogu.com.cn/article/l04znkar)套路,在动态开点的线段树中,递归合并根 $u$ 和根 $v$ 的每一个节点,如果其中有一个是空或者碰到叶子节点了就返回,正常合并左右子树即可。只不过这里的“线段树”是李超线段树,合并的方式需要按照李超线段树的更新方式,即调用一遍 ```Update``` 函数,把 $v$ 节点的信息合并到 $u$。
```cpp
int Merge(int u, int v, int l, int r){ //v 合并到 u
if(u == 0 || v == 0) return u + v;
Update(tree[v].id, u, l, r);
if(l == r) return u; //这里叶子节点的更新方式与其它节点相同,所以在更新后直接返回即可
int mid = l + r >> 1;
tree[u].ls = Merge(tree[u].ls, tree[v].ls, l, mid);
tree[u].rs = Merge(tree[u].rs, tree[v].rs, mid + 1, r);
return u;
}
```
关于时间复杂度,一共有 $n$ 条**直线**(线段分析同理),由李超线段树的性质(标记永久化,且满足 $x = mid$ 交点最高并存入线段树节点的直线只有一条):
- 每条直线只会被存于一个线段树节点。
- 一个线段树节点只会存一条线。
合并、更新的过程中,“劣”直线会一直和优直线交换,直到交换到线段树的最后一层(或者到空节点),而线段树的高度是 $\log n$ 的,每条直线最多换 $\log n$ 次,所以总时间复杂度为 $O(n \log n)$。
### 练习
- [CF932F Escape Through Leaf](https://www.luogu.com.cn/problem/CF932F),转移方程:$f_u \gets \min(f_v + a_u \times b_v)$,其中 $v$ 是 $u$ 子树的所有节点。把 $b_v$ 当斜率,$f_v$ 当截距,最后求与 $x = a_u$ 相交的最小交点。李超线段树合并,把儿子合并到父亲即可。注意这里由于是动态开点,没必要偏移可能为负的横坐标(因为很麻烦)。
::::success[CF932F AC代码]
```cpp
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ld long double
const int N = 1e5 + 5, M = 2e5 + 1, Base = 1e5 + 1;
int cntd;
int a[N], b[N], root[N];
vector<int> e[N];
struct line{
LL k, b;
}p[N];
struct node{
int ls, rs, id;
}tree[M * 50];
LL f[N];
inline LL calc(int id, int x){
if(id == 0) return 1e18;
return (LL)p[id].k * x + p[id].b;
}
void Update(int id, int &rt, int l, int r){
if(rt == 0) rt = ++cntd;
if(tree[rt].id == 0){
tree[rt].id = id;
return;
}
int mid = l + r >> 1;
if(calc(id, mid) < calc(tree[rt].id, mid)) swap(tree[rt].id, id);
if(calc(id, l) < calc(tree[rt].id, l)) Update(id, tree[rt].ls, l, mid);
if(calc(id, r) < calc(tree[rt].id, r)) Update(id, tree[rt].rs, mid + 1, r);
}
pair<LL, int> Query(int x, int rt, int l, int r){
if(rt == 0) return {1e18, 0};
if(l == r) return {calc(tree[rt].id, x), tree[rt].id};
pair<LL, int> Ans = {calc(tree[rt].id, x), tree[rt].id};
int mid = l + r >> 1;
if(x <= mid) Ans = min(Ans, Query(x, tree[rt].ls, l, mid));
else Ans = min(Ans, Query(x, tree[rt].rs, mid + 1, r));
return Ans;
}
int Merge(int u, int v, int l, int r){
if(u == 0 || v == 0) return u + v;
Update(tree[v].id, u, l, r);
if(l == r) return u;
int mid = l + r >> 1;
tree[u].ls = Merge(tree[u].ls, tree[v].ls, l, mid);
tree[u].rs = Merge(tree[u].rs, tree[v].rs, mid + 1, r);
return u;
}
void dfs(int u, int fa){
for(int v : e[u]){
if(v == fa) continue;
dfs(v, u);
root[u] = Merge(root[u], root[v], -Base, Base);
}
pair<LL, int> Ans = Query(a[u], root[u], -Base, Base);
if(e[u].size() == 1 && u != 1) f[u] = Ans.first = 0;
else f[u] = calc(Ans.second, a[u]);
p[u].k = b[u]; p[u].b = Ans.first;
Update(u, root[u], -Base, Base);
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i];
}
for(int i = 1; i <= n; ++i){
cin >> b[i];
}
for(int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
e[u].emplace_back(v);
e[v].emplace_back(u);
}
dfs(1, 0);
for(int i = 1; i <= n; ++i){
cout << f[i] << ' ';
}
return 0;
}
```
::::