#include <bits/stdc++.h>
using namespace std;
#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
using ll = long long;
typedef pair<int, int> pii;
typedef array<int, 3> p3i;
const int MXN = 30005, MXM = 5000005, INF = 1e9;
int n, m;
map<pii, int> M;
namespace GRAPH {
int rt[MXM], dis[MXM], nc;
vector<p3i> edge;
int new_node() {
rt[nc] = -1;
return nc++;
}
void add_edge(int u, int v, int w) {
debug(u, v, w);
edge.push_back({w, v, rt[u]});
rt[u] = edge.size() - 1;
}
int ADD_CHAIN(int mod, int r) {
if (M.find(mp(mod, r)) != M.end()) return M[mp(mod, r)];
vector<int> v;
for (int i = r; i < n; i += mod) {
v.push_back(new_node());
add_edge(v.back(), i, 0);
}
FOR(i, 1, v.size()) {
add_edge(v[i - 1], v[i], 1);
add_edge(v[i], v[i - 1], 1);
}
debug(mod, r, v.front());
return (M[mp(mod, r)] = v.front());
}
void ZOBFS(int sr) {
// FOR(i, 0, nc) cout << rt[i] << ' ';
// cout << endl;
// for (auto [u, v, w] : edge) cout << '<' << u << ' ' << v << ' ' << w << '>' << '\n';
deque<pii> dq;
fill(dis, dis + nc, INF);
dq.push_back(mp(0, sr));
while (dq.size()) {
auto [len, id] = dq.front();
dq.pop_front();
if (dis[id] < INF) continue;
dis[id] = len;
debug(id, len);
int p = rt[id];
while (p != -1) {
auto [w, i, nxt] = edge[p];
// debug(p, w, i, nxt);
if (dis[i] == INF) {
if (w) dq.push_back(mp(len + 1, i));
else dq.push_front(mp(len, i));
}
p = nxt;
}
}
}
}
int minimum_jumps(int N, int M, vector<int> &B, vector<int> &P) {
using namespace GRAPH;
n = N;
m = M;
FOR(i, 0, n) new_node();
FOR(i, 0, m) {
int now = new_node();
add_edge(B[i], now, 0);
add_edge(now, B[i], 0);
}
FOR(i, 0, m) {
int rt = ADD_CHAIN(P[i], B[i] % P[i]);
int now = rt + B[i] / P[i];
add_edge(n + i, now, 0);
}
ZOBFS(n);
return (dis[n + 1] == INF ? -1 : dis[n + 1]);
}
void miku() {
int n, m;
cin >> n >> m;
vector<int> b(m), p(m);
FOR(i, 0, m) cin >> b[i] >> p[i];
cout << minimum_jumps(n, m, b, p) << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
return 0;
}
Compilation message
skyscraper.cpp: In function 'void GRAPH::add_edge(int, int, int)':
skyscraper.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
skyscraper.cpp:36:9: note: in expansion of macro 'debug'
36 | debug(u, v, w);
| ^~~~~
skyscraper.cpp: In function 'int GRAPH::ADD_CHAIN(int, int)':
skyscraper.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
skyscraper.cpp:52:9: note: in expansion of macro 'debug'
52 | debug(mod, r, v.front());
| ^~~~~
skyscraper.cpp: In function 'void GRAPH::ZOBFS(int)':
skyscraper.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
skyscraper.cpp:68:13: note: in expansion of macro 'debug'
68 | debug(id, len);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4508 KB |
Output is correct |
11 |
Correct |
2 ms |
4920 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4984 KB |
Output is correct |
15 |
Correct |
2 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4508 KB |
Output is correct |
11 |
Correct |
2 ms |
4916 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4984 KB |
Output is correct |
15 |
Correct |
2 ms |
4968 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
3 ms |
5472 KB |
Output is correct |
18 |
Correct |
2 ms |
4972 KB |
Output is correct |
19 |
Correct |
1 ms |
4912 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4920 KB |
Output is correct |
23 |
Correct |
2 ms |
4968 KB |
Output is correct |
24 |
Correct |
2 ms |
5480 KB |
Output is correct |
25 |
Correct |
2 ms |
4952 KB |
Output is correct |
26 |
Correct |
2 ms |
4956 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
3 ms |
6104 KB |
Output is correct |
29 |
Correct |
6 ms |
9424 KB |
Output is correct |
30 |
Correct |
2 ms |
5336 KB |
Output is correct |
31 |
Correct |
3 ms |
6100 KB |
Output is correct |
32 |
Correct |
3 ms |
6356 KB |
Output is correct |
33 |
Correct |
8 ms |
12240 KB |
Output is correct |
34 |
Correct |
8 ms |
12240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4808 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4984 KB |
Output is correct |
15 |
Correct |
3 ms |
4984 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
3 ms |
5472 KB |
Output is correct |
18 |
Correct |
1 ms |
4972 KB |
Output is correct |
19 |
Correct |
1 ms |
4736 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4920 KB |
Output is correct |
23 |
Correct |
2 ms |
4972 KB |
Output is correct |
24 |
Correct |
3 ms |
5480 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
2 ms |
4956 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
3 ms |
6100 KB |
Output is correct |
29 |
Correct |
6 ms |
9164 KB |
Output is correct |
30 |
Correct |
2 ms |
5332 KB |
Output is correct |
31 |
Correct |
3 ms |
6104 KB |
Output is correct |
32 |
Correct |
3 ms |
6104 KB |
Output is correct |
33 |
Correct |
10 ms |
13004 KB |
Output is correct |
34 |
Correct |
11 ms |
12492 KB |
Output is correct |
35 |
Correct |
27 ms |
14300 KB |
Output is correct |
36 |
Correct |
4 ms |
5588 KB |
Output is correct |
37 |
Correct |
26 ms |
19652 KB |
Output is correct |
38 |
Correct |
30 ms |
15040 KB |
Output is correct |
39 |
Correct |
30 ms |
15048 KB |
Output is correct |
40 |
Correct |
29 ms |
15044 KB |
Output is correct |
41 |
Correct |
30 ms |
15048 KB |
Output is correct |
42 |
Correct |
6 ms |
6356 KB |
Output is correct |
43 |
Correct |
6 ms |
6356 KB |
Output is correct |
44 |
Correct |
6 ms |
6356 KB |
Output is correct |
45 |
Correct |
65 ms |
33476 KB |
Output is correct |
46 |
Correct |
66 ms |
34536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4596 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4696 KB |
Output is correct |
11 |
Correct |
2 ms |
4920 KB |
Output is correct |
12 |
Correct |
1 ms |
4700 KB |
Output is correct |
13 |
Correct |
1 ms |
4700 KB |
Output is correct |
14 |
Correct |
3 ms |
4988 KB |
Output is correct |
15 |
Correct |
2 ms |
4984 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
3 ms |
5472 KB |
Output is correct |
18 |
Correct |
1 ms |
4972 KB |
Output is correct |
19 |
Correct |
1 ms |
4696 KB |
Output is correct |
20 |
Correct |
1 ms |
4696 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4916 KB |
Output is correct |
23 |
Correct |
2 ms |
4972 KB |
Output is correct |
24 |
Correct |
2 ms |
5480 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
2 ms |
4956 KB |
Output is correct |
27 |
Correct |
2 ms |
4956 KB |
Output is correct |
28 |
Correct |
3 ms |
6104 KB |
Output is correct |
29 |
Correct |
5 ms |
9164 KB |
Output is correct |
30 |
Correct |
2 ms |
5336 KB |
Output is correct |
31 |
Correct |
3 ms |
6104 KB |
Output is correct |
32 |
Correct |
3 ms |
6104 KB |
Output is correct |
33 |
Correct |
11 ms |
12408 KB |
Output is correct |
34 |
Correct |
10 ms |
12492 KB |
Output is correct |
35 |
Correct |
22 ms |
13776 KB |
Output is correct |
36 |
Correct |
4 ms |
5592 KB |
Output is correct |
37 |
Correct |
25 ms |
18108 KB |
Output is correct |
38 |
Correct |
28 ms |
15048 KB |
Output is correct |
39 |
Correct |
26 ms |
15036 KB |
Output is correct |
40 |
Correct |
28 ms |
15300 KB |
Output is correct |
41 |
Correct |
28 ms |
15036 KB |
Output is correct |
42 |
Correct |
7 ms |
6352 KB |
Output is correct |
43 |
Correct |
6 ms |
6352 KB |
Output is correct |
44 |
Correct |
6 ms |
6352 KB |
Output is correct |
45 |
Correct |
77 ms |
34428 KB |
Output is correct |
46 |
Correct |
64 ms |
32708 KB |
Output is correct |
47 |
Correct |
89 ms |
58276 KB |
Output is correct |
48 |
Correct |
22 ms |
19144 KB |
Output is correct |
49 |
Correct |
16 ms |
13516 KB |
Output is correct |
50 |
Correct |
12 ms |
13260 KB |
Output is correct |
51 |
Correct |
43 ms |
22996 KB |
Output is correct |
52 |
Correct |
44 ms |
33484 KB |
Output is correct |
53 |
Correct |
22 ms |
13328 KB |
Output is correct |
54 |
Correct |
3 ms |
7952 KB |
Output is correct |
55 |
Correct |
4 ms |
8784 KB |
Output is correct |
56 |
Correct |
8 ms |
9164 KB |
Output is correct |
57 |
Correct |
30 ms |
31972 KB |
Output is correct |
58 |
Correct |
10 ms |
12232 KB |
Output is correct |
59 |
Correct |
12 ms |
13000 KB |
Output is correct |
60 |
Correct |
13 ms |
13256 KB |
Output is correct |
61 |
Correct |
13 ms |
13512 KB |
Output is correct |
62 |
Correct |
48 ms |
33472 KB |
Output is correct |
63 |
Correct |
224 ms |
111260 KB |
Output is correct |
64 |
Correct |
306 ms |
215996 KB |
Output is correct |
65 |
Correct |
387 ms |
215184 KB |
Output is correct |
66 |
Execution timed out |
2517 ms |
262144 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |