#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;
#define x first
#define y second
#define all(v) v.begin(),v.end()
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vpii v;
map<pii, int> ord;
vector<vint> e(n + 1);
const static int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
for(int i = 1; i <= n; i++) {
pii p;
cin >> p.x >> p.y;
int cur = int(ord.size()) + 1;
ord[p] = cur;
for(int j = 0; j < 4; j++) {
pii t = {p.x + dx[j], p.y + dy[j]};
if(ord.count(t)) {
e[i].push_back(ord[t]);
e[ord[t]].push_back(i);
}
}
v.push_back(p);
}
vint gn(n + 1), sn(n + 1), id(n);
iota(all(id), 0);
sort(all(id), [&](int x, int y){ return v[x] < v[y]; });
for(int i = 0, j, k = 1; i < n; i = j, k++) {
for(j = i; j < n; j++) {
if(v[id[i]].x != v[id[j]].x || v[id[j]].y - v[id[i]].y != j - i) break;
sn[id[j] + 1] = k;
}
}
sort(all(id), [&](int x, int y){
return pii(v[x].y, v[x].x) < pii(v[y].y, v[y].x);
});
for(int i = 0, j, k = 1; i < n; i = j, k++) {
for(j = i; j < n; j++) {
if(v[id[i]].y != v[id[j]].y || v[id[j]].x - v[id[i]].x != j - i) break;
gn[id[j] + 1] = k;
}
}
const static int L = 17;
vector<vint> ge(n + 1), se(n + 1), gsp(L, vint(n + 1)), ssp(L, vint(n + 1));
vint gdep(n + 1), sdep(n + 1);
auto build_edge = [&](const vint &tn, vector<vint> &te) {
for(int i = 1; i <= n; i++) for(int j : e[i]) {
if(tn[i] != tn[j]) te[tn[i]].push_back(tn[j]);
}
for(int i = 1; i <= n; i++) {
sort(all(te[i]));
te[i].erase(unique(all(te[i])), te[i].end());
}
};
build_edge(gn, ge);
build_edge(sn, se);
function<void(const vector<vint>&, vint&, vector<vint>&, int, int, int)> init_spt = [&](
const vector<vint> &te, vint &dep, vector<vint> &sp,
int x, int y, int z) {
dep[x] = z;
sp[0][x] = y;
for(int i = 1; i < L; i++) sp[i][x] = sp[i - 1][sp[i - 1][x]];
for(int i : te[x]) if(i != y) init_spt(te, dep, sp, i, x, z + 1);
};
init_spt(ge, gdep, gsp, 1, 0, 1);
init_spt(se, sdep, ssp, 1, 0, 1);
auto dist = [&](const vector<vint> &sp, const vint &dep, int x, int y) {
int r = dep[x] + dep[y];
if(dep[x] > dep[y]) swap(x, y);
for(int i = L - 1; i >= 0; i--) {
if(((dep[y] - dep[x]) >> i) & 1) y = sp[i][y];
}
if(x == y) return r - 2 * dep[x];
for(int i = L - 1; i >= 0; i--) {
if(sp[i][y] != sp[i][x]) {
y = sp[i][y];
x = sp[i][x];
}
}
return r - 2 * (dep[x] - 1);
};
vint d(n + 1, n);
auto upd = [&](vint &v) {
queue<int> q;
for(int x : v) { d[x] = 0; q.push(x); }
while(!q.empty()) {
int x = q.front();
q.pop();
for(int i : e[x]) {
if(d[i] > d[x] + 1) {
d[i] = d[x] + 1;
q.push(i);
}
}
}
v.clear();
};
int q;
cin >> q;
vint qv;
const static int thr = 100;
for(int i = 0; i < q; i++) {
int t;
pii p;
cin >> t >> p.x >> p.y;
if(t == 1) qv.push_back(ord[p]);
else {
if(qv.size() > thr) upd(qv);
int x = ord[p];
int ans = d[x];
for(int y : qv) {
ans = min(
ans,
dist(gsp, gdep, gn[x], gn[y]) + dist(ssp, sdep, sn[x], sn[y])
);
}
cout << (ans == n ? -1 : ans) << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
36 ms |
1920 KB |
Output is correct |
5 |
Correct |
38 ms |
2168 KB |
Output is correct |
6 |
Correct |
36 ms |
2176 KB |
Output is correct |
7 |
Correct |
44 ms |
2176 KB |
Output is correct |
8 |
Correct |
45 ms |
2304 KB |
Output is correct |
9 |
Correct |
44 ms |
2312 KB |
Output is correct |
10 |
Correct |
48 ms |
2176 KB |
Output is correct |
11 |
Correct |
42 ms |
2296 KB |
Output is correct |
12 |
Correct |
46 ms |
2304 KB |
Output is correct |
13 |
Correct |
43 ms |
2304 KB |
Output is correct |
14 |
Correct |
52 ms |
2304 KB |
Output is correct |
15 |
Correct |
46 ms |
2432 KB |
Output is correct |
16 |
Correct |
48 ms |
2304 KB |
Output is correct |
17 |
Correct |
41 ms |
2304 KB |
Output is correct |
18 |
Correct |
45 ms |
2176 KB |
Output is correct |
19 |
Correct |
41 ms |
2176 KB |
Output is correct |
20 |
Correct |
47 ms |
2304 KB |
Output is correct |
21 |
Correct |
45 ms |
2304 KB |
Output is correct |
22 |
Correct |
42 ms |
2424 KB |
Output is correct |
23 |
Correct |
36 ms |
2560 KB |
Output is correct |
24 |
Correct |
38 ms |
2560 KB |
Output is correct |
25 |
Correct |
29 ms |
2816 KB |
Output is correct |
26 |
Correct |
31 ms |
2432 KB |
Output is correct |
27 |
Correct |
26 ms |
2816 KB |
Output is correct |
28 |
Correct |
33 ms |
2560 KB |
Output is correct |
29 |
Correct |
36 ms |
2560 KB |
Output is correct |
30 |
Correct |
32 ms |
2688 KB |
Output is correct |
31 |
Correct |
33 ms |
2560 KB |
Output is correct |
32 |
Correct |
36 ms |
2560 KB |
Output is correct |
33 |
Correct |
41 ms |
2560 KB |
Output is correct |
34 |
Correct |
33 ms |
2560 KB |
Output is correct |
35 |
Correct |
41 ms |
2704 KB |
Output is correct |
36 |
Correct |
28 ms |
2816 KB |
Output is correct |
37 |
Correct |
36 ms |
2560 KB |
Output is correct |
38 |
Correct |
38 ms |
2560 KB |
Output is correct |
39 |
Correct |
35 ms |
2560 KB |
Output is correct |
40 |
Correct |
35 ms |
2688 KB |
Output is correct |
41 |
Correct |
35 ms |
2560 KB |
Output is correct |
42 |
Correct |
41 ms |
2556 KB |
Output is correct |
43 |
Correct |
33 ms |
2688 KB |
Output is correct |
44 |
Correct |
35 ms |
2432 KB |
Output is correct |
45 |
Correct |
35 ms |
2560 KB |
Output is correct |
46 |
Correct |
28 ms |
2688 KB |
Output is correct |
47 |
Correct |
50 ms |
2304 KB |
Output is correct |
48 |
Correct |
43 ms |
2176 KB |
Output is correct |
49 |
Correct |
49 ms |
2176 KB |
Output is correct |
50 |
Correct |
43 ms |
2304 KB |
Output is correct |
51 |
Correct |
44 ms |
2176 KB |
Output is correct |
52 |
Correct |
39 ms |
2304 KB |
Output is correct |
53 |
Correct |
36 ms |
2432 KB |
Output is correct |
54 |
Correct |
39 ms |
2304 KB |
Output is correct |
55 |
Correct |
38 ms |
2304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
36 ms |
1920 KB |
Output is correct |
5 |
Correct |
38 ms |
2168 KB |
Output is correct |
6 |
Correct |
36 ms |
2176 KB |
Output is correct |
7 |
Correct |
44 ms |
2176 KB |
Output is correct |
8 |
Correct |
45 ms |
2304 KB |
Output is correct |
9 |
Correct |
44 ms |
2312 KB |
Output is correct |
10 |
Correct |
48 ms |
2176 KB |
Output is correct |
11 |
Correct |
42 ms |
2296 KB |
Output is correct |
12 |
Correct |
46 ms |
2304 KB |
Output is correct |
13 |
Correct |
43 ms |
2304 KB |
Output is correct |
14 |
Correct |
52 ms |
2304 KB |
Output is correct |
15 |
Correct |
46 ms |
2432 KB |
Output is correct |
16 |
Correct |
48 ms |
2304 KB |
Output is correct |
17 |
Correct |
41 ms |
2304 KB |
Output is correct |
18 |
Correct |
45 ms |
2176 KB |
Output is correct |
19 |
Correct |
41 ms |
2176 KB |
Output is correct |
20 |
Correct |
47 ms |
2304 KB |
Output is correct |
21 |
Correct |
45 ms |
2304 KB |
Output is correct |
22 |
Correct |
42 ms |
2424 KB |
Output is correct |
23 |
Correct |
36 ms |
2560 KB |
Output is correct |
24 |
Correct |
38 ms |
2560 KB |
Output is correct |
25 |
Correct |
29 ms |
2816 KB |
Output is correct |
26 |
Correct |
31 ms |
2432 KB |
Output is correct |
27 |
Correct |
26 ms |
2816 KB |
Output is correct |
28 |
Correct |
33 ms |
2560 KB |
Output is correct |
29 |
Correct |
36 ms |
2560 KB |
Output is correct |
30 |
Correct |
32 ms |
2688 KB |
Output is correct |
31 |
Correct |
33 ms |
2560 KB |
Output is correct |
32 |
Correct |
36 ms |
2560 KB |
Output is correct |
33 |
Correct |
41 ms |
2560 KB |
Output is correct |
34 |
Correct |
33 ms |
2560 KB |
Output is correct |
35 |
Correct |
41 ms |
2704 KB |
Output is correct |
36 |
Correct |
28 ms |
2816 KB |
Output is correct |
37 |
Correct |
36 ms |
2560 KB |
Output is correct |
38 |
Correct |
38 ms |
2560 KB |
Output is correct |
39 |
Correct |
35 ms |
2560 KB |
Output is correct |
40 |
Correct |
35 ms |
2688 KB |
Output is correct |
41 |
Correct |
35 ms |
2560 KB |
Output is correct |
42 |
Correct |
41 ms |
2556 KB |
Output is correct |
43 |
Correct |
33 ms |
2688 KB |
Output is correct |
44 |
Correct |
35 ms |
2432 KB |
Output is correct |
45 |
Correct |
35 ms |
2560 KB |
Output is correct |
46 |
Correct |
28 ms |
2688 KB |
Output is correct |
47 |
Correct |
50 ms |
2304 KB |
Output is correct |
48 |
Correct |
43 ms |
2176 KB |
Output is correct |
49 |
Correct |
49 ms |
2176 KB |
Output is correct |
50 |
Correct |
43 ms |
2304 KB |
Output is correct |
51 |
Correct |
44 ms |
2176 KB |
Output is correct |
52 |
Correct |
39 ms |
2304 KB |
Output is correct |
53 |
Correct |
36 ms |
2432 KB |
Output is correct |
54 |
Correct |
39 ms |
2304 KB |
Output is correct |
55 |
Correct |
38 ms |
2304 KB |
Output is correct |
56 |
Correct |
822 ms |
3104 KB |
Output is correct |
57 |
Correct |
761 ms |
3184 KB |
Output is correct |
58 |
Correct |
771 ms |
3448 KB |
Output is correct |
59 |
Correct |
1123 ms |
3744 KB |
Output is correct |
60 |
Correct |
1252 ms |
3348 KB |
Output is correct |
61 |
Correct |
1199 ms |
4016 KB |
Output is correct |
62 |
Correct |
1213 ms |
3836 KB |
Output is correct |
63 |
Correct |
1295 ms |
3788 KB |
Output is correct |
64 |
Correct |
1353 ms |
3960 KB |
Output is correct |
65 |
Correct |
1428 ms |
3376 KB |
Output is correct |
66 |
Correct |
1239 ms |
3384 KB |
Output is correct |
67 |
Correct |
1309 ms |
3448 KB |
Output is correct |
68 |
Correct |
1270 ms |
3896 KB |
Output is correct |
69 |
Correct |
1239 ms |
3320 KB |
Output is correct |
70 |
Correct |
1120 ms |
3832 KB |
Output is correct |
71 |
Correct |
1208 ms |
3540 KB |
Output is correct |
72 |
Correct |
1351 ms |
4040 KB |
Output is correct |
73 |
Correct |
1133 ms |
3332 KB |
Output is correct |
74 |
Correct |
642 ms |
4412 KB |
Output is correct |
75 |
Correct |
726 ms |
3704 KB |
Output is correct |
76 |
Correct |
631 ms |
4692 KB |
Output is correct |
77 |
Correct |
761 ms |
4220 KB |
Output is correct |
78 |
Correct |
745 ms |
4452 KB |
Output is correct |
79 |
Correct |
559 ms |
4728 KB |
Output is correct |
80 |
Correct |
751 ms |
4248 KB |
Output is correct |
81 |
Correct |
841 ms |
4344 KB |
Output is correct |
82 |
Correct |
794 ms |
3844 KB |
Output is correct |
83 |
Correct |
795 ms |
4264 KB |
Output is correct |
84 |
Correct |
565 ms |
4600 KB |
Output is correct |
85 |
Correct |
832 ms |
3960 KB |
Output is correct |
86 |
Correct |
801 ms |
3844 KB |
Output is correct |
87 |
Correct |
581 ms |
4092 KB |
Output is correct |
88 |
Correct |
1187 ms |
3960 KB |
Output is correct |
89 |
Correct |
1139 ms |
3900 KB |
Output is correct |
90 |
Correct |
1105 ms |
3896 KB |
Output is correct |
91 |
Correct |
1002 ms |
3432 KB |
Output is correct |
92 |
Correct |
910 ms |
3176 KB |
Output is correct |
93 |
Correct |
904 ms |
3760 KB |
Output is correct |
94 |
Correct |
795 ms |
4072 KB |
Output is correct |
95 |
Correct |
960 ms |
3972 KB |
Output is correct |
96 |
Correct |
850 ms |
3900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2062 ms |
49044 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2085 ms |
36528 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
36 ms |
1920 KB |
Output is correct |
5 |
Correct |
38 ms |
2168 KB |
Output is correct |
6 |
Correct |
36 ms |
2176 KB |
Output is correct |
7 |
Correct |
44 ms |
2176 KB |
Output is correct |
8 |
Correct |
45 ms |
2304 KB |
Output is correct |
9 |
Correct |
44 ms |
2312 KB |
Output is correct |
10 |
Correct |
48 ms |
2176 KB |
Output is correct |
11 |
Correct |
42 ms |
2296 KB |
Output is correct |
12 |
Correct |
46 ms |
2304 KB |
Output is correct |
13 |
Correct |
43 ms |
2304 KB |
Output is correct |
14 |
Correct |
52 ms |
2304 KB |
Output is correct |
15 |
Correct |
46 ms |
2432 KB |
Output is correct |
16 |
Correct |
48 ms |
2304 KB |
Output is correct |
17 |
Correct |
41 ms |
2304 KB |
Output is correct |
18 |
Correct |
45 ms |
2176 KB |
Output is correct |
19 |
Correct |
41 ms |
2176 KB |
Output is correct |
20 |
Correct |
47 ms |
2304 KB |
Output is correct |
21 |
Correct |
45 ms |
2304 KB |
Output is correct |
22 |
Correct |
42 ms |
2424 KB |
Output is correct |
23 |
Correct |
36 ms |
2560 KB |
Output is correct |
24 |
Correct |
38 ms |
2560 KB |
Output is correct |
25 |
Correct |
29 ms |
2816 KB |
Output is correct |
26 |
Correct |
31 ms |
2432 KB |
Output is correct |
27 |
Correct |
26 ms |
2816 KB |
Output is correct |
28 |
Correct |
33 ms |
2560 KB |
Output is correct |
29 |
Correct |
36 ms |
2560 KB |
Output is correct |
30 |
Correct |
32 ms |
2688 KB |
Output is correct |
31 |
Correct |
33 ms |
2560 KB |
Output is correct |
32 |
Correct |
36 ms |
2560 KB |
Output is correct |
33 |
Correct |
41 ms |
2560 KB |
Output is correct |
34 |
Correct |
33 ms |
2560 KB |
Output is correct |
35 |
Correct |
41 ms |
2704 KB |
Output is correct |
36 |
Correct |
28 ms |
2816 KB |
Output is correct |
37 |
Correct |
36 ms |
2560 KB |
Output is correct |
38 |
Correct |
38 ms |
2560 KB |
Output is correct |
39 |
Correct |
35 ms |
2560 KB |
Output is correct |
40 |
Correct |
35 ms |
2688 KB |
Output is correct |
41 |
Correct |
35 ms |
2560 KB |
Output is correct |
42 |
Correct |
41 ms |
2556 KB |
Output is correct |
43 |
Correct |
33 ms |
2688 KB |
Output is correct |
44 |
Correct |
35 ms |
2432 KB |
Output is correct |
45 |
Correct |
35 ms |
2560 KB |
Output is correct |
46 |
Correct |
28 ms |
2688 KB |
Output is correct |
47 |
Correct |
50 ms |
2304 KB |
Output is correct |
48 |
Correct |
43 ms |
2176 KB |
Output is correct |
49 |
Correct |
49 ms |
2176 KB |
Output is correct |
50 |
Correct |
43 ms |
2304 KB |
Output is correct |
51 |
Correct |
44 ms |
2176 KB |
Output is correct |
52 |
Correct |
39 ms |
2304 KB |
Output is correct |
53 |
Correct |
36 ms |
2432 KB |
Output is correct |
54 |
Correct |
39 ms |
2304 KB |
Output is correct |
55 |
Correct |
38 ms |
2304 KB |
Output is correct |
56 |
Correct |
822 ms |
3104 KB |
Output is correct |
57 |
Correct |
761 ms |
3184 KB |
Output is correct |
58 |
Correct |
771 ms |
3448 KB |
Output is correct |
59 |
Correct |
1123 ms |
3744 KB |
Output is correct |
60 |
Correct |
1252 ms |
3348 KB |
Output is correct |
61 |
Correct |
1199 ms |
4016 KB |
Output is correct |
62 |
Correct |
1213 ms |
3836 KB |
Output is correct |
63 |
Correct |
1295 ms |
3788 KB |
Output is correct |
64 |
Correct |
1353 ms |
3960 KB |
Output is correct |
65 |
Correct |
1428 ms |
3376 KB |
Output is correct |
66 |
Correct |
1239 ms |
3384 KB |
Output is correct |
67 |
Correct |
1309 ms |
3448 KB |
Output is correct |
68 |
Correct |
1270 ms |
3896 KB |
Output is correct |
69 |
Correct |
1239 ms |
3320 KB |
Output is correct |
70 |
Correct |
1120 ms |
3832 KB |
Output is correct |
71 |
Correct |
1208 ms |
3540 KB |
Output is correct |
72 |
Correct |
1351 ms |
4040 KB |
Output is correct |
73 |
Correct |
1133 ms |
3332 KB |
Output is correct |
74 |
Correct |
642 ms |
4412 KB |
Output is correct |
75 |
Correct |
726 ms |
3704 KB |
Output is correct |
76 |
Correct |
631 ms |
4692 KB |
Output is correct |
77 |
Correct |
761 ms |
4220 KB |
Output is correct |
78 |
Correct |
745 ms |
4452 KB |
Output is correct |
79 |
Correct |
559 ms |
4728 KB |
Output is correct |
80 |
Correct |
751 ms |
4248 KB |
Output is correct |
81 |
Correct |
841 ms |
4344 KB |
Output is correct |
82 |
Correct |
794 ms |
3844 KB |
Output is correct |
83 |
Correct |
795 ms |
4264 KB |
Output is correct |
84 |
Correct |
565 ms |
4600 KB |
Output is correct |
85 |
Correct |
832 ms |
3960 KB |
Output is correct |
86 |
Correct |
801 ms |
3844 KB |
Output is correct |
87 |
Correct |
581 ms |
4092 KB |
Output is correct |
88 |
Correct |
1187 ms |
3960 KB |
Output is correct |
89 |
Correct |
1139 ms |
3900 KB |
Output is correct |
90 |
Correct |
1105 ms |
3896 KB |
Output is correct |
91 |
Correct |
1002 ms |
3432 KB |
Output is correct |
92 |
Correct |
910 ms |
3176 KB |
Output is correct |
93 |
Correct |
904 ms |
3760 KB |
Output is correct |
94 |
Correct |
795 ms |
4072 KB |
Output is correct |
95 |
Correct |
960 ms |
3972 KB |
Output is correct |
96 |
Correct |
850 ms |
3900 KB |
Output is correct |
97 |
Execution timed out |
2062 ms |
49044 KB |
Time limit exceeded |
98 |
Halted |
0 ms |
0 KB |
- |