题目链接
#题解
题目大意 给你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;
}