跳到正文

算法·理论

『算法笔记 4』李超线段树

LPR_318约 6 分钟
原文

这是 2026年2月13日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年2月13日
更新
2026年7月14日
篇幅
约 6 分钟
获赞
1

版本

  1. v2当前
  2. v1

顾名思义,一个线段树。
插入直线/线段,查询单点极值。
定义 tree.da 表示在 midmid 位置得到极值的线段的编号。
考虑插入:
若在 tree 的值肯定是取两条线段在 midmid 上更大的那一条。
但是没取的那条线段不一定就没用了。
如果在 ll 上没取的那一条更大,就往左子树递归插入;如果 rr 上没取的那一条更大,就往右子树递归插入;如果都不满足,由一次函数单调性,舍弃。
查询很简单。

标记永久化。
——tianqishuaishuai

用一个类似于标记永久化的东西,把所有含 pos 的线段树区间搞出来取一个最大值就行。

小心浮点数精度问题。

代码
C++
#include <bits/stdc++.h>
using namespace std;
#define y0 weurghs
#define y1 ajkhduw
#define endl "\n"
#define mod 39989
#define mod2 1000000000
#define max(a,b) (a>b?a:b)
struct ppp2{
	int x0,y0,x1,y1;
	double a,b;
}lines[100005];
#define get(x,k) (lines[x].a*(double)k + lines[x].b)
int n,op,lastans,cnt,aaa;
struct ppp{
	int da;
}tree[160005];
#define mid ((l+r)>>1)
#define emp (l == r)
#define in (ql <= l && r <= qr)
void insert(int x,int l,int r,int ql,int qr,int d){
	if(in){
		if(tree[x].da == 0){
			tree[x].da = d;
			return;
		}
		int l1 = tree[x].da,l2 = d;
		if(get(l1,mid)+0.001 < get(l2,mid)) swap(l1,l2);
		tree[x].da = l1;
		if(get(l1,l) > get(l2,l)+0.001 && get(l1,r) > get(l2,r)+0.001) return;
		if(fabs(get(l1,l) - get(l2,l)) <= 0.001 && l1 < l2 && fabs(get(l1,r) - get(l2,r)) <= 0.001) return;
		if((get(l2,l) > get(l1,l)+0.001)) insert(x<<1,l,mid,ql,qr,l2);
		else if(abs(get(l2,l) - get(l1,l)) <= 0.001 && l2 < l1) insert(x<<1,l,mid,ql,qr,l2);
		else insert(x<<1|1,mid+1,r,ql,qr,l2);
		return;
	}
	if(ql <= mid) insert(x<<1,l,mid,ql,qr,d);
	if(qr > mid) insert(x<<1|1,mid+1,r,ql,qr,d);
}
int ask(int x,int l,int r,int pos){
	if(emp) return tree[x].da;
	int ans = 0;
	if(pos <= mid) ans = ask(x<<1,l,mid,pos);
	else ans = ask(x<<1|1,mid+1,r,pos);
	if(ans == 0) return tree[x].da;
	if(tree[x].da == 0) return ans;
	if(fabs(get(ans,pos) - get(tree[x].da,pos)) <= 0.0001){
		if(ans < tree[x].da) return ans;
		else return tree[x].da;
	}
	if(get(ans,pos) > get(tree[x].da,pos)) return ans;
	if(get(tree[x].da,pos) > get(ans,pos)) return tree[x].da;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;++i){
		cin >> op;
		if(op == 1){
			++cnt;
			cin >> lines[cnt].x0 >> lines[cnt].y0 >> lines[cnt].x1 >> lines[cnt].y1;
			lines[cnt].x0 = (lines[cnt].x0 + lastans - 1) % mod + 1;
			lines[cnt].x1 = (lines[cnt].x1 + lastans - 1) % mod + 1;
			lines[cnt].y0 = (lines[cnt].y0 + lastans - 1) % mod2 + 1;
			lines[cnt].y1 = (lines[cnt].y1 + lastans - 1) % mod2 + 1;
            if(lines[cnt].x0 > lines[cnt].x1) swap(lines[cnt].x0,lines[cnt].x1),swap(lines[cnt].y0,lines[cnt].y1);
            if(lines[cnt].x0 == lines[cnt].x1){
				lines[cnt].a = 0;
				lines[cnt].b = max(lines[cnt].y0,lines[cnt].y1);
			}
			else{
				lines[cnt].a = (((double)lines[cnt].y0-(double)lines[cnt].y1) / ((double)lines[cnt].x0 - (double)lines[cnt].x1));
				lines[cnt].b = ((double)lines[cnt].y0) - (lines[cnt].a) * ((double)lines[cnt].x0);
			}
			insert(1,1,40000,lines[cnt].x0,lines[cnt].x1,cnt);
		}
		else{
			cin >> aaa;
			aaa = (aaa + lastans - 1) % mod + 1;
			lastans = ask(1,1,40000,aaa);
			cout << lastans << endl;
		}
	}
	return 0;
}