#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 3e4;
const int inf = 1e9;
vector<pii> edges[MAXN];
unordered_set<int> at[MAXN];
int n, m;
int dist[MAXN];
vector<int> jump_occs[MAXN+1];
int occ_pos[MAXN+1];
vector<int> valsat[MAXN];
int posf;
struct comp {
bool operator()(const pii &a, const pii &b) {
return (a.first == b.first) ? (a.second > b.second) : (a.first > b.first);
}
};
void dijkstra(int s) {
fill(dist, dist+n, inf);
dist[s] = 0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push(pii(0, s));
while (!pq.empty()) {
pii p = pq.top();
pq.pop();
int cur = p.second;
if (cur == posf) return;
if (dist[cur] < p.first) continue;
for (pii e: edges[cur]) {
if (dist[cur]+e.second < dist[e.first]) {
dist[e.first] = dist[cur]+e.second;
pq.push(pii(dist[e.first], e.first));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> m;
int poss;
for (int i = 0; i < m; i++) {
int x, y; cin >> x >> y;
if (i == 1) posf = x;
if (i == 0) poss = x;
jump_occs[y].push_back(x);
occ_pos[y]++;
//valsat[x].push_back(y);
at[x].insert(y);
}
for (int i = n-1; i >= 0; i--) {
for (int p: at[i]) {
occ_pos[p]--;
int cur = occ_pos[p];
int psize = jump_occs[p].size();
for (int j = i+p; j < n; j += p) {
edges[i].push_back(pii(j, (j-i)/p));
while (cur < psize && jump_occs[p][cur] < j) cur++;
if (cur < psize && jump_occs[p][cur] == j) break;
}
}
}
for (int i = 0; i < n; i++) {
for (int p: at[i]) {
int cur = occ_pos[p];
for (int j = i-p; j >= 0; j -= p) {
edges[i].push_back(pii(j, (i-j)/p));
while (cur >= 0 && jump_occs[p][cur] > j) cur--;
if (cur >= 0 && jump_occs[p][cur] == j) break;
}
occ_pos[p]++;
}
}
dijkstra(poss);
cout << ((dist[posf] == inf) ? (-1) : dist[posf]) << "\n";
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:82:10: warning: 'poss' may be used uninitialized in this function [-Wmaybe-uninitialized]
82 | dijkstra(poss);
| ~~~~~~~~^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4052 KB |
Output is correct |
2 |
Correct |
3 ms |
3988 KB |
Output is correct |
3 |
Correct |
2 ms |
4052 KB |
Output is correct |
4 |
Correct |
3 ms |
4052 KB |
Output is correct |
5 |
Correct |
2 ms |
4052 KB |
Output is correct |
6 |
Correct |
3 ms |
4052 KB |
Output is correct |
7 |
Correct |
2 ms |
4052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4052 KB |
Output is correct |
2 |
Correct |
2 ms |
4052 KB |
Output is correct |
3 |
Correct |
2 ms |
4052 KB |
Output is correct |
4 |
Correct |
2 ms |
4052 KB |
Output is correct |
5 |
Correct |
2 ms |
4052 KB |
Output is correct |
6 |
Correct |
2 ms |
4052 KB |
Output is correct |
7 |
Correct |
2 ms |
4052 KB |
Output is correct |
8 |
Correct |
2 ms |
4052 KB |
Output is correct |
9 |
Correct |
3 ms |
4052 KB |
Output is correct |
10 |
Correct |
3 ms |
4052 KB |
Output is correct |
11 |
Correct |
4 ms |
4180 KB |
Output is correct |
12 |
Correct |
4 ms |
4052 KB |
Output is correct |
13 |
Correct |
3 ms |
4180 KB |
Output is correct |
14 |
Correct |
3 ms |
4180 KB |
Output is correct |
15 |
Correct |
3 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4052 KB |
Output is correct |
2 |
Correct |
2 ms |
4052 KB |
Output is correct |
3 |
Correct |
2 ms |
4052 KB |
Output is correct |
4 |
Correct |
3 ms |
4052 KB |
Output is correct |
5 |
Correct |
2 ms |
4052 KB |
Output is correct |
6 |
Correct |
3 ms |
4052 KB |
Output is correct |
7 |
Correct |
4 ms |
4052 KB |
Output is correct |
8 |
Correct |
2 ms |
4052 KB |
Output is correct |
9 |
Correct |
2 ms |
4052 KB |
Output is correct |
10 |
Correct |
3 ms |
4052 KB |
Output is correct |
11 |
Correct |
3 ms |
4180 KB |
Output is correct |
12 |
Correct |
3 ms |
4052 KB |
Output is correct |
13 |
Correct |
3 ms |
4180 KB |
Output is correct |
14 |
Correct |
3 ms |
4180 KB |
Output is correct |
15 |
Correct |
4 ms |
4180 KB |
Output is correct |
16 |
Correct |
3 ms |
4180 KB |
Output is correct |
17 |
Correct |
3 ms |
4436 KB |
Output is correct |
18 |
Correct |
3 ms |
4308 KB |
Output is correct |
19 |
Correct |
4 ms |
4180 KB |
Output is correct |
20 |
Correct |
36 ms |
26072 KB |
Output is correct |
21 |
Correct |
3 ms |
4180 KB |
Output is correct |
22 |
Correct |
3 ms |
4180 KB |
Output is correct |
23 |
Correct |
4 ms |
4308 KB |
Output is correct |
24 |
Correct |
4 ms |
4564 KB |
Output is correct |
25 |
Correct |
3 ms |
4340 KB |
Output is correct |
26 |
Correct |
3 ms |
4180 KB |
Output is correct |
27 |
Correct |
3 ms |
4152 KB |
Output is correct |
28 |
Correct |
4 ms |
4564 KB |
Output is correct |
29 |
Correct |
5 ms |
4948 KB |
Output is correct |
30 |
Correct |
4 ms |
4336 KB |
Output is correct |
31 |
Correct |
4 ms |
4564 KB |
Output is correct |
32 |
Correct |
4 ms |
4436 KB |
Output is correct |
33 |
Correct |
6 ms |
5460 KB |
Output is correct |
34 |
Correct |
5 ms |
5460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4052 KB |
Output is correct |
2 |
Correct |
2 ms |
4052 KB |
Output is correct |
3 |
Correct |
2 ms |
4052 KB |
Output is correct |
4 |
Correct |
3 ms |
4052 KB |
Output is correct |
5 |
Correct |
3 ms |
4052 KB |
Output is correct |
6 |
Correct |
3 ms |
4052 KB |
Output is correct |
7 |
Correct |
3 ms |
4052 KB |
Output is correct |
8 |
Correct |
3 ms |
4052 KB |
Output is correct |
9 |
Correct |
3 ms |
4052 KB |
Output is correct |
10 |
Correct |
3 ms |
4052 KB |
Output is correct |
11 |
Correct |
4 ms |
4180 KB |
Output is correct |
12 |
Correct |
3 ms |
4052 KB |
Output is correct |
13 |
Correct |
3 ms |
4140 KB |
Output is correct |
14 |
Correct |
3 ms |
4180 KB |
Output is correct |
15 |
Correct |
3 ms |
4180 KB |
Output is correct |
16 |
Correct |
3 ms |
4180 KB |
Output is correct |
17 |
Correct |
4 ms |
4372 KB |
Output is correct |
18 |
Correct |
3 ms |
4308 KB |
Output is correct |
19 |
Correct |
3 ms |
4180 KB |
Output is correct |
20 |
Correct |
38 ms |
26084 KB |
Output is correct |
21 |
Correct |
3 ms |
4200 KB |
Output is correct |
22 |
Correct |
3 ms |
4180 KB |
Output is correct |
23 |
Correct |
3 ms |
4308 KB |
Output is correct |
24 |
Correct |
4 ms |
4436 KB |
Output is correct |
25 |
Correct |
4 ms |
4444 KB |
Output is correct |
26 |
Correct |
3 ms |
4180 KB |
Output is correct |
27 |
Correct |
3 ms |
4180 KB |
Output is correct |
28 |
Correct |
5 ms |
4564 KB |
Output is correct |
29 |
Correct |
6 ms |
4924 KB |
Output is correct |
30 |
Correct |
4 ms |
4308 KB |
Output is correct |
31 |
Correct |
5 ms |
4564 KB |
Output is correct |
32 |
Correct |
3 ms |
4436 KB |
Output is correct |
33 |
Correct |
6 ms |
5440 KB |
Output is correct |
34 |
Correct |
6 ms |
5460 KB |
Output is correct |
35 |
Correct |
14 ms |
6716 KB |
Output is correct |
36 |
Correct |
5 ms |
4628 KB |
Output is correct |
37 |
Correct |
15 ms |
7892 KB |
Output is correct |
38 |
Correct |
22 ms |
7824 KB |
Output is correct |
39 |
Correct |
18 ms |
7892 KB |
Output is correct |
40 |
Correct |
20 ms |
7864 KB |
Output is correct |
41 |
Correct |
20 ms |
7892 KB |
Output is correct |
42 |
Correct |
9 ms |
4308 KB |
Output is correct |
43 |
Correct |
6 ms |
4356 KB |
Output is correct |
44 |
Correct |
48 ms |
31220 KB |
Output is correct |
45 |
Correct |
19 ms |
9936 KB |
Output is correct |
46 |
Correct |
20 ms |
9884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4052 KB |
Output is correct |
2 |
Correct |
2 ms |
4052 KB |
Output is correct |
3 |
Correct |
3 ms |
4052 KB |
Output is correct |
4 |
Correct |
2 ms |
4052 KB |
Output is correct |
5 |
Correct |
3 ms |
4052 KB |
Output is correct |
6 |
Correct |
3 ms |
4052 KB |
Output is correct |
7 |
Correct |
3 ms |
4052 KB |
Output is correct |
8 |
Correct |
2 ms |
4052 KB |
Output is correct |
9 |
Correct |
3 ms |
4052 KB |
Output is correct |
10 |
Correct |
3 ms |
4052 KB |
Output is correct |
11 |
Correct |
5 ms |
4180 KB |
Output is correct |
12 |
Correct |
3 ms |
4052 KB |
Output is correct |
13 |
Correct |
3 ms |
4180 KB |
Output is correct |
14 |
Correct |
3 ms |
4180 KB |
Output is correct |
15 |
Correct |
5 ms |
4180 KB |
Output is correct |
16 |
Correct |
3 ms |
4180 KB |
Output is correct |
17 |
Correct |
4 ms |
4468 KB |
Output is correct |
18 |
Correct |
3 ms |
4308 KB |
Output is correct |
19 |
Correct |
3 ms |
4180 KB |
Output is correct |
20 |
Correct |
38 ms |
26060 KB |
Output is correct |
21 |
Correct |
3 ms |
4216 KB |
Output is correct |
22 |
Correct |
3 ms |
4180 KB |
Output is correct |
23 |
Correct |
3 ms |
4308 KB |
Output is correct |
24 |
Correct |
5 ms |
4436 KB |
Output is correct |
25 |
Correct |
4 ms |
4436 KB |
Output is correct |
26 |
Correct |
4 ms |
4212 KB |
Output is correct |
27 |
Correct |
3 ms |
4180 KB |
Output is correct |
28 |
Correct |
4 ms |
4564 KB |
Output is correct |
29 |
Correct |
5 ms |
4948 KB |
Output is correct |
30 |
Correct |
4 ms |
4308 KB |
Output is correct |
31 |
Correct |
4 ms |
4564 KB |
Output is correct |
32 |
Correct |
3 ms |
4436 KB |
Output is correct |
33 |
Correct |
6 ms |
5460 KB |
Output is correct |
34 |
Correct |
6 ms |
5484 KB |
Output is correct |
35 |
Correct |
17 ms |
6668 KB |
Output is correct |
36 |
Correct |
5 ms |
4564 KB |
Output is correct |
37 |
Correct |
14 ms |
7908 KB |
Output is correct |
38 |
Correct |
20 ms |
7892 KB |
Output is correct |
39 |
Correct |
18 ms |
7892 KB |
Output is correct |
40 |
Correct |
18 ms |
7892 KB |
Output is correct |
41 |
Correct |
18 ms |
7892 KB |
Output is correct |
42 |
Correct |
8 ms |
4352 KB |
Output is correct |
43 |
Correct |
7 ms |
4308 KB |
Output is correct |
44 |
Correct |
49 ms |
31220 KB |
Output is correct |
45 |
Correct |
21 ms |
9976 KB |
Output is correct |
46 |
Correct |
20 ms |
9920 KB |
Output is correct |
47 |
Correct |
35 ms |
16644 KB |
Output is correct |
48 |
Correct |
25 ms |
9552 KB |
Output is correct |
49 |
Correct |
18 ms |
8604 KB |
Output is correct |
50 |
Correct |
14 ms |
8068 KB |
Output is correct |
51 |
Correct |
37 ms |
12280 KB |
Output is correct |
52 |
Correct |
39 ms |
13180 KB |
Output is correct |
53 |
Correct |
21 ms |
8780 KB |
Output is correct |
54 |
Correct |
4 ms |
4692 KB |
Output is correct |
55 |
Correct |
6 ms |
5076 KB |
Output is correct |
56 |
Runtime error |
398 ms |
262144 KB |
Execution killed with signal 9 |
57 |
Halted |
0 ms |
0 KB |
- |