#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=150;
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 |
2508 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
2 ms |
2508 KB |
Output is correct |
4 |
Correct |
1 ms |
2508 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2508 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2508 KB |
Output is correct |
4 |
Correct |
2 ms |
2508 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
3 ms |
3148 KB |
Output is correct |
9 |
Correct |
5 ms |
3532 KB |
Output is correct |
10 |
Correct |
5 ms |
4300 KB |
Output is correct |
11 |
Correct |
7 ms |
4300 KB |
Output is correct |
12 |
Correct |
5 ms |
4300 KB |
Output is correct |
13 |
Correct |
6 ms |
4428 KB |
Output is correct |
14 |
Correct |
5 ms |
4352 KB |
Output is correct |
15 |
Correct |
6 ms |
4232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2508 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2508 KB |
Output is correct |
4 |
Correct |
1 ms |
2508 KB |
Output is correct |
5 |
Correct |
1 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
3148 KB |
Output is correct |
9 |
Correct |
3 ms |
3532 KB |
Output is correct |
10 |
Correct |
5 ms |
4300 KB |
Output is correct |
11 |
Correct |
6 ms |
4300 KB |
Output is correct |
12 |
Correct |
4 ms |
4300 KB |
Output is correct |
13 |
Correct |
4 ms |
4300 KB |
Output is correct |
14 |
Correct |
5 ms |
4300 KB |
Output is correct |
15 |
Correct |
8 ms |
4300 KB |
Output is correct |
16 |
Correct |
6 ms |
5324 KB |
Output is correct |
17 |
Correct |
47 ms |
11316 KB |
Output is correct |
18 |
Correct |
27 ms |
22272 KB |
Output is correct |
19 |
Correct |
33 ms |
25064 KB |
Output is correct |
20 |
Correct |
89 ms |
25324 KB |
Output is correct |
21 |
Correct |
8 ms |
7372 KB |
Output is correct |
22 |
Correct |
27 ms |
22688 KB |
Output is correct |
23 |
Correct |
89 ms |
20732 KB |
Output is correct |
24 |
Correct |
133 ms |
24064 KB |
Output is correct |
25 |
Correct |
126 ms |
25164 KB |
Output is correct |
26 |
Correct |
106 ms |
25020 KB |
Output is correct |
27 |
Correct |
89 ms |
25056 KB |
Output is correct |
28 |
Correct |
92 ms |
25184 KB |
Output is correct |
29 |
Correct |
334 ms |
25256 KB |
Output is correct |
30 |
Correct |
193 ms |
25396 KB |
Output is correct |
31 |
Correct |
224 ms |
25156 KB |
Output is correct |
32 |
Correct |
192 ms |
25216 KB |
Output is correct |
33 |
Correct |
464 ms |
25384 KB |
Output is correct |
34 |
Correct |
402 ms |
25280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2508 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2508 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
1 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
3148 KB |
Output is correct |
9 |
Correct |
3 ms |
3532 KB |
Output is correct |
10 |
Correct |
5 ms |
4300 KB |
Output is correct |
11 |
Correct |
5 ms |
4328 KB |
Output is correct |
12 |
Correct |
5 ms |
4244 KB |
Output is correct |
13 |
Correct |
5 ms |
4248 KB |
Output is correct |
14 |
Correct |
6 ms |
4300 KB |
Output is correct |
15 |
Correct |
5 ms |
4300 KB |
Output is correct |
16 |
Correct |
5 ms |
5324 KB |
Output is correct |
17 |
Correct |
49 ms |
11308 KB |
Output is correct |
18 |
Correct |
27 ms |
22344 KB |
Output is correct |
19 |
Correct |
32 ms |
25128 KB |
Output is correct |
20 |
Correct |
92 ms |
25108 KB |
Output is correct |
21 |
Correct |
10 ms |
7372 KB |
Output is correct |
22 |
Correct |
28 ms |
22668 KB |
Output is correct |
23 |
Correct |
82 ms |
20736 KB |
Output is correct |
24 |
Correct |
132 ms |
24060 KB |
Output is correct |
25 |
Correct |
143 ms |
25164 KB |
Output is correct |
26 |
Correct |
91 ms |
25084 KB |
Output is correct |
27 |
Correct |
96 ms |
25144 KB |
Output is correct |
28 |
Correct |
90 ms |
25284 KB |
Output is correct |
29 |
Correct |
332 ms |
25284 KB |
Output is correct |
30 |
Correct |
187 ms |
25112 KB |
Output is correct |
31 |
Correct |
241 ms |
25408 KB |
Output is correct |
32 |
Correct |
169 ms |
25216 KB |
Output is correct |
33 |
Correct |
386 ms |
25260 KB |
Output is correct |
34 |
Correct |
495 ms |
25248 KB |
Output is correct |
35 |
Correct |
141 ms |
19396 KB |
Output is correct |
36 |
Correct |
70 ms |
15208 KB |
Output is correct |
37 |
Correct |
211 ms |
24932 KB |
Output is correct |
38 |
Correct |
227 ms |
25492 KB |
Output is correct |
39 |
Correct |
195 ms |
25588 KB |
Output is correct |
40 |
Correct |
254 ms |
25612 KB |
Output is correct |
41 |
Correct |
220 ms |
25540 KB |
Output is correct |
42 |
Correct |
97 ms |
25468 KB |
Output is correct |
43 |
Correct |
98 ms |
25468 KB |
Output is correct |
44 |
Correct |
90 ms |
25672 KB |
Output is correct |
45 |
Correct |
314 ms |
25656 KB |
Output is correct |
46 |
Correct |
307 ms |
25632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2508 KB |
Output is correct |
2 |
Correct |
1 ms |
2380 KB |
Output is correct |
3 |
Correct |
1 ms |
2508 KB |
Output is correct |
4 |
Correct |
1 ms |
2508 KB |
Output is correct |
5 |
Correct |
1 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
1 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
3148 KB |
Output is correct |
9 |
Correct |
3 ms |
3532 KB |
Output is correct |
10 |
Correct |
4 ms |
4300 KB |
Output is correct |
11 |
Correct |
5 ms |
4300 KB |
Output is correct |
12 |
Correct |
4 ms |
4300 KB |
Output is correct |
13 |
Correct |
5 ms |
4340 KB |
Output is correct |
14 |
Correct |
5 ms |
4300 KB |
Output is correct |
15 |
Correct |
5 ms |
4300 KB |
Output is correct |
16 |
Correct |
5 ms |
5324 KB |
Output is correct |
17 |
Correct |
45 ms |
11316 KB |
Output is correct |
18 |
Correct |
28 ms |
22340 KB |
Output is correct |
19 |
Correct |
33 ms |
25132 KB |
Output is correct |
20 |
Correct |
81 ms |
25188 KB |
Output is correct |
21 |
Correct |
8 ms |
7372 KB |
Output is correct |
22 |
Correct |
32 ms |
22592 KB |
Output is correct |
23 |
Correct |
86 ms |
20736 KB |
Output is correct |
24 |
Correct |
126 ms |
24072 KB |
Output is correct |
25 |
Correct |
128 ms |
25172 KB |
Output is correct |
26 |
Correct |
95 ms |
25156 KB |
Output is correct |
27 |
Correct |
89 ms |
25028 KB |
Output is correct |
28 |
Correct |
96 ms |
25284 KB |
Output is correct |
29 |
Correct |
332 ms |
25236 KB |
Output is correct |
30 |
Correct |
186 ms |
25204 KB |
Output is correct |
31 |
Correct |
230 ms |
25412 KB |
Output is correct |
32 |
Correct |
165 ms |
25216 KB |
Output is correct |
33 |
Correct |
495 ms |
25276 KB |
Output is correct |
34 |
Correct |
423 ms |
25272 KB |
Output is correct |
35 |
Correct |
130 ms |
19312 KB |
Output is correct |
36 |
Correct |
78 ms |
15212 KB |
Output is correct |
37 |
Correct |
219 ms |
24868 KB |
Output is correct |
38 |
Correct |
241 ms |
25488 KB |
Output is correct |
39 |
Correct |
197 ms |
25552 KB |
Output is correct |
40 |
Correct |
280 ms |
25760 KB |
Output is correct |
41 |
Correct |
228 ms |
25508 KB |
Output is correct |
42 |
Correct |
98 ms |
25412 KB |
Output is correct |
43 |
Correct |
95 ms |
25412 KB |
Output is correct |
44 |
Correct |
89 ms |
25568 KB |
Output is correct |
45 |
Correct |
389 ms |
25648 KB |
Output is correct |
46 |
Correct |
308 ms |
25668 KB |
Output is correct |
47 |
Execution timed out |
1098 ms |
140240 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |