跳到正文

题解

P4097 [HEOI2013]Segment

Linshey约 20 分钟
原文

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

文章信息与版本

文章信息

发布
2020年2月26日
收录
2026年5月14日
更新
2026年11月12日
篇幅
约 20 分钟
题目
P4097

版本

  1. v2当前
  2. v1

既然名字叫Segment,那么解法那当然就是Segment_Tree

我们注意到有区间操作,而且查询都是整数点 \color{red}\text{查询都是整数点}, 所以就锁定在线段树上了。

具体怎么线段树呢?我们考虑一个暴力一点的方法,线段树的每一个节点,存经过这一段区间的、还未下放的所有线段,这样每一个点都是一个vector<struct>。当要查询的时候,我们就一路下放下去,直到叶子节点,这样最后在叶子节点就会积累一定数量的线段,我们在这些线段中取最大的即可。时间复杂度O(过不了(30pts)

Code.30pts

C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define lson(x) x << 1
#define rson(x) x << 1 | 1

using namespace std;
const int maxn = 1e5 + 5;

vector<double> k[maxn * 4], b[maxn * 4], pos[maxn * 4];
double ans[maxn * 4], num[maxn * 4];

void spread(int p)
{
	if (k[p].size())
	{
		for (auto e : k[p]) k[lson(p)].push_back(e);
		for (auto e : k[p]) k[rson(p)].push_back(e);
		for (auto e : b[p]) b[lson(p)].push_back(e);
		for (auto e : b[p]) b[rson(p)].push_back(e);
		for (auto e : pos[p]) pos[lson(p)].push_back(e);
		for (auto e : pos[p]) pos[rson(p)].push_back(e);
		k[p].clear();
		b[p].clear();
		pos[p].clear();
	}
}

void change(int p, int l, int r, int ll, int rr, double kk, double bb, int t) /// bug double
{
	if (l >= ll && r <= rr)
	{
		k[p].push_back(kk);
		b[p].push_back(bb);
		pos[p].push_back(t);
		return;
	}
	spread(p);
	int mid = (l + r) >> 1;
	if (ll <= mid) change(lson(p), l, mid, ll, rr, kk, bb, t);
	if (rr > mid) change(rson(p), mid + 1, r, ll, rr, kk, bb, t);
}

void query(int p, int l, int r, int x)
{
	if (l == r && r == x)
	{
		for (int i = 0; i < k[p].size(); i++)
		{
			if (ans[x] < k[p][i] * x + b[p][i])
			{
				ans[x] = k[p][i] * x + b[p][i];
				num[x] = pos[p][i];
			}
		}
		k[p].clear();b[p].clear(); pos[p].clear();
		return;
	}
	spread(p);
	int mid = (l + r) >> 1;
	if (x <= mid)
	{
		query(lson(p), l, mid, x);
	}
	else
	{
		query(rson(p), mid + 1, r, x);
	}
}

void init()
{
	memset(ans, -127/3, sizeof(ans));
}

int n, lastans, tail;

int main()
{
	init();
	cin >> n;
	while (n--)
	{
		bool op;
		cin >> op;
		if (op)
		{
			long long x0, x1, y0, y1;
			cin >> x0 >> y0 >> x1 >> y1;
			x0 = (x0 + lastans - 1) % 39989 + 1;
			x1 = (x1 + lastans - 1) % 39989 + 1;
			y0 = (y0 + lastans - 1) % 1000000000 + 1;
			y1 = (y1 + lastans - 1) % 1000000000 + 1;
			if (x0 > x1)
			{
				swap(x0, x1);
				swap(y0, y1);
			}
			double kk, bb;
			if (x0 != x1)
			{
				kk = (y1 - y0 + 0.0) / (x1 - x0);
				bb = ((long double)y0 * x1 - x0 * y1) / (x1 - x0);
			}
			else
			{
				kk = 0;
				bb = max(y0, y1);
			}
			tail++;
			change(1, 1, 40000, x0, x1, kk, bb, tail);
		}
		else
		{
			int k, x;
			cin >> k;
			x = (k + lastans - 1) % 39989 + 1;//x = k;
			query(1, 1, 40000, x);
			lastans = num[x];
			cout << lastans << endl;
		}
	}
	return 0;
}

注意这里面有一大堆细节,要小心加小心。这段代码最后是MLE了,原因是线段不断被细分,直到每个叶子节点都有上万个线段,当然过不了。怎么优化呢?我要叫我的算法叫线段树剪枝 \color{green}\text{线段树剪枝}

意思是每一个节点,我将完全在某一条线段下面的线段直接删掉,这样就可以有效减少线段数量

Code.30pts’

C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define lson(x) x << 1
#define rson(x) x << 1 | 1

using namespace std;
const int maxn = 1e5 + 5;

vector<double> k[maxn * 4], b[maxn * 4], pos[maxn * 4];
double ans[maxn * 4], num[maxn * 4];
bool improved[maxn * 4];

void improve(int p, int l, int r)
{
	int debug = k[p].size() - 1;
	for (int i = 0; i < debug; i++)
	{
		for (int j = i + 1; j < k[p].size(); j++)
		{
			if (k[p][i] * l + b[p][i] > k[p][j] * l + b[p][j] && k[p][i] * r + b[p][i] > k[p][j] * r + b[p][j])
			{
				k[p].erase(k[p].begin() + j);
				b[p].erase(b[p].begin() + j);
				pos[p].erase(pos[p].begin() + j);
				j--;
			}
		}
	}
	for (int i = k[p].size() - 1; i > 0; i--)
	{
		for (int j = i - 1; j > 0; j--)
		{
			if (k[p][j] * l + b[p][j] > k[p][i] * l + b[p][i] && k[p][j] * r + b[p][j] > k[p][i] * r + b[p][i])
			{
				k[p].erase(k[p].begin() + i);
				b[p].erase(b[p].begin() + i);
				pos[p].erase(pos[p].begin() + i);
				i--;
			}
		}
	}
	improved[p] = true;
}

void spread(int p)
{
	if (k[p].size())
	{
		for (auto e : k[p]) k[lson(p)].push_back(e);
		for (auto e : k[p]) k[rson(p)].push_back(e);
		for (auto e : b[p]) b[lson(p)].push_back(e);
		for (auto e : b[p]) b[rson(p)].push_back(e);
		for (auto e : pos[p]) pos[lson(p)].push_back(e);
		for (auto e : pos[p]) pos[rson(p)].push_back(e);
		k[p].clear();
		b[p].clear();
		pos[p].clear();
		improved[p] = true;
		improved[lson(p)] = false; improved[rson(p)] = false;
	}
}

void change(int p, int l, int r, int ll, int rr, double kk, double bb, int t) /// bug double
{
	if (l >= ll && r <= rr)
	{
		k[p].push_back(kk);
		b[p].push_back(bb);
		pos[p].push_back(t);
		improved[p] = false;
		return;
	}
	if (!improved[p]) improve(p, l, r);
	//spread(p);
	int mid = (l + r) >> 1;
	if (ll <= mid) change(lson(p), l, mid, ll, rr, kk, bb, t);
	if (rr > mid) change(rson(p), mid + 1, r, ll, rr, kk, bb, t);
}

void query(int p, int l, int r, int x)
{
	if (l == r && r == x)
	{
		for (int i = 0; i < k[p].size(); i++)
		{
			if (ans[x] < k[p][i] * x + b[p][i])
			{
				ans[x] = k[p][i] * x + b[p][i];
				num[x] = pos[p][i];
			}
		}
		k[p].clear(); b[p].clear(); pos[p].clear();
		return;
	}
	if (!improved[p]) improve(p, l, r);
	spread(p);
	int mid = (l + r) >> 1;
	if (x <= mid)
	{
		query(lson(p), l, mid, x);
	}
	else
	{
		query(rson(p), mid + 1, r, x);
	}
}

void init()
{
	memset(ans, -127/3, sizeof(ans));
}

int n, lastans, tail;

int main()
{
	init();
	cin >> n;
	while (n--)
	{
		bool op;
		cin >> op;
		if (op)
		{
			long long x0, x1, y0, y1;
			cin >> x0 >> y0 >> x1 >> y1;
			x0 = (x0 + lastans - 1) % 39989 + 1;
			x1 = (x1 + lastans - 1) % 39989 + 1;
			y0 = (y0 + lastans - 1) % 1000000000 + 1;
			y1 = (y1 + lastans - 1) % 1000000000 + 1;
			if (x0 > x1)
			{
				swap(x0, x1);
				swap(y0, y1);
			}
			double kk, bb;
			if (x0 != x1)
			{
				kk = (y1 - y0 + 0.0) / (x1 - x0);
				bb = ((long double)y0 * x1 - x0 * y1) / (x1 - x0);
			}
			else
			{
				kk = 0;
				bb = max(y0, y1);
			}
			tail++;
			change(1, 1, 40000, x0, x1, kk, bb, tail);
		}
		else
		{
			int k, x;
			cin >> k;
			x = (k + lastans - 1) % 39989 + 1;//x = k;
			query(1, 1, 40000, x);
			lastans = num[x];
			cout << lastans << endl;
		}
	}
	return 0;
}

可以看到只是增加了一个improve函数。依然是30pts,只是这回改成TLE了。

这说明一件严重的事情:线段树每个节点不能保存一组线段,要想个办法,使得每个节点只存一个线段,且可以避免麻烦的下方操作。

经过痛哭的思索,可以想到:线段间的关系只有相交和相离。对于相离的线段,我们可以将下面的线段直接删掉(就像第二段代码),对于相交的线段,我们在这个节点存储平均高度比较大的线段,剩下那条线段我们下放交点上方的一部分(相当于一个二分\color{red}\text{二分}),最后统计时取这一整条链的最大值。

这说明,线段树玩着玩着,又会返璞归真,回到最初的二分思想

Code.100pts

C++
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define lson(x) x << 1
#define rson(x) x << 1 | 1

using namespace std;
const int maxn = 1e5 + 5;

double k[maxn * 4], b[maxn * 4];
int pos[maxn * 4];
double ans[maxn * 4], num[maxn * 4];
bool improved[maxn * 4];

void change(int p, int l, int r, int ll, int rr, double kk, double bb, int t) /// bug double
{
	if (l >= ll && r <= rr)
	{
		if (!improved[p])
		{
			k[p] = kk;
			b[p] = bb;
			pos[p] = t;
			improved[p] = true;
			return;
		}
		if (k[p] * l + b[p] > kk * l + bb && k[p] * r + b[p] > kk * r + bb) return;
		if (k[p] * l + b[p] < kk * l + bb && k[p] * r + b[p] < kk * r + bb)
		{
			k[p] = kk;
			b[p] = bb;
			pos[p] = t;
			return;
		}
		if (k[p] * l + b[p] == kk * l + bb && k[p] * r + b[p] == kk * r + bb)
		{
			if (t < pos[p])
			{
				k[p] = kk;
				b[p] = bb;
				pos[p] = t;
				return;
			}
			else return;
		}
		double x = (bb - b[p]) / (k[p] - kk);
		int mid = (l + r) >> 1;
		if (x <= mid)
		{
			if (k[p] * l + b[p] > kk * l + bb)
			{
				change(lson(p), l, mid, l, x, k[p], b[p], pos[p]);
				k[p] = kk;
				b[p] = bb;
				pos[p] = t;
			}
			else
			{
				change(lson(p), l, mid, l, x, kk, bb, t);
			}
		}
		else
		{
			if (k[p] * r + b[p] > kk * r + bb)
			{
				change(rson(p), mid + 1, r, ceil(x), r, k[p], b[p], pos[p]);
				k[p] = kk;
				b[p] = bb;
				pos[p] = t;
			}
			else
			{
				change(rson(p), mid + 1, r, ceil(x), r, kk, bb, t);
			}
		}
		return;
	}
	int mid = (l + r) >> 1;
	if (ll <= mid) change(lson(p), l, mid, ll, rr, kk, bb, t);
	if (rr > mid) change(rson(p), mid + 1, r, ll, rr, kk, bb, t);
}

pair<int, double> query(int p, int l, int r, int x)
{
	if (l == r && r == x)
	{
		if (improved[p])
			return make_pair(pos[p], k[p] * x + b[p]);
		return make_pair(0, -100000000);
	}
	int mid = (l + r) >> 1;
	pair<int, double> ret;
	if (x <= mid)
	{
		ret = query(lson(p), l, mid, x);
	}
	else
	{
		ret = query(rson(p), mid + 1, r, x);
	}
	if (improved[p])
	{
		if (k[p] * x + b[p] > ret.second || (k[p] * x + b[p] == ret.second && pos[p] < ret.first))
		{
			ret = make_pair(pos[p], k[p] * x + b[p]);
		}
	}
	return ret;
}

void init()
{
	memset(ans, -127/3, sizeof(ans));
}

int n, lastans, tail;

int main()
{
	init();
	cin >> n;
	while (n--)
	{
		bool op;
		cin >> op;
		if (op)
		{
			long long x0, x1, y0, y1;
			cin >> x0 >> y0 >> x1 >> y1;
			x0 = (x0 + lastans - 1) % 39989 + 1;
			x1 = (x1 + lastans - 1) % 39989 + 1;
			y0 = (y0 + lastans - 1) % 1000000000 + 1;
			y1 = (y1 + lastans - 1) % 1000000000 + 1;
			if (x0 > x1)
			{
				swap(x0, x1);
				swap(y0, y1);
			}
			double kk, bb;
			if (x0 != x1)
			{
				kk = (y1 - y0 + 0.0) / (x1 - x0);
				bb = ((long double)y0 * x1 - x0 * y1) / (x1 - x0);
			}
			else
			{
				kk = 0;
				bb = max(y0, y1);
			}
			tail++;
			change(1, 1, 40000, x0, x1, kk, bb, tail);
		}
		else
		{
			int k, x;
			cin >> k;
			x = (k + lastans - 1) % 39989 + 1;//x = k;
			pair<int, double> ret = query(1, 1, 40000, x);
			lastans = ret.first;
			cout << lastans << endl;
		}
	}
	return 0;
}

感谢