题解:P7559 [JOISC 2021] IOI 熱の感染拡大 (Day1)
happy_zero · · 题解
参考:这篇题解。更详细地说明了一下实现,因为我写假了好几回。
在这题中,发现确定了方向也不好做,所以猜测每个点方向唯一。
当然除了一号点,但这个简单,每种方向都做一遍即可(可以旋转坐标轴做四次,同时为了方便令一号点为原点)。然后尝试一下上面那个猜测。首先肯定是得朝着一号点走,那么范围就缩到了两种。
注意审题,点不重合,这其实避免了一号点直接分裂多个方向一起走。然后简单尝试一下,若
容易想到两两判断是否会相交然后跑 dijkstra,紧接着考虑优化建图。刻画边的形式,发现可能的其实只有:
四种情况分别考虑(这样有什么好处呢?就是形式很简单),若当前
前缀后缀可以直接上线段树,边权只有两种形式可以直接维护。但不妨考虑一个更加优美的做法:这种图很有特殊性质的题目,最短路的建边是前后缀,那么可以考虑每次只找到能转移的最近的,然后当这个最近的点从堆中取出时再去找下一个点。可以维护若干个 set 来找,记得感染后要从 set 中扔掉。
为什么复杂度是对的呢?不妨只考虑四类情况中的一种,因为合起来只会多一些常数。此时可以视作在一条线上运动(斜着的放缩一下可以直接视作在斜线上走),那么若
由于写的是 set 所以复杂度是两只
易错点:不能在
i 找到j 时就把i 入队复杂度是错误的,但是数据水放过去了。
:::info[code 4.5k]
#include <bits/stdc++.h>
#define pii pair <int, int>
#define fi first
#define se second
using namespace std;
const int N = 1e5 + 5;
int n, op[N];
struct _ { int x, y; } a[N];
struct Umap {
int ts;
set <pii> o[N];
unordered_map <int, int> p;
inline int get(int x) {
return p[x] ? p[x] : p[x] = ++ts;
}
inline void ins(int x, int y, int id) {
o[get(x)].insert({y, id});
}
inline void erase(int x, int y, int id) {
o[get(x)].erase({y, id});
}
inline int pre(int i, int x) {
i = get(i);
auto it = o[i].upper_bound((pii){x, 0});
return it == o[i].begin() ? -1 : (*--it).se;
}
inline int suc(int i, int x) {
i = get(i);
auto it = o[i].upper_bound((pii){x, 0});
return it == o[i].end() ? -1 : (*it).se;
}
} p[5], pa[5], pb[5];
// 这里把第一二种情况放到一次
int dis[N], vis[N], pre[N][5], w[N][5];
struct node {
int dis, id, op;
friend bool operator <(node x, node y) {
if (x.dis ^ y.dis) return x.dis > y.dis;
else return x.op > y.op;
}
};
priority_queue <node> q;
void upd(int x, int y, int t) {
int &p = pre[x][op[t]];
if (!p || y < w[x][op[t]]) p = t, w[x][op[t]] = y;
// 记录每个方向来的最近点和距离
if (y < dis[x])
q.push({dis[x] = y, x, vis[x]});
}
int solve() {
op[1] = 1;
for (int i = 2; i <= n; i++) {
int X = abs(a[i].x), Y = abs(a[i].y);
if (X > Y) op[i] = a[i].x > 0 ? 3 : 1;
else if (X < Y) op[i] = a[i].y > 0 ? 4 : 2;
else if (a[i].x > 0) op[i] = a[i].y > 0 ? 4 : 2;
else op[i] = 1;
// 1 右 2 上 3 左 4 下
}
int ta = 0, tb = 0, tl = 0, tc = 0;
for (int _ = 1; _ <= 4; _++)
pa[_] = pb[_] = p[_] = Umap();
for (int i = 1; i <= n; i++) {
if (op[i] == 1 || op[i] == 3)
p[op[i]].ins(a[i].y, a[i].x, i);
else p[op[i]].ins(a[i].x, a[i].y, i);
pa[op[i]].ins(a[i].x + a[i].y, a[i].x, i);
pb[op[i]].ins(a[i].x - a[i].y, a[i].x, i);
}
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
memset(pre, 0, sizeof(pre));
while (q.size()) q.pop();
q.push({dis[1] = 0, 1, 0});
while (q.size()) {
int t = q.top().id, w = dis[t], Op = q.top().op; q.pop();
if (Op < vis[t]) continue; vis[t] = Op + 1;
// 取出 t 时还在队列中的 t 不用更新
if (!Op) {
if (op[t] == 1 || op[t] == 3)
p[op[t]].erase(a[t].y, a[t].x, t);
else p[op[t]].erase(a[t].x, a[t].y, t);
pa[op[t]].erase(a[t].x + a[t].y, a[t].x, t);
pb[op[t]].erase(a[t].x - a[t].y, a[t].x, t);
for (int i = 1; i <= 4; i++)
q.push({w, pre[t][i], vis[pre[t][i]]});
}
if (op[t] == 1) {
int x = p[3].suc(a[t].y, a[t].x + 2 * w);
if (~x) upd(x, a[x].x - a[t].x >> 1, t);
x = pa[2].suc(a[t].x + a[t].y, a[t].x + w);
if (~x) upd(x, a[x].x - a[t].x, t);
x = pb[4].suc(a[t].x - a[t].y, a[t].x + w);
if (~x) upd(x, a[x].x - a[t].x, t);
} else if (op[t] == 3) {
int x = p[1].pre(a[t].y, a[t].x - 2 * w);
if (~x) upd(x, a[t].x - a[x].x >> 1, t);
x = pa[4].pre(a[t].x + a[t].y, a[t].x - w);
if (~x) upd(x, a[t].x - a[x].x, t);
x = pb[2].pre(a[t].x - a[t].y, a[t].x - w);
if (~x) upd(x, a[t].x - a[x].x, t);
} else if (op[t] == 2) {
int x = p[4].suc(a[t].x, a[t].y + 2 * w);
if (~x) upd(x, a[x].y - a[t].y >> 1, t);
x = pa[1].pre(a[t].x + a[t].y, a[t].x - w);
if (~x) upd(x, a[t].x - a[x].x, t);
x = pb[3].suc(a[t].x - a[t].y, a[t].x + w);
if (~x) upd(x, a[x].x - a[t].x, t);
} else {
int x = p[2].pre(a[t].x, a[t].y - 2 * w);
if (~x) upd(x, a[t].y - a[x].y >> 1, t);
x = pa[3].suc(a[t].x + a[t].y, a[t].x + w);
if (~x) upd(x, a[x].x - a[t].x, t);
x = pb[1].pre(a[t].x - a[t].y, a[t].x - w);
if (~x) upd(x, a[t].x - a[x].x, t);
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
ans += dis[i] != dis[0];
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].x >> a[i].y;
a[i].x *= 2, a[i].y *= 2;
// 避免小数时间,这样相遇都是在整数时间
}
for (int i = n; i; i--)
a[i].x -= a[1].x, a[i].y -= a[1].y;
int ans = 1;
for (int _ = 1; _ <= 4; _++) {
ans = max(ans, solve());
for (int i = 1; i <= n; i++)
a[i] = {a[i].y, -a[i].x};
}
cout << ans << '\n';
return 0;
}
:::