// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x)
// __builtin_clz(x) vodece nule
// __builtin_clzll(x)
// __builtin_ctz(x) nule na pocetku
// __builtin_ctzll(x)
// 2000000011
// 2000000033
#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) ((ll)(a.size()))
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pi 3.14159265358979323846
#define here cerr<<"=============================================================================\n"
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define ceri2(a,l,r,n,m) {for(ll i = l;i<=r;i++){for(ll j = n;j<=m;j++) cerr<<a[i][j]<< " ";cerr<<endl;}}
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
using namespace __gnu_pbds;
template<typename T> using vc = vector<T>;
template<typename T> using vvc = vc<vc<T>>;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
void setIO(string inoutname)
{
freopen((inoutname+".in").c_str(),"r",stdin);
freopen((inoutname+".out").c_str(),"w",stdout);
}
#define mod 1
ll gcd(ll a, ll b)
{
if(b==0) return a;
if(a==0) return b;
if(a>=b) return gcd(a%b,b);
return gcd(a,b%a);
}
ll lcm(ll a,ll b){
return (a/gcd(a,b))*b;
}
ll add(ll x,ll y){
x+=y;
if(x<0){
x%=mod;
x+=mod;
if(x==mod) x = 0;
}else{
if(x>=mod) x%=mod;
}
return x;
}
ll mul(ll a,ll b){
ll ans = (a*b)%mod;
if(ans<0) ans+=mod;
return ans;
}
ll po(ll x,ll y){
if(y==0) return 1LL;
ll ans = po(x,y/2);
ans = mul(ans,ans);
if(y&1) ans = mul(ans,x);
return ans;
}
ll inv(ll x){return po(x,mod-2);}
#define maxn 30005
ll n,m;
vector<pll> g[maxn];
set<ll> v[maxn];
pll a[maxn];
ll d[maxn];
bool cmp(pll x,pll y){
return x.sc<y.sc;
}
void tc(){
daj_mi_malo_vremena
cin >> n >> m;
for(ll i = 1;i<=m;i++){
ll x,y; cin >> x >> y;
a[i] = {x,y};
v[x].insert(y);
}
for(ll i = 0;i<n;i++){
for(ll x : v[i]){
for(ll j = i+x;j<n;j+=x){
if(sz(v[j])==0) continue;
g[i].pb({j,(j-i)/x});
if(v[j].count(x)) break;
}
for(ll j = i-x;j>=0;j-=x){
if(sz(v[j])==0) continue;
g[i].pb({j,(i-j)/x});
if(v[j].count(x)) break;
}
}
}
fill(d,d+maxn-1,llinf);
d[a[1].fi] = 0;
priority_queue<pll,vector<pll>,decltype(&cmp)> pq(cmp);
pq.push({a[1].fi,0});
while(sz(pq)){
ll u = pq.top().fi;
pq.pop();
for(pll p : g[u]){
ll s = p.fi;
ll c = p.sc;
if(d[u]+c<d[s]){
d[s] = d[u] + c;
pq.push({s,d[s]});
}
}
}
ll ans = d[a[2].fi];
if(ans==llinf) ans = -1;
cout<<ans<<endl;
}
int main(){
daj_mi_malo_vremena
//setIO("lol");
int t; t = 1;
while(t--){
tc();
}
return 0;
}
Compilation message
skyscraper.cpp: In function 'void setIO(std::string)':
skyscraper.cpp:50:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen((inoutname+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:51:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen((inoutname+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2572 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2568 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2832 KB |
Output is correct |
12 |
Correct |
2 ms |
2704 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2564 KB |
Output is correct |
2 |
Correct |
1 ms |
2568 KB |
Output is correct |
3 |
Correct |
2 ms |
2572 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2704 KB |
Output is correct |
14 |
Correct |
3 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
18 ms |
3028 KB |
Output is correct |
18 |
Correct |
2 ms |
2700 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
3 ms |
2844 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2704 KB |
Output is correct |
23 |
Correct |
4 ms |
2712 KB |
Output is correct |
24 |
Correct |
22 ms |
2900 KB |
Output is correct |
25 |
Correct |
4 ms |
2836 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
4 ms |
3284 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2704 KB |
Output is correct |
32 |
Correct |
2 ms |
2704 KB |
Output is correct |
33 |
Correct |
4 ms |
2772 KB |
Output is correct |
34 |
Correct |
4 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2580 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2704 KB |
Output is correct |
13 |
Correct |
2 ms |
2708 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
18 ms |
3028 KB |
Output is correct |
18 |
Correct |
2 ms |
2708 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
4 ms |
2772 KB |
Output is correct |
24 |
Correct |
21 ms |
2972 KB |
Output is correct |
25 |
Correct |
4 ms |
2912 KB |
Output is correct |
26 |
Correct |
2 ms |
2708 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
3 ms |
3284 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2836 KB |
Output is correct |
35 |
Correct |
114 ms |
6476 KB |
Output is correct |
36 |
Correct |
29 ms |
3284 KB |
Output is correct |
37 |
Correct |
184 ms |
8172 KB |
Output is correct |
38 |
Correct |
216 ms |
8448 KB |
Output is correct |
39 |
Correct |
254 ms |
8368 KB |
Output is correct |
40 |
Correct |
265 ms |
8312 KB |
Output is correct |
41 |
Correct |
252 ms |
8320 KB |
Output is correct |
42 |
Correct |
5 ms |
3284 KB |
Output is correct |
43 |
Correct |
6 ms |
3284 KB |
Output is correct |
44 |
Correct |
7 ms |
3460 KB |
Output is correct |
45 |
Correct |
17 ms |
5336 KB |
Output is correct |
46 |
Correct |
24 ms |
5292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2568 KB |
Output is correct |
5 |
Correct |
2 ms |
2572 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2724 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
17 ms |
3028 KB |
Output is correct |
18 |
Correct |
3 ms |
2712 KB |
Output is correct |
19 |
Correct |
2 ms |
2700 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
4 ms |
2744 KB |
Output is correct |
24 |
Correct |
24 ms |
2900 KB |
Output is correct |
25 |
Correct |
4 ms |
2900 KB |
Output is correct |
26 |
Correct |
2 ms |
2708 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
3 ms |
3284 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
3 ms |
2568 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
3 ms |
2836 KB |
Output is correct |
34 |
Correct |
3 ms |
2772 KB |
Output is correct |
35 |
Correct |
127 ms |
6520 KB |
Output is correct |
36 |
Correct |
31 ms |
3404 KB |
Output is correct |
37 |
Correct |
180 ms |
8208 KB |
Output is correct |
38 |
Correct |
234 ms |
8444 KB |
Output is correct |
39 |
Correct |
266 ms |
8432 KB |
Output is correct |
40 |
Correct |
313 ms |
8252 KB |
Output is correct |
41 |
Correct |
294 ms |
8328 KB |
Output is correct |
42 |
Correct |
7 ms |
3352 KB |
Output is correct |
43 |
Correct |
7 ms |
3356 KB |
Output is correct |
44 |
Correct |
7 ms |
3460 KB |
Output is correct |
45 |
Correct |
17 ms |
5392 KB |
Output is correct |
46 |
Correct |
18 ms |
5304 KB |
Output is correct |
47 |
Execution timed out |
1070 ms |
18980 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |