#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;
struct PairHash {
size_t operator()(const pair<int, int>& p) const {
auto h1 = std::hash<int>()(p.first);
auto h2 = std::hash<int>()(p.second);
return h1 ^ (h2 + 0x9e3779b97f4a7c15ULL + (h1 << 6) + (h1 >> 2));
}
};
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;
}
};
unordered_map<pair<int, int>, int, PairHash> 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 cur = q.top();
q.pop();
int val = cur.val, pos1 = cur.pos, type = cur.type;
if (cnt[{pos1, type}] < val) continue;
if (type == 0) {
for (auto p : z[pos1]) {
int newPos = pos1 + p;
if (newPos < a) {
pair<int,int> key = {newPos, p};
if (!cnt.count(key) || cnt[key] > val + 1) {
cnt[key] = val + 1;
q.push({val + 1, newPos, p});
}
}
newPos = pos1 - p;
if (newPos >= 0) {
pair<int,int> key = {newPos, p};
if (!cnt.count(key) || cnt[key] > val + 1) {
cnt[key] = val + 1;
q.push({val + 1, newPos, p});
}
}
}
} else {
pair<int,int> key0 = {pos1, 0};
if (!cnt.count(key0) || cnt[key0] > val) {
cnt[key0] = val;
q.push({val, pos1, 0});
}
if (type > blocksize) {
int step = 1;
while (pos1 + step * type < a) {
int newPos = pos1 + step * type;
pair<int,int> key0 = {newPos, 0};
if (!cnt.count(key0) || cnt[key0] > val + step) {
cnt[key0] = val + step;
q.push({val + step, newPos, 0});
}
step++;
}
step = 1;
while (pos1 - step * type >= 0) {
int newPos = pos1 - step * type;
pair<int,int> key0 = {newPos, 0};
if (!cnt.count(key0) || cnt[key0] > val + step) {
cnt[key0] = val + step;
q.push({val + step, newPos, 0});
}
step++;
}
} else {
int newPos = pos1 + type;
if (newPos < a) {
pair<int,int> key = {newPos, type};
if (!cnt.count(key) || cnt[key] > val + 1) {
cnt[key] = val + 1;
q.push({val + 1, newPos, type});
}
}
newPos = pos1 - type;
if (newPos >= 0) {
pair<int,int> key = {newPos, type};
if (!cnt.count(key) || cnt[key] > val + 1) {
cnt[key] = val + 1;
q.push({val + 1, newPos, type});
}
}
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
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();
pair<int,int> finalKey = {mark[1].first, 0};
if(cnt.count(finalKey)){
cout << cnt[finalKey];
} 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:53:48: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
53 | size_t operator()(const pair<int, int>& p) const {
| ^~~~~
skyscraper.cpp:53:48: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:53:48: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:53:48: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:67:39: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
67 | bool operator>(const node &other) const {
| ^~~~~
skyscraper.cpp:67:39: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:67:39: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:67:39: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:75:15: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
75 | void dijkstra() {
| ^
skyscraper.cpp:75:15: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:75:15: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:75:15: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:157:10: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
157 | int main(){
| ^
skyscraper.cpp:157:10: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:157:10: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
skyscraper.cpp:157:10: 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... |