const int LG = 21;
const int N = 400005;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define pb push_back
#define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin())
#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL
const int K = 800;
signed main()
{
int n, m;
cin >> n >> m;
vector<map<int, int> > dist_big(n);
vector<vector<int> > dist_small(n, vector<int>(K, INF));
vector<int> b(m), p(m);
vector<vi> kek(n);
for (int i = 0; i < m; i++) cin >> b[i] >> p[i], kek[b[i]].push_back(p[i]);
for (int i = 0; i < n; i++)
{
uniq(kek[i]);
}
deque<pii> q;
int s = b[0], f = b[1];
for (auto i : kek[s])
{
if (i < K) dist_small[s][i] = 0;
else dist_big[s][i] = 0;
q.push_back({s, i});
}
vector<int> init(n, INF);
init[s] = 0;
if (f == s) cout << 0, exit(0);
while (q.size())
{
pair<int, int> x = q.front();
q.pop_front();
int di = 0;
if (x.se < K) di = dist_small[x.fi][x.se];
else di = dist_big[x.fi][x.se];
if (init[x.first] == INF)
{
for (auto j : kek[x.fi])
{
if (j < K)
{
if (dist_small[x.fi][j] == INF)
{
dist_small[x.fi][j] = di;
q.push_front({x.fi, j});
}
}
else
{
if (dist_big[x.fi].find(j) == dist_big[x.fi].end())
{
dist_big[x.fi][j] = di;
q.push_front({x.fi, j});
}
}
}
init[x.fi] = di;
if (x.fi == f) cout << init[x.fi], exit(0);
}
if (x.se < K)
{
if (x.fi - x.se >= 0 && dist_small[x.fi - x.se][x.se] == INF)
{
dist_small[x.fi - x.se][x.se] = di + 1;
q.push_back({x.fi - x.se, x.se});
}
if (x.fi + x.se < n && dist_small[x.fi + x.se][x.se] == INF)
{
dist_small[x.fi + x.se][x.se] = di + 1;
q.push_back({x.fi + x.se, x.se});
}
}
else
{
if (x.fi - x.se >= 0 && dist_big[x.fi - x.se].find(x.se) == dist_big[x.fi - x.se].end())
{
dist_big[x.fi - x.se][x.se] = di + 1;
q.push_back({x.fi - x.se, x.se});
}
if (x.fi + x.se < n && dist_big[x.fi + x.se].find(x.se) == dist_big[x.fi + x.se].end())
{
dist_big[x.fi + x.se][x.se] = di + 1;
q.push_back({x.fi + x.se, x.se});
}
}
}
cout << -1;
}
/* Note:
Check constants at the beginning of the code.
N is set to 4e5 but be careful in problems with large constant factor.
Setting N in every problem is more effective.
Check corner cases.
N = 1
No def int long long for now.
Add something here.
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
3 ms |
2816 KB |
Output is correct |
18 |
Correct |
4 ms |
6016 KB |
Output is correct |
19 |
Correct |
4 ms |
6784 KB |
Output is correct |
20 |
Correct |
5 ms |
6912 KB |
Output is correct |
21 |
Correct |
2 ms |
1664 KB |
Output is correct |
22 |
Correct |
5 ms |
6144 KB |
Output is correct |
23 |
Correct |
4 ms |
5504 KB |
Output is correct |
24 |
Correct |
6 ms |
6656 KB |
Output is correct |
25 |
Correct |
6 ms |
6912 KB |
Output is correct |
26 |
Correct |
6 ms |
6784 KB |
Output is correct |
27 |
Correct |
5 ms |
6784 KB |
Output is correct |
28 |
Correct |
7 ms |
6912 KB |
Output is correct |
29 |
Correct |
5 ms |
6784 KB |
Output is correct |
30 |
Correct |
4 ms |
6784 KB |
Output is correct |
31 |
Correct |
5 ms |
6784 KB |
Output is correct |
32 |
Correct |
4 ms |
6784 KB |
Output is correct |
33 |
Correct |
10 ms |
6784 KB |
Output is correct |
34 |
Correct |
6 ms |
6784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
3 ms |
2816 KB |
Output is correct |
18 |
Correct |
4 ms |
6016 KB |
Output is correct |
19 |
Correct |
4 ms |
6784 KB |
Output is correct |
20 |
Correct |
5 ms |
6912 KB |
Output is correct |
21 |
Correct |
2 ms |
1664 KB |
Output is correct |
22 |
Correct |
4 ms |
6144 KB |
Output is correct |
23 |
Correct |
4 ms |
5504 KB |
Output is correct |
24 |
Correct |
5 ms |
6656 KB |
Output is correct |
25 |
Correct |
5 ms |
6912 KB |
Output is correct |
26 |
Correct |
5 ms |
6784 KB |
Output is correct |
27 |
Correct |
5 ms |
6784 KB |
Output is correct |
28 |
Correct |
8 ms |
6912 KB |
Output is correct |
29 |
Correct |
5 ms |
6784 KB |
Output is correct |
30 |
Correct |
4 ms |
6784 KB |
Output is correct |
31 |
Correct |
5 ms |
6784 KB |
Output is correct |
32 |
Correct |
6 ms |
6784 KB |
Output is correct |
33 |
Correct |
8 ms |
6784 KB |
Output is correct |
34 |
Correct |
6 ms |
6912 KB |
Output is correct |
35 |
Correct |
27 ms |
6264 KB |
Output is correct |
36 |
Correct |
6 ms |
3968 KB |
Output is correct |
37 |
Correct |
26 ms |
7168 KB |
Output is correct |
38 |
Correct |
32 ms |
7928 KB |
Output is correct |
39 |
Correct |
28 ms |
7288 KB |
Output is correct |
40 |
Correct |
26 ms |
7424 KB |
Output is correct |
41 |
Correct |
27 ms |
7424 KB |
Output is correct |
42 |
Correct |
23 ms |
7296 KB |
Output is correct |
43 |
Correct |
22 ms |
7296 KB |
Output is correct |
44 |
Correct |
22 ms |
7296 KB |
Output is correct |
45 |
Correct |
34 ms |
7416 KB |
Output is correct |
46 |
Correct |
31 ms |
7416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
640 KB |
Output is correct |
13 |
Correct |
1 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
3 ms |
2816 KB |
Output is correct |
18 |
Correct |
4 ms |
6016 KB |
Output is correct |
19 |
Correct |
4 ms |
6784 KB |
Output is correct |
20 |
Correct |
5 ms |
6912 KB |
Output is correct |
21 |
Correct |
2 ms |
1664 KB |
Output is correct |
22 |
Correct |
4 ms |
6144 KB |
Output is correct |
23 |
Correct |
4 ms |
5504 KB |
Output is correct |
24 |
Correct |
5 ms |
6656 KB |
Output is correct |
25 |
Correct |
5 ms |
6912 KB |
Output is correct |
26 |
Correct |
5 ms |
6784 KB |
Output is correct |
27 |
Correct |
5 ms |
6784 KB |
Output is correct |
28 |
Correct |
8 ms |
6912 KB |
Output is correct |
29 |
Correct |
5 ms |
6784 KB |
Output is correct |
30 |
Correct |
4 ms |
6784 KB |
Output is correct |
31 |
Correct |
5 ms |
6784 KB |
Output is correct |
32 |
Correct |
5 ms |
6784 KB |
Output is correct |
33 |
Correct |
8 ms |
6784 KB |
Output is correct |
34 |
Correct |
6 ms |
6784 KB |
Output is correct |
35 |
Correct |
25 ms |
6272 KB |
Output is correct |
36 |
Correct |
6 ms |
3968 KB |
Output is correct |
37 |
Correct |
19 ms |
7296 KB |
Output is correct |
38 |
Correct |
27 ms |
7928 KB |
Output is correct |
39 |
Correct |
26 ms |
7296 KB |
Output is correct |
40 |
Correct |
26 ms |
7288 KB |
Output is correct |
41 |
Correct |
26 ms |
7416 KB |
Output is correct |
42 |
Correct |
22 ms |
7296 KB |
Output is correct |
43 |
Correct |
22 ms |
7296 KB |
Output is correct |
44 |
Correct |
22 ms |
7296 KB |
Output is correct |
45 |
Correct |
36 ms |
7424 KB |
Output is correct |
46 |
Correct |
29 ms |
7416 KB |
Output is correct |
47 |
Correct |
47 ms |
41592 KB |
Output is correct |
48 |
Correct |
57 ms |
76280 KB |
Output is correct |
49 |
Correct |
59 ms |
83320 KB |
Output is correct |
50 |
Correct |
59 ms |
91384 KB |
Output is correct |
51 |
Correct |
82 ms |
100856 KB |
Output is correct |
52 |
Correct |
89 ms |
101240 KB |
Output is correct |
53 |
Correct |
74 ms |
98808 KB |
Output is correct |
54 |
Correct |
49 ms |
97664 KB |
Output is correct |
55 |
Correct |
51 ms |
97656 KB |
Output is correct |
56 |
Correct |
69 ms |
98808 KB |
Output is correct |
57 |
Correct |
58 ms |
97784 KB |
Output is correct |
58 |
Correct |
63 ms |
98040 KB |
Output is correct |
59 |
Correct |
73 ms |
98040 KB |
Output is correct |
60 |
Correct |
71 ms |
98040 KB |
Output is correct |
61 |
Correct |
76 ms |
98296 KB |
Output is correct |
62 |
Correct |
147 ms |
105080 KB |
Output is correct |
63 |
Correct |
226 ms |
98296 KB |
Output is correct |
64 |
Correct |
231 ms |
98484 KB |
Output is correct |
65 |
Correct |
301 ms |
98552 KB |
Output is correct |
66 |
Correct |
358 ms |
98552 KB |
Output is correct |
67 |
Correct |
204 ms |
98580 KB |
Output is correct |