//Challenge: Accepted
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <stack>
#include <set>
#include <map>
#include <assert.h>
#include <queue>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define ll long long
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
#define maxn 200005
#define mod 1000000007
using namespace std;
using namespace __gnu_pbds;
void debug() {
cout << endl;
}
template<class T, class ...U> void debug(T a, U ...b) {
cout << a << " ";debug(b...);
}
template<class T> void pary(T l, T r) {
while (l != r) {
cout << *l << " ";
l++;
}
cout << endl;
}
//template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//gp_hash_table<int, int> mp;
//find by order, order of key
#include "jumps.h"
vector<int> adj[maxn];
int to[18][maxn], anc[18][maxn], par[maxn], dep[maxn];
int lef[maxn], rig[maxn];
int ti = 0;
void dfs(int n, int par, int d) {
dep[n] = d;
lef[n] = n, rig[n] = n;
for (int v:adj[n]) {
if (v != par) {
dfs(v, n, d + 1);
lef[n] = min(lef[n], lef[v]);
rig[n] = max(rig[n], rig[v]);
}
}
}
void init(int N, vector<int> H) {
stack<int> stk;
for (int i = 0;i < N;i++) par[i] = N, lef[i] = N, rig[i] = N;
for (int i = 0;i < N;i++) {
vector<int> po;
while (stk.size() && H[i] >= H[stk.top()]) {
po.push_back(stk.top());
rig[stk.top()] = i;
stk.pop();
}
if (po.size()) {
adj[i].push_back(po.back());
par[po.back()] = i;
for (int j = 0;j < po.size() - 1;j++) {
adj[po[j + 1]].push_back(po[j]);
par[po[j]] = po[j + 1];
}
}
if (stk.size()) lef[i] = stk.top();
stk.push(i);
}
int prv = -1;
while (stk.size()) {
int cur = stk.top();
if (prv != -1) {
adj[cur].push_back(prv);
par[prv] = cur;
}
prv = cur;
stk.pop();
}
//pary(par, par + N);
for (int i = 0;i < N;i++) {
anc[0][i] = par[i];
if (par[i] == lef[i]) to[0][i] = rig[i];
else to[0][i] = lef[i];
}
dep[N] = -1, to[0][N] = anc[0][N] = N;
//pary(to[0], to[0] + N);
for (int i = 1;i < 18;i++) {
for (int j = 0;j <= N;j++) to[i][j] = to[i - 1][to[i - 1][j]], anc[i][j] = anc[i - 1][anc[i - 1][j]];
}
int root = max_element(H.begin(), H.end()) - H.begin();
dfs(root, root, 0);
}
int curl = 0, curr = 0;
int dis(int s, int t) {
int dif = dep[s] - dep[t];
if (dif < 0) return -1;
int ret = 0, cur = s;
for (int i = 17;i >= 0;i--) {
if (dep[to[i][cur]] >= dep[t] && to[i][cur] < curl) {
cur = to[i][cur];
ret += 1<<i;
}
}
if (curl <= to[0][cur] && to[0][cur] <= curr) return ret + 1;
for (int i = 17;i >= 0;i--) {
if (dep[anc[i][cur]] >= dep[t] && anc[i][cur] < curl) {
cur = anc[i][cur];
ret += 1<<i;
}
}
if (curl <= anc[0][cur] && anc[0][cur] <= curr) return ret + 1;
return -1;
}
int lca(int a, int b) {
if (dep[a] < dep[b]) swap(a, b);
int hdif = dep[a] - dep[b], cnt = 0;
while (hdif) {
if (hdif & 1) {
a = anc[cnt][a];
}
hdif>>=1, cnt++;
}
if (a == b) return a;
for (int i = 17;i >= 0;i--) {
if (anc[i][a] != anc[i][b]) {
a = anc[i][a], b = anc[i][b];
}
}
return anc[0][a];
}
int minimum_jumps(int A, int B, int C, int D) {
int ret = -1;
int m = lca(C, D);
int l = max(lef[m], A), r = min(rig[m], B);
if (r < l) return -1;
curl = C, curr = D;
return dis(lca(l, r), m);
}
/*
7 4
3 2 1 6 4 5 7
4 5 6 6
0 3 6 6
0 1 2 2
1 1 3 3
*/
Compilation message
jumps.cpp: In function 'void init(int, std::vector<int>)':
jumps.cpp:71:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int j = 0;j < po.size() - 1;j++) {
| ~~^~~~~~~~~~~~~~~
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:142:6: warning: unused variable 'ret' [-Wunused-variable]
142 | int ret = -1;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5192 KB |
Output is correct |
2 |
Correct |
3 ms |
5192 KB |
Output is correct |
3 |
Correct |
185 ms |
48628 KB |
Output is correct |
4 |
Correct |
991 ms |
59712 KB |
Output is correct |
5 |
Correct |
1002 ms |
32664 KB |
Output is correct |
6 |
Correct |
1287 ms |
59784 KB |
Output is correct |
7 |
Correct |
985 ms |
42560 KB |
Output is correct |
8 |
Correct |
1154 ms |
59712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5192 KB |
Output is correct |
2 |
Correct |
3 ms |
5192 KB |
Output is correct |
3 |
Correct |
3 ms |
5192 KB |
Output is correct |
4 |
Correct |
3 ms |
5192 KB |
Output is correct |
5 |
Correct |
5 ms |
5192 KB |
Output is correct |
6 |
Incorrect |
5 ms |
5192 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5192 KB |
Output is correct |
2 |
Correct |
3 ms |
5192 KB |
Output is correct |
3 |
Correct |
3 ms |
5192 KB |
Output is correct |
4 |
Correct |
3 ms |
5192 KB |
Output is correct |
5 |
Correct |
5 ms |
5192 KB |
Output is correct |
6 |
Incorrect |
5 ms |
5192 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5192 KB |
Output is correct |
2 |
Correct |
3 ms |
5192 KB |
Output is correct |
3 |
Correct |
3 ms |
5192 KB |
Output is correct |
4 |
Correct |
3 ms |
5192 KB |
Output is correct |
5 |
Correct |
61 ms |
34920 KB |
Output is correct |
6 |
Correct |
89 ms |
41920 KB |
Output is correct |
7 |
Correct |
46 ms |
24000 KB |
Output is correct |
8 |
Correct |
86 ms |
41932 KB |
Output is correct |
9 |
Correct |
16 ms |
10696 KB |
Output is correct |
10 |
Correct |
76 ms |
41968 KB |
Output is correct |
11 |
Correct |
71 ms |
59764 KB |
Output is correct |
12 |
Correct |
74 ms |
55404 KB |
Output is correct |
13 |
Correct |
73 ms |
55172 KB |
Output is correct |
14 |
Correct |
74 ms |
42176 KB |
Output is correct |
15 |
Incorrect |
79 ms |
59796 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5192 KB |
Output is correct |
2 |
Correct |
3 ms |
5192 KB |
Output is correct |
3 |
Correct |
3 ms |
5192 KB |
Output is correct |
4 |
Correct |
237 ms |
22080 KB |
Output is correct |
5 |
Correct |
795 ms |
42048 KB |
Output is correct |
6 |
Correct |
630 ms |
11208 KB |
Output is correct |
7 |
Correct |
906 ms |
41920 KB |
Output is correct |
8 |
Correct |
549 ms |
17940 KB |
Output is correct |
9 |
Correct |
794 ms |
41920 KB |
Output is correct |
10 |
Correct |
1062 ms |
59712 KB |
Output is correct |
11 |
Correct |
1036 ms |
58432 KB |
Output is correct |
12 |
Correct |
1095 ms |
57536 KB |
Output is correct |
13 |
Correct |
907 ms |
42192 KB |
Output is correct |
14 |
Correct |
1035 ms |
59796 KB |
Output is correct |
15 |
Correct |
846 ms |
59792 KB |
Output is correct |
16 |
Correct |
922 ms |
59892 KB |
Output is correct |
17 |
Correct |
3 ms |
5192 KB |
Output is correct |
18 |
Correct |
3 ms |
5192 KB |
Output is correct |
19 |
Correct |
4 ms |
5192 KB |
Output is correct |
20 |
Correct |
5 ms |
5192 KB |
Output is correct |
21 |
Correct |
5 ms |
5192 KB |
Output is correct |
22 |
Correct |
5 ms |
5192 KB |
Output is correct |
23 |
Correct |
5 ms |
5192 KB |
Output is correct |
24 |
Correct |
4 ms |
5192 KB |
Output is correct |
25 |
Correct |
3 ms |
5192 KB |
Output is correct |
26 |
Correct |
4 ms |
5320 KB |
Output is correct |
27 |
Correct |
26 ms |
5448 KB |
Output is correct |
28 |
Correct |
21 ms |
5704 KB |
Output is correct |
29 |
Correct |
21 ms |
5576 KB |
Output is correct |
30 |
Correct |
20 ms |
5704 KB |
Output is correct |
31 |
Correct |
21 ms |
5704 KB |
Output is correct |
32 |
Correct |
3 ms |
5192 KB |
Output is correct |
33 |
Correct |
45 ms |
26556 KB |
Output is correct |
34 |
Correct |
76 ms |
41976 KB |
Output is correct |
35 |
Correct |
76 ms |
57332 KB |
Output is correct |
36 |
Correct |
90 ms |
42192 KB |
Output is correct |
37 |
Correct |
76 ms |
59780 KB |
Output is correct |
38 |
Correct |
70 ms |
59784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5192 KB |
Output is correct |
2 |
Correct |
3 ms |
5192 KB |
Output is correct |
3 |
Correct |
3 ms |
5192 KB |
Output is correct |
4 |
Correct |
237 ms |
22080 KB |
Output is correct |
5 |
Correct |
795 ms |
42048 KB |
Output is correct |
6 |
Correct |
630 ms |
11208 KB |
Output is correct |
7 |
Correct |
906 ms |
41920 KB |
Output is correct |
8 |
Correct |
549 ms |
17940 KB |
Output is correct |
9 |
Correct |
794 ms |
41920 KB |
Output is correct |
10 |
Correct |
1062 ms |
59712 KB |
Output is correct |
11 |
Correct |
1036 ms |
58432 KB |
Output is correct |
12 |
Correct |
1095 ms |
57536 KB |
Output is correct |
13 |
Correct |
907 ms |
42192 KB |
Output is correct |
14 |
Correct |
1035 ms |
59796 KB |
Output is correct |
15 |
Correct |
846 ms |
59792 KB |
Output is correct |
16 |
Correct |
922 ms |
59892 KB |
Output is correct |
17 |
Correct |
3 ms |
5192 KB |
Output is correct |
18 |
Correct |
3 ms |
5192 KB |
Output is correct |
19 |
Correct |
4 ms |
5192 KB |
Output is correct |
20 |
Correct |
5 ms |
5192 KB |
Output is correct |
21 |
Correct |
5 ms |
5192 KB |
Output is correct |
22 |
Correct |
5 ms |
5192 KB |
Output is correct |
23 |
Correct |
5 ms |
5192 KB |
Output is correct |
24 |
Correct |
4 ms |
5192 KB |
Output is correct |
25 |
Correct |
3 ms |
5192 KB |
Output is correct |
26 |
Correct |
4 ms |
5320 KB |
Output is correct |
27 |
Correct |
26 ms |
5448 KB |
Output is correct |
28 |
Correct |
21 ms |
5704 KB |
Output is correct |
29 |
Correct |
21 ms |
5576 KB |
Output is correct |
30 |
Correct |
20 ms |
5704 KB |
Output is correct |
31 |
Correct |
21 ms |
5704 KB |
Output is correct |
32 |
Correct |
3 ms |
5192 KB |
Output is correct |
33 |
Correct |
45 ms |
26556 KB |
Output is correct |
34 |
Correct |
76 ms |
41976 KB |
Output is correct |
35 |
Correct |
76 ms |
57332 KB |
Output is correct |
36 |
Correct |
90 ms |
42192 KB |
Output is correct |
37 |
Correct |
76 ms |
59780 KB |
Output is correct |
38 |
Correct |
70 ms |
59784 KB |
Output is correct |
39 |
Correct |
3 ms |
5192 KB |
Output is correct |
40 |
Correct |
3 ms |
5192 KB |
Output is correct |
41 |
Correct |
3 ms |
5192 KB |
Output is correct |
42 |
Correct |
238 ms |
21984 KB |
Output is correct |
43 |
Correct |
773 ms |
42128 KB |
Output is correct |
44 |
Correct |
494 ms |
11292 KB |
Output is correct |
45 |
Correct |
949 ms |
41920 KB |
Output is correct |
46 |
Correct |
580 ms |
17856 KB |
Output is correct |
47 |
Correct |
918 ms |
41976 KB |
Output is correct |
48 |
Correct |
859 ms |
59840 KB |
Output is correct |
49 |
Correct |
945 ms |
58432 KB |
Output is correct |
50 |
Correct |
1048 ms |
57592 KB |
Output is correct |
51 |
Correct |
967 ms |
42176 KB |
Output is correct |
52 |
Correct |
1067 ms |
59860 KB |
Output is correct |
53 |
Correct |
920 ms |
59784 KB |
Output is correct |
54 |
Correct |
956 ms |
59764 KB |
Output is correct |
55 |
Correct |
3 ms |
5192 KB |
Output is correct |
56 |
Correct |
140 ms |
41932 KB |
Output is correct |
57 |
Correct |
927 ms |
41920 KB |
Output is correct |
58 |
Correct |
463 ms |
11592 KB |
Output is correct |
59 |
Correct |
819 ms |
41980 KB |
Output is correct |
60 |
Correct |
378 ms |
18240 KB |
Output is correct |
61 |
Correct |
696 ms |
41984 KB |
Output is correct |
62 |
Correct |
1159 ms |
59764 KB |
Output is correct |
63 |
Correct |
1061 ms |
55812 KB |
Output is correct |
64 |
Correct |
1044 ms |
53952 KB |
Output is correct |
65 |
Correct |
928 ms |
42176 KB |
Output is correct |
66 |
Correct |
1104 ms |
59768 KB |
Output is correct |
67 |
Correct |
883 ms |
59720 KB |
Output is correct |
68 |
Correct |
954 ms |
59788 KB |
Output is correct |
69 |
Correct |
3 ms |
5192 KB |
Output is correct |
70 |
Correct |
3 ms |
5192 KB |
Output is correct |
71 |
Correct |
6 ms |
5192 KB |
Output is correct |
72 |
Correct |
7 ms |
5192 KB |
Output is correct |
73 |
Correct |
5 ms |
5192 KB |
Output is correct |
74 |
Correct |
6 ms |
5192 KB |
Output is correct |
75 |
Correct |
5 ms |
5192 KB |
Output is correct |
76 |
Correct |
4 ms |
5192 KB |
Output is correct |
77 |
Correct |
3 ms |
5192 KB |
Output is correct |
78 |
Correct |
4 ms |
5192 KB |
Output is correct |
79 |
Correct |
5 ms |
5192 KB |
Output is correct |
80 |
Correct |
5 ms |
5192 KB |
Output is correct |
81 |
Correct |
4 ms |
5192 KB |
Output is correct |
82 |
Correct |
4 ms |
5192 KB |
Output is correct |
83 |
Correct |
4 ms |
5192 KB |
Output is correct |
84 |
Correct |
3 ms |
5192 KB |
Output is correct |
85 |
Correct |
7 ms |
5192 KB |
Output is correct |
86 |
Correct |
27 ms |
5448 KB |
Output is correct |
87 |
Correct |
24 ms |
5576 KB |
Output is correct |
88 |
Correct |
21 ms |
5448 KB |
Output is correct |
89 |
Correct |
18 ms |
5704 KB |
Output is correct |
90 |
Correct |
22 ms |
5704 KB |
Output is correct |
91 |
Correct |
3 ms |
5192 KB |
Output is correct |
92 |
Correct |
4 ms |
5320 KB |
Output is correct |
93 |
Correct |
25 ms |
5576 KB |
Output is correct |
94 |
Correct |
21 ms |
5704 KB |
Output is correct |
95 |
Correct |
20 ms |
5448 KB |
Output is correct |
96 |
Correct |
20 ms |
5704 KB |
Output is correct |
97 |
Correct |
23 ms |
5704 KB |
Output is correct |
98 |
Correct |
3 ms |
5128 KB |
Output is correct |
99 |
Correct |
76 ms |
42100 KB |
Output is correct |
100 |
Correct |
88 ms |
41924 KB |
Output is correct |
101 |
Correct |
71 ms |
56000 KB |
Output is correct |
102 |
Correct |
75 ms |
42176 KB |
Output is correct |
103 |
Correct |
101 ms |
59792 KB |
Output is correct |
104 |
Correct |
76 ms |
59812 KB |
Output is correct |
105 |
Correct |
3 ms |
5192 KB |
Output is correct |
106 |
Correct |
51 ms |
26484 KB |
Output is correct |
107 |
Correct |
89 ms |
41920 KB |
Output is correct |
108 |
Correct |
73 ms |
57332 KB |
Output is correct |
109 |
Correct |
73 ms |
42116 KB |
Output is correct |
110 |
Correct |
79 ms |
59688 KB |
Output is correct |
111 |
Correct |
69 ms |
59764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5192 KB |
Output is correct |
2 |
Correct |
3 ms |
5192 KB |
Output is correct |
3 |
Correct |
185 ms |
48628 KB |
Output is correct |
4 |
Correct |
991 ms |
59712 KB |
Output is correct |
5 |
Correct |
1002 ms |
32664 KB |
Output is correct |
6 |
Correct |
1287 ms |
59784 KB |
Output is correct |
7 |
Correct |
985 ms |
42560 KB |
Output is correct |
8 |
Correct |
1154 ms |
59712 KB |
Output is correct |
9 |
Correct |
3 ms |
5192 KB |
Output is correct |
10 |
Correct |
3 ms |
5192 KB |
Output is correct |
11 |
Correct |
3 ms |
5192 KB |
Output is correct |
12 |
Correct |
3 ms |
5192 KB |
Output is correct |
13 |
Correct |
5 ms |
5192 KB |
Output is correct |
14 |
Incorrect |
5 ms |
5192 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |