// 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";
}
void timer(){
clock_t begin = clock();
go();
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
cout<<time_spent<<"\n";
}
int main() {
//fastio;
go();
//timer();
return 0;
}
# |
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 |
2384 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 |
2412 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 |
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 |
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 |
2420 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 |
2420 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 |
2508 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
3 ms |
2380 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 |
2552 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 |
3 ms |
2636 KB |
Output is correct |
31 |
Correct |
4 ms |
2892 KB |
Output is correct |
32 |
Correct |
3 ms |
2764 KB |
Output is correct |
33 |
Correct |
8 ms |
3916 KB |
Output is correct |
34 |
Correct |
8 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 |
2380 KB |
Output is correct |
13 |
Correct |
3 ms |
2380 KB |
Output is correct |
14 |
Correct |
3 ms |
2548 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 |
2508 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
3 ms |
2380 KB |
Output is correct |
22 |
Correct |
3 ms |
2420 KB |
Output is correct |
23 |
Correct |
4 ms |
2592 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 |
2552 KB |
Output is correct |
27 |
Correct |
3 ms |
2508 KB |
Output is correct |
28 |
Correct |
4 ms |
2684 KB |
Output is correct |
29 |
Correct |
5 ms |
3152 KB |
Output is correct |
30 |
Correct |
3 ms |
2636 KB |
Output is correct |
31 |
Correct |
4 ms |
2892 KB |
Output is correct |
32 |
Correct |
3 ms |
2636 KB |
Output is correct |
33 |
Correct |
8 ms |
3920 KB |
Output is correct |
34 |
Correct |
8 ms |
3964 KB |
Output is correct |
35 |
Correct |
27 ms |
4792 KB |
Output is correct |
36 |
Correct |
6 ms |
2764 KB |
Output is correct |
37 |
Correct |
27 ms |
5500 KB |
Output is correct |
38 |
Correct |
34 ms |
5828 KB |
Output is correct |
39 |
Correct |
34 ms |
5764 KB |
Output is correct |
40 |
Correct |
34 ms |
5776 KB |
Output is correct |
41 |
Correct |
34 ms |
5748 KB |
Output is correct |
42 |
Correct |
15 ms |
2680 KB |
Output is correct |
43 |
Correct |
16 ms |
2636 KB |
Output is correct |
44 |
Correct |
16 ms |
2696 KB |
Output is correct |
45 |
Correct |
36 ms |
8388 KB |
Output is correct |
46 |
Correct |
36 ms |
8440 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 |
3 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 |
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 |
4 ms |
2636 KB |
Output is correct |
18 |
Correct |
3 ms |
2508 KB |
Output is correct |
19 |
Correct |
3 ms |
2508 KB |
Output is correct |
20 |
Correct |
3 ms |
2508 KB |
Output is correct |
21 |
Correct |
3 ms |
2380 KB |
Output is correct |
22 |
Correct |
3 ms |
2612 KB |
Output is correct |
23 |
Correct |
3 ms |
2508 KB |
Output is correct |
24 |
Correct |
4 ms |
2656 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 |
2512 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 |
2796 KB |
Output is correct |
32 |
Correct |
3 ms |
2636 KB |
Output is correct |
33 |
Correct |
8 ms |
3964 KB |
Output is correct |
34 |
Correct |
8 ms |
3916 KB |
Output is correct |
35 |
Correct |
28 ms |
4816 KB |
Output is correct |
36 |
Correct |
6 ms |
2764 KB |
Output is correct |
37 |
Correct |
26 ms |
5468 KB |
Output is correct |
38 |
Correct |
34 ms |
5824 KB |
Output is correct |
39 |
Correct |
34 ms |
5828 KB |
Output is correct |
40 |
Correct |
34 ms |
5832 KB |
Output is correct |
41 |
Correct |
34 ms |
5828 KB |
Output is correct |
42 |
Correct |
16 ms |
2628 KB |
Output is correct |
43 |
Correct |
16 ms |
2632 KB |
Output is correct |
44 |
Correct |
16 ms |
2764 KB |
Output is correct |
45 |
Correct |
36 ms |
8428 KB |
Output is correct |
46 |
Correct |
44 ms |
8432 KB |
Output is correct |
47 |
Correct |
59 ms |
12864 KB |
Output is correct |
48 |
Correct |
27 ms |
6168 KB |
Output is correct |
49 |
Correct |
23 ms |
5144 KB |
Output is correct |
50 |
Correct |
17 ms |
4876 KB |
Output is correct |
51 |
Correct |
53 ms |
8212 KB |
Output is correct |
52 |
Correct |
55 ms |
8644 KB |
Output is correct |
53 |
Correct |
33 ms |
5056 KB |
Output is correct |
54 |
Correct |
4 ms |
2892 KB |
Output is correct |
55 |
Correct |
5 ms |
3176 KB |
Output is correct |
56 |
Correct |
23 ms |
5068 KB |
Output is correct |
57 |
Correct |
39 ms |
11600 KB |
Output is correct |
58 |
Correct |
16 ms |
3420 KB |
Output is correct |
59 |
Correct |
21 ms |
4040 KB |
Output is correct |
60 |
Correct |
21 ms |
4552 KB |
Output is correct |
61 |
Correct |
28 ms |
4084 KB |
Output is correct |
62 |
Correct |
38 ms |
9064 KB |
Output is correct |
63 |
Correct |
96 ms |
26132 KB |
Output is correct |
64 |
Correct |
103 ms |
29444 KB |
Output is correct |
65 |
Correct |
140 ms |
38828 KB |
Output is correct |
66 |
Correct |
234 ms |
65588 KB |
Output is correct |
67 |
Correct |
235 ms |
65616 KB |
Output is correct |