#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define fast ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int inf = 1e9;
const ll INF = 1e18;
int n;
unordered_map<int, int> ans;
struct T{
int n;
vector<int> g[100010];
map<pii, int> m;
int p[100010];
int par[100010];
int sum[100010];
int d[100010];
int sp[100010][21];
bool chk[100010];
int dis[100010][21];
int Find(int a) { return p[a] = p[a] == a ? a : Find(p[a]); }
void Union(int a, int b) { if(Find(a) != Find(b)) p[Find(b)] = Find(a); }
void dfs(int x) {
for(int i=1; i<=20; i++) sp[x][i] = sp[sp[x][i-1]][i-1];
sum[x] = 1;
for(auto i : g[x]) {
if(i == sp[x][0]) continue;
sp[i][0] = x;
d[i] = d[x] + 1;
dfs(i);
sum[x] += sum[i];
}
}
int lca(int a, int b) {
if(d[a] > d[b]) swap(a, b);
for(int i=20; i>=0; i--) if(d[b] - d[a] >= (1 << i)) b = sp[b][i];
if(a == b) return a;
for(int i=20; i>=0; i--) if(sp[a][i] != sp[b][i]) a = sp[a][i], b = sp[b][i];
return sp[a][0];
}
int dist(int a, int b) { return d[a] + d[b] - 2 * d[lca(a, b)]; }
int Cen(int x) {
int s = 1, mx = -1, mxi;
for(auto i : g[x]) {
if(chk[i]) continue;
s += sum[i];
if(sum[i] > mx) {
mx = sum[i];
mxi = i;
}
}
if(mx > s/2) {
sum[x] = s - mx;
return Cen(mxi);
}
return x;
}
void makeCen(int x, int p) {
int c = Cen(x);
par[c] = p;
chk[c] = true;
for(auto i : g[c]) {
if(i == p || chk[i]) continue;
makeCen(i, c);
}
}
void init() {
n = m.size();
for(int i=1; i<=n; i++) {
p[i] = i;
d[i] = 0;
sum[i] = 0;
sp[i][0] = 0;
}
for(auto i : m) {
int x = i.fi.fi;
int y = i.fi.se;
if(m.find(mp(x, y+1)) != m.end()) Union(m[mp(x, y+1)], i.se);
if(m.find(mp(x, y-1)) != m.end()) Union(m[mp(x, y-1)], i.se);
}
for(auto i : m) {
int x = i.fi.fi;
int y = i.fi.se;
if(m.find(mp(x+1, y)) != m.end()) {
int j = m[mp(x+1, y)];
g[Find(j)].eb(Find(i.se));
}
if(m.find(mp(x-1, y)) != m.end()) {
int j = m[mp(x-1, y)];
g[Find(j)].eb(Find(i.se));
}
}
for(int i=1; i<=n; i++) {
sort(all(g[i]));
g[i].erase(unique(all(g[i])), g[i].end());
}
dfs(Find(1));
makeCen(Find(1), 0);
for(int i=1; i<=n; i++) {
if(i != Find(i)) continue;
for(int j=0, k = i; k && j<=20; j++, k = par[k]) {
dis[i][j] = dist(i, k);
}
}
}
} X, Y;
int main() {
fast;
int n;
cin >> n;
for(int i=1; i<=n; i++) {
int x, y;
cin >> x >> y;
X.m[mp(x, y)] = i;
Y.m[mp(y, x)] = i;
}
X.init();
Y.init();
int q;
cin >> q;
while(q--) {
int T, x, y;
cin >> T >> x >> y;
int u = X.Find(X.m[mp(x, y)]);
int v = Y.Find(Y.m[mp(y, x)]);
if(T == 1) {
for(int i = u, c1 = 0; i; i = X.par[i], c1++) {
for(int j = v, c2 = 0; j; j = Y.par[j], c2++) {
ans[n * i + j] = min(ans[n * i + j], X.dis[u][c1] + Y.dis[v][c2] - inf);
}
}
}
else {
int ret = inf;
for(int i = u, c1 = 0; i; i = X.par[i], c1++) {
for(int j = v, c2 = 0; j; j = Y.par[j], c2++) {
ret = min(ret, ans[n * i + j] + X.dis[u][c1] + Y.dis[v][c2] + inf);
}
}
if(ret == inf) cout << "-1\n";
else cout << ret << "\n";
}
}
}
Compilation message
C.cpp: In member function 'void T::makeCen(int, int)':
C.cpp:67:23: warning: 'mxi' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | int s = 1, mx = -1, mxi;
| ^~~
C.cpp: In member function 'void T::init()':
C.cpp:67:23: warning: 'mxi' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
25 ms |
7680 KB |
Output is correct |
5 |
Correct |
25 ms |
7808 KB |
Output is correct |
6 |
Correct |
27 ms |
8056 KB |
Output is correct |
7 |
Correct |
38 ms |
8832 KB |
Output is correct |
8 |
Correct |
40 ms |
8952 KB |
Output is correct |
9 |
Correct |
41 ms |
8956 KB |
Output is correct |
10 |
Correct |
36 ms |
8704 KB |
Output is correct |
11 |
Correct |
40 ms |
8952 KB |
Output is correct |
12 |
Correct |
39 ms |
8952 KB |
Output is correct |
13 |
Correct |
39 ms |
9080 KB |
Output is correct |
14 |
Correct |
38 ms |
8952 KB |
Output is correct |
15 |
Correct |
40 ms |
8952 KB |
Output is correct |
16 |
Correct |
39 ms |
8952 KB |
Output is correct |
17 |
Correct |
38 ms |
8832 KB |
Output is correct |
18 |
Correct |
41 ms |
8824 KB |
Output is correct |
19 |
Correct |
44 ms |
8696 KB |
Output is correct |
20 |
Correct |
42 ms |
9080 KB |
Output is correct |
21 |
Correct |
41 ms |
8952 KB |
Output is correct |
22 |
Correct |
48 ms |
9604 KB |
Output is correct |
23 |
Correct |
55 ms |
9604 KB |
Output is correct |
24 |
Correct |
51 ms |
9732 KB |
Output is correct |
25 |
Correct |
28 ms |
8456 KB |
Output is correct |
26 |
Correct |
29 ms |
8864 KB |
Output is correct |
27 |
Correct |
25 ms |
7928 KB |
Output is correct |
28 |
Correct |
51 ms |
9732 KB |
Output is correct |
29 |
Correct |
74 ms |
9732 KB |
Output is correct |
30 |
Correct |
52 ms |
9860 KB |
Output is correct |
31 |
Correct |
64 ms |
9860 KB |
Output is correct |
32 |
Correct |
51 ms |
9732 KB |
Output is correct |
33 |
Correct |
54 ms |
9740 KB |
Output is correct |
34 |
Correct |
57 ms |
9732 KB |
Output is correct |
35 |
Correct |
53 ms |
9860 KB |
Output is correct |
36 |
Correct |
30 ms |
7936 KB |
Output is correct |
37 |
Correct |
52 ms |
9860 KB |
Output is correct |
38 |
Correct |
56 ms |
9860 KB |
Output is correct |
39 |
Correct |
60 ms |
9732 KB |
Output is correct |
40 |
Correct |
61 ms |
9996 KB |
Output is correct |
41 |
Correct |
48 ms |
9724 KB |
Output is correct |
42 |
Correct |
55 ms |
9604 KB |
Output is correct |
43 |
Correct |
26 ms |
8320 KB |
Output is correct |
44 |
Correct |
52 ms |
9604 KB |
Output is correct |
45 |
Correct |
49 ms |
9848 KB |
Output is correct |
46 |
Correct |
27 ms |
8440 KB |
Output is correct |
47 |
Correct |
36 ms |
8696 KB |
Output is correct |
48 |
Correct |
34 ms |
8704 KB |
Output is correct |
49 |
Correct |
42 ms |
8336 KB |
Output is correct |
50 |
Correct |
41 ms |
8312 KB |
Output is correct |
51 |
Correct |
29 ms |
8192 KB |
Output is correct |
52 |
Correct |
39 ms |
8824 KB |
Output is correct |
53 |
Correct |
38 ms |
8828 KB |
Output is correct |
54 |
Correct |
51 ms |
8824 KB |
Output is correct |
55 |
Correct |
41 ms |
8824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
25 ms |
7680 KB |
Output is correct |
5 |
Correct |
25 ms |
7808 KB |
Output is correct |
6 |
Correct |
27 ms |
8056 KB |
Output is correct |
7 |
Correct |
38 ms |
8832 KB |
Output is correct |
8 |
Correct |
40 ms |
8952 KB |
Output is correct |
9 |
Correct |
41 ms |
8956 KB |
Output is correct |
10 |
Correct |
36 ms |
8704 KB |
Output is correct |
11 |
Correct |
40 ms |
8952 KB |
Output is correct |
12 |
Correct |
39 ms |
8952 KB |
Output is correct |
13 |
Correct |
39 ms |
9080 KB |
Output is correct |
14 |
Correct |
38 ms |
8952 KB |
Output is correct |
15 |
Correct |
40 ms |
8952 KB |
Output is correct |
16 |
Correct |
39 ms |
8952 KB |
Output is correct |
17 |
Correct |
38 ms |
8832 KB |
Output is correct |
18 |
Correct |
41 ms |
8824 KB |
Output is correct |
19 |
Correct |
44 ms |
8696 KB |
Output is correct |
20 |
Correct |
42 ms |
9080 KB |
Output is correct |
21 |
Correct |
41 ms |
8952 KB |
Output is correct |
22 |
Correct |
48 ms |
9604 KB |
Output is correct |
23 |
Correct |
55 ms |
9604 KB |
Output is correct |
24 |
Correct |
51 ms |
9732 KB |
Output is correct |
25 |
Correct |
28 ms |
8456 KB |
Output is correct |
26 |
Correct |
29 ms |
8864 KB |
Output is correct |
27 |
Correct |
25 ms |
7928 KB |
Output is correct |
28 |
Correct |
51 ms |
9732 KB |
Output is correct |
29 |
Correct |
74 ms |
9732 KB |
Output is correct |
30 |
Correct |
52 ms |
9860 KB |
Output is correct |
31 |
Correct |
64 ms |
9860 KB |
Output is correct |
32 |
Correct |
51 ms |
9732 KB |
Output is correct |
33 |
Correct |
54 ms |
9740 KB |
Output is correct |
34 |
Correct |
57 ms |
9732 KB |
Output is correct |
35 |
Correct |
53 ms |
9860 KB |
Output is correct |
36 |
Correct |
30 ms |
7936 KB |
Output is correct |
37 |
Correct |
52 ms |
9860 KB |
Output is correct |
38 |
Correct |
56 ms |
9860 KB |
Output is correct |
39 |
Correct |
60 ms |
9732 KB |
Output is correct |
40 |
Correct |
61 ms |
9996 KB |
Output is correct |
41 |
Correct |
48 ms |
9724 KB |
Output is correct |
42 |
Correct |
55 ms |
9604 KB |
Output is correct |
43 |
Correct |
26 ms |
8320 KB |
Output is correct |
44 |
Correct |
52 ms |
9604 KB |
Output is correct |
45 |
Correct |
49 ms |
9848 KB |
Output is correct |
46 |
Correct |
27 ms |
8440 KB |
Output is correct |
47 |
Correct |
36 ms |
8696 KB |
Output is correct |
48 |
Correct |
34 ms |
8704 KB |
Output is correct |
49 |
Correct |
42 ms |
8336 KB |
Output is correct |
50 |
Correct |
41 ms |
8312 KB |
Output is correct |
51 |
Correct |
29 ms |
8192 KB |
Output is correct |
52 |
Correct |
39 ms |
8824 KB |
Output is correct |
53 |
Correct |
38 ms |
8828 KB |
Output is correct |
54 |
Correct |
51 ms |
8824 KB |
Output is correct |
55 |
Correct |
41 ms |
8824 KB |
Output is correct |
56 |
Correct |
208 ms |
9080 KB |
Output is correct |
57 |
Correct |
233 ms |
8952 KB |
Output is correct |
58 |
Correct |
185 ms |
9208 KB |
Output is correct |
59 |
Correct |
344 ms |
10964 KB |
Output is correct |
60 |
Correct |
332 ms |
10528 KB |
Output is correct |
61 |
Correct |
313 ms |
11100 KB |
Output is correct |
62 |
Correct |
319 ms |
10884 KB |
Output is correct |
63 |
Correct |
312 ms |
11012 KB |
Output is correct |
64 |
Correct |
340 ms |
11136 KB |
Output is correct |
65 |
Correct |
338 ms |
10756 KB |
Output is correct |
66 |
Correct |
319 ms |
10672 KB |
Output is correct |
67 |
Correct |
378 ms |
10528 KB |
Output is correct |
68 |
Correct |
328 ms |
11012 KB |
Output is correct |
69 |
Correct |
393 ms |
10132 KB |
Output is correct |
70 |
Correct |
323 ms |
11012 KB |
Output is correct |
71 |
Correct |
349 ms |
10232 KB |
Output is correct |
72 |
Correct |
364 ms |
11140 KB |
Output is correct |
73 |
Correct |
308 ms |
10628 KB |
Output is correct |
74 |
Correct |
144 ms |
10360 KB |
Output is correct |
75 |
Correct |
183 ms |
10364 KB |
Output is correct |
76 |
Correct |
113 ms |
9592 KB |
Output is correct |
77 |
Correct |
550 ms |
11628 KB |
Output is correct |
78 |
Correct |
512 ms |
11832 KB |
Output is correct |
79 |
Correct |
121 ms |
9724 KB |
Output is correct |
80 |
Correct |
504 ms |
11864 KB |
Output is correct |
81 |
Correct |
616 ms |
11576 KB |
Output is correct |
82 |
Correct |
436 ms |
11140 KB |
Output is correct |
83 |
Correct |
463 ms |
11396 KB |
Output is correct |
84 |
Correct |
134 ms |
10104 KB |
Output is correct |
85 |
Correct |
493 ms |
11268 KB |
Output is correct |
86 |
Correct |
445 ms |
11012 KB |
Output is correct |
87 |
Correct |
129 ms |
9592 KB |
Output is correct |
88 |
Correct |
315 ms |
10360 KB |
Output is correct |
89 |
Correct |
287 ms |
10232 KB |
Output is correct |
90 |
Correct |
263 ms |
10236 KB |
Output is correct |
91 |
Correct |
282 ms |
9464 KB |
Output is correct |
92 |
Correct |
212 ms |
9208 KB |
Output is correct |
93 |
Correct |
366 ms |
10104 KB |
Output is correct |
94 |
Correct |
336 ms |
10616 KB |
Output is correct |
95 |
Correct |
401 ms |
10488 KB |
Output is correct |
96 |
Correct |
408 ms |
10488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1266 ms |
64180 KB |
Output is correct |
2 |
Correct |
1234 ms |
62908 KB |
Output is correct |
3 |
Execution timed out |
2086 ms |
97188 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2081 ms |
90468 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
25 ms |
7680 KB |
Output is correct |
5 |
Correct |
25 ms |
7808 KB |
Output is correct |
6 |
Correct |
27 ms |
8056 KB |
Output is correct |
7 |
Correct |
38 ms |
8832 KB |
Output is correct |
8 |
Correct |
40 ms |
8952 KB |
Output is correct |
9 |
Correct |
41 ms |
8956 KB |
Output is correct |
10 |
Correct |
36 ms |
8704 KB |
Output is correct |
11 |
Correct |
40 ms |
8952 KB |
Output is correct |
12 |
Correct |
39 ms |
8952 KB |
Output is correct |
13 |
Correct |
39 ms |
9080 KB |
Output is correct |
14 |
Correct |
38 ms |
8952 KB |
Output is correct |
15 |
Correct |
40 ms |
8952 KB |
Output is correct |
16 |
Correct |
39 ms |
8952 KB |
Output is correct |
17 |
Correct |
38 ms |
8832 KB |
Output is correct |
18 |
Correct |
41 ms |
8824 KB |
Output is correct |
19 |
Correct |
44 ms |
8696 KB |
Output is correct |
20 |
Correct |
42 ms |
9080 KB |
Output is correct |
21 |
Correct |
41 ms |
8952 KB |
Output is correct |
22 |
Correct |
48 ms |
9604 KB |
Output is correct |
23 |
Correct |
55 ms |
9604 KB |
Output is correct |
24 |
Correct |
51 ms |
9732 KB |
Output is correct |
25 |
Correct |
28 ms |
8456 KB |
Output is correct |
26 |
Correct |
29 ms |
8864 KB |
Output is correct |
27 |
Correct |
25 ms |
7928 KB |
Output is correct |
28 |
Correct |
51 ms |
9732 KB |
Output is correct |
29 |
Correct |
74 ms |
9732 KB |
Output is correct |
30 |
Correct |
52 ms |
9860 KB |
Output is correct |
31 |
Correct |
64 ms |
9860 KB |
Output is correct |
32 |
Correct |
51 ms |
9732 KB |
Output is correct |
33 |
Correct |
54 ms |
9740 KB |
Output is correct |
34 |
Correct |
57 ms |
9732 KB |
Output is correct |
35 |
Correct |
53 ms |
9860 KB |
Output is correct |
36 |
Correct |
30 ms |
7936 KB |
Output is correct |
37 |
Correct |
52 ms |
9860 KB |
Output is correct |
38 |
Correct |
56 ms |
9860 KB |
Output is correct |
39 |
Correct |
60 ms |
9732 KB |
Output is correct |
40 |
Correct |
61 ms |
9996 KB |
Output is correct |
41 |
Correct |
48 ms |
9724 KB |
Output is correct |
42 |
Correct |
55 ms |
9604 KB |
Output is correct |
43 |
Correct |
26 ms |
8320 KB |
Output is correct |
44 |
Correct |
52 ms |
9604 KB |
Output is correct |
45 |
Correct |
49 ms |
9848 KB |
Output is correct |
46 |
Correct |
27 ms |
8440 KB |
Output is correct |
47 |
Correct |
36 ms |
8696 KB |
Output is correct |
48 |
Correct |
34 ms |
8704 KB |
Output is correct |
49 |
Correct |
42 ms |
8336 KB |
Output is correct |
50 |
Correct |
41 ms |
8312 KB |
Output is correct |
51 |
Correct |
29 ms |
8192 KB |
Output is correct |
52 |
Correct |
39 ms |
8824 KB |
Output is correct |
53 |
Correct |
38 ms |
8828 KB |
Output is correct |
54 |
Correct |
51 ms |
8824 KB |
Output is correct |
55 |
Correct |
41 ms |
8824 KB |
Output is correct |
56 |
Correct |
208 ms |
9080 KB |
Output is correct |
57 |
Correct |
233 ms |
8952 KB |
Output is correct |
58 |
Correct |
185 ms |
9208 KB |
Output is correct |
59 |
Correct |
344 ms |
10964 KB |
Output is correct |
60 |
Correct |
332 ms |
10528 KB |
Output is correct |
61 |
Correct |
313 ms |
11100 KB |
Output is correct |
62 |
Correct |
319 ms |
10884 KB |
Output is correct |
63 |
Correct |
312 ms |
11012 KB |
Output is correct |
64 |
Correct |
340 ms |
11136 KB |
Output is correct |
65 |
Correct |
338 ms |
10756 KB |
Output is correct |
66 |
Correct |
319 ms |
10672 KB |
Output is correct |
67 |
Correct |
378 ms |
10528 KB |
Output is correct |
68 |
Correct |
328 ms |
11012 KB |
Output is correct |
69 |
Correct |
393 ms |
10132 KB |
Output is correct |
70 |
Correct |
323 ms |
11012 KB |
Output is correct |
71 |
Correct |
349 ms |
10232 KB |
Output is correct |
72 |
Correct |
364 ms |
11140 KB |
Output is correct |
73 |
Correct |
308 ms |
10628 KB |
Output is correct |
74 |
Correct |
144 ms |
10360 KB |
Output is correct |
75 |
Correct |
183 ms |
10364 KB |
Output is correct |
76 |
Correct |
113 ms |
9592 KB |
Output is correct |
77 |
Correct |
550 ms |
11628 KB |
Output is correct |
78 |
Correct |
512 ms |
11832 KB |
Output is correct |
79 |
Correct |
121 ms |
9724 KB |
Output is correct |
80 |
Correct |
504 ms |
11864 KB |
Output is correct |
81 |
Correct |
616 ms |
11576 KB |
Output is correct |
82 |
Correct |
436 ms |
11140 KB |
Output is correct |
83 |
Correct |
463 ms |
11396 KB |
Output is correct |
84 |
Correct |
134 ms |
10104 KB |
Output is correct |
85 |
Correct |
493 ms |
11268 KB |
Output is correct |
86 |
Correct |
445 ms |
11012 KB |
Output is correct |
87 |
Correct |
129 ms |
9592 KB |
Output is correct |
88 |
Correct |
315 ms |
10360 KB |
Output is correct |
89 |
Correct |
287 ms |
10232 KB |
Output is correct |
90 |
Correct |
263 ms |
10236 KB |
Output is correct |
91 |
Correct |
282 ms |
9464 KB |
Output is correct |
92 |
Correct |
212 ms |
9208 KB |
Output is correct |
93 |
Correct |
366 ms |
10104 KB |
Output is correct |
94 |
Correct |
336 ms |
10616 KB |
Output is correct |
95 |
Correct |
401 ms |
10488 KB |
Output is correct |
96 |
Correct |
408 ms |
10488 KB |
Output is correct |
97 |
Correct |
1266 ms |
64180 KB |
Output is correct |
98 |
Correct |
1234 ms |
62908 KB |
Output is correct |
99 |
Execution timed out |
2086 ms |
97188 KB |
Time limit exceeded |
100 |
Halted |
0 ms |
0 KB |
- |