QOJ168 Cube Dividing
维护 \(a\times b \times c\) 的三维空间。给定 \(n\) 个障碍 \((x_i,y_i,z_i)\),将共面且非障碍的整点连边,求连通块数。
\(a,b,c\le 10^6\),\(n \le 2\times 10^4\),1 秒,512 MB。
不妨考察二维情形。对 \(x\) 轴扫描线,相邻两列形成若干连续段,可使用双指针合并有交线段。
连续段总数线性,使用并查集维护连通块即可。

升到三维,我们固定 \(x,y\) 令 \(z=1\to c\) 形成直四棱柱。只要对柱体的 \(4\) 个表面分别执行上述算法。柱子的总数是 \(a \times b\)。

我们需要缩掉无障碍的柱子。在俯视图上研究,将空柱子标记为空地,特殊柱标记为障碍,并执行二维算法,将二维连通块缩点。
这样只要扫描特殊柱,若邻居是空柱则直接与空柱点合并,特殊柱再使用双指针。
视 \(n,a,b,c\) 同阶,总复杂度 \(O(n\log n)\)。
const int N = 2e6 + 5;
int a, b, c, n, cnt;
using seg = pair<int, int>;
using point = pair<int, int>;
auto chk = [](const seg& a, const seg& b) -> bool {
return max(a.fi, b.fi) <= min(a.se, b.se);
};
struct dsu {
int fa[N];
void init() {iota(fa, fa + N, 0);}
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
void unite(int x, int y) {fa[find(x)] = find(y);}
} U;
vector<int> f[N];
vector<seg> s[N];
map<tuple<int, int, int>, int> id; // (x, y1, y2)
int search(int x, int y) {
auto it = lower_bound(all(s[x]), seg(y, -1));
if (it != s[x].end() && it->fi == y) return id[{x, it->fi, it->se}];
--it; return id[{x, it->fi, it->se}];
}
map<point, vector<seg>> t;
map<point, vector<int>> g;
void work2() {
for (int x = 1; x <= a; x++)
sort(all(f[x])), f[x].erase(unique(all(f[x])), f[x].end());
for (int x = 1; x <= a; x++) {
int l = 1;
for (int i : f[x]) {
if (l < i) s[x].pb(l, i-1);
l = i+1;
}
if (l <= b) s[x].pb(l, b);
}
for (auto [l, r] : s[1]) id[{1, l, r}] = ++cnt;
for (int x = 2; x <= a; x++) {
const auto &p = s[x-1], &q = s[x];
for (auto [l, r] : q) id[{x, l, r}] = ++cnt;
int n = p.size(), m = q.size();
for (int i = 0, j = 0; i < n && j < m; ) {
if (chk(p[i], q[j]))
U.unite(id[{x-1, p[i].fi, p[i].se}], id[{x, q[j].fi, q[j].se}]);
if (p[i].se < q[j].se) i++;
else if (p[i].se > q[j].se) j++;
else i++, j++;
}
}
}
const int dirx[] = {1, -1, 0, 0};
const int diry[] = {0, 0, 1, -1};
void work3() {
for (auto& [_, v] : g) sort(all(v));
for (auto [xy, v] : g) {
auto& vec = t[xy];
int l = 1;
for (int i : v) {
if (l < i) vec.pb(l, i-1);
l = i+1;
}
if (l <= c) vec.pb(l, c);
}
map<tuple<int, int, int, int>, int> mp;
auto num = [&](int z1, int z2, int x, int y) {
tuple<int, int, int, int> st = {z1, z2, x, y};
if (!mp.count(st)) mp[st] = ++cnt;
return mp[st];
};
for (auto [_xy, v] : g) {
auto [x, y] = _xy;
for (auto [l, r] : t[_xy]) num(l, r, x, y);
}
auto sol = [&](vector<seg>& p, vector<seg>& q, int x1, int y1, int x2, int y2) -> void {
int n = p.size(), m = q.size();
for (int i = 0, j = 0; i < n && j < m; ) {
if (chk(p[i], q[j]))
U.unite(num(p[i].fi, p[i].se, x1, y1),
num(q[j].fi, q[j].se, x2, y2));
if (p[i].se < q[j].se) i++;
else if (p[i].se > q[j].se) j++;
else i++, j++;
}
};
for (auto [_xy, v] : g) {
auto [x, y] = _xy;
for (int d = 0; d < 4; d++) {
int nx = x + dirx[d], ny = y + diry[d];
if (nx < 1 || nx > a || ny < 1 || ny > b) continue;
if (!g.count({nx, ny})) {
for (auto [l, r] : t[{x, y}])
U.unite(num(l, r, x, y), search(nx, ny));
continue;
}
sol(t[{x, y}], t[{nx, ny}], x, y, nx, ny);
}
}
}
void _main() {
cin >> a >> b >> c >> n; U.init();
for (int i = 1, x, y, z; i <= n; i++) {
cin >> x >> y >> z, ++x, ++y, ++z;
f[x].pb(y), g[{x, y}].pb(z);
}
work2(), work3();
int res = 0;
for (int i = 1; i <= cnt; i++)
if (i == U.find(i)) res++;
cout << res;
}

浙公网安备 33010602011771号