//{ <defines>
#ifndef _LOCAL
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#endif
#include <bits/stdc++.h>
using namespace std;
#define fr(i, n) for(int i = 0; i < n; ++i)
#define fo(n) fr(i, n)
#define re return
#define ef else if
#define ifn(x) if(!(x))
#define _ << ' ' <<
#define ft first
#define sd second
#define ve vector
#define pb push_back
#define eb emplace_back
#define sz(x) int(x.size())
#define pw(x) (1 << (x))
#define PW(x) (1ll << (x))
#define bnd(x) x.begin(), x.end()
#define clr(x, y) memset(x, y, sizeof x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ve<int> vi;
const int oo = 2e9;
const ll OO = 4e18;
//const ld pi = arg(complex<ld>(-1, 0));
//const ld pi2 = pi + pi;
const int md = 0x3b800001;
const int MD = 1e9 + 7;
inline ll time() {re chrono :: system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
mt19937_64 RND(time());
template<typename t> inline void umin(t &a, t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, t b) {a = max(a, b);}
//} </defines>
const int N = 1e5 + 5;
const int D = 1e9;
int n, m, st, fn, d[N];
vi g[N];
bool mk[N];
inline int mn(int x, int y) {re d[x] < d[y] ? x : y;}
struct tree {
int v[N << 2];
int p[N];
void assign(int l, int r, int u) {
assert(u < (N << 2));
if(l == r) {
p[l] = u >> 1;
v[u] = r;
} else {
int m = (l + r) >> 1;
assign(l, m, u + u);
assign(m + 1, r, u + u + 1);
v[u] = mn(v[u + u], v[u + u + 1]);
}
}
tree() {
assign(0, n - 1, 1);
}
inline void upd(int i) {
for(i = p[i]; i; i >>= 1)
v[i] = mn(v[i + i], v[i + i + 1]);
}
};
void solve() {
cin >> n >> m;
fo(m) {
int b, p;
cin >> b >> p;
if(i == 0) st = b;
if(i == 1) fn = b;
g[b].pb(p);
}
fo(n) d[i] = D;
d[st] = 0;
fo(n) sort(bnd(g[i]));
tree *t = new tree();
for(;;) {
int v = t -> v[1];
if(mk[v]) break;
if(v == fn) re void(cout << d[fn] << endl);
int pr = -1;
for(int p : g[v]) if(p != pr) {
pr = p;
for(int c = -p; c <= p; c += p + p) {
for(int w = d[v] + 1, u = v + c; u >= 0 && u < n; ++w, u += c) {
if(mk[u]) continue;
if(w < d[u]) {
d[u] = w;
t -> upd(u);
}
}
}
}
d[v] = D; t -> upd(v); mk[v] = true;
}
cout << -1 << endl;
}
/*
5 3
0 2
1 1
4 1
*/
int main() {
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2684 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2680 KB |
Output is correct |
13 |
Correct |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
5 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2656 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2740 KB |
Output is correct |
5 |
Correct |
4 ms |
2684 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
5 ms |
2684 KB |
Output is correct |
12 |
Correct |
5 ms |
2680 KB |
Output is correct |
13 |
Correct |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
5 ms |
2680 KB |
Output is correct |
15 |
Correct |
6 ms |
2680 KB |
Output is correct |
16 |
Correct |
5 ms |
2680 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
10 ms |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2680 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
7 ms |
2808 KB |
Output is correct |
25 |
Correct |
6 ms |
2808 KB |
Output is correct |
26 |
Correct |
6 ms |
2808 KB |
Output is correct |
27 |
Correct |
6 ms |
2808 KB |
Output is correct |
28 |
Correct |
6 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2680 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
5 ms |
2680 KB |
Output is correct |
32 |
Correct |
5 ms |
2680 KB |
Output is correct |
33 |
Correct |
6 ms |
2808 KB |
Output is correct |
34 |
Correct |
6 ms |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2684 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
5 ms |
2692 KB |
Output is correct |
11 |
Correct |
6 ms |
2680 KB |
Output is correct |
12 |
Correct |
5 ms |
2680 KB |
Output is correct |
13 |
Correct |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
6 ms |
2680 KB |
Output is correct |
15 |
Correct |
6 ms |
2680 KB |
Output is correct |
16 |
Correct |
5 ms |
2680 KB |
Output is correct |
17 |
Correct |
6 ms |
2812 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
10 ms |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2680 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
6 ms |
2808 KB |
Output is correct |
25 |
Correct |
6 ms |
2808 KB |
Output is correct |
26 |
Correct |
6 ms |
2680 KB |
Output is correct |
27 |
Correct |
6 ms |
2680 KB |
Output is correct |
28 |
Correct |
6 ms |
2812 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
5 ms |
2680 KB |
Output is correct |
32 |
Correct |
5 ms |
2680 KB |
Output is correct |
33 |
Correct |
6 ms |
2808 KB |
Output is correct |
34 |
Correct |
6 ms |
2808 KB |
Output is correct |
35 |
Correct |
27 ms |
2936 KB |
Output is correct |
36 |
Correct |
8 ms |
2808 KB |
Output is correct |
37 |
Correct |
22 ms |
2936 KB |
Output is correct |
38 |
Correct |
31 ms |
3064 KB |
Output is correct |
39 |
Correct |
30 ms |
2940 KB |
Output is correct |
40 |
Correct |
30 ms |
3064 KB |
Output is correct |
41 |
Correct |
31 ms |
3008 KB |
Output is correct |
42 |
Correct |
27 ms |
3064 KB |
Output is correct |
43 |
Correct |
27 ms |
2936 KB |
Output is correct |
44 |
Correct |
31 ms |
2936 KB |
Output is correct |
45 |
Correct |
28 ms |
2936 KB |
Output is correct |
46 |
Correct |
28 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2808 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
5 ms |
2680 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2680 KB |
Output is correct |
13 |
Correct |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
5 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
5 ms |
2680 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
10 ms |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
6 ms |
2808 KB |
Output is correct |
25 |
Correct |
6 ms |
2808 KB |
Output is correct |
26 |
Correct |
6 ms |
2808 KB |
Output is correct |
27 |
Correct |
6 ms |
2680 KB |
Output is correct |
28 |
Correct |
6 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2680 KB |
Output is correct |
33 |
Correct |
6 ms |
2808 KB |
Output is correct |
34 |
Correct |
6 ms |
2808 KB |
Output is correct |
35 |
Correct |
27 ms |
2936 KB |
Output is correct |
36 |
Correct |
8 ms |
2808 KB |
Output is correct |
37 |
Correct |
23 ms |
2936 KB |
Output is correct |
38 |
Correct |
31 ms |
3064 KB |
Output is correct |
39 |
Correct |
30 ms |
2936 KB |
Output is correct |
40 |
Correct |
31 ms |
3064 KB |
Output is correct |
41 |
Correct |
32 ms |
2936 KB |
Output is correct |
42 |
Correct |
26 ms |
2920 KB |
Output is correct |
43 |
Correct |
26 ms |
3064 KB |
Output is correct |
44 |
Correct |
31 ms |
3064 KB |
Output is correct |
45 |
Correct |
28 ms |
2928 KB |
Output is correct |
46 |
Correct |
28 ms |
2936 KB |
Output is correct |
47 |
Correct |
32 ms |
3320 KB |
Output is correct |
48 |
Correct |
28 ms |
3548 KB |
Output is correct |
49 |
Correct |
28 ms |
3576 KB |
Output is correct |
50 |
Correct |
21 ms |
3576 KB |
Output is correct |
51 |
Correct |
50 ms |
3808 KB |
Output is correct |
52 |
Correct |
53 ms |
3768 KB |
Output is correct |
53 |
Correct |
40 ms |
3832 KB |
Output is correct |
54 |
Correct |
6 ms |
3192 KB |
Output is correct |
55 |
Correct |
7 ms |
3192 KB |
Output is correct |
56 |
Correct |
985 ms |
4432 KB |
Output is correct |
57 |
Correct |
11 ms |
3320 KB |
Output is correct |
58 |
Correct |
24 ms |
3448 KB |
Output is correct |
59 |
Correct |
32 ms |
3448 KB |
Output is correct |
60 |
Correct |
30 ms |
3448 KB |
Output is correct |
61 |
Correct |
33 ms |
3448 KB |
Output is correct |
62 |
Correct |
45 ms |
4224 KB |
Output is correct |
63 |
Correct |
43 ms |
3704 KB |
Output is correct |
64 |
Correct |
42 ms |
3572 KB |
Output is correct |
65 |
Correct |
46 ms |
3672 KB |
Output is correct |
66 |
Correct |
52 ms |
3576 KB |
Output is correct |
67 |
Correct |
56 ms |
3576 KB |
Output is correct |