// Restart
// += O(logn) ; + = O(n)
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define fst first
#define snd second
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define sz size()
#define FORN(i,j,n) for(int i=j; i<(int)n;i++)
#define FOR(i,n) FORN(i,0,n)
#define FORIT(i,x) for( auto i = x.begin() ; i != x.end() ; i++ )
#define MOD 998244353LL
#define MOD1 1000000007LL
#define LIM 262150
#define ones(x) __builtin_popcount(x)
#define trace(x) cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y,z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define INF 1000000000
#define in(x) cin>>x;
#define out(x) cout<<x;
#define MAXIMO 1LL<<60
#define loga2(x) __builtin_ctzll(x)
//SOLO PARA POTENCIAS DE 2
using namespace std;
using namespace __gnu_pbds;
typedef long long ll ;
typedef unsigned long long ull ;
typedef vector <int> vi;
typedef pair <int,int> ii;
typedef pair <int,ii> iii;
typedef vector <string> vs;
typedef vector <ii> vii;
typedef vector <iii> viii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int n,m;
set<int> walks[30005];
vii G[30005];
int D[30005];
void Dijkstra(int start) {
priority_queue<ii, vii, greater<ii> > pq;
D[start] = 0;
pq.push({0, start});
while(!pq.empty()) {
int v = pq.top().snd; int d = pq.top().fst;
pq.pop();
//trace2(v,d);
if(d <= D[v]){
for(auto uw : G[v]) {
int u = uw.fst;
int w = uw.snd;
if(D[u] > w + d) {
D[u] = w + d;
pq.push({D[u], u});
}
}
}
}
}
void go(){
cin>>n>>m;
int pos , jump;
int t0, t1;
cin>> t0 >> jump; walks[t0].insert(jump);//0
cin>> t1 >> jump; //1
for(int i=2; i<m; i++){
cin>> pos >> jump;
walks[pos].insert(jump);
}
FOR(i,n) D[i] = INF;
FOR(i,n){
for(auto j : walks[i]){
int cont = 1;
while(1) {
int tower = i + cont * j;
if( tower >= n )break;
G[i].pb({tower, cont});
if(walks[tower].count(j))break;
cont += 1;
}
}
for(auto j : walks[i]){
int cont = 1;
while(1) {
int tower = i - cont * j;
if( tower < 0 )break;
G[i].pb({tower, cont});
if(walks[tower].count(j))break;
cont += 1;
}
}
}
Dijkstra(t0);
cout << (D[t1] == INF ? -1 : D[t1]) << "\n";
}
int main() {
go();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2380 KB |
Output is correct |
2 |
Correct |
2 ms |
2380 KB |
Output is correct |
3 |
Correct |
2 ms |
2380 KB |
Output is correct |
4 |
Correct |
2 ms |
2380 KB |
Output is correct |
5 |
Correct |
2 ms |
2380 KB |
Output is correct |
6 |
Correct |
2 ms |
2380 KB |
Output is correct |
7 |
Correct |
2 ms |
2380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2380 KB |
Output is correct |
2 |
Correct |
2 ms |
2380 KB |
Output is correct |
3 |
Correct |
2 ms |
2380 KB |
Output is correct |
4 |
Correct |
2 ms |
2380 KB |
Output is correct |
5 |
Correct |
2 ms |
2380 KB |
Output is correct |
6 |
Correct |
3 ms |
2380 KB |
Output is correct |
7 |
Correct |
2 ms |
2380 KB |
Output is correct |
8 |
Correct |
2 ms |
2380 KB |
Output is correct |
9 |
Correct |
2 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2508 KB |
Output is correct |
12 |
Correct |
4 ms |
2380 KB |
Output is correct |
13 |
Correct |
3 ms |
2380 KB |
Output is correct |
14 |
Correct |
3 ms |
2508 KB |
Output is correct |
15 |
Correct |
3 ms |
2508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2380 KB |
Output is correct |
2 |
Correct |
2 ms |
2380 KB |
Output is correct |
3 |
Correct |
2 ms |
2380 KB |
Output is correct |
4 |
Correct |
2 ms |
2328 KB |
Output is correct |
5 |
Correct |
2 ms |
2380 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
2380 KB |
Output is correct |
8 |
Correct |
2 ms |
2380 KB |
Output is correct |
9 |
Correct |
2 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2508 KB |
Output is correct |
12 |
Correct |
3 ms |
2380 KB |
Output is correct |
13 |
Correct |
3 ms |
2380 KB |
Output is correct |
14 |
Correct |
4 ms |
2508 KB |
Output is correct |
15 |
Correct |
3 ms |
2508 KB |
Output is correct |
16 |
Correct |
2 ms |
2380 KB |
Output is correct |
17 |
Correct |
4 ms |
2636 KB |
Output is correct |
18 |
Correct |
3 ms |
2508 KB |
Output is correct |
19 |
Correct |
3 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
3 ms |
2384 KB |
Output is correct |
22 |
Correct |
3 ms |
2508 KB |
Output is correct |
23 |
Correct |
3 ms |
2508 KB |
Output is correct |
24 |
Correct |
4 ms |
2636 KB |
Output is correct |
25 |
Correct |
4 ms |
2508 KB |
Output is correct |
26 |
Correct |
3 ms |
2508 KB |
Output is correct |
27 |
Correct |
3 ms |
2508 KB |
Output is correct |
28 |
Correct |
4 ms |
2636 KB |
Output is correct |
29 |
Correct |
5 ms |
3148 KB |
Output is correct |
30 |
Correct |
4 ms |
2636 KB |
Output is correct |
31 |
Correct |
4 ms |
2764 KB |
Output is correct |
32 |
Correct |
3 ms |
2636 KB |
Output is correct |
33 |
Correct |
10 ms |
4048 KB |
Output is correct |
34 |
Correct |
9 ms |
3916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2380 KB |
Output is correct |
2 |
Correct |
2 ms |
2380 KB |
Output is correct |
3 |
Correct |
2 ms |
2380 KB |
Output is correct |
4 |
Correct |
2 ms |
2380 KB |
Output is correct |
5 |
Correct |
2 ms |
2380 KB |
Output is correct |
6 |
Correct |
2 ms |
2380 KB |
Output is correct |
7 |
Correct |
2 ms |
2380 KB |
Output is correct |
8 |
Correct |
2 ms |
2380 KB |
Output is correct |
9 |
Correct |
2 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2508 KB |
Output is correct |
12 |
Correct |
3 ms |
2416 KB |
Output is correct |
13 |
Correct |
3 ms |
2380 KB |
Output is correct |
14 |
Correct |
3 ms |
2508 KB |
Output is correct |
15 |
Correct |
3 ms |
2508 KB |
Output is correct |
16 |
Correct |
3 ms |
2380 KB |
Output is correct |
17 |
Correct |
5 ms |
2636 KB |
Output is correct |
18 |
Correct |
3 ms |
2508 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
3 ms |
2380 KB |
Output is correct |
22 |
Correct |
2 ms |
2508 KB |
Output is correct |
23 |
Correct |
3 ms |
2508 KB |
Output is correct |
24 |
Correct |
4 ms |
2636 KB |
Output is correct |
25 |
Correct |
4 ms |
2508 KB |
Output is correct |
26 |
Correct |
3 ms |
2508 KB |
Output is correct |
27 |
Correct |
3 ms |
2508 KB |
Output is correct |
28 |
Correct |
3 ms |
2636 KB |
Output is correct |
29 |
Correct |
5 ms |
3148 KB |
Output is correct |
30 |
Correct |
3 ms |
2636 KB |
Output is correct |
31 |
Correct |
4 ms |
2764 KB |
Output is correct |
32 |
Correct |
3 ms |
2636 KB |
Output is correct |
33 |
Correct |
9 ms |
3916 KB |
Output is correct |
34 |
Correct |
9 ms |
3916 KB |
Output is correct |
35 |
Correct |
27 ms |
4588 KB |
Output is correct |
36 |
Correct |
6 ms |
2764 KB |
Output is correct |
37 |
Correct |
27 ms |
5380 KB |
Output is correct |
38 |
Correct |
34 ms |
5488 KB |
Output is correct |
39 |
Correct |
34 ms |
5572 KB |
Output is correct |
40 |
Correct |
34 ms |
5492 KB |
Output is correct |
41 |
Correct |
34 ms |
5572 KB |
Output is correct |
42 |
Correct |
16 ms |
2508 KB |
Output is correct |
43 |
Correct |
15 ms |
2508 KB |
Output is correct |
44 |
Correct |
15 ms |
2508 KB |
Output is correct |
45 |
Correct |
35 ms |
8256 KB |
Output is correct |
46 |
Correct |
36 ms |
8176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2380 KB |
Output is correct |
2 |
Correct |
2 ms |
2380 KB |
Output is correct |
3 |
Correct |
2 ms |
2380 KB |
Output is correct |
4 |
Correct |
2 ms |
2380 KB |
Output is correct |
5 |
Correct |
2 ms |
2380 KB |
Output is correct |
6 |
Correct |
2 ms |
2380 KB |
Output is correct |
7 |
Correct |
2 ms |
2380 KB |
Output is correct |
8 |
Correct |
2 ms |
2380 KB |
Output is correct |
9 |
Correct |
2 ms |
2380 KB |
Output is correct |
10 |
Correct |
2 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2508 KB |
Output is correct |
12 |
Correct |
3 ms |
2380 KB |
Output is correct |
13 |
Correct |
3 ms |
2392 KB |
Output is correct |
14 |
Correct |
3 ms |
2508 KB |
Output is correct |
15 |
Correct |
3 ms |
2508 KB |
Output is correct |
16 |
Correct |
3 ms |
2380 KB |
Output is correct |
17 |
Correct |
4 ms |
2636 KB |
Output is correct |
18 |
Correct |
3 ms |
2508 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
3 ms |
2380 KB |
Output is correct |
22 |
Correct |
2 ms |
2508 KB |
Output is correct |
23 |
Correct |
4 ms |
2508 KB |
Output is correct |
24 |
Correct |
4 ms |
2636 KB |
Output is correct |
25 |
Correct |
4 ms |
2508 KB |
Output is correct |
26 |
Correct |
3 ms |
2508 KB |
Output is correct |
27 |
Correct |
3 ms |
2508 KB |
Output is correct |
28 |
Correct |
5 ms |
2636 KB |
Output is correct |
29 |
Correct |
6 ms |
3256 KB |
Output is correct |
30 |
Correct |
3 ms |
2636 KB |
Output is correct |
31 |
Correct |
4 ms |
2764 KB |
Output is correct |
32 |
Correct |
3 ms |
2636 KB |
Output is correct |
33 |
Correct |
8 ms |
3916 KB |
Output is correct |
34 |
Correct |
8 ms |
3916 KB |
Output is correct |
35 |
Correct |
28 ms |
4580 KB |
Output is correct |
36 |
Correct |
6 ms |
2764 KB |
Output is correct |
37 |
Correct |
27 ms |
5372 KB |
Output is correct |
38 |
Correct |
34 ms |
5520 KB |
Output is correct |
39 |
Correct |
35 ms |
5492 KB |
Output is correct |
40 |
Correct |
35 ms |
5524 KB |
Output is correct |
41 |
Correct |
34 ms |
5564 KB |
Output is correct |
42 |
Correct |
15 ms |
2508 KB |
Output is correct |
43 |
Correct |
15 ms |
2508 KB |
Output is correct |
44 |
Correct |
16 ms |
2480 KB |
Output is correct |
45 |
Correct |
35 ms |
8248 KB |
Output is correct |
46 |
Correct |
35 ms |
8176 KB |
Output is correct |
47 |
Correct |
58 ms |
12644 KB |
Output is correct |
48 |
Correct |
26 ms |
5824 KB |
Output is correct |
49 |
Correct |
23 ms |
4980 KB |
Output is correct |
50 |
Correct |
17 ms |
4700 KB |
Output is correct |
51 |
Correct |
54 ms |
7976 KB |
Output is correct |
52 |
Correct |
55 ms |
8224 KB |
Output is correct |
53 |
Correct |
28 ms |
4672 KB |
Output is correct |
54 |
Correct |
4 ms |
2892 KB |
Output is correct |
55 |
Correct |
5 ms |
3144 KB |
Output is correct |
56 |
Correct |
24 ms |
4812 KB |
Output is correct |
57 |
Correct |
44 ms |
11600 KB |
Output is correct |
58 |
Correct |
16 ms |
3272 KB |
Output is correct |
59 |
Correct |
21 ms |
3784 KB |
Output is correct |
60 |
Correct |
21 ms |
4300 KB |
Output is correct |
61 |
Correct |
22 ms |
3896 KB |
Output is correct |
62 |
Correct |
36 ms |
8744 KB |
Output is correct |
63 |
Correct |
92 ms |
25656 KB |
Output is correct |
64 |
Correct |
103 ms |
29208 KB |
Output is correct |
65 |
Correct |
135 ms |
38628 KB |
Output is correct |
66 |
Correct |
252 ms |
65332 KB |
Output is correct |
67 |
Correct |
240 ms |
65296 KB |
Output is correct |