#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define rrep(i, a, b) for(int i = b; i >= a; i --)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#define INF 1000000000
#define MOD 1000000007
#define eps (1e-9)
using namespace std;
#ifdef wiwihorz
#define print(a...)cerr<<"Line "<<__LINE__<<":",kout("["+string(#a)+"] = ", a)
void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
void kout() { cerr << endl; }
template<class T1,class ... T2>void kout(T1 a,T2 ... e){cerr<<a<<" ",kout(e...);}
#else
#define print(...) 0
#define vprint(...) 0
#endif
#define x first
#define y second
namespace solver {
int n, m, s, t, ii;
const int P = 175, mx = 8000000;
vector<pii> a;
vector<int> v[P + 1][P + 1];
vector<vector<int>> to;
vector<int> dis, yes, dir;
void init_(int _n, int _m) {
ii = _n;
n = _n, m = _m;
a.clear();
to.assign(n, vector<int>());
yes.assign(mx, 0);
dir.assign(mx, 0);
dis.assign(mx, INF);
}
void solve() {
for(auto i : a) {
int st = i.y % i.x;
for(int j = st; j < n; j += i.x) {
ii++;
if(j == i.y) to[j].push_back(ii);
yes[ii] = j;
dir[ii] = i.x;
}
}
rep(i, 1, P) rep(j, 0, i - 1) {
sort(all(v[i][j]));
if(!v[i][j].size()) continue;
int cur = 0;
for(int k = j; k < n; k += i) {
ii++;
if(cur < v[i][j].size() &&
v[i][j][cur] == k) to[k].push_back(ii), cur++;
yes[ii] = k;
dir[ii] = i;
}
}
dis[s] = 0;
deque<int> q;
q.push_back(s);
auto trans = [&](int a, int b, int c) {
if(dis[b] != INF) return;
dis[b] = dis[a] + c;
if(c) q.push_back(b);
else q.push_front(b);
};
while(q.size()) {
int cur = q.front();
q.pop_front();
if(cur == t) {
cout << dis[cur] << "\n";
return;
}
if(cur < n) for(auto i : to[cur]) trans(cur, i, 0);
else {
if(yes[cur] - dir[cur] >= 0) trans(cur, cur - 1, 1);
if(yes[cur] + dir[cur] < n) trans(cur, cur + 1, 1);
trans(cur, yes[cur], 0);
}
}
cout << "-1\n";
return;
}
};
using namespace solver;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
cin >> n >> m;
init_(n, m);
rep(i, 0, m - 1) {
int b, p;
cin >> b >> p;
if(i == 0) s = b;
if(i == 1) t = b;
if(p <= P) v[p][b % p].push_back(b);
else a.push_back({p, b});
}
solve();
return 0;
}
Compilation message
skyscraper.cpp:5: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
5 | #pragma loop-opt(on)
|
skyscraper.cpp:19:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
| ^~~~
skyscraper.cpp:19:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
| ^~~~
skyscraper.cpp: In function 'void solver::solve()':
skyscraper.cpp:60:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if(cur < v[i][j].size() &&
| ~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
94976 KB |
Output is correct |
2 |
Correct |
37 ms |
94988 KB |
Output is correct |
3 |
Correct |
37 ms |
94960 KB |
Output is correct |
4 |
Correct |
35 ms |
94932 KB |
Output is correct |
5 |
Correct |
41 ms |
94896 KB |
Output is correct |
6 |
Correct |
34 ms |
94968 KB |
Output is correct |
7 |
Correct |
35 ms |
94976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
95060 KB |
Output is correct |
2 |
Correct |
41 ms |
94952 KB |
Output is correct |
3 |
Correct |
39 ms |
94944 KB |
Output is correct |
4 |
Correct |
39 ms |
94876 KB |
Output is correct |
5 |
Correct |
41 ms |
94972 KB |
Output is correct |
6 |
Correct |
35 ms |
94924 KB |
Output is correct |
7 |
Correct |
36 ms |
94916 KB |
Output is correct |
8 |
Correct |
35 ms |
94960 KB |
Output is correct |
9 |
Correct |
35 ms |
94936 KB |
Output is correct |
10 |
Correct |
37 ms |
94968 KB |
Output is correct |
11 |
Correct |
39 ms |
94924 KB |
Output is correct |
12 |
Correct |
37 ms |
94924 KB |
Output is correct |
13 |
Correct |
39 ms |
95000 KB |
Output is correct |
14 |
Correct |
36 ms |
95060 KB |
Output is correct |
15 |
Correct |
36 ms |
95060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
94940 KB |
Output is correct |
2 |
Correct |
36 ms |
94888 KB |
Output is correct |
3 |
Correct |
35 ms |
94908 KB |
Output is correct |
4 |
Correct |
40 ms |
94908 KB |
Output is correct |
5 |
Correct |
38 ms |
94924 KB |
Output is correct |
6 |
Correct |
37 ms |
95052 KB |
Output is correct |
7 |
Correct |
35 ms |
94952 KB |
Output is correct |
8 |
Correct |
36 ms |
94972 KB |
Output is correct |
9 |
Correct |
36 ms |
94932 KB |
Output is correct |
10 |
Correct |
38 ms |
94956 KB |
Output is correct |
11 |
Correct |
36 ms |
95060 KB |
Output is correct |
12 |
Correct |
36 ms |
94904 KB |
Output is correct |
13 |
Correct |
35 ms |
94972 KB |
Output is correct |
14 |
Correct |
35 ms |
95084 KB |
Output is correct |
15 |
Correct |
40 ms |
95032 KB |
Output is correct |
16 |
Correct |
38 ms |
95000 KB |
Output is correct |
17 |
Correct |
44 ms |
95032 KB |
Output is correct |
18 |
Correct |
35 ms |
95060 KB |
Output is correct |
19 |
Correct |
34 ms |
94964 KB |
Output is correct |
20 |
Correct |
36 ms |
94996 KB |
Output is correct |
21 |
Correct |
36 ms |
95016 KB |
Output is correct |
22 |
Correct |
36 ms |
95060 KB |
Output is correct |
23 |
Correct |
37 ms |
94996 KB |
Output is correct |
24 |
Correct |
36 ms |
95060 KB |
Output is correct |
25 |
Correct |
38 ms |
94976 KB |
Output is correct |
26 |
Correct |
38 ms |
94952 KB |
Output is correct |
27 |
Correct |
36 ms |
95004 KB |
Output is correct |
28 |
Correct |
37 ms |
95120 KB |
Output is correct |
29 |
Correct |
36 ms |
95036 KB |
Output is correct |
30 |
Correct |
36 ms |
95036 KB |
Output is correct |
31 |
Correct |
36 ms |
94952 KB |
Output is correct |
32 |
Correct |
36 ms |
94944 KB |
Output is correct |
33 |
Correct |
38 ms |
95052 KB |
Output is correct |
34 |
Correct |
39 ms |
95096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
94980 KB |
Output is correct |
2 |
Correct |
37 ms |
94900 KB |
Output is correct |
3 |
Correct |
38 ms |
94872 KB |
Output is correct |
4 |
Correct |
36 ms |
94952 KB |
Output is correct |
5 |
Correct |
36 ms |
94948 KB |
Output is correct |
6 |
Correct |
35 ms |
94960 KB |
Output is correct |
7 |
Correct |
38 ms |
94876 KB |
Output is correct |
8 |
Correct |
36 ms |
94928 KB |
Output is correct |
9 |
Correct |
41 ms |
94872 KB |
Output is correct |
10 |
Correct |
36 ms |
94924 KB |
Output is correct |
11 |
Correct |
34 ms |
95064 KB |
Output is correct |
12 |
Correct |
35 ms |
94936 KB |
Output is correct |
13 |
Correct |
37 ms |
94932 KB |
Output is correct |
14 |
Correct |
36 ms |
95052 KB |
Output is correct |
15 |
Correct |
35 ms |
95060 KB |
Output is correct |
16 |
Correct |
36 ms |
94920 KB |
Output is correct |
17 |
Correct |
40 ms |
95036 KB |
Output is correct |
18 |
Correct |
38 ms |
94996 KB |
Output is correct |
19 |
Correct |
36 ms |
95008 KB |
Output is correct |
20 |
Correct |
35 ms |
95004 KB |
Output is correct |
21 |
Correct |
36 ms |
94976 KB |
Output is correct |
22 |
Correct |
35 ms |
94940 KB |
Output is correct |
23 |
Correct |
36 ms |
95036 KB |
Output is correct |
24 |
Correct |
37 ms |
95092 KB |
Output is correct |
25 |
Correct |
36 ms |
95136 KB |
Output is correct |
26 |
Correct |
36 ms |
95008 KB |
Output is correct |
27 |
Correct |
35 ms |
95068 KB |
Output is correct |
28 |
Correct |
42 ms |
95112 KB |
Output is correct |
29 |
Correct |
39 ms |
95128 KB |
Output is correct |
30 |
Correct |
37 ms |
94996 KB |
Output is correct |
31 |
Correct |
36 ms |
95032 KB |
Output is correct |
32 |
Correct |
35 ms |
95052 KB |
Output is correct |
33 |
Correct |
37 ms |
95028 KB |
Output is correct |
34 |
Correct |
39 ms |
95064 KB |
Output is correct |
35 |
Correct |
49 ms |
95448 KB |
Output is correct |
36 |
Correct |
46 ms |
95096 KB |
Output is correct |
37 |
Correct |
42 ms |
95468 KB |
Output is correct |
38 |
Correct |
43 ms |
95580 KB |
Output is correct |
39 |
Correct |
42 ms |
95540 KB |
Output is correct |
40 |
Correct |
42 ms |
95536 KB |
Output is correct |
41 |
Correct |
43 ms |
95496 KB |
Output is correct |
42 |
Correct |
38 ms |
95168 KB |
Output is correct |
43 |
Correct |
42 ms |
95264 KB |
Output is correct |
44 |
Correct |
40 ms |
95164 KB |
Output is correct |
45 |
Correct |
58 ms |
95832 KB |
Output is correct |
46 |
Correct |
46 ms |
95820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
94960 KB |
Output is correct |
2 |
Correct |
36 ms |
94884 KB |
Output is correct |
3 |
Correct |
38 ms |
94864 KB |
Output is correct |
4 |
Correct |
37 ms |
94876 KB |
Output is correct |
5 |
Correct |
36 ms |
94908 KB |
Output is correct |
6 |
Correct |
36 ms |
94904 KB |
Output is correct |
7 |
Correct |
35 ms |
94944 KB |
Output is correct |
8 |
Correct |
36 ms |
94976 KB |
Output is correct |
9 |
Correct |
37 ms |
94924 KB |
Output is correct |
10 |
Correct |
36 ms |
94888 KB |
Output is correct |
11 |
Correct |
38 ms |
94924 KB |
Output is correct |
12 |
Correct |
40 ms |
94984 KB |
Output is correct |
13 |
Correct |
37 ms |
95000 KB |
Output is correct |
14 |
Correct |
36 ms |
95044 KB |
Output is correct |
15 |
Correct |
35 ms |
95076 KB |
Output is correct |
16 |
Correct |
36 ms |
94924 KB |
Output is correct |
17 |
Correct |
39 ms |
95088 KB |
Output is correct |
18 |
Correct |
36 ms |
95052 KB |
Output is correct |
19 |
Correct |
37 ms |
95052 KB |
Output is correct |
20 |
Correct |
37 ms |
95096 KB |
Output is correct |
21 |
Correct |
40 ms |
94936 KB |
Output is correct |
22 |
Correct |
38 ms |
94996 KB |
Output is correct |
23 |
Correct |
36 ms |
95044 KB |
Output is correct |
24 |
Correct |
35 ms |
95068 KB |
Output is correct |
25 |
Correct |
36 ms |
94980 KB |
Output is correct |
26 |
Correct |
35 ms |
94932 KB |
Output is correct |
27 |
Correct |
36 ms |
95000 KB |
Output is correct |
28 |
Correct |
40 ms |
95052 KB |
Output is correct |
29 |
Correct |
37 ms |
95052 KB |
Output is correct |
30 |
Correct |
37 ms |
94976 KB |
Output is correct |
31 |
Correct |
37 ms |
95052 KB |
Output is correct |
32 |
Correct |
36 ms |
94992 KB |
Output is correct |
33 |
Correct |
38 ms |
95012 KB |
Output is correct |
34 |
Correct |
39 ms |
95060 KB |
Output is correct |
35 |
Correct |
42 ms |
95432 KB |
Output is correct |
36 |
Correct |
39 ms |
95080 KB |
Output is correct |
37 |
Correct |
42 ms |
95424 KB |
Output is correct |
38 |
Correct |
42 ms |
95560 KB |
Output is correct |
39 |
Correct |
44 ms |
95532 KB |
Output is correct |
40 |
Correct |
46 ms |
95568 KB |
Output is correct |
41 |
Correct |
43 ms |
95568 KB |
Output is correct |
42 |
Correct |
44 ms |
95180 KB |
Output is correct |
43 |
Correct |
40 ms |
95228 KB |
Output is correct |
44 |
Correct |
39 ms |
95228 KB |
Output is correct |
45 |
Correct |
52 ms |
95840 KB |
Output is correct |
46 |
Correct |
51 ms |
95944 KB |
Output is correct |
47 |
Correct |
48 ms |
95804 KB |
Output is correct |
48 |
Correct |
42 ms |
96080 KB |
Output is correct |
49 |
Correct |
41 ms |
96200 KB |
Output is correct |
50 |
Correct |
39 ms |
96008 KB |
Output is correct |
51 |
Correct |
45 ms |
96548 KB |
Output is correct |
52 |
Correct |
49 ms |
96504 KB |
Output is correct |
53 |
Correct |
44 ms |
96464 KB |
Output is correct |
54 |
Correct |
35 ms |
95620 KB |
Output is correct |
55 |
Correct |
36 ms |
95552 KB |
Output is correct |
56 |
Correct |
42 ms |
96712 KB |
Output is correct |
57 |
Correct |
38 ms |
95784 KB |
Output is correct |
58 |
Correct |
40 ms |
95816 KB |
Output is correct |
59 |
Correct |
44 ms |
95864 KB |
Output is correct |
60 |
Correct |
42 ms |
95824 KB |
Output is correct |
61 |
Correct |
43 ms |
95896 KB |
Output is correct |
62 |
Correct |
53 ms |
96748 KB |
Output is correct |
63 |
Correct |
125 ms |
96248 KB |
Output is correct |
64 |
Correct |
161 ms |
96444 KB |
Output is correct |
65 |
Correct |
229 ms |
96568 KB |
Output is correct |
66 |
Correct |
473 ms |
96824 KB |
Output is correct |
67 |
Correct |
263 ms |
96736 KB |
Output is correct |