//In the name of God
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll maxn = 3e4 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
const int sq = 1000;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
ll pw(ll a, ll b){
ll c = 1;
while(b){
if(b & 1) c = c * a % mod;
a = a * a % mod;
b >>= 1;
}
return c;
}
map<int, bool> vis[maxn];
int n, m, start, goal, dis;
bool vs[maxn];
bool siv[sq][maxn];
vector<int> vec[maxn];
vector<int> q, qu;
void add(int v){
if(0 > v || v >= m || vs[v]) return;
vs[v] = 1;
if(v == goal){
cout << dis;
exit(0);
}
for(int x : vec[v]){
if(x < sq) siv[x][v] = 1;
else vis[x][v] = 1;
q.pb((x << 15) | v);
}
return;
}
int main(){
fast_io;
q.reserve(1000000);
qu.reserve(1000000);
cin >> m >> n;
for(int i = 0; i < n; i++){
int v, x;
cin >> v >> x;
if(i == 0) start = v;
if(i == 1) goal = v;
vec[v].pb(x);
}
add(start);
int cnt = 0;
while(!q.empty()){
dis++;
qu = q;
q.clear();
cnt += Sz(qu);
assert(cnt < 7500000);
for(int i : qu){
int x = (i >> 15);
int v = (i ^ (x << 15));
add(v - x);
add(v + x);
if(v + x < m && !(x < sq ? siv[x][v + x] : vis[x][v + x])){
if(x < sq) siv[x][v + x] = 1;
else vis[x][v + x] = 1;
q.pb((x << 15) | (v + x));
}
if(0 < v - x && !(x < sq ? siv[x][v - x] : vis[x][v - x])){
if(x < sq) siv[x][v - x] = 1;
else vis[x][v - x] = 1;
q.pb((x << 15) | (v - x));
}
}
}
cout << -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2432 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2524 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2756 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2516 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
1 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
1 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2756 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2388 KB |
Output is correct |
17 |
Correct |
3 ms |
4308 KB |
Output is correct |
18 |
Correct |
1 ms |
2388 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
1 ms |
2388 KB |
Output is correct |
23 |
Correct |
2 ms |
4180 KB |
Output is correct |
24 |
Correct |
3 ms |
5588 KB |
Output is correct |
25 |
Correct |
2 ms |
3028 KB |
Output is correct |
26 |
Correct |
1 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
3156 KB |
Output is correct |
28 |
Correct |
3 ms |
3904 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
2 ms |
2516 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
3 ms |
2772 KB |
Output is correct |
34 |
Correct |
3 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2356 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
1 ms |
2516 KB |
Output is correct |
13 |
Correct |
1 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2388 KB |
Output is correct |
17 |
Correct |
2 ms |
4308 KB |
Output is correct |
18 |
Correct |
1 ms |
2388 KB |
Output is correct |
19 |
Correct |
1 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2388 KB |
Output is correct |
23 |
Correct |
2 ms |
4180 KB |
Output is correct |
24 |
Correct |
4 ms |
5588 KB |
Output is correct |
25 |
Correct |
2 ms |
3028 KB |
Output is correct |
26 |
Correct |
1 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
3156 KB |
Output is correct |
28 |
Correct |
3 ms |
3924 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
2 ms |
2516 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
4 ms |
2772 KB |
Output is correct |
34 |
Correct |
3 ms |
2772 KB |
Output is correct |
35 |
Correct |
10 ms |
8660 KB |
Output is correct |
36 |
Correct |
3 ms |
4052 KB |
Output is correct |
37 |
Correct |
6 ms |
7892 KB |
Output is correct |
38 |
Correct |
10 ms |
9044 KB |
Output is correct |
39 |
Correct |
6 ms |
2900 KB |
Output is correct |
40 |
Correct |
6 ms |
4052 KB |
Output is correct |
41 |
Correct |
8 ms |
7124 KB |
Output is correct |
42 |
Correct |
5 ms |
2720 KB |
Output is correct |
43 |
Correct |
5 ms |
3444 KB |
Output is correct |
44 |
Correct |
6 ms |
2828 KB |
Output is correct |
45 |
Correct |
14 ms |
4308 KB |
Output is correct |
46 |
Correct |
11 ms |
4052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
1 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
1 ms |
2388 KB |
Output is correct |
6 |
Correct |
1 ms |
2388 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2416 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
1 ms |
2516 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2388 KB |
Output is correct |
17 |
Correct |
2 ms |
4308 KB |
Output is correct |
18 |
Correct |
1 ms |
2388 KB |
Output is correct |
19 |
Correct |
1 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2388 KB |
Output is correct |
23 |
Correct |
3 ms |
4252 KB |
Output is correct |
24 |
Correct |
3 ms |
5588 KB |
Output is correct |
25 |
Correct |
2 ms |
3028 KB |
Output is correct |
26 |
Correct |
2 ms |
2516 KB |
Output is correct |
27 |
Correct |
2 ms |
3156 KB |
Output is correct |
28 |
Correct |
3 ms |
3924 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2516 KB |
Output is correct |
31 |
Correct |
2 ms |
2516 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
3 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
10 ms |
8608 KB |
Output is correct |
36 |
Correct |
3 ms |
4052 KB |
Output is correct |
37 |
Correct |
6 ms |
7892 KB |
Output is correct |
38 |
Correct |
11 ms |
8968 KB |
Output is correct |
39 |
Correct |
6 ms |
2900 KB |
Output is correct |
40 |
Correct |
6 ms |
4052 KB |
Output is correct |
41 |
Correct |
8 ms |
7124 KB |
Output is correct |
42 |
Correct |
5 ms |
2644 KB |
Output is correct |
43 |
Correct |
5 ms |
3412 KB |
Output is correct |
44 |
Correct |
5 ms |
2900 KB |
Output is correct |
45 |
Correct |
12 ms |
4344 KB |
Output is correct |
46 |
Correct |
8 ms |
4052 KB |
Output is correct |
47 |
Correct |
9 ms |
9720 KB |
Output is correct |
48 |
Correct |
6 ms |
2856 KB |
Output is correct |
49 |
Correct |
6 ms |
2900 KB |
Output is correct |
50 |
Correct |
5 ms |
2772 KB |
Output is correct |
51 |
Correct |
21 ms |
8916 KB |
Output is correct |
52 |
Correct |
19 ms |
10020 KB |
Output is correct |
53 |
Correct |
8 ms |
3284 KB |
Output is correct |
54 |
Correct |
2 ms |
2516 KB |
Output is correct |
55 |
Correct |
2 ms |
2516 KB |
Output is correct |
56 |
Correct |
7 ms |
3412 KB |
Output is correct |
57 |
Correct |
2 ms |
2516 KB |
Output is correct |
58 |
Correct |
6 ms |
3392 KB |
Output is correct |
59 |
Correct |
6 ms |
3328 KB |
Output is correct |
60 |
Correct |
6 ms |
3284 KB |
Output is correct |
61 |
Correct |
7 ms |
4948 KB |
Output is correct |
62 |
Correct |
53 ms |
17152 KB |
Output is correct |
63 |
Correct |
53 ms |
14688 KB |
Output is correct |
64 |
Correct |
60 ms |
10420 KB |
Output is correct |
65 |
Correct |
65 ms |
10548 KB |
Output is correct |
66 |
Correct |
104 ms |
10288 KB |
Output is correct |
67 |
Correct |
58 ms |
7604 KB |
Output is correct |