#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e4, maxb = 175, maxv = 1.05e7, maxe = 2.1e7, magic = 1 << 20;
int n, m, cnt, bnd, s, t, l, r, Q[magic + 3], pnt[maxb + 3][maxn + 3], tot, msk[maxe + 3], nxt[maxe + 3], lnk[maxv + 3], dist[maxv + 3];
bool vis[maxb + 3][maxb + 3];
void add(int u, int v, int w) {
msk[++tot] = v * 2 + w;
nxt[tot] = lnk[u], lnk[u] = tot;
}
int main() {
scanf("%d %d", &n, &m);
cnt = n, bnd = sqrt(m) + .5;
for (int i = 1, b, p; i <= m; i++) {
scanf("%d %d", &b, &p);
b++;
if (i == 1) s = b;
if (i == 2) t = b;
if (p > bnd) {
int x = ++cnt, u = x, v;
add(b, x, 0);
for (int i = b - p; i >= 1; i -= p) {
v = ++cnt;
add(u, v, 1);
add(v, i, 0);
u = v;
}
u = x;
for (int i = b + p; i <= n; i += p) {
v = ++cnt;
add(u, v, 1);
add(v, i, 0);
u = v;
}
} else {
if (!vis[p][b % p]) {
vis[p][b % p] = 1;
int x = (b - 1) % p + 1;
for (int i = x; i <= n; i += p) {
add(pnt[p][i] = ++cnt, i, 0);
if (i > x) {
add(pnt[p][i], pnt[p][i - p], 1);
add(pnt[p][i - p], pnt[p][i], 1);
}
}
}
add(b, pnt[p][b], 0);
}
}
memset(dist, -1, sizeof(dist));
Q[r++] = s;
dist[s] = 0;
while (l != r) {
int u = Q[l++];
if (l == magic) l = 0;
if (u == t) break;
for (int i = lnk[u], v, w; i; i = nxt[i]) {
v = msk[i] >> 1, w = msk[i] & 1;
if (~dist[v]) continue;
dist[v] = dist[u] + w;
if (w) {
Q[r++] = v;
if (r == magic) r = 0;
} else {
if (--l < 0) l = magic - 1;
Q[l] = v;
}
}
}
printf("%d\n", dist[t]);
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:20:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:26:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | scanf("%d %d", &b, &p);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
41420 KB |
Output is correct |
2 |
Correct |
19 ms |
41340 KB |
Output is correct |
3 |
Correct |
19 ms |
41316 KB |
Output is correct |
4 |
Correct |
18 ms |
41420 KB |
Output is correct |
5 |
Correct |
18 ms |
41344 KB |
Output is correct |
6 |
Correct |
20 ms |
41420 KB |
Output is correct |
7 |
Correct |
19 ms |
41324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
41420 KB |
Output is correct |
2 |
Correct |
19 ms |
41416 KB |
Output is correct |
3 |
Correct |
19 ms |
41364 KB |
Output is correct |
4 |
Correct |
19 ms |
41324 KB |
Output is correct |
5 |
Correct |
21 ms |
41408 KB |
Output is correct |
6 |
Correct |
19 ms |
41340 KB |
Output is correct |
7 |
Correct |
20 ms |
41420 KB |
Output is correct |
8 |
Correct |
19 ms |
41352 KB |
Output is correct |
9 |
Correct |
19 ms |
41364 KB |
Output is correct |
10 |
Correct |
21 ms |
41548 KB |
Output is correct |
11 |
Correct |
20 ms |
41676 KB |
Output is correct |
12 |
Correct |
19 ms |
41472 KB |
Output is correct |
13 |
Correct |
21 ms |
41424 KB |
Output is correct |
14 |
Correct |
20 ms |
41688 KB |
Output is correct |
15 |
Correct |
19 ms |
41692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
41420 KB |
Output is correct |
2 |
Correct |
17 ms |
41336 KB |
Output is correct |
3 |
Correct |
19 ms |
41416 KB |
Output is correct |
4 |
Correct |
20 ms |
41340 KB |
Output is correct |
5 |
Correct |
19 ms |
41340 KB |
Output is correct |
6 |
Correct |
19 ms |
41428 KB |
Output is correct |
7 |
Correct |
20 ms |
41412 KB |
Output is correct |
8 |
Correct |
19 ms |
41420 KB |
Output is correct |
9 |
Correct |
21 ms |
41420 KB |
Output is correct |
10 |
Correct |
22 ms |
41504 KB |
Output is correct |
11 |
Correct |
20 ms |
41676 KB |
Output is correct |
12 |
Correct |
19 ms |
41420 KB |
Output is correct |
13 |
Correct |
19 ms |
41416 KB |
Output is correct |
14 |
Correct |
19 ms |
41764 KB |
Output is correct |
15 |
Correct |
19 ms |
41676 KB |
Output is correct |
16 |
Correct |
20 ms |
41548 KB |
Output is correct |
17 |
Correct |
20 ms |
42012 KB |
Output is correct |
18 |
Correct |
19 ms |
41640 KB |
Output is correct |
19 |
Correct |
19 ms |
41540 KB |
Output is correct |
20 |
Correct |
19 ms |
41420 KB |
Output is correct |
21 |
Correct |
19 ms |
41548 KB |
Output is correct |
22 |
Correct |
19 ms |
41620 KB |
Output is correct |
23 |
Correct |
20 ms |
41676 KB |
Output is correct |
24 |
Correct |
20 ms |
42016 KB |
Output is correct |
25 |
Correct |
22 ms |
41568 KB |
Output is correct |
26 |
Correct |
19 ms |
41928 KB |
Output is correct |
27 |
Correct |
19 ms |
41788 KB |
Output is correct |
28 |
Correct |
20 ms |
42444 KB |
Output is correct |
29 |
Correct |
22 ms |
43344 KB |
Output is correct |
30 |
Correct |
19 ms |
41860 KB |
Output is correct |
31 |
Correct |
21 ms |
42236 KB |
Output is correct |
32 |
Correct |
19 ms |
42000 KB |
Output is correct |
33 |
Correct |
27 ms |
45332 KB |
Output is correct |
34 |
Correct |
28 ms |
45128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
41352 KB |
Output is correct |
2 |
Correct |
19 ms |
41420 KB |
Output is correct |
3 |
Correct |
19 ms |
41420 KB |
Output is correct |
4 |
Correct |
20 ms |
41416 KB |
Output is correct |
5 |
Correct |
20 ms |
41416 KB |
Output is correct |
6 |
Correct |
18 ms |
41420 KB |
Output is correct |
7 |
Correct |
19 ms |
41424 KB |
Output is correct |
8 |
Correct |
20 ms |
41364 KB |
Output is correct |
9 |
Correct |
19 ms |
41436 KB |
Output is correct |
10 |
Correct |
20 ms |
41572 KB |
Output is correct |
11 |
Correct |
21 ms |
41724 KB |
Output is correct |
12 |
Correct |
20 ms |
41444 KB |
Output is correct |
13 |
Correct |
20 ms |
41460 KB |
Output is correct |
14 |
Correct |
20 ms |
41804 KB |
Output is correct |
15 |
Correct |
22 ms |
41668 KB |
Output is correct |
16 |
Correct |
19 ms |
41568 KB |
Output is correct |
17 |
Correct |
20 ms |
42000 KB |
Output is correct |
18 |
Correct |
19 ms |
41676 KB |
Output is correct |
19 |
Correct |
20 ms |
41516 KB |
Output is correct |
20 |
Correct |
20 ms |
41452 KB |
Output is correct |
21 |
Correct |
19 ms |
41524 KB |
Output is correct |
22 |
Correct |
19 ms |
41548 KB |
Output is correct |
23 |
Correct |
20 ms |
41716 KB |
Output is correct |
24 |
Correct |
21 ms |
42060 KB |
Output is correct |
25 |
Correct |
22 ms |
41616 KB |
Output is correct |
26 |
Correct |
21 ms |
41936 KB |
Output is correct |
27 |
Correct |
21 ms |
41804 KB |
Output is correct |
28 |
Correct |
21 ms |
42368 KB |
Output is correct |
29 |
Correct |
23 ms |
43340 KB |
Output is correct |
30 |
Correct |
20 ms |
41740 KB |
Output is correct |
31 |
Correct |
20 ms |
42180 KB |
Output is correct |
32 |
Correct |
21 ms |
42008 KB |
Output is correct |
33 |
Correct |
28 ms |
45328 KB |
Output is correct |
34 |
Correct |
25 ms |
45132 KB |
Output is correct |
35 |
Correct |
30 ms |
45340 KB |
Output is correct |
36 |
Correct |
21 ms |
42316 KB |
Output is correct |
37 |
Correct |
29 ms |
47180 KB |
Output is correct |
38 |
Correct |
34 ms |
47204 KB |
Output is correct |
39 |
Correct |
30 ms |
47140 KB |
Output is correct |
40 |
Correct |
32 ms |
47108 KB |
Output is correct |
41 |
Correct |
32 ms |
47076 KB |
Output is correct |
42 |
Correct |
29 ms |
42956 KB |
Output is correct |
43 |
Correct |
26 ms |
42400 KB |
Output is correct |
44 |
Correct |
25 ms |
41676 KB |
Output is correct |
45 |
Correct |
99 ms |
57064 KB |
Output is correct |
46 |
Correct |
76 ms |
56260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
41344 KB |
Output is correct |
2 |
Correct |
18 ms |
41416 KB |
Output is correct |
3 |
Correct |
19 ms |
41404 KB |
Output is correct |
4 |
Correct |
19 ms |
41420 KB |
Output is correct |
5 |
Correct |
19 ms |
41384 KB |
Output is correct |
6 |
Correct |
22 ms |
41424 KB |
Output is correct |
7 |
Correct |
20 ms |
41396 KB |
Output is correct |
8 |
Correct |
19 ms |
41420 KB |
Output is correct |
9 |
Correct |
18 ms |
41420 KB |
Output is correct |
10 |
Correct |
19 ms |
41484 KB |
Output is correct |
11 |
Correct |
19 ms |
41712 KB |
Output is correct |
12 |
Correct |
20 ms |
41420 KB |
Output is correct |
13 |
Correct |
20 ms |
41384 KB |
Output is correct |
14 |
Correct |
19 ms |
41716 KB |
Output is correct |
15 |
Correct |
20 ms |
41776 KB |
Output is correct |
16 |
Correct |
20 ms |
41548 KB |
Output is correct |
17 |
Correct |
21 ms |
42072 KB |
Output is correct |
18 |
Correct |
20 ms |
41656 KB |
Output is correct |
19 |
Correct |
19 ms |
41548 KB |
Output is correct |
20 |
Correct |
20 ms |
41448 KB |
Output is correct |
21 |
Correct |
20 ms |
41548 KB |
Output is correct |
22 |
Correct |
19 ms |
41588 KB |
Output is correct |
23 |
Correct |
19 ms |
41676 KB |
Output is correct |
24 |
Correct |
20 ms |
42060 KB |
Output is correct |
25 |
Correct |
19 ms |
41568 KB |
Output is correct |
26 |
Correct |
20 ms |
41988 KB |
Output is correct |
27 |
Correct |
20 ms |
41804 KB |
Output is correct |
28 |
Correct |
20 ms |
42444 KB |
Output is correct |
29 |
Correct |
22 ms |
43340 KB |
Output is correct |
30 |
Correct |
21 ms |
41824 KB |
Output is correct |
31 |
Correct |
21 ms |
42188 KB |
Output is correct |
32 |
Correct |
21 ms |
41972 KB |
Output is correct |
33 |
Correct |
27 ms |
45392 KB |
Output is correct |
34 |
Correct |
26 ms |
45168 KB |
Output is correct |
35 |
Correct |
31 ms |
45252 KB |
Output is correct |
36 |
Correct |
21 ms |
42392 KB |
Output is correct |
37 |
Correct |
28 ms |
47192 KB |
Output is correct |
38 |
Correct |
38 ms |
47156 KB |
Output is correct |
39 |
Correct |
29 ms |
47056 KB |
Output is correct |
40 |
Correct |
29 ms |
47100 KB |
Output is correct |
41 |
Correct |
30 ms |
47148 KB |
Output is correct |
42 |
Correct |
25 ms |
42908 KB |
Output is correct |
43 |
Correct |
25 ms |
42348 KB |
Output is correct |
44 |
Correct |
26 ms |
41768 KB |
Output is correct |
45 |
Correct |
113 ms |
57024 KB |
Output is correct |
46 |
Correct |
84 ms |
56220 KB |
Output is correct |
47 |
Correct |
42 ms |
67048 KB |
Output is correct |
48 |
Correct |
33 ms |
55760 KB |
Output is correct |
49 |
Correct |
29 ms |
50832 KB |
Output is correct |
50 |
Correct |
27 ms |
50148 KB |
Output is correct |
51 |
Correct |
40 ms |
61944 KB |
Output is correct |
52 |
Correct |
43 ms |
64232 KB |
Output is correct |
53 |
Correct |
28 ms |
43884 KB |
Output is correct |
54 |
Correct |
19 ms |
42060 KB |
Output is correct |
55 |
Correct |
20 ms |
43208 KB |
Output is correct |
56 |
Correct |
26 ms |
42828 KB |
Output is correct |
57 |
Correct |
29 ms |
56140 KB |
Output is correct |
58 |
Correct |
28 ms |
45312 KB |
Output is correct |
59 |
Correct |
32 ms |
48472 KB |
Output is correct |
60 |
Correct |
34 ms |
56168 KB |
Output is correct |
61 |
Correct |
32 ms |
49680 KB |
Output is correct |
62 |
Correct |
47 ms |
64384 KB |
Output is correct |
63 |
Correct |
353 ms |
90248 KB |
Output is correct |
64 |
Correct |
392 ms |
100420 KB |
Output is correct |
65 |
Correct |
939 ms |
142448 KB |
Output is correct |
66 |
Execution timed out |
1096 ms |
246340 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |