#pragma GCC optimization ("O3")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int n,m;
pii niz[30005];
vector<pii> graf[30005];
int dist[30005];
set<int> pos[30005];
priority_queue<pii>pq;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> m >> n;
ff(i,1,n){
cin >> niz[i].xx >> niz[i].yy;
pos[niz[i].xx].insert(niz[i].yy);
}
ff(i,0,m - 1){
for(auto c:pos[i]){
for(int j = i + c; j < m; j += c){
if(!pos[j].size())continue;
graf[i].pb({j, (j - i) / c});
if(pos[j].count(c))break;
}
for(int j = i - c; j >= 0; j -= c){
if(!pos[j].size())continue;
graf[i].pb({j, (i - j) / c});
if(pos[j].count(c))break;
}
}
}
ff(i,0,m - 1)dist[i] = 1e9;
dist[niz[1].xx] = 0;
pq.push({0,niz[1].xx});
while(!pq.empty()){
pii sta = pq.top();
pq.pop();
sta.xx = -sta.xx;
if(dist[sta.yy] != sta.xx)continue;
for(auto c:graf[sta.yy]){
if(dist[c.xx] > sta.xx + c.yy){
dist[c.xx] = sta.xx + c.yy;
pq.push({-dist[c.xx], c.xx});
}
}
}
if(dist[niz[2].xx] == 1e9)cout << -1 << "\n";
else cout << dist[niz[2].xx] << "\n";
return 0;
}
Compilation message
skyscraper.cpp:1:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("O3")
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2432 KB |
Output is correct |
2 |
Correct |
6 ms |
2432 KB |
Output is correct |
3 |
Correct |
6 ms |
2480 KB |
Output is correct |
4 |
Correct |
6 ms |
2432 KB |
Output is correct |
5 |
Correct |
6 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2432 KB |
Output is correct |
7 |
Correct |
6 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2560 KB |
Output is correct |
2 |
Correct |
7 ms |
2432 KB |
Output is correct |
3 |
Correct |
6 ms |
2432 KB |
Output is correct |
4 |
Correct |
7 ms |
2432 KB |
Output is correct |
5 |
Correct |
6 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2432 KB |
Output is correct |
7 |
Correct |
6 ms |
2432 KB |
Output is correct |
8 |
Correct |
6 ms |
2432 KB |
Output is correct |
9 |
Correct |
6 ms |
2432 KB |
Output is correct |
10 |
Correct |
7 ms |
2560 KB |
Output is correct |
11 |
Correct |
7 ms |
2560 KB |
Output is correct |
12 |
Correct |
6 ms |
2432 KB |
Output is correct |
13 |
Correct |
6 ms |
2560 KB |
Output is correct |
14 |
Correct |
7 ms |
2560 KB |
Output is correct |
15 |
Correct |
6 ms |
2560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2432 KB |
Output is correct |
2 |
Correct |
6 ms |
2432 KB |
Output is correct |
3 |
Correct |
7 ms |
2432 KB |
Output is correct |
4 |
Correct |
6 ms |
2432 KB |
Output is correct |
5 |
Correct |
6 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2432 KB |
Output is correct |
7 |
Correct |
6 ms |
2432 KB |
Output is correct |
8 |
Correct |
6 ms |
2432 KB |
Output is correct |
9 |
Correct |
6 ms |
2432 KB |
Output is correct |
10 |
Correct |
6 ms |
2560 KB |
Output is correct |
11 |
Correct |
7 ms |
2560 KB |
Output is correct |
12 |
Correct |
6 ms |
2432 KB |
Output is correct |
13 |
Correct |
6 ms |
2432 KB |
Output is correct |
14 |
Correct |
7 ms |
2560 KB |
Output is correct |
15 |
Correct |
7 ms |
2560 KB |
Output is correct |
16 |
Correct |
7 ms |
2560 KB |
Output is correct |
17 |
Correct |
8 ms |
2688 KB |
Output is correct |
18 |
Correct |
6 ms |
2560 KB |
Output is correct |
19 |
Correct |
6 ms |
2560 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2560 KB |
Output is correct |
22 |
Correct |
6 ms |
2560 KB |
Output is correct |
23 |
Correct |
7 ms |
2560 KB |
Output is correct |
24 |
Correct |
7 ms |
2688 KB |
Output is correct |
25 |
Correct |
7 ms |
2688 KB |
Output is correct |
26 |
Correct |
6 ms |
2432 KB |
Output is correct |
27 |
Correct |
6 ms |
2560 KB |
Output is correct |
28 |
Correct |
7 ms |
2816 KB |
Output is correct |
29 |
Correct |
6 ms |
2560 KB |
Output is correct |
30 |
Correct |
7 ms |
2432 KB |
Output is correct |
31 |
Correct |
6 ms |
2560 KB |
Output is correct |
32 |
Correct |
6 ms |
2560 KB |
Output is correct |
33 |
Correct |
7 ms |
2560 KB |
Output is correct |
34 |
Correct |
7 ms |
2560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2432 KB |
Output is correct |
2 |
Correct |
6 ms |
2432 KB |
Output is correct |
3 |
Correct |
6 ms |
2432 KB |
Output is correct |
4 |
Correct |
6 ms |
2432 KB |
Output is correct |
5 |
Correct |
6 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2432 KB |
Output is correct |
7 |
Correct |
6 ms |
2432 KB |
Output is correct |
8 |
Correct |
6 ms |
2432 KB |
Output is correct |
9 |
Correct |
6 ms |
2432 KB |
Output is correct |
10 |
Correct |
6 ms |
2432 KB |
Output is correct |
11 |
Correct |
6 ms |
2560 KB |
Output is correct |
12 |
Correct |
6 ms |
2560 KB |
Output is correct |
13 |
Correct |
6 ms |
2432 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
6 ms |
2560 KB |
Output is correct |
17 |
Correct |
7 ms |
2688 KB |
Output is correct |
18 |
Correct |
6 ms |
2560 KB |
Output is correct |
19 |
Correct |
6 ms |
2560 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
7 ms |
2560 KB |
Output is correct |
22 |
Correct |
6 ms |
2560 KB |
Output is correct |
23 |
Correct |
6 ms |
2560 KB |
Output is correct |
24 |
Correct |
8 ms |
2688 KB |
Output is correct |
25 |
Correct |
7 ms |
2688 KB |
Output is correct |
26 |
Correct |
6 ms |
2560 KB |
Output is correct |
27 |
Correct |
6 ms |
2560 KB |
Output is correct |
28 |
Correct |
7 ms |
2816 KB |
Output is correct |
29 |
Correct |
6 ms |
2560 KB |
Output is correct |
30 |
Correct |
6 ms |
2432 KB |
Output is correct |
31 |
Correct |
6 ms |
2560 KB |
Output is correct |
32 |
Correct |
6 ms |
2432 KB |
Output is correct |
33 |
Correct |
6 ms |
2688 KB |
Output is correct |
34 |
Correct |
6 ms |
2560 KB |
Output is correct |
35 |
Correct |
22 ms |
4864 KB |
Output is correct |
36 |
Correct |
8 ms |
2944 KB |
Output is correct |
37 |
Correct |
24 ms |
5632 KB |
Output is correct |
38 |
Correct |
27 ms |
5888 KB |
Output is correct |
39 |
Correct |
28 ms |
5888 KB |
Output is correct |
40 |
Correct |
32 ms |
5880 KB |
Output is correct |
41 |
Correct |
30 ms |
6016 KB |
Output is correct |
42 |
Correct |
11 ms |
2688 KB |
Output is correct |
43 |
Correct |
12 ms |
2688 KB |
Output is correct |
44 |
Correct |
11 ms |
2816 KB |
Output is correct |
45 |
Correct |
21 ms |
4480 KB |
Output is correct |
46 |
Correct |
21 ms |
4480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2432 KB |
Output is correct |
2 |
Correct |
6 ms |
2432 KB |
Output is correct |
3 |
Correct |
6 ms |
2432 KB |
Output is correct |
4 |
Correct |
6 ms |
2432 KB |
Output is correct |
5 |
Correct |
6 ms |
2432 KB |
Output is correct |
6 |
Correct |
6 ms |
2432 KB |
Output is correct |
7 |
Correct |
6 ms |
2432 KB |
Output is correct |
8 |
Correct |
6 ms |
2432 KB |
Output is correct |
9 |
Correct |
6 ms |
2432 KB |
Output is correct |
10 |
Correct |
6 ms |
2432 KB |
Output is correct |
11 |
Correct |
6 ms |
2560 KB |
Output is correct |
12 |
Correct |
7 ms |
2560 KB |
Output is correct |
13 |
Correct |
6 ms |
2432 KB |
Output is correct |
14 |
Correct |
7 ms |
2560 KB |
Output is correct |
15 |
Correct |
7 ms |
2560 KB |
Output is correct |
16 |
Correct |
6 ms |
2560 KB |
Output is correct |
17 |
Correct |
7 ms |
2688 KB |
Output is correct |
18 |
Correct |
6 ms |
2560 KB |
Output is correct |
19 |
Correct |
6 ms |
2560 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2560 KB |
Output is correct |
22 |
Correct |
6 ms |
2560 KB |
Output is correct |
23 |
Correct |
7 ms |
2560 KB |
Output is correct |
24 |
Correct |
7 ms |
2688 KB |
Output is correct |
25 |
Correct |
7 ms |
2688 KB |
Output is correct |
26 |
Correct |
6 ms |
2560 KB |
Output is correct |
27 |
Correct |
6 ms |
2432 KB |
Output is correct |
28 |
Correct |
7 ms |
2944 KB |
Output is correct |
29 |
Correct |
6 ms |
2560 KB |
Output is correct |
30 |
Correct |
6 ms |
2432 KB |
Output is correct |
31 |
Correct |
6 ms |
2560 KB |
Output is correct |
32 |
Correct |
7 ms |
2432 KB |
Output is correct |
33 |
Correct |
7 ms |
2560 KB |
Output is correct |
34 |
Correct |
7 ms |
2560 KB |
Output is correct |
35 |
Correct |
22 ms |
4864 KB |
Output is correct |
36 |
Correct |
8 ms |
2944 KB |
Output is correct |
37 |
Correct |
24 ms |
5632 KB |
Output is correct |
38 |
Correct |
28 ms |
5888 KB |
Output is correct |
39 |
Correct |
30 ms |
6016 KB |
Output is correct |
40 |
Correct |
32 ms |
5880 KB |
Output is correct |
41 |
Correct |
28 ms |
5888 KB |
Output is correct |
42 |
Correct |
11 ms |
2688 KB |
Output is correct |
43 |
Correct |
11 ms |
2688 KB |
Output is correct |
44 |
Correct |
11 ms |
2816 KB |
Output is correct |
45 |
Correct |
21 ms |
4480 KB |
Output is correct |
46 |
Correct |
20 ms |
4480 KB |
Output is correct |
47 |
Correct |
49 ms |
11648 KB |
Output is correct |
48 |
Correct |
20 ms |
5248 KB |
Output is correct |
49 |
Correct |
17 ms |
4608 KB |
Output is correct |
50 |
Correct |
14 ms |
4096 KB |
Output is correct |
51 |
Correct |
37 ms |
7040 KB |
Output is correct |
52 |
Correct |
38 ms |
7360 KB |
Output is correct |
53 |
Correct |
22 ms |
5180 KB |
Output is correct |
54 |
Correct |
6 ms |
2560 KB |
Output is correct |
55 |
Correct |
6 ms |
2560 KB |
Output is correct |
56 |
Correct |
19 ms |
5248 KB |
Output is correct |
57 |
Correct |
12 ms |
3456 KB |
Output is correct |
58 |
Correct |
9 ms |
2816 KB |
Output is correct |
59 |
Correct |
12 ms |
2816 KB |
Output is correct |
60 |
Correct |
11 ms |
2816 KB |
Output is correct |
61 |
Correct |
13 ms |
3072 KB |
Output is correct |
62 |
Correct |
29 ms |
9340 KB |
Output is correct |
63 |
Correct |
23 ms |
4480 KB |
Output is correct |
64 |
Correct |
24 ms |
4352 KB |
Output is correct |
65 |
Correct |
25 ms |
4480 KB |
Output is correct |
66 |
Correct |
29 ms |
4608 KB |
Output is correct |
67 |
Correct |
31 ms |
4604 KB |
Output is correct |