裸的 01BFS,时间复杂度 \(\mathcal O(nm)\)。
相邻的无障碍行可以缩成一行,列同理,所以全图的规模可以缩成 \((k + 1) \times (k + 1)\),再 01BFS,时间复杂度 \(\mathcal O(k^2)\)。
进一步地,所有 \(1 \times t\) 或 \(t \times 1\) 大小的无障碍连通块均可缩成一个点,两个连通块相交,则它们俩对应的点连边权为 \(1\) 的边,点数 \(\mathcal O(k)\),边数 \(\mathcal O(k^2)\),01BFS,时间复杂度 \(\mathcal O(k^2)\)。
继续优化,现在点数很优,边数很劣,想到点数换边数的线段树优化建图。
记 \(V_x(y)\) 表示过点 \((x, y)\) 的竖直连通块,则 \(V_x(y)\) 可通过 \(V_x(y - 1)\) 并上 \((x, y)\) 处的信息得来,所以从 \(V_1\) 到 \(V_n\) 最多只会修改 \(k\) 次,所以用可持久化线段树优化建图。这是水平连通块向竖直连通块连边的情况,竖直连通块向水平连通块连边同理。最后点数和边数均为 \(\mathcal O(k \log k)\)。此时再跑 01BFS,时间复杂度 \(\mathcal O(k \log k)\)。
如果把所有边显式地建出来的话要好好算算空间,容易 MLE(但是好写)。
代码
#include <bits/stdc++.h>
using namespace std;
constexpr int K = 5e4 + 10, N = 5.5e6, INF = 0x3f3f3f3f;
int n, m, k, q, x[K], y[K];
int nbx, nby, bx[K << 1], by[K << 1];
int cnt;
int npx, npy;
struct Node{int x, y, id;} px[K << 2], py[K << 2];
bool cmpx(const Node &lhs, const Node &rhs) {return lhs.x == rhs.x ? lhs.y < rhs.y : lhs.x < rhs.x;}
bool cmpy(const Node &lhs, const Node &rhs) {return lhs.y == rhs.y ? lhs.x < rhs.x : lhs.y < rhs.y;}
inline int findx(int x, int y) {return prev(upper_bound(px + 1, px + npx + 1, Node{x, y}, cmpx))->id;}
inline int findy(int x, int y) {return prev(upper_bound(py + 1, py + npy + 1, Node{x, y}, cmpy))->id;}
int tot, head[N];
struct Edge{int to, nxt, w;} e[N << 1];
inline void add(int u, int v, int w) {e[++tot] = Edge{v, head[u], w}, head[u] = tot;}
int rtL[K << 1], rtV[K << 1];
struct PST {
int nds;
struct Seg{int id, ls, rs;} t[N];
void upd(int pos0, int &pos, int l, int r, int x, int id) {
t[pos = ++nds] = t[pos0];
if (l == r) {t[pos].id = id; return;}
t[pos].id = ++cnt;
int mid = (l + r) >> 1;
if (x <= mid) upd(t[pos0].ls, t[pos].ls, l, mid, x, id);
else upd(t[pos0].rs, t[pos].rs, mid + 1, r, x, id);
if (t[pos].ls) add(t[pos].id, t[t[pos].ls].id, 0);
if (t[pos].rs) add(t[pos].id, t[t[pos].rs].id, 0);
}
void addedge(int pos, int l, int r, int x, int y, int id) {
if (!pos) return;
if (x <= l && r <= y) {add(id, t[pos].id, 1); return;}
int mid = (l + r) >> 1;
if (x <= mid) addedge(t[pos].ls, l, mid, x, y, id);
if (y > mid) addedge(t[pos].rs, mid + 1, r, x, y, id);
}
} V, L;
int ql = N, qr = N + 1, dq[N << 1], d[N]; bool vis[N];
inline void bfs() {
int sx = findx(x[k + 1], y[k + 1]), sy = findy(x[k + 1], y[k + 1]);
memset(d, 0x3f, sizeof(d)); d[sx] = d[sy] = 0;
dq[ql] = sx, dq[qr] = sy;
while (ql <= qr) {
int u = dq[ql++]; if (vis[u]) continue; vis[u] = 1;
for (int i = head[u], v; v = e[i].to, i; i = e[i].nxt) if (d[u] + e[i].w < d[v]) d[dq[e[i].w ? ++qr : --ql] = v] = d[u] + e[i].w;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(nullptr), cout.tie(nullptr);
cin >> n >> m >> k >> q;
bx[nbx = 1] = 1, by[nby = 1] = 1;
for (int i = 1; i <= k + 1; i++) {
cin >> x[i] >> y[i], bx[++nbx] = x[i], by[++nby] = y[i];
if (x[i] < n) bx[++nbx] = x[i] + 1;
if (y[i] < m) by[++nby] = y[i] + 1;
}
sort(bx + 1, bx + nbx + 1), nbx = unique(bx + 1, bx + nbx + 1) - bx - 1;
sort(by + 1, by + nby + 1), nby = unique(by + 1, by + nby + 1) - by - 1;
for (int i = 1; i <= k + 1; i++) x[i] = lower_bound(bx + 1, bx + nbx + 1, x[i]) - bx, y[i] = lower_bound(by + 1, by + nby + 1, y[i]) - by;
for (int i = 1; i <= nbx; i++) px[++npx] = {i, 0, ++cnt};
for (int i = 1; i <= nby; i++) py[++npy] = {0, i, ++cnt};
for (int i = 1; i <= k; i++) px[++npx] = {x[i], y[i], ++cnt}, py[++npy] = {x[i], y[i], ++cnt};
sort(px + 1, px + npx + 1, cmpy), sort(py + 1, py + npy + 1, cmpx);
for (int i = 1, cur = 1; i <= nbx; i++) {
rtV[i] = rtV[i - 1];
while (cur <= npy && py[cur].x == i - 1) V.upd(rtV[i], rtV[i], 1, nby, py[cur].y, py[cur].id), cur++;
}
for (int i = 1, cur = 1; i <= nby; i++) {
rtL[i] = rtL[i - 1];
while (cur <= npx && px[cur].y == i - 1) L.upd(rtL[i], rtL[i], 1, nbx, px[cur].x, px[cur].id), cur++;
}
sort(px, px + npx + 1, cmpx), sort(py, py + npy + 1, cmpy);
for (int i = 1; i <= npx; i++) V.addedge(rtV[px[i].x], 1, nby, px[i].y + 1, (px[i + 1].x == px[i].x ? px[i + 1].y - 1 : nby), px[i].id);
for (int i = 1; i <= npy; i++) L.addedge(rtL[py[i].y], 1, nbx, py[i].x + 1, (py[i + 1].y == py[i].y ? py[i + 1].x - 1 : nbx), py[i].id);
bfs();
while (q--) {
int qx, qy, dis; cin >> qx >> qy;
qx = upper_bound(bx + 1, bx + nbx + 1, qx) - bx - 1, qy = upper_bound(by + 1, by + nby + 1, qy) - by - 1;
dis = min(d[findx(qx, qy)], d[findy(qx, qy)]);
cout << (dis < INF ? dis : -1) << '\n';
}
return 0;
}
标签:nby,nbx,loj,连通,int,546,mathcal,bx,Round
From: https://www.cnblogs.com/chy12321/p/18195708