#include <bits/stdc++.h>
using namespace std;
int n ,m ,q;
vector <bool> road;
vector <int> prv;
vector <int> nxt;
int dist(int x ,int y){
return min(abs(x-y), n+n - abs(x-y));
}
int dist(int x ,int k ,int y){
return dist(x ,k) + 1 + dist(k < n? k+n : k-n ,y);
}
void process(){
prv.resize(n+n);
nxt.resize(n+n);
nxt[0] = n+n;
for(int i = 0; i < n+n; i++) if(road[i]){
prv[0] = max(prv[0], i);
nxt[0] = min(nxt[0], i);
}
int z = n+n-1;
prv[0] = (road[0]? 0 : prv[0]);
nxt[z] = (road[z]? z : nxt[0]);
for(int i = 1; i <= z; i++)
prv[i] = (road[i]? i : prv[i-1]);
for(int i = z-1; i >= 0; i--)
nxt[i] = (road[i]? i : nxt[i+1]);
}
int solve(int x ,int y){
if(m == 0)
return dist(x ,y);
return min({dist(x ,y)
,dist(x ,prv[x] ,y)
,dist(x ,nxt[x] ,y)});
}
int main()
{
scanf("%d%d%d",&n,&m,&q);
road.resize(n+n);
for(int a, i = 0; i < m; i++){
scanf("%d",&a);
road[a] = true;
road[a+n] = true;
}
process();
while(q--){
int x ,y;
scanf("%d%d",&x,&y);
printf("%d\n",solve(x ,y));
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d%d%d",&n,&m,&q);
| ~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d",&a);
| ~~~~~^~~~~~~~~
Main.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d%d",&x,&y);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
2360 KB |
Output is correct |
6 |
Runtime error |
406 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
388 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
360 KB |
Output is correct |
34 |
Correct |
0 ms |
360 KB |
Output is correct |
35 |
Correct |
0 ms |
356 KB |
Output is correct |
36 |
Correct |
0 ms |
360 KB |
Output is correct |
37 |
Correct |
62 ms |
107608 KB |
Output is correct |
38 |
Correct |
65 ms |
108836 KB |
Output is correct |
39 |
Correct |
51 ms |
91344 KB |
Output is correct |
40 |
Correct |
80 ms |
132176 KB |
Output is correct |
41 |
Correct |
52 ms |
95600 KB |
Output is correct |
42 |
Correct |
94 ms |
154504 KB |
Output is correct |
43 |
Correct |
25 ms |
50008 KB |
Output is correct |
44 |
Correct |
89 ms |
149876 KB |
Output is correct |
45 |
Correct |
95 ms |
159348 KB |
Output is correct |
46 |
Correct |
95 ms |
159364 KB |
Output is correct |
47 |
Correct |
92 ms |
159312 KB |
Output is correct |
48 |
Correct |
99 ms |
159244 KB |
Output is correct |
49 |
Correct |
0 ms |
344 KB |
Output is correct |
50 |
Correct |
0 ms |
604 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
411 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
2360 KB |
Output is correct |
6 |
Runtime error |
406 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |