#pragma GCC target("avx")
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include <bits/stdc++.h>
using namespace std;
int a, b;
vector<int> z[1000005];
int blocksize;
pair<int, int> mark[30005];
struct node {
int val, pos, type;
bool operator>(const node &other) const {
return val > other.val;
}
};
map<pair<int, int>, int> cnt;
void dijkstra() {
priority_queue<node, vector<node>, greater<node>> q;
q.push({0, mark[0].first, mark[0].second});
cnt[{mark[0].first, mark[0].second}] = 0;
while (!q.empty()) {
node pos = q.top();
q.pop();
int val = pos.val;
int pos1 = pos.pos;
int type = pos.type;
if (cnt[{pos1, type}] < val) continue;
if (type == 0) {
for (auto p : z[pos1]) {
if (p + pos1 < a) {
if (!cnt.count({p + pos1, p}) || cnt[{p + pos1, p}] > val + 1) {
cnt[{p + pos1, p}] = val + 1;
q.push({cnt[{p + pos1, p}], p + pos1, p});
}
}
if (pos1 - p >= 0) {
if (!cnt.count({pos1 - p, p}) || cnt[{pos1 - p, p}] > val + 1) {
cnt[{pos1 - p, p}] = val + 1;
q.push({cnt[{pos1 - p, p}], pos1 - p, p});
}
}
}
} else {
if (!cnt.count({pos1, 0}) || cnt[{pos1, 0}] > val) {
cnt[{pos1, 0}] = val;
q.push({val, pos1, 0});
}
if (type > blocksize) {
int cnt1 = 1;
while (pos1 + cnt1 * type < a) {
int nxt = pos1 + cnt1 * type;
if (!cnt.count({nxt, 0}) || cnt[{nxt, 0}] > val + cnt1) {
cnt[{nxt, 0}] = val + cnt1;
q.push({cnt[{nxt, 0}], nxt, 0});
}
cnt1++;
}
cnt1 = 1;
while (pos1 - cnt1 * type >= 0) {
int nxt = pos1 - cnt1 * type;
if (!cnt.count({nxt, 0}) || cnt[{nxt, 0}] > val + cnt1) {
cnt[{nxt, 0}] = val + cnt1;
q.push({cnt[{nxt, 0}], nxt, 0});
}
cnt1++;
}
} else {
int nxt1 = pos1 + type;
int nxt2 = pos1 - type;
if (nxt1 < a && (!cnt.count({nxt1, type}) || cnt[{nxt1, type}] > val + 1)) {
cnt[{nxt1, type}] = val + 1;
q.push({cnt[{nxt1, type}], nxt1, type});
}
if (nxt2 >= 0 && (!cnt.count({nxt2, type}) || cnt[{nxt2, type}] > val + 1)) {
cnt[{nxt2, type}] = val + 1;
q.push({cnt[{nxt2, type}], nxt2, type});
}
}
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> a >> b;
blocksize = sqrt(a);
for (int i = 0; i < b; i++) {
int x, y;
cin >> x >> y;
z[x].push_back(y);
mark[i] = {x, y};
}
if (b == 0) {
cout << -1;
return 0;
}
dijkstra();
if (cnt.count({mark[1].first, 0})) {
cout << cnt[{mark[1].first, 0}];
} else {
cout << -1;
}
return 0;
}
Compilation message (stderr)
skyscraper.cpp:22:39: warning: bad option '-fwhole-program' to pragma 'optimize' [-Wpragmas]
22 | #pragma GCC optimize("-fwhole-program")
| ^
skyscraper.cpp:29:41: warning: bad option '-fstrict-overflow' to pragma 'optimize' [-Wpragmas]
29 | #pragma GCC optimize("-fstrict-overflow")
| ^
skyscraper.cpp:31:41: warning: bad option '-fcse-skip-blocks' to pragma 'optimize' [-Wpragmas]
31 | #pragma GCC optimize("-fcse-skip-blocks")
| ^
skyscraper.cpp:45:51: warning: bad option '-funsafe-loop-optimizations' to pragma 'optimize' [-Wpragmas]
45 | #pragma GCC optimize("-funsafe-loop-optimizations")
| ^
skyscraper.cpp:59:39: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
59 | bool operator>(const node &other) const {
| ^~~~~
skyscraper.cpp:59:39: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:59:39: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:59:39: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:66:15: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
66 | void dijkstra() {
| ^
skyscraper.cpp:66:15: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:66:15: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:66:15: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:140:13: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
140 | signed main() {
| ^
skyscraper.cpp:140:13: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:140:13: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:140:13: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |