#include <bits/stdc++.h>
using namespace std;
typedef pair <int, int> pii;
struct point{
int x, y, i;
point() {}
point(int x, int y, int i) : x(x), y(y), i(i) {}
};
const int sz = 100;
vector <point> P;
vector <pii> K;
queue <pii> Q;
vector <int> T[202020], V[101010];
int U[20][202020], W[202020];
int X[101010], Y[101010], D[101010];
bool chk[101010];
int n, k;
void dfs(int p, int r)
{
U[0][p] = r; W[p] = W[r] + 1;
for(int &t: T[p]){
if(t != r) dfs(t, p);
}
}
int dist(int p, int q)
{
int i, ret;
ret = 0;
if(W[p] < W[q]) swap(p, q);
for(i=0; i<18; i++){
if(W[p] - W[q] & (1 << i)){
p = U[i][p];
ret += 1 << i;
}
}
if(p == q) return ret;
for(i--; i>=0; i--){
if(U[i][p] != U[i][q]){
p = U[i][p]; q = U[i][q];
ret += 2 << i;
}
}
return ret + 2;
}
void bfs()
{
int i, p, d;
for(i=0; i<n; i++){
chk[i] = 0;
}
for(pii &t: K){
chk[t.first] = 1;
Q.push(pii(t.first, 0));
}
K.clear();
for(; !Q.empty(); ){
tie(p, d) = Q.front(); Q.pop();
D[p] = min(D[p], d);
for(int &t: V[p]){
if(!chk[t]){
Q.push(pii(t, d + 1));
chk[t] = 1;
}
}
}
}
int query(int p)
{
int ret;
ret = D[p];
for(pii &t: K){
ret = min(ret, dist(X[p], X[t.first]) + dist(Y[p], Y[t.first]));
}
return ret < 1e9? ret : -1;
}
int main()
{
int q, i, j, x, y, t;
scanf("%d", &n);
for(i=0; i<n; i++){
scanf("%d%d", &x, &y);
P.emplace_back(x, y, i);
D[i] = 1e9;
}
sort(P.begin(), P.end(), [&](point &pa, point &pb){
if(pa.x != pb.x) return pa.x < pb.x;
else return pa.y < pb.y;
});
for(i=0; i<n; i=j){
k ++;
for(j=i; j<n && P[i].x == P[j].x && P[i].y == P[j].y - j + i; j++){
X[P[j].i] = k;
}
}
sort(P.begin(), P.end(), [&](point &pa, point &pb){
if(pa.y != pb.y) return pa.y < pb.y;
else return pa.x < pb.x;
});
for(i=0; i<n; i=j){
k ++;
for(j=i; j<n && P[i].y == P[j].y && P[i].x == P[j].x - j + i; j++){
Y[P[j].i] = k;
}
}
for(i=1; i<n; i++){
if(P[i - 1].y == P[i].y && P[i - 1].x + 1 == P[i].x){
T[X[P[i].i]].push_back(X[P[i - 1].i]);
T[X[P[i - 1].i]].push_back(X[P[i].i]);
V[P[i].i].push_back(P[i - 1].i);
V[P[i - 1].i].push_back(P[i].i);
}
}
sort(P.begin(), P.end(), [&](point &pa, point &pb){
if(pa.x != pb.x) return pa.x < pb.x;
else return pa.y < pb.y;
});
for(i=1; i<n; i++){
if(P[i - 1].x == P[i].x && P[i - 1].y + 1 == P[i].y){
T[Y[P[i].i]].push_back(Y[P[i - 1].i]);
T[Y[P[i - 1].i]].push_back(Y[P[i].i]);
V[P[i].i].push_back(P[i - 1].i);
V[P[i - 1].i].push_back(P[i].i);
}
}
for(i=1; i<=k; i++){
sort(T[i].begin(), T[i].end());
T[i].erase(unique(T[i].begin(), T[i].end()), T[i].end());
}
dfs(1, 0); dfs(k, 0);
for(i=1; i<18; i++){
for(j=1; j<=k; j++){
U[i][j] = U[i - 1][U[i - 1][j]];
}
}
scanf("%d", &q);
for(; q--; ){
scanf("%d%d%d", &t, &x, &y);
i = lower_bound(P.begin(), P.end(), point(x, y, -1), [&](point pa, point pb){
if(pa.x != pb.x) return pa.x < pb.x;
else return pa.y < pb.y;
}) -> i;
if(t == 1) K.push_back(pii(i, 0));
else printf("%d\n", query(i));
if(K.size() > sz) bfs();
}
return 0;
}
Compilation message
C.cpp: In function 'int dist(int, int)':
C.cpp:42:11: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
if(W[p] - W[q] & (1 << i)){
~~~~~^~~~~~
C.cpp: In function 'int main()':
C.cpp:104:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
C.cpp:107:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
C.cpp:173:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
C.cpp:176:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &t, &x, &y);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7552 KB |
Output is correct |
2 |
Correct |
8 ms |
7552 KB |
Output is correct |
3 |
Correct |
10 ms |
7552 KB |
Output is correct |
4 |
Correct |
35 ms |
7936 KB |
Output is correct |
5 |
Correct |
36 ms |
7936 KB |
Output is correct |
6 |
Correct |
35 ms |
8064 KB |
Output is correct |
7 |
Correct |
44 ms |
8320 KB |
Output is correct |
8 |
Correct |
45 ms |
8384 KB |
Output is correct |
9 |
Correct |
45 ms |
8376 KB |
Output is correct |
10 |
Correct |
49 ms |
8320 KB |
Output is correct |
11 |
Correct |
46 ms |
8320 KB |
Output is correct |
12 |
Correct |
43 ms |
8444 KB |
Output is correct |
13 |
Correct |
47 ms |
8440 KB |
Output is correct |
14 |
Correct |
50 ms |
8320 KB |
Output is correct |
15 |
Correct |
44 ms |
8448 KB |
Output is correct |
16 |
Correct |
46 ms |
8440 KB |
Output is correct |
17 |
Correct |
44 ms |
8380 KB |
Output is correct |
18 |
Correct |
42 ms |
8320 KB |
Output is correct |
19 |
Correct |
40 ms |
8320 KB |
Output is correct |
20 |
Correct |
46 ms |
8568 KB |
Output is correct |
21 |
Correct |
42 ms |
8448 KB |
Output is correct |
22 |
Correct |
42 ms |
8448 KB |
Output is correct |
23 |
Correct |
41 ms |
8448 KB |
Output is correct |
24 |
Correct |
40 ms |
8536 KB |
Output is correct |
25 |
Correct |
33 ms |
8576 KB |
Output is correct |
26 |
Correct |
34 ms |
8448 KB |
Output is correct |
27 |
Correct |
32 ms |
8624 KB |
Output is correct |
28 |
Correct |
36 ms |
8576 KB |
Output is correct |
29 |
Correct |
37 ms |
8700 KB |
Output is correct |
30 |
Correct |
36 ms |
8576 KB |
Output is correct |
31 |
Correct |
36 ms |
8576 KB |
Output is correct |
32 |
Correct |
38 ms |
8576 KB |
Output is correct |
33 |
Correct |
39 ms |
8576 KB |
Output is correct |
34 |
Correct |
39 ms |
8456 KB |
Output is correct |
35 |
Correct |
43 ms |
8448 KB |
Output is correct |
36 |
Correct |
31 ms |
8588 KB |
Output is correct |
37 |
Correct |
36 ms |
8696 KB |
Output is correct |
38 |
Correct |
35 ms |
8448 KB |
Output is correct |
39 |
Correct |
36 ms |
8576 KB |
Output is correct |
40 |
Correct |
52 ms |
8576 KB |
Output is correct |
41 |
Correct |
37 ms |
8448 KB |
Output is correct |
42 |
Correct |
37 ms |
8448 KB |
Output is correct |
43 |
Correct |
31 ms |
8448 KB |
Output is correct |
44 |
Correct |
37 ms |
8448 KB |
Output is correct |
45 |
Correct |
39 ms |
8448 KB |
Output is correct |
46 |
Correct |
32 ms |
8620 KB |
Output is correct |
47 |
Correct |
44 ms |
8192 KB |
Output is correct |
48 |
Correct |
41 ms |
8184 KB |
Output is correct |
49 |
Correct |
39 ms |
8064 KB |
Output is correct |
50 |
Correct |
41 ms |
8064 KB |
Output is correct |
51 |
Correct |
37 ms |
8064 KB |
Output is correct |
52 |
Correct |
40 ms |
8320 KB |
Output is correct |
53 |
Correct |
37 ms |
8320 KB |
Output is correct |
54 |
Correct |
40 ms |
8312 KB |
Output is correct |
55 |
Correct |
40 ms |
8312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7552 KB |
Output is correct |
2 |
Correct |
8 ms |
7552 KB |
Output is correct |
3 |
Correct |
10 ms |
7552 KB |
Output is correct |
4 |
Correct |
35 ms |
7936 KB |
Output is correct |
5 |
Correct |
36 ms |
7936 KB |
Output is correct |
6 |
Correct |
35 ms |
8064 KB |
Output is correct |
7 |
Correct |
44 ms |
8320 KB |
Output is correct |
8 |
Correct |
45 ms |
8384 KB |
Output is correct |
9 |
Correct |
45 ms |
8376 KB |
Output is correct |
10 |
Correct |
49 ms |
8320 KB |
Output is correct |
11 |
Correct |
46 ms |
8320 KB |
Output is correct |
12 |
Correct |
43 ms |
8444 KB |
Output is correct |
13 |
Correct |
47 ms |
8440 KB |
Output is correct |
14 |
Correct |
50 ms |
8320 KB |
Output is correct |
15 |
Correct |
44 ms |
8448 KB |
Output is correct |
16 |
Correct |
46 ms |
8440 KB |
Output is correct |
17 |
Correct |
44 ms |
8380 KB |
Output is correct |
18 |
Correct |
42 ms |
8320 KB |
Output is correct |
19 |
Correct |
40 ms |
8320 KB |
Output is correct |
20 |
Correct |
46 ms |
8568 KB |
Output is correct |
21 |
Correct |
42 ms |
8448 KB |
Output is correct |
22 |
Correct |
42 ms |
8448 KB |
Output is correct |
23 |
Correct |
41 ms |
8448 KB |
Output is correct |
24 |
Correct |
40 ms |
8536 KB |
Output is correct |
25 |
Correct |
33 ms |
8576 KB |
Output is correct |
26 |
Correct |
34 ms |
8448 KB |
Output is correct |
27 |
Correct |
32 ms |
8624 KB |
Output is correct |
28 |
Correct |
36 ms |
8576 KB |
Output is correct |
29 |
Correct |
37 ms |
8700 KB |
Output is correct |
30 |
Correct |
36 ms |
8576 KB |
Output is correct |
31 |
Correct |
36 ms |
8576 KB |
Output is correct |
32 |
Correct |
38 ms |
8576 KB |
Output is correct |
33 |
Correct |
39 ms |
8576 KB |
Output is correct |
34 |
Correct |
39 ms |
8456 KB |
Output is correct |
35 |
Correct |
43 ms |
8448 KB |
Output is correct |
36 |
Correct |
31 ms |
8588 KB |
Output is correct |
37 |
Correct |
36 ms |
8696 KB |
Output is correct |
38 |
Correct |
35 ms |
8448 KB |
Output is correct |
39 |
Correct |
36 ms |
8576 KB |
Output is correct |
40 |
Correct |
52 ms |
8576 KB |
Output is correct |
41 |
Correct |
37 ms |
8448 KB |
Output is correct |
42 |
Correct |
37 ms |
8448 KB |
Output is correct |
43 |
Correct |
31 ms |
8448 KB |
Output is correct |
44 |
Correct |
37 ms |
8448 KB |
Output is correct |
45 |
Correct |
39 ms |
8448 KB |
Output is correct |
46 |
Correct |
32 ms |
8620 KB |
Output is correct |
47 |
Correct |
44 ms |
8192 KB |
Output is correct |
48 |
Correct |
41 ms |
8184 KB |
Output is correct |
49 |
Correct |
39 ms |
8064 KB |
Output is correct |
50 |
Correct |
41 ms |
8064 KB |
Output is correct |
51 |
Correct |
37 ms |
8064 KB |
Output is correct |
52 |
Correct |
40 ms |
8320 KB |
Output is correct |
53 |
Correct |
37 ms |
8320 KB |
Output is correct |
54 |
Correct |
40 ms |
8312 KB |
Output is correct |
55 |
Correct |
40 ms |
8312 KB |
Output is correct |
56 |
Correct |
719 ms |
8388 KB |
Output is correct |
57 |
Correct |
697 ms |
8368 KB |
Output is correct |
58 |
Correct |
732 ms |
8456 KB |
Output is correct |
59 |
Correct |
1112 ms |
8844 KB |
Output is correct |
60 |
Correct |
1094 ms |
8964 KB |
Output is correct |
61 |
Correct |
1072 ms |
8824 KB |
Output is correct |
62 |
Correct |
1063 ms |
8952 KB |
Output is correct |
63 |
Correct |
1192 ms |
8952 KB |
Output is correct |
64 |
Correct |
1182 ms |
8976 KB |
Output is correct |
65 |
Correct |
1115 ms |
8944 KB |
Output is correct |
66 |
Correct |
1118 ms |
8904 KB |
Output is correct |
67 |
Correct |
1128 ms |
8824 KB |
Output is correct |
68 |
Correct |
1064 ms |
8984 KB |
Output is correct |
69 |
Correct |
1056 ms |
8944 KB |
Output is correct |
70 |
Correct |
1134 ms |
8784 KB |
Output is correct |
71 |
Correct |
1073 ms |
8860 KB |
Output is correct |
72 |
Correct |
1178 ms |
8716 KB |
Output is correct |
73 |
Correct |
1098 ms |
8824 KB |
Output is correct |
74 |
Correct |
666 ms |
9080 KB |
Output is correct |
75 |
Correct |
733 ms |
8824 KB |
Output is correct |
76 |
Correct |
679 ms |
8952 KB |
Output is correct |
77 |
Correct |
807 ms |
9116 KB |
Output is correct |
78 |
Correct |
827 ms |
8964 KB |
Output is correct |
79 |
Correct |
662 ms |
9080 KB |
Output is correct |
80 |
Correct |
819 ms |
9080 KB |
Output is correct |
81 |
Correct |
950 ms |
9080 KB |
Output is correct |
82 |
Correct |
831 ms |
9080 KB |
Output is correct |
83 |
Correct |
853 ms |
8924 KB |
Output is correct |
84 |
Correct |
683 ms |
8952 KB |
Output is correct |
85 |
Correct |
852 ms |
8952 KB |
Output is correct |
86 |
Correct |
829 ms |
8952 KB |
Output is correct |
87 |
Correct |
652 ms |
8824 KB |
Output is correct |
88 |
Correct |
1094 ms |
8632 KB |
Output is correct |
89 |
Correct |
931 ms |
8496 KB |
Output is correct |
90 |
Correct |
905 ms |
8572 KB |
Output is correct |
91 |
Correct |
919 ms |
8508 KB |
Output is correct |
92 |
Correct |
838 ms |
8440 KB |
Output is correct |
93 |
Correct |
874 ms |
8720 KB |
Output is correct |
94 |
Correct |
842 ms |
8672 KB |
Output is correct |
95 |
Correct |
889 ms |
8580 KB |
Output is correct |
96 |
Correct |
855 ms |
8588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2053 ms |
28640 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2041 ms |
23952 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7552 KB |
Output is correct |
2 |
Correct |
8 ms |
7552 KB |
Output is correct |
3 |
Correct |
10 ms |
7552 KB |
Output is correct |
4 |
Correct |
35 ms |
7936 KB |
Output is correct |
5 |
Correct |
36 ms |
7936 KB |
Output is correct |
6 |
Correct |
35 ms |
8064 KB |
Output is correct |
7 |
Correct |
44 ms |
8320 KB |
Output is correct |
8 |
Correct |
45 ms |
8384 KB |
Output is correct |
9 |
Correct |
45 ms |
8376 KB |
Output is correct |
10 |
Correct |
49 ms |
8320 KB |
Output is correct |
11 |
Correct |
46 ms |
8320 KB |
Output is correct |
12 |
Correct |
43 ms |
8444 KB |
Output is correct |
13 |
Correct |
47 ms |
8440 KB |
Output is correct |
14 |
Correct |
50 ms |
8320 KB |
Output is correct |
15 |
Correct |
44 ms |
8448 KB |
Output is correct |
16 |
Correct |
46 ms |
8440 KB |
Output is correct |
17 |
Correct |
44 ms |
8380 KB |
Output is correct |
18 |
Correct |
42 ms |
8320 KB |
Output is correct |
19 |
Correct |
40 ms |
8320 KB |
Output is correct |
20 |
Correct |
46 ms |
8568 KB |
Output is correct |
21 |
Correct |
42 ms |
8448 KB |
Output is correct |
22 |
Correct |
42 ms |
8448 KB |
Output is correct |
23 |
Correct |
41 ms |
8448 KB |
Output is correct |
24 |
Correct |
40 ms |
8536 KB |
Output is correct |
25 |
Correct |
33 ms |
8576 KB |
Output is correct |
26 |
Correct |
34 ms |
8448 KB |
Output is correct |
27 |
Correct |
32 ms |
8624 KB |
Output is correct |
28 |
Correct |
36 ms |
8576 KB |
Output is correct |
29 |
Correct |
37 ms |
8700 KB |
Output is correct |
30 |
Correct |
36 ms |
8576 KB |
Output is correct |
31 |
Correct |
36 ms |
8576 KB |
Output is correct |
32 |
Correct |
38 ms |
8576 KB |
Output is correct |
33 |
Correct |
39 ms |
8576 KB |
Output is correct |
34 |
Correct |
39 ms |
8456 KB |
Output is correct |
35 |
Correct |
43 ms |
8448 KB |
Output is correct |
36 |
Correct |
31 ms |
8588 KB |
Output is correct |
37 |
Correct |
36 ms |
8696 KB |
Output is correct |
38 |
Correct |
35 ms |
8448 KB |
Output is correct |
39 |
Correct |
36 ms |
8576 KB |
Output is correct |
40 |
Correct |
52 ms |
8576 KB |
Output is correct |
41 |
Correct |
37 ms |
8448 KB |
Output is correct |
42 |
Correct |
37 ms |
8448 KB |
Output is correct |
43 |
Correct |
31 ms |
8448 KB |
Output is correct |
44 |
Correct |
37 ms |
8448 KB |
Output is correct |
45 |
Correct |
39 ms |
8448 KB |
Output is correct |
46 |
Correct |
32 ms |
8620 KB |
Output is correct |
47 |
Correct |
44 ms |
8192 KB |
Output is correct |
48 |
Correct |
41 ms |
8184 KB |
Output is correct |
49 |
Correct |
39 ms |
8064 KB |
Output is correct |
50 |
Correct |
41 ms |
8064 KB |
Output is correct |
51 |
Correct |
37 ms |
8064 KB |
Output is correct |
52 |
Correct |
40 ms |
8320 KB |
Output is correct |
53 |
Correct |
37 ms |
8320 KB |
Output is correct |
54 |
Correct |
40 ms |
8312 KB |
Output is correct |
55 |
Correct |
40 ms |
8312 KB |
Output is correct |
56 |
Correct |
719 ms |
8388 KB |
Output is correct |
57 |
Correct |
697 ms |
8368 KB |
Output is correct |
58 |
Correct |
732 ms |
8456 KB |
Output is correct |
59 |
Correct |
1112 ms |
8844 KB |
Output is correct |
60 |
Correct |
1094 ms |
8964 KB |
Output is correct |
61 |
Correct |
1072 ms |
8824 KB |
Output is correct |
62 |
Correct |
1063 ms |
8952 KB |
Output is correct |
63 |
Correct |
1192 ms |
8952 KB |
Output is correct |
64 |
Correct |
1182 ms |
8976 KB |
Output is correct |
65 |
Correct |
1115 ms |
8944 KB |
Output is correct |
66 |
Correct |
1118 ms |
8904 KB |
Output is correct |
67 |
Correct |
1128 ms |
8824 KB |
Output is correct |
68 |
Correct |
1064 ms |
8984 KB |
Output is correct |
69 |
Correct |
1056 ms |
8944 KB |
Output is correct |
70 |
Correct |
1134 ms |
8784 KB |
Output is correct |
71 |
Correct |
1073 ms |
8860 KB |
Output is correct |
72 |
Correct |
1178 ms |
8716 KB |
Output is correct |
73 |
Correct |
1098 ms |
8824 KB |
Output is correct |
74 |
Correct |
666 ms |
9080 KB |
Output is correct |
75 |
Correct |
733 ms |
8824 KB |
Output is correct |
76 |
Correct |
679 ms |
8952 KB |
Output is correct |
77 |
Correct |
807 ms |
9116 KB |
Output is correct |
78 |
Correct |
827 ms |
8964 KB |
Output is correct |
79 |
Correct |
662 ms |
9080 KB |
Output is correct |
80 |
Correct |
819 ms |
9080 KB |
Output is correct |
81 |
Correct |
950 ms |
9080 KB |
Output is correct |
82 |
Correct |
831 ms |
9080 KB |
Output is correct |
83 |
Correct |
853 ms |
8924 KB |
Output is correct |
84 |
Correct |
683 ms |
8952 KB |
Output is correct |
85 |
Correct |
852 ms |
8952 KB |
Output is correct |
86 |
Correct |
829 ms |
8952 KB |
Output is correct |
87 |
Correct |
652 ms |
8824 KB |
Output is correct |
88 |
Correct |
1094 ms |
8632 KB |
Output is correct |
89 |
Correct |
931 ms |
8496 KB |
Output is correct |
90 |
Correct |
905 ms |
8572 KB |
Output is correct |
91 |
Correct |
919 ms |
8508 KB |
Output is correct |
92 |
Correct |
838 ms |
8440 KB |
Output is correct |
93 |
Correct |
874 ms |
8720 KB |
Output is correct |
94 |
Correct |
842 ms |
8672 KB |
Output is correct |
95 |
Correct |
889 ms |
8580 KB |
Output is correct |
96 |
Correct |
855 ms |
8588 KB |
Output is correct |
97 |
Execution timed out |
2053 ms |
28640 KB |
Time limit exceeded |
98 |
Halted |
0 ms |
0 KB |
- |