#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define vec vector
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define fast_iati ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
template <class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template <class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int K=80;
struct segtree{
vec<int> ids;
vec<pii>t;
void build(int v,int t1,int t2,int tl,int tr){
if(tl>tr) return;
if(tl==tr){
t[v]={2e9+t1*(ids[tl]/t2),ids[tl]};
return;
}
int tm=(tl+tr)>>1;
build(2*v,t1,t2,tl,tm);
build(2*v+1,t1,t2,tm+1,tr);
t[v]=max(t[2*v],t[2*v+1]);
}
void init(int t1,int t2){
t.resize(sz(ids)*4);
build(1,t1,t2,0,sz(ids)-1);
}
void upd(int id,int x,int v,int tl,int tr){
if(tl==tr){
assert(ids[tl]==id && t[v].f>x);
t[v]={x,id};
// cout<<"UPD "<<id<<' '<<x<<endl;
return;
}
int tm=(tl+tr)>>1;
if(ids[tm]>=id)
upd(id,x,2*v,tl,tm);
else
upd(id,x,2*v+1,tm+1,tr);
t[v]=max(t[2*v],t[2*v+1]);
}
pii get(int l,int r,int v,int tl,int tr){
if(ids[tl]>=l&&ids[tr]<=r) return t[v];
if(ids[tl]>r||ids[tr]<l) return {-2e9,1};
int tm=(tl+tr)>>1;
return max(get(l,r,2*v,tl,tm),get(l,r,2*v+1,tm+1,tr));
}
}lft[K][K],rgt[K][K];
/// nd+(i-j)<=d1
/// nd+i<=d1+1
void change(int id,int x){
for(int j=1;j<K;j++){
lft[j][id%j].upd(id,x+(id/j),1,0,sz(lft[j][id%j].ids)-1);
rgt[j][id%j].upd(id,x-(id/j),1,0,sz(rgt[j][id%j].ids)-1);
}
}
signed main(){
fast_iati;
int n,m;
cin>>n>>m;
vec<int>b(m),p(m);
vec<vec<int>>have(n,vec<int>());
for(int i=0;i<m;i++)
cin>>b[i]>>p[i],have[b[i]].pb(i);
priority_queue<pii,vec<pii>,greater<pii>>pq;
for(int t=1;t<K;t++){
for(int i=0;i<n;i++)
lft[t][i%t].ids.pb(i),rgt[t][i%t].ids.pb(i);
for(int j=0;j<t;j++){
lft[t][j].init(+1,t),rgt[t][j].init(-1,t);
}
}
vec<int>dp(n,2e9);
pq.push({0,b[0]});
dp[b[0]]=0;
change(b[0],0);
while(!pq.empty()){
int v=pq.top().s;
int x=pq.top().f;
pq.pop();
if(dp[v]!=x) continue;
for(auto &u : have[v]){
if(p[u]>=K){
int j=b[u]-p[u];
int cnt=1;
while(j>=0){
if(umin(dp[j],dp[v]+cnt))
pq.push({dp[j],j}),change(j,dp[j]);
j-=p[u];cnt++;
}
j=b[u]+p[u];cnt=1;
while(j<n){
if(umin(dp[j],dp[v]+cnt))
pq.push({dp[j],j}),change(j,dp[j]);
j+=p[u];cnt++;
}
}
else{
while(1){
pii js=lft[p[u]][b[u]%p[u]].get(0,b[u]-1,1,0,sz(lft[p[u]][b[u]%p[u]].ids)-1);
int f=js.f,j=js.s;
// if(v==37 && p[u]==5){
// cout<<"A E "<<j<<' '<<dp[v]+v<<' '<<' '<<f<<endl;
// }
if(dp[v]+v/p[u]<f){
umin(dp[j],dp[v]+((v-j)/p[u]));
change(j,dp[j]);
// if(v==37)cout<<"NEW "<<j<<' '<<dp[j]<<' '<<dp[v]<<' '<<dp[v]+((v-j)/p[u])<<' '<<v<<' '<<p[u]<<endl;
pq.push({dp[j],j});
}
else break;
}
while(1){
pii js=rgt[p[u]][b[u]%p[u]].get(b[u]+1,n-1,1,0,sz(rgt[p[u]][b[u]%p[u]].ids)-1);
int f=js.f,j=js.s;
// if(v==37){
// cout<<"A E "<<j<<' '<<dp[v]-v<<' '<<' '<<f<<endl;
// }
if(dp[v]-v/p[u]<f){
umin(dp[j],dp[v]+((j-v)/p[u]));
change(j,dp[j]);
pq.push({dp[j],j});
}
else break;
}
}
}
}
cout<<(dp[b[1]]==2e9?-1:dp[b[1]]);
return 0;
}
/*
6 1 3
8 1 2 1 5 4
5 3
0 2
1 1
4 1
dp[v]+(v-j)<=dp[j]
//dp[v]+v < dp[j]+
dp[v]-v<=dp[j]-j
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
0 ms |
844 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
844 KB |
Output is correct |
2 |
Correct |
1 ms |
844 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
2 ms |
1484 KB |
Output is correct |
10 |
Correct |
2 ms |
1612 KB |
Output is correct |
11 |
Correct |
3 ms |
1740 KB |
Output is correct |
12 |
Correct |
3 ms |
1740 KB |
Output is correct |
13 |
Correct |
3 ms |
1740 KB |
Output is correct |
14 |
Correct |
3 ms |
1740 KB |
Output is correct |
15 |
Correct |
3 ms |
1740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
844 KB |
Output is correct |
2 |
Correct |
1 ms |
844 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1484 KB |
Output is correct |
10 |
Correct |
2 ms |
1612 KB |
Output is correct |
11 |
Correct |
3 ms |
1740 KB |
Output is correct |
12 |
Correct |
3 ms |
1740 KB |
Output is correct |
13 |
Correct |
3 ms |
1740 KB |
Output is correct |
14 |
Correct |
3 ms |
1740 KB |
Output is correct |
15 |
Correct |
3 ms |
1740 KB |
Output is correct |
16 |
Correct |
2 ms |
2252 KB |
Output is correct |
17 |
Correct |
21 ms |
5512 KB |
Output is correct |
18 |
Correct |
12 ms |
11280 KB |
Output is correct |
19 |
Correct |
15 ms |
12708 KB |
Output is correct |
20 |
Correct |
34 ms |
12844 KB |
Output is correct |
21 |
Correct |
4 ms |
3404 KB |
Output is correct |
22 |
Correct |
12 ms |
11488 KB |
Output is correct |
23 |
Correct |
39 ms |
10444 KB |
Output is correct |
24 |
Correct |
67 ms |
12296 KB |
Output is correct |
25 |
Correct |
79 ms |
12876 KB |
Output is correct |
26 |
Correct |
48 ms |
12840 KB |
Output is correct |
27 |
Correct |
35 ms |
12836 KB |
Output is correct |
28 |
Correct |
42 ms |
12872 KB |
Output is correct |
29 |
Correct |
169 ms |
13044 KB |
Output is correct |
30 |
Correct |
86 ms |
12844 KB |
Output is correct |
31 |
Correct |
118 ms |
13040 KB |
Output is correct |
32 |
Correct |
94 ms |
12908 KB |
Output is correct |
33 |
Correct |
180 ms |
12944 KB |
Output is correct |
34 |
Correct |
198 ms |
13036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
0 ms |
844 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1484 KB |
Output is correct |
10 |
Correct |
2 ms |
1740 KB |
Output is correct |
11 |
Correct |
3 ms |
1740 KB |
Output is correct |
12 |
Correct |
3 ms |
1740 KB |
Output is correct |
13 |
Correct |
3 ms |
1656 KB |
Output is correct |
14 |
Correct |
3 ms |
1756 KB |
Output is correct |
15 |
Correct |
3 ms |
1740 KB |
Output is correct |
16 |
Correct |
3 ms |
2252 KB |
Output is correct |
17 |
Correct |
22 ms |
5452 KB |
Output is correct |
18 |
Correct |
13 ms |
11340 KB |
Output is correct |
19 |
Correct |
15 ms |
12760 KB |
Output is correct |
20 |
Correct |
33 ms |
12848 KB |
Output is correct |
21 |
Correct |
4 ms |
3404 KB |
Output is correct |
22 |
Correct |
12 ms |
11468 KB |
Output is correct |
23 |
Correct |
38 ms |
10444 KB |
Output is correct |
24 |
Correct |
68 ms |
12296 KB |
Output is correct |
25 |
Correct |
67 ms |
12872 KB |
Output is correct |
26 |
Correct |
43 ms |
12732 KB |
Output is correct |
27 |
Correct |
43 ms |
12784 KB |
Output is correct |
28 |
Correct |
43 ms |
12788 KB |
Output is correct |
29 |
Correct |
168 ms |
12944 KB |
Output is correct |
30 |
Correct |
85 ms |
12900 KB |
Output is correct |
31 |
Correct |
105 ms |
12960 KB |
Output is correct |
32 |
Correct |
103 ms |
12824 KB |
Output is correct |
33 |
Correct |
179 ms |
13024 KB |
Output is correct |
34 |
Correct |
190 ms |
13044 KB |
Output is correct |
35 |
Correct |
67 ms |
9944 KB |
Output is correct |
36 |
Correct |
35 ms |
7604 KB |
Output is correct |
37 |
Correct |
106 ms |
12868 KB |
Output is correct |
38 |
Correct |
130 ms |
13232 KB |
Output is correct |
39 |
Correct |
110 ms |
13248 KB |
Output is correct |
40 |
Correct |
129 ms |
13332 KB |
Output is correct |
41 |
Correct |
106 ms |
13260 KB |
Output is correct |
42 |
Correct |
46 ms |
13132 KB |
Output is correct |
43 |
Correct |
43 ms |
13172 KB |
Output is correct |
44 |
Correct |
44 ms |
13232 KB |
Output is correct |
45 |
Correct |
181 ms |
13276 KB |
Output is correct |
46 |
Correct |
149 ms |
13384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
844 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
2 ms |
1484 KB |
Output is correct |
10 |
Correct |
3 ms |
1792 KB |
Output is correct |
11 |
Correct |
3 ms |
1740 KB |
Output is correct |
12 |
Correct |
2 ms |
1740 KB |
Output is correct |
13 |
Correct |
2 ms |
1716 KB |
Output is correct |
14 |
Correct |
3 ms |
1740 KB |
Output is correct |
15 |
Correct |
3 ms |
1740 KB |
Output is correct |
16 |
Correct |
3 ms |
2252 KB |
Output is correct |
17 |
Correct |
20 ms |
5452 KB |
Output is correct |
18 |
Correct |
13 ms |
11340 KB |
Output is correct |
19 |
Correct |
14 ms |
12748 KB |
Output is correct |
20 |
Correct |
33 ms |
12904 KB |
Output is correct |
21 |
Correct |
4 ms |
3404 KB |
Output is correct |
22 |
Correct |
13 ms |
11436 KB |
Output is correct |
23 |
Correct |
40 ms |
10532 KB |
Output is correct |
24 |
Correct |
69 ms |
12420 KB |
Output is correct |
25 |
Correct |
65 ms |
12876 KB |
Output is correct |
26 |
Correct |
39 ms |
12772 KB |
Output is correct |
27 |
Correct |
35 ms |
12748 KB |
Output is correct |
28 |
Correct |
40 ms |
12876 KB |
Output is correct |
29 |
Correct |
170 ms |
12992 KB |
Output is correct |
30 |
Correct |
99 ms |
12868 KB |
Output is correct |
31 |
Correct |
111 ms |
12864 KB |
Output is correct |
32 |
Correct |
77 ms |
12868 KB |
Output is correct |
33 |
Correct |
203 ms |
13008 KB |
Output is correct |
34 |
Correct |
225 ms |
13112 KB |
Output is correct |
35 |
Correct |
72 ms |
9964 KB |
Output is correct |
36 |
Correct |
31 ms |
7624 KB |
Output is correct |
37 |
Correct |
104 ms |
12748 KB |
Output is correct |
38 |
Correct |
128 ms |
13220 KB |
Output is correct |
39 |
Correct |
97 ms |
13260 KB |
Output is correct |
40 |
Correct |
143 ms |
13396 KB |
Output is correct |
41 |
Correct |
113 ms |
13340 KB |
Output is correct |
42 |
Correct |
46 ms |
13168 KB |
Output is correct |
43 |
Correct |
43 ms |
13168 KB |
Output is correct |
44 |
Correct |
47 ms |
13152 KB |
Output is correct |
45 |
Correct |
171 ms |
13348 KB |
Output is correct |
46 |
Correct |
178 ms |
13376 KB |
Output is correct |
47 |
Execution timed out |
1095 ms |
74756 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |