#pragma GCC optimize("O3")
#pragma GCC target("sse2,sse3,ssse3,sse4,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define form2(i, a, b) for (int i = (a); i < (b); ++i)
#define ford2(i, a, b) for (int i = (a-1); i >= (b); --i)
#define form(i, n) form2(i, 0, n)
#define ford(i, n) ford2(i, n, 0)
#define chmax(x, v) x = max(x, (v))
#define chmin(x, v) x = min(x, (v))
#define fi first
#define se second
const int BIG = 2*(int)(1e9);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
void solve();
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
const int borneSuj = 30*1000 + 5;
const int borneTab = 200*borneSuj;
int gt[borneSuj][200];
int inv[borneTab];
bool witPrev[borneTab];
bool witSuiv[borneTab];
int arr = -1, dep=-1;
int nbPlaces, nbDoges, nbNoeuds;
int limSqd;
vector<int> doge[borneSuj];
void solve()
{
cin >> nbPlaces >> nbDoges;
chmax(nbPlaces, 20);
nbNoeuds = nbPlaces;
limSqd = sqrt(nbPlaces);
form2(distance, 1, limSqd) {
form(ini, distance) {
for (int place = ini; place < nbPlaces; place += distance) {
int corNod = nbNoeuds; ++nbNoeuds;
inv[corNod] = place+1;
gt[place][distance] = corNod;
if (place != ini) {
witPrev[corNod] = true;
witSuiv[corNod-1] = true;
}
}
}
}
form(iDoge, nbDoges) {
int pos, saut;
cin >> pos >> saut;
if (iDoge == 0) dep = pos;
if (iDoge == 1) arr = pos;
doge[pos].push_back(saut);
}
vector<int> md(nbNoeuds, BIG);
vector<int> etat(nbNoeuds, 2);
deque<int> m1;
m1.push_front(dep);
md[dep] = 0;
while (! m1.empty()) {
int nod = m1.front(); m1.pop_front();
etat[nod] = 0;
int dis = md[nod];
auto propag = [&] (int vo, int poids) {
if (etat[vo] == 2) {
m1.push_back(vo);
etat[vo] = 1;
md[vo] = dis + poids;
} else if (etat[vo] == 1) {
chmin(md[vo], dis + poids);
} else {
if (dis+poids < md[vo]) {
md[vo] = dis+poids;
etat[vo] = 1;
m1.push_back(vo);
}
}
};
if (nod < nbPlaces) {
int pos = nod;
for (int saut : doge[nod]) {
if (saut < limSqd) {
propag(gt[pos][saut], 0);
} else {
for (int dd = 1; pos + dd*saut < nbPlaces; ++dd) {
propag(pos+dd*saut, dd);
}
for (int dd = 1; pos - dd*saut >= 0; ++dd) {
propag(pos-dd*saut, dd);
}
}
}
}
if (nod >= nbPlaces) {
if (inv[nod]) propag(inv[nod]-1, 0);
if (witPrev[nod]) propag(nod-1, 1);
if (witSuiv[nod]) propag(nod+1, 1);
}
}
if (md[arr] == BIG) cout << "-1\n";
else cout << md[arr] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1024 KB |
Output is correct |
2 |
Correct |
2 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1152 KB |
Output is correct |
4 |
Correct |
3 ms |
1152 KB |
Output is correct |
5 |
Correct |
3 ms |
1152 KB |
Output is correct |
6 |
Correct |
3 ms |
1152 KB |
Output is correct |
7 |
Correct |
3 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1152 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
4 ms |
1024 KB |
Output is correct |
8 |
Correct |
3 ms |
1152 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
4 ms |
1152 KB |
Output is correct |
11 |
Correct |
3 ms |
1152 KB |
Output is correct |
12 |
Correct |
3 ms |
1152 KB |
Output is correct |
13 |
Correct |
3 ms |
1152 KB |
Output is correct |
14 |
Correct |
3 ms |
1152 KB |
Output is correct |
15 |
Correct |
3 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1024 KB |
Output is correct |
2 |
Correct |
4 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1152 KB |
Output is correct |
7 |
Correct |
4 ms |
1152 KB |
Output is correct |
8 |
Correct |
3 ms |
1152 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
3 ms |
1152 KB |
Output is correct |
11 |
Correct |
4 ms |
1152 KB |
Output is correct |
12 |
Correct |
4 ms |
1152 KB |
Output is correct |
13 |
Correct |
4 ms |
1152 KB |
Output is correct |
14 |
Correct |
3 ms |
1152 KB |
Output is correct |
15 |
Correct |
3 ms |
1152 KB |
Output is correct |
16 |
Correct |
3 ms |
1280 KB |
Output is correct |
17 |
Correct |
4 ms |
1920 KB |
Output is correct |
18 |
Correct |
6 ms |
3456 KB |
Output is correct |
19 |
Correct |
6 ms |
3840 KB |
Output is correct |
20 |
Correct |
7 ms |
3968 KB |
Output is correct |
21 |
Correct |
4 ms |
1536 KB |
Output is correct |
22 |
Correct |
7 ms |
3556 KB |
Output is correct |
23 |
Correct |
6 ms |
3196 KB |
Output is correct |
24 |
Correct |
7 ms |
3712 KB |
Output is correct |
25 |
Correct |
6 ms |
3840 KB |
Output is correct |
26 |
Correct |
7 ms |
3840 KB |
Output is correct |
27 |
Correct |
6 ms |
3840 KB |
Output is correct |
28 |
Correct |
9 ms |
3968 KB |
Output is correct |
29 |
Correct |
7 ms |
3840 KB |
Output is correct |
30 |
Correct |
6 ms |
3840 KB |
Output is correct |
31 |
Correct |
7 ms |
3840 KB |
Output is correct |
32 |
Correct |
7 ms |
3840 KB |
Output is correct |
33 |
Correct |
8 ms |
3840 KB |
Output is correct |
34 |
Correct |
14 ms |
3840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
2 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1152 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
3 ms |
1152 KB |
Output is correct |
8 |
Correct |
4 ms |
1152 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
4 ms |
1152 KB |
Output is correct |
11 |
Correct |
4 ms |
1152 KB |
Output is correct |
12 |
Correct |
4 ms |
1152 KB |
Output is correct |
13 |
Correct |
5 ms |
1152 KB |
Output is correct |
14 |
Correct |
5 ms |
1152 KB |
Output is correct |
15 |
Correct |
4 ms |
1152 KB |
Output is correct |
16 |
Correct |
3 ms |
1280 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
5 ms |
3456 KB |
Output is correct |
19 |
Correct |
7 ms |
3812 KB |
Output is correct |
20 |
Correct |
8 ms |
3940 KB |
Output is correct |
21 |
Correct |
4 ms |
1536 KB |
Output is correct |
22 |
Correct |
6 ms |
3584 KB |
Output is correct |
23 |
Correct |
5 ms |
3200 KB |
Output is correct |
24 |
Correct |
7 ms |
3840 KB |
Output is correct |
25 |
Correct |
7 ms |
3968 KB |
Output is correct |
26 |
Correct |
7 ms |
3840 KB |
Output is correct |
27 |
Correct |
6 ms |
3840 KB |
Output is correct |
28 |
Correct |
7 ms |
3840 KB |
Output is correct |
29 |
Correct |
7 ms |
3840 KB |
Output is correct |
30 |
Correct |
7 ms |
3840 KB |
Output is correct |
31 |
Correct |
7 ms |
3840 KB |
Output is correct |
32 |
Correct |
10 ms |
3840 KB |
Output is correct |
33 |
Correct |
10 ms |
3840 KB |
Output is correct |
34 |
Correct |
9 ms |
3840 KB |
Output is correct |
35 |
Correct |
15 ms |
3200 KB |
Output is correct |
36 |
Correct |
5 ms |
2432 KB |
Output is correct |
37 |
Correct |
14 ms |
3968 KB |
Output is correct |
38 |
Correct |
15 ms |
4096 KB |
Output is correct |
39 |
Correct |
16 ms |
4096 KB |
Output is correct |
40 |
Correct |
27 ms |
4096 KB |
Output is correct |
41 |
Correct |
20 ms |
4096 KB |
Output is correct |
42 |
Correct |
15 ms |
4096 KB |
Output is correct |
43 |
Correct |
10 ms |
4096 KB |
Output is correct |
44 |
Correct |
11 ms |
4096 KB |
Output is correct |
45 |
Correct |
20 ms |
4096 KB |
Output is correct |
46 |
Correct |
16 ms |
3968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1152 KB |
Output is correct |
2 |
Correct |
3 ms |
1152 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1152 KB |
Output is correct |
5 |
Correct |
3 ms |
1152 KB |
Output is correct |
6 |
Correct |
3 ms |
1152 KB |
Output is correct |
7 |
Correct |
3 ms |
1152 KB |
Output is correct |
8 |
Correct |
3 ms |
1152 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
3 ms |
1152 KB |
Output is correct |
11 |
Correct |
4 ms |
1152 KB |
Output is correct |
12 |
Correct |
3 ms |
1152 KB |
Output is correct |
13 |
Correct |
3 ms |
1152 KB |
Output is correct |
14 |
Correct |
4 ms |
1152 KB |
Output is correct |
15 |
Correct |
4 ms |
1152 KB |
Output is correct |
16 |
Correct |
3 ms |
1280 KB |
Output is correct |
17 |
Correct |
5 ms |
1920 KB |
Output is correct |
18 |
Correct |
7 ms |
3456 KB |
Output is correct |
19 |
Correct |
7 ms |
3840 KB |
Output is correct |
20 |
Correct |
8 ms |
3968 KB |
Output is correct |
21 |
Correct |
3 ms |
1536 KB |
Output is correct |
22 |
Correct |
7 ms |
3584 KB |
Output is correct |
23 |
Correct |
6 ms |
3200 KB |
Output is correct |
24 |
Correct |
6 ms |
3712 KB |
Output is correct |
25 |
Correct |
9 ms |
3968 KB |
Output is correct |
26 |
Correct |
10 ms |
3840 KB |
Output is correct |
27 |
Correct |
10 ms |
3840 KB |
Output is correct |
28 |
Correct |
8 ms |
3840 KB |
Output is correct |
29 |
Correct |
7 ms |
3840 KB |
Output is correct |
30 |
Correct |
6 ms |
3840 KB |
Output is correct |
31 |
Correct |
7 ms |
3840 KB |
Output is correct |
32 |
Correct |
6 ms |
3840 KB |
Output is correct |
33 |
Correct |
9 ms |
3840 KB |
Output is correct |
34 |
Correct |
9 ms |
3840 KB |
Output is correct |
35 |
Correct |
12 ms |
3200 KB |
Output is correct |
36 |
Correct |
5 ms |
2432 KB |
Output is correct |
37 |
Correct |
14 ms |
3968 KB |
Output is correct |
38 |
Correct |
16 ms |
4096 KB |
Output is correct |
39 |
Correct |
14 ms |
4096 KB |
Output is correct |
40 |
Correct |
15 ms |
4096 KB |
Output is correct |
41 |
Correct |
20 ms |
4096 KB |
Output is correct |
42 |
Correct |
12 ms |
4096 KB |
Output is correct |
43 |
Correct |
12 ms |
4096 KB |
Output is correct |
44 |
Correct |
13 ms |
4096 KB |
Output is correct |
45 |
Correct |
18 ms |
4048 KB |
Output is correct |
46 |
Correct |
21 ms |
4096 KB |
Output is correct |
47 |
Correct |
156 ms |
29776 KB |
Output is correct |
48 |
Correct |
248 ms |
67996 KB |
Output is correct |
49 |
Correct |
341 ms |
76488 KB |
Output is correct |
50 |
Correct |
338 ms |
87112 KB |
Output is correct |
51 |
Correct |
385 ms |
96156 KB |
Output is correct |
52 |
Correct |
597 ms |
96124 KB |
Output is correct |
53 |
Correct |
422 ms |
96124 KB |
Output is correct |
54 |
Correct |
411 ms |
95576 KB |
Output is correct |
55 |
Correct |
308 ms |
95680 KB |
Output is correct |
56 |
Correct |
387 ms |
96504 KB |
Output is correct |
57 |
Correct |
436 ms |
95512 KB |
Output is correct |
58 |
Correct |
376 ms |
95836 KB |
Output is correct |
59 |
Correct |
402 ms |
95896 KB |
Output is correct |
60 |
Correct |
335 ms |
95864 KB |
Output is correct |
61 |
Correct |
381 ms |
95736 KB |
Output is correct |
62 |
Correct |
277 ms |
96520 KB |
Output is correct |
63 |
Correct |
493 ms |
95832 KB |
Output is correct |
64 |
Correct |
330 ms |
95840 KB |
Output is correct |
65 |
Correct |
570 ms |
95996 KB |
Output is correct |
66 |
Correct |
760 ms |
95840 KB |
Output is correct |
67 |
Correct |
940 ms |
96360 KB |
Output is correct |