2020 Petrozavodsk Winter Camp Day 5 J | Nanako

2020 Petrozavodsk Winter Camp Day 5 J

Problem J. Space Gophers

Statement

有一个由边长为 $1$ 的小正方体组成的边长为 $10^6$ 的实心正方体。在其中挖 $n$ 条隧道,每条隧道用 $(-,y_i,z_i)$ 或 $(x_i,-,z_i)$ 或 $(x_i,y_i,-)$ 表示。挖隧道的含义是,指定其中两个维度的坐标,沿平行于另一个维度轴线的方向把 $10^6$ 个方块拿走。挖完 $n$ 条隧道之后,$q$ 次询问两个点 $(s_x,s_y,s_z)$ 和 $(t_x,t_y,t_z)$ 是否可以通过若干条隧道连通。保证 $s$ 和 $t$ 处于隧道中。有 $z$ 组测试数据。

$1 \leq z \leq 6$

$1 \leq n \leq 3 \times 10^5$, $1 \leq x_i,y_i,z_i \leq 10^6$

$1 \leq q \leq 5 \times 10^5$, $1 \leq s_x,s_y,s_z,t_x,t_y,t_z \leq 10^6$

Solution

clp 单切之前大致跟我讲了一下做法。实际上两条隧道连通 iff 某一个维度坐标相等或相差 1,于是我们要做的事情就是把所有这样的隧道对找出来,在并查集上 merge 起来。

这个东西说起来简单,实现起来就比较呕吐……具体还是看代码吧……似乎是这篇文章里最长的代码

Code (By clp012345)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <bits/stdc++.h>

#define FOE(i, s, t) for (int i = s; i <= t; i++)
#define FOR(i, s, t) for (int i = s; i < t; i++)
#define K 5000001
#define D 1000000
#define mp make_pair
#define pb push_back
#define LL long long

using namespace std;

int nxt[K], sz[K];

int par(int u) {
return (nxt[u] == u ? u : nxt[u] = par(nxt[u]));
}

void merge(int u, int v) {
int s1 = par(u);
int s2 = par(v);

if (s1 == s2) return;

if (sz[s1] > sz[s2]) swap(s1, s2);

nxt[s1] = s2;
sz[s2] += sz[s1];
}

int n;

vector<int> x[D + 100], y[D + 100], z[D + 100];

int goodX[D], goodY[D], goodZ[D];

map<pair<pair<int, int>, int>, int > M;

int px[D], py[D], pz[D];

int seek(int x, int y, int z) {
// printf("seek %d %d %d ? %d %d %d \n", x, y, z, M[mp(mp(x, y), -1)], M[mp(mp(x, -1), z)], M[mp(mp(-1, y), z)]);
int a1 = M[mp(mp(x, y), -1)];
if (a1) return a1;

int a2 = M[mp(mp(x, -1), z)];

if (a2) return a2;
return M[mp(mp(-1, y), z)];
}

int q;

void solve() {
scanf("%d", &n);

M.clear();

FOE(i, 1, D) {
x[i].clear(); y[i].clear(); z[i].clear();
}

FOE(i, 1, n) {
scanf("%d%d%d", &px[i], &py[i], &pz[i]);

M[mp(mp(px[i], py[i]), pz[i])] = i;

if (px[i] != -1) {
x[px[i]].pb(i);
}

if (py[i] != -1) {
y[py[i]].pb(i);
}

if (pz[i] != -1) {
z[pz[i]].pb(i);
}
}

FOE(i, 1, n + 3 * D) {
nxt[i] = i;
sz[i] = 1;
}

FOE(i, 1, n) {
if (pz[i] == -1) {
for (int dx = 0; dx <= 1; dx++) for (int dy = 0; dy <= 1; dy++) if (abs(dx) + abs(dy) == 1) {
int id2 = M[mp(mp(px[i] + dx, py[i] + dy), -1)];

if (id2) merge(id2, i);
}
}
if (py[i] == -1) {
for (int dx = 0; dx <= 1; dx++) for (int dz = 0; dz <= 1; dz++) if (abs(dx) + abs(dz) == 1) {
int id2 = M[mp(mp(px[i] + dx, -1), pz[i] + dz)];

if (id2) merge(id2, i);
}
}
if (px[i] == -1) {
for (int dy = 0; dy <= 1; dy++) for (int dz = 0; dz <= 1; dz++) if (abs(dy) + abs(dz) == 1) {
int id2 = M[mp(mp(-1, py[i] + dy), pz[i] + dz)];

if (id2) merge(id2, i);
}
}
}

FOE(i, 1, D) {
int g1 = 0, g2 = 0;
FOR(j, 0, x[i].size()) {
int id = x[i][j];

if (py[id] == -1) g1 = 1;
if (pz[id] == -1) g2 = 1;
}

goodX[i] = g1 * 2 + g2;
}

FOR(i, 1, D) {
if ((goodX[i] | goodX[i + 1]) == 3) {
FOR(j, 0, x[i].size()) merge(i + n, x[i][j]);
FOR(j, 0, x[i + 1].size()) merge(i + n, x[i + 1][j]);
}
}

FOE(i, 1, D) {
int g1 = 0, g2 = 0;
FOR(j, 0, y[i].size()) {
int id = y[i][j];

if (px[id] == -1) g1 = 1;
if (pz[id] == -1) g2 = 1;
}

goodY[i] = g1 * 2 + g2;
}

FOR(i, 1, D) {
if ((goodY[i] | goodY[i + 1]) == 3) {
FOR(j, 0, y[i].size()) merge(i + D + n, y[i][j]);
FOR(j, 0, y[i + 1].size()) merge(i + D + n, y[i + 1][j]);
}
}

FOE(i, 1, D) {
int g1 = 0, g2 = 0;
FOR(j, 0, z[i].size()) {
int id = z[i][j];

if (px[id] == -1) g1 = 1;
if (py[id] == -1) g2 = 1;
}

goodZ[i] = g1 * 2 + g2;
}

FOR(i, 1, D) {
if ((goodZ[i] | goodZ[i + 1]) == 3) {
FOR(j, 0, z[i].size()) merge(i + 2 * D + n, z[i][j]);
FOR(j, 0, z[i + 1].size()) merge(i + 2 * D + n, z[i + 1][j]);
}
}

scanf("%d", &q);

while (q--) {
int a1, a2, a3, a4, a5, a6;
scanf("%d%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5, &a6);
int id1 = seek(a1, a2, a3);
int id2 = seek(a4, a5, a6);

// printf("conv %d %d\n", id1, id2);

if (par(id1) == par(id2)) {
puts("YES");
} else {
puts("NO");
}
}
}

int main() {
int t; scanf("%d", &t);
while (t--) solve();
}

欢迎关注我的其它发布渠道