ACM-ICPC 2018 徐州赛区网络预赛 G. Trace

题目链接
#题解
题目大意 给你n个坐标点 每次这个左边点都会向X轴和Y轴做垂线 但是会把这两条垂线和X轴Y轴内部的线段擦掉
问最后剩余线段总长 题目保证不会有覆盖现象

先把数据接受下来 倒着处理 这样就不用考虑擦去线段
每次新的点i对答案的贡献就是x[i] - yy[y[i]] + y[i] - xx[x[i]](x和y是题目所给坐标点 xx是x轴当前位置最靠上的边的距离 yy是y轴最靠右的边的距离)
xx和yy用线段树区间修改 单点查询最值维护
#AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
int x[MAXN], y[MAXN];
vector<int> dic;

struct node
{
	int l, r;
	ll mx, lzy;
}xx[MAXN << 2], yy[MAXN << 2];
inline void PushUp(int x, node *tree)
{
	tree[x].mx = max(tree[x << 1].mx, tree[x << 1 | 1].mx);
}
inline void PushDown(int x, node *tree)
{
	if (tree[x].lzy)
	{
		tree[x << 1].lzy = max(tree[x << 1].lzy, tree[x].lzy);
		tree[x << 1 | 1].lzy = max(tree[x << 1 | 1].lzy, tree[x].lzy);
		tree[x << 1].mx = max(tree[x << 1].mx, tree[x].lzy);
		tree[x << 1 | 1].mx = max(tree[x << 1 | 1].mx, tree[x].lzy);
		tree[x].lzy = 0;
	}
}
void Build(int x, int l, int r, node *tree)
{
	tree[x].l = l, tree[x].r = r, tree[x].mx = tree[x].lzy = 0;
	if (l == r)
		return;
	else
	{
		int m = l + r >> 1;
		Build(x << 1, l, m, tree);
		Build(x << 1 | 1, m + 1, r, tree);
		PushUp(x, tree);
	}
}
void Update(int x, int l, int r, ll v, node *tree)
{
	int xl = tree[x].l, xr = tree[x].r;
	if (l <= xl && r >= xr)
		tree[x].mx = max(tree[x].mx, v), tree[x].lzy = max(tree[x].lzy, v);
	else
	{
		PushDown(x, tree);
		int xm = xl + xr >> 1;
		if (xm >= l)
			Update(x << 1, l, r, v, tree);
		if (xm < r)
			Update(x << 1 | 1, l, r, v, tree);
		PushUp(x, tree);
	}
}
ll Query(int x, int l, int r, node *tree)
{
	int xl = tree[x].l, xr = tree[x].r;
	if (l <= xl && r >= xr)
		return tree[x].mx;
	else
	{
		PushDown(x, tree);
		int xm = xl + xr >> 1;
		ll res = 0;
		if (xm >= l)
			res = max(res, Query(x << 1, l, r, tree));
		if (xm < r)
			res = max(res, Query(x << 1 | 1, l, r, tree));
		return res;
	}
}
int main()
{
#ifdef LOCAL
	freopen("C:/input.txt", "r", stdin);
#endif
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		scanf("%d%d", &x[i], &y[i]);
		dic.push_back(x[i]), dic.push_back(y[i]);
	}
	sort(dic.begin(), dic.end());
	dic.erase(unique(dic.begin(), dic.end()), dic.end());
	ll ans = 0;
	Build(1, 1, dic.size(), xx);
	Build(1, 1, dic.size(), yy);
	for (int i = n; i >= 1; i--)
	{
		int dx = lower_bound(dic.begin(), dic.end(), x[i]) - dic.begin() + 1;
		int dy = lower_bound(dic.begin(), dic.end(), y[i]) - dic.begin() + 1;
		ans += x[i] - Query(1, dy, dy, yy);
		ans += y[i] - Query(1, dx, dx, xx);
		Update(1, 1, dx, y[i], xx);
		Update(1, 1, dy, x[i], yy);
	}
	cout << ans << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值