李超线段树 笔记
本文原在 2024-11-11 07:31 发布于本人洛谷博客。
P4097 【模板】李超线段树 / [HEOI2013] Segment
令 \(tree_{rt}\) 管辖完全覆盖 \(x\) 坐标为 \([l,r]\) 的线段中,在 \(mid\) 这一点上 \(y\) 最大的线段的编号。
更新的时候,先算出一条线段 \(y=kx+b\) 的 \(k\) 和 \(b\),如果这是一条 \(x=a\) 的线段,那就 \(k=0\),\(b\) 等于这条线段两端的较大值。然后正常的 \(\operatorname{update}\),当找到被更新操作包含的区间时,按照上面的定义执行比较操作,假设区间 \([l,r]\) 原本的答案是 \(u\),新比较一个 \(v\):
-
在 \(mid\) 处,\(y_v\) 优于 \(y_u\)。
- \(v\) 成为 \(tree_{rt}\) 中应存的东西,交换 \(u,v\)。
-
在 \(l,r\) 处,\(y_v\) 均优于或均劣于 \(y_u\)。
- 不用接着更新了,返回。
-
在 \(l\) 处或 \(r\) 处,\(y_v\) 优于 \(y_u\)。
- 接着递归较优的那边的子区间。
求答案的时候,从根节点一路找到 \([x,x]\) 这一个区间,返回这一路上在 \(x\) 这一点的最优线段即可。
#include <bits/stdc++.h>
#define int long long
#define pdi pair<double, int>
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define ls rt << 1
#define rs rt << 1 | 1
using namespace std;
const int N = 1e6 + 10, M = 39989, mod = 1e9;
const double eps = 1e-7;
int n, cnt, tree[N], ans;
struct Segment {
double k, b;
} e[N];
void add(int x0, int y0, int x1, int y1) {
double k, b;
if (x0 == x1)
k = 0, b = max(y0, y1);
else
k = 1.0 * (y0 - y1) / (x0 - x1), b = y0 - k * x0;
e[++cnt] = {k, b};
}
double gety(int id, int x) {
return e[id].k * x + e[id].b;
}
bool check(int id1, int id2, int x) {
double y1 = gety(id1, x), y2 = gety(id2, x);
if (y1 - y2 > eps)
return true;
if (y2 - y1 > eps)
return false;
return id1 < id2;
}
void upd(int rt, int l, int r, int id) {
if (l > r)
return;
int mid = l + r >> 1;
bool bmid = check(id, tree[rt], mid);
if (bmid)
swap(tree[rt], id);
bool bl = check(id, tree[rt], l), br = check(id, tree[rt], r);
if (bl and br)
return;
if (bl)
upd(ls, l, mid, id);
if (br)
upd(rs, mid + 1, r, id);
}
void update(int rt, int l, int r, int x, int y, int id) {
if (l > y or r < x)
return;
if (x <= l and r <= y) {
upd(rt, l, r, id);
return;
}
int mid = l + r >> 1;
update(ls, l, mid, x, y, id);
update(rs, mid + 1, r, x, y, id);
}
pdi getmax(pdi x, pdi y) {
if (x.first - y.first > eps)
return x;
if (y.first - x.first > eps)
return y;
return x.second < y.second ? x : y;
}
pdi query(int rt, int l, int r, int x) {
pdi ret = {gety(tree[rt], x), tree[rt]};
if (l == x and r == x)
return ret;
int mid = l + r >> 1;
return x <= mid ? getmax(ret, query(ls, l, mid, x)) : getmax(ret, query(rs, mid + 1, r, x));
}
signed main() {
IOS;
cin >> n;
while (n--) {
int opt, x0, y0, x1, y1;
cin >> opt;
if (!opt) {
cin >> x0;
x0 = (x0 + ans - 1) % M + 1;
ans = query(1, 1, M, x0).second;
cout << ans << "\n";
} else {
cin >> x0 >> y0 >> x1 >> y1;
x0 = (x0 + ans - 1) % M + 1;
x1 = (x1 + ans - 1) % M + 1;
y0 = (y0 + ans - 1) % mod + 1;
y1 = (y1 + ans - 1) % mod + 1;
if (x0 > x1)
swap(x0, x1), swap(y0, y1);
add(x0, y0, x1, y1);
update(1, 1, M, x0, x1, cnt);
}
}
return 0;
}

浙公网安备 33010602011771号