#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=100;
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 |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1356 KB |
Output is correct |
5 |
Correct |
1 ms |
1356 KB |
Output is correct |
6 |
Correct |
1 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1356 KB |
Output is correct |
5 |
Correct |
1 ms |
1356 KB |
Output is correct |
6 |
Correct |
1 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
2 ms |
1740 KB |
Output is correct |
9 |
Correct |
2 ms |
1996 KB |
Output is correct |
10 |
Correct |
3 ms |
2256 KB |
Output is correct |
11 |
Correct |
3 ms |
2288 KB |
Output is correct |
12 |
Correct |
3 ms |
2384 KB |
Output is correct |
13 |
Correct |
3 ms |
2384 KB |
Output is correct |
14 |
Correct |
4 ms |
2384 KB |
Output is correct |
15 |
Correct |
3 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1356 KB |
Output is correct |
5 |
Correct |
1 ms |
1356 KB |
Output is correct |
6 |
Correct |
2 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
2 ms |
1740 KB |
Output is correct |
9 |
Correct |
3 ms |
1996 KB |
Output is correct |
10 |
Correct |
3 ms |
2256 KB |
Output is correct |
11 |
Correct |
5 ms |
2384 KB |
Output is correct |
12 |
Correct |
3 ms |
2408 KB |
Output is correct |
13 |
Correct |
3 ms |
2384 KB |
Output is correct |
14 |
Correct |
6 ms |
2384 KB |
Output is correct |
15 |
Correct |
4 ms |
2384 KB |
Output is correct |
16 |
Correct |
3 ms |
3024 KB |
Output is correct |
17 |
Correct |
28 ms |
7080 KB |
Output is correct |
18 |
Correct |
17 ms |
14416 KB |
Output is correct |
19 |
Correct |
22 ms |
16212 KB |
Output is correct |
20 |
Correct |
50 ms |
16312 KB |
Output is correct |
21 |
Correct |
6 ms |
4444 KB |
Output is correct |
22 |
Correct |
21 ms |
14652 KB |
Output is correct |
23 |
Correct |
69 ms |
13408 KB |
Output is correct |
24 |
Correct |
86 ms |
15800 KB |
Output is correct |
25 |
Correct |
83 ms |
16332 KB |
Output is correct |
26 |
Correct |
53 ms |
16260 KB |
Output is correct |
27 |
Correct |
53 ms |
16256 KB |
Output is correct |
28 |
Correct |
64 ms |
16300 KB |
Output is correct |
29 |
Correct |
247 ms |
16356 KB |
Output is correct |
30 |
Correct |
110 ms |
16320 KB |
Output is correct |
31 |
Correct |
157 ms |
16328 KB |
Output is correct |
32 |
Correct |
130 ms |
16284 KB |
Output is correct |
33 |
Correct |
300 ms |
16348 KB |
Output is correct |
34 |
Correct |
267 ms |
16456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1356 KB |
Output is correct |
5 |
Correct |
2 ms |
1356 KB |
Output is correct |
6 |
Correct |
1 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1740 KB |
Output is correct |
9 |
Correct |
2 ms |
1996 KB |
Output is correct |
10 |
Correct |
3 ms |
2256 KB |
Output is correct |
11 |
Correct |
3 ms |
2384 KB |
Output is correct |
12 |
Correct |
5 ms |
2336 KB |
Output is correct |
13 |
Correct |
3 ms |
2384 KB |
Output is correct |
14 |
Correct |
4 ms |
2384 KB |
Output is correct |
15 |
Correct |
4 ms |
2384 KB |
Output is correct |
16 |
Correct |
4 ms |
3024 KB |
Output is correct |
17 |
Correct |
38 ms |
7060 KB |
Output is correct |
18 |
Correct |
19 ms |
14364 KB |
Output is correct |
19 |
Correct |
24 ms |
16132 KB |
Output is correct |
20 |
Correct |
50 ms |
16316 KB |
Output is correct |
21 |
Correct |
5 ms |
4432 KB |
Output is correct |
22 |
Correct |
20 ms |
14684 KB |
Output is correct |
23 |
Correct |
63 ms |
13416 KB |
Output is correct |
24 |
Correct |
97 ms |
15604 KB |
Output is correct |
25 |
Correct |
104 ms |
16252 KB |
Output is correct |
26 |
Correct |
61 ms |
16244 KB |
Output is correct |
27 |
Correct |
51 ms |
16200 KB |
Output is correct |
28 |
Correct |
65 ms |
16304 KB |
Output is correct |
29 |
Correct |
257 ms |
16460 KB |
Output is correct |
30 |
Correct |
126 ms |
16236 KB |
Output is correct |
31 |
Correct |
151 ms |
16308 KB |
Output is correct |
32 |
Correct |
109 ms |
16248 KB |
Output is correct |
33 |
Correct |
306 ms |
16392 KB |
Output is correct |
34 |
Correct |
290 ms |
16448 KB |
Output is correct |
35 |
Correct |
91 ms |
12740 KB |
Output is correct |
36 |
Correct |
47 ms |
9692 KB |
Output is correct |
37 |
Correct |
138 ms |
16240 KB |
Output is correct |
38 |
Correct |
197 ms |
17072 KB |
Output is correct |
39 |
Correct |
141 ms |
16960 KB |
Output is correct |
40 |
Correct |
156 ms |
16976 KB |
Output is correct |
41 |
Correct |
168 ms |
16964 KB |
Output is correct |
42 |
Correct |
70 ms |
16896 KB |
Output is correct |
43 |
Correct |
79 ms |
16744 KB |
Output is correct |
44 |
Correct |
57 ms |
16752 KB |
Output is correct |
45 |
Correct |
284 ms |
16960 KB |
Output is correct |
46 |
Correct |
223 ms |
17104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1356 KB |
Output is correct |
5 |
Correct |
2 ms |
1356 KB |
Output is correct |
6 |
Correct |
2 ms |
1356 KB |
Output is correct |
7 |
Correct |
2 ms |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1740 KB |
Output is correct |
9 |
Correct |
2 ms |
1996 KB |
Output is correct |
10 |
Correct |
3 ms |
2336 KB |
Output is correct |
11 |
Correct |
4 ms |
2384 KB |
Output is correct |
12 |
Correct |
3 ms |
2384 KB |
Output is correct |
13 |
Correct |
3 ms |
2384 KB |
Output is correct |
14 |
Correct |
4 ms |
2384 KB |
Output is correct |
15 |
Correct |
3 ms |
2384 KB |
Output is correct |
16 |
Correct |
3 ms |
3024 KB |
Output is correct |
17 |
Correct |
26 ms |
6992 KB |
Output is correct |
18 |
Correct |
16 ms |
14476 KB |
Output is correct |
19 |
Correct |
18 ms |
16208 KB |
Output is correct |
20 |
Correct |
53 ms |
16328 KB |
Output is correct |
21 |
Correct |
6 ms |
4468 KB |
Output is correct |
22 |
Correct |
17 ms |
14676 KB |
Output is correct |
23 |
Correct |
69 ms |
13408 KB |
Output is correct |
24 |
Correct |
115 ms |
15588 KB |
Output is correct |
25 |
Correct |
93 ms |
16328 KB |
Output is correct |
26 |
Correct |
55 ms |
16256 KB |
Output is correct |
27 |
Correct |
60 ms |
16328 KB |
Output is correct |
28 |
Correct |
55 ms |
16300 KB |
Output is correct |
29 |
Correct |
243 ms |
16432 KB |
Output is correct |
30 |
Correct |
177 ms |
16236 KB |
Output is correct |
31 |
Correct |
154 ms |
16248 KB |
Output is correct |
32 |
Correct |
99 ms |
16336 KB |
Output is correct |
33 |
Correct |
252 ms |
16376 KB |
Output is correct |
34 |
Correct |
298 ms |
16444 KB |
Output is correct |
35 |
Correct |
112 ms |
12792 KB |
Output is correct |
36 |
Correct |
50 ms |
9684 KB |
Output is correct |
37 |
Correct |
143 ms |
16280 KB |
Output is correct |
38 |
Correct |
149 ms |
16896 KB |
Output is correct |
39 |
Correct |
139 ms |
17008 KB |
Output is correct |
40 |
Correct |
211 ms |
17044 KB |
Output is correct |
41 |
Correct |
187 ms |
16968 KB |
Output is correct |
42 |
Correct |
60 ms |
16788 KB |
Output is correct |
43 |
Correct |
72 ms |
16776 KB |
Output is correct |
44 |
Correct |
76 ms |
16844 KB |
Output is correct |
45 |
Correct |
216 ms |
17040 KB |
Output is correct |
46 |
Correct |
204 ms |
17016 KB |
Output is correct |
47 |
Execution timed out |
1092 ms |
93936 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |