题目概括:
P4737 [CERC2017] Buffalo Barricades
在第一象限按照给出顺序产生 个点,同时还有 个已知的目标点,每次产生一个点会同时产生一条向左和一条向右以该点为原点的射线并被已有射线或坐标轴截断形成临域 ,求对每个点所产生的领域中分别包含多少个目标点。
思路分析:
- 我们注意到当一个点右上方有一个点划分时,右上方的点会包含这个点所划分的区域,而且两者的先后关系非常重要,我们记当前点为 ,右上方的点为 :
- 当 先划分时, 划分就不能拿到 所包含的目标点,因为 发出的射线也是 的邻域的边界一部分。
- 当 先划分时, 还没有划分,所以 可以拿到 的邻域中所包含的目标点。
- 这样我们就可以记录一个包含关系,从而做到只需要统计画好图后每个点的邻域包含的目标点后再按照包含关系累加就能得到答案,问题就变成了静态的。
- 我们注意到射线具有两个方向,这是不好做的,所以考虑将其拆分,我们发现横线可以截断竖线,所以考虑利用
set维护所有竖线,每维护一个就向左扫描所有其它竖线,如果其它竖线的加点时间还比当前点时间晚,那么就会被截断。 - 这样我们就可以维护出来具体划分后的图形,我们思考
set要按照什么顺序加入,考虑到可以对 排序从上到下加入就能确保被截断的竖线总是完整的,同时我们还可以维护每个人的邻域内直接的目标点,当然这个也被叫做扫描线,具体而言set的维护分两种:- 扫描到目标点:相对目标点的 向右找到第一个竖线,这一定是直接包含目标点的邻域。
- 扫描到加入点:先加入竖线,然后找到右边的第一条竖线,这就是包含它的竖线,但是这可能会出现 1 中提到的时间顺序问题,我们之后解决。然后再向左看,删除所有加入时间比当前点晚的竖线,特殊的,如果有一个点比当前点早,立刻停止向左扫描因为那条竖线会截断我们的横线,故不能继续延伸。
- 还记得 4 说到的问题吗,我们可以利用倒序扫描这些点利用并查集进行维护,这样如果右面来的更晚会被率先遍历到,此时我们把它的邻域中的数量转移到
ans数组中,之后遍历到错误的包含信息增加答案时ans就能规避开,保证了答案正确。
具体实现流程:
- 缺省源。
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y, type;
}a[600001];
set<pair<int, int> > line;
int n, m, fa[300001], cnt[300001], ans[300001], bcz[300001];- 并查集查找。
int find(int x) {
if (bcz[x] == x)
return x;
return bcz[x] = find(bcz[x]);
}- 按照 排序做扫描线。
bool cmp(node x, node y) {
if (x.y != y.y)
return x.y > y.y;
return x.type > y.type;
}- 输入数据,这里
type为 表示目标点,其它情况代表加入时间。
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].x >> a[i].y;
a[i].type = -1;
}
cin >> m;
for (int i = n + 1; i <= n + m; i++) {
cin >> a[i].x >> a[i].y;
a[i].type = i - n;
}- 按 排序。
sort(a + 1, a + n + m + 1, cmp);- 如果是目标点就增加答案。
for (int i = 1; i <= n + m; i++)
if (a[i].type == -1) {
auto it = line.lower_bound({ a[i].x, 0 });
if (it != line.end())
cnt[(*it).second]++;
}- 否则建立包含关系并向左截断竖线。
else {
line.insert({ a[i].x, a[i].type });
auto it = next(line.lower_bound({ a[i].x, a[i].type }));
if (it != line.end())
fa[a[i].type] = (*it).second;
it = line.lower_bound({ a[i].x, a[i].type });
while (it != line.begin()) {
auto lst = prev(it);
if ((*lst).second > a[i].type)
line.erase(lst);
else
break;
}
}- 利用并查集合并答案并输出。
for (int i = 1; i <= m; i++)
bcz[i] = i;
for (int i = m; i >= 1; i--) {
ans[i] = cnt[i];
if (bcz[i] != 0) {
cnt[find(fa[i])] += cnt[i];
bcz[i] = find(fa[i]);
}
}
for (int i = 1; i <= m; i++)
cout << ans[i] << '\n';
return 0;
}完整代码:
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y, type;
}a[600001];
set<pair<int, int> > line;
int n, m, fa[300001], cnt[300001], ans[300001], bcz[300001];
int find(int x) {
if (bcz[x] == x)
return x;
return bcz[x] = find(bcz[x]);
}
bool cmp(node x, node y) {
if (x.y != y.y)
return x.y > y.y;
return x.type > y.type;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].x >> a[i].y;
a[i].type = -1;
}
cin >> m;
for (int i = n + 1; i <= n + m; i++) {
cin >> a[i].x >> a[i].y;
a[i].type = i - n;
}
sort(a + 1, a + n + m + 1, cmp);
for (int i = 1; i <= n + m; i++)
if (a[i].type == -1) {
auto it = line.lower_bound({ a[i].x, 0 });
if (it != line.end())
cnt[(*it).second]++;
}
else {
line.insert({ a[i].x, a[i].type });
auto it = next(line.lower_bound({ a[i].x, a[i].type }));
if (it != line.end())
fa[a[i].type] = (*it).second;
it = line.lower_bound({ a[i].x, a[i].type });
while (it != line.begin()) {
auto lst = prev(it);
if ((*lst).second > a[i].type)
line.erase(lst);
else
break;
}
}
for (int i = 1; i <= m; i++)
bcz[i] = i;
for (int i = m; i >= 1; i--) {
ans[i] = cnt[i];
if (bcz[i] != 0) {
cnt[find(fa[i])] += cnt[i];
bcz[i] = find(fa[i]);
}
}
for (int i = 1; i <= m; i++)
cout << ans[i] << '\n';
return 0;
}