#include "jumps.h"
#include <vector>
#include <iostream>
#include <stack>
using namespace std;
const int maxN = 200'000;
const int logN = 18;
const int INF = 1e9;
int N;
vector<int> H(2+maxN);
vector<int> leftHigh(2+maxN);
vector<int> rightHigh(2+maxN);
vector< vector<int> > highEdge(2+maxN, vector<int>(logN));
vector< vector<int> > lowEdge(2+maxN, vector<int>(logN));
vector< vector<int> > rightEdge(2+maxN, vector<int>(logN));
struct segtree
{
int l;
int r;
int mn = -INF;
int mx = INF;
segtree* left = NULL;
segtree* right = NULL;
segtree()
{
;
}
segtree(int L, int R)
{
l = L;
r = R;
if(l == r)
{
mn = mx = H[l];
}
else
{
int m = (l+r)/2;
left = new segtree(l, m);
right = new segtree(m+1, r);
mn = min(left->mn, right->mn);
mx = max(left->mx, right->mx);
}
}
int rangemax(int L, int R)
{
if(R < l || r < L) return -INF;
else if(L <= l && r <= R) return mx;
else return max(left->rangemax(L, R), right->rangemax(L, R));
}
int rangemin(int L, int R)
{
if(R < l || r < L) return INF;
else if(L <= l && r <= R) return mn;
else return min(left->rangemin(L, R), right->rangemin(L, R));
}
};
segtree ST;
void init(int n, vector<int> h)
{
N = n;
H[0] = H[N+1] = INF;
for(int i = 1; i <= N; i++) H[i] = h[i-1];
stack<int> S;
leftHigh[0] = 0;
S.push(0);
for(int i = 1; i <= N; i++)
{
while(!S.empty() && H[S.top()] < H[i])
S.pop();
if(!S.empty()) leftHigh[i] = S.top();
S.push(i);
}
while(!S.empty()) S.pop();
leftHigh[N+1] = N+1;
rightHigh[N+1] = N+1;
S.push(N+1);
for(int i = N; i >= 1; i--)
{
while(!S.empty() && H[S.top()] < H[i])
S.pop();
if(!S.empty()) rightHigh[i] = S.top();
S.push(i);
}
leftHigh[0] = 0;
// for(int i = 0; i <= N+1; i++) cerr << leftHigh[i] << ' ';
// cerr << '\n';
// for(int i = 0; i <= N+1; i++) cerr << rightHigh[i] << ' ';
// cerr << '\n';
highEdge[0][0] = 0;
lowEdge[0][0] = 0;
rightEdge[0][0] = 0;
for(int i = 1; i <= N; i++)
{
highEdge[i][0] = (H[ leftHigh[i] ] > H[ rightHigh[i] ]) ? leftHigh[i] : rightHigh[i];
lowEdge[i][0] = (H[ leftHigh[i] ] < H[ rightHigh[i] ]) ? leftHigh[i] : rightHigh[i];
rightEdge[i][0] = rightHigh[i];
}
highEdge[N+1][0] = N+1;
lowEdge[N+1][0] = N+1;
rightEdge[N+1][0] = N+1;
for(int e = 1; e < logN; e++)
{
for(int i = 0; i <= N+1; i++)
{
highEdge[i][e] = highEdge[ highEdge[i][e-1] ][e-1];
lowEdge[i][e] = lowEdge[ lowEdge[i][e-1] ][e-1];
rightEdge[i][e] = rightEdge[ rightEdge[i][e-1] ][e-1];
}
}
ST = segtree(0, N+1);
}
int minimum_jumps(int A, int B, int C, int D)
{
A++;
B++;
C++;
D++;
int oldB = B;
int CDmax = ST.rangemax(C, D);
if(ST.rangemax(B, oldB) > CDmax) return -1;
while(A != B)
{
int m = (A+B)/2;
if(ST.rangemax(m, oldB) <= CDmax) B = m;
else A = m+1;
}
for(int bit = logN-1; bit >= 0; bit--)
{
int newA = A + (1 << bit);
if(newA > oldB) continue;
if(ST.rangemax(A, oldB) == ST.rangemax(newA, oldB)) A = newA;
}
// cerr << "A = B = " << A << '\n';
int o = A;
int res = 0;
while(1)
{
if(C <= o && o <= D) return res;
else if(o > D || H[o] > CDmax) return -1;
else if(highEdge[o][0] < C && H[highEdge[o][0]] <= CDmax && lowEdge[o][0] < C)
{
o = highEdge[o][0];
res++;
}
else
{
o = rightEdge[o][0];
res++;
}
}
// for(int bit = logN-1; bit >= 0; bit--)
// {
// if(highEdge[o][bit] > D) continue;
// if(H[ highEdge[o][bit] ] > CDmax) continue;
//
// }
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
63584 KB |
Output is correct |
2 |
Correct |
52 ms |
63700 KB |
Output is correct |
3 |
Execution timed out |
4078 ms |
80440 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
63704 KB |
Output is correct |
2 |
Correct |
51 ms |
63700 KB |
Output is correct |
3 |
Correct |
52 ms |
63680 KB |
Output is correct |
4 |
Correct |
54 ms |
63576 KB |
Output is correct |
5 |
Correct |
54 ms |
63680 KB |
Output is correct |
6 |
Correct |
56 ms |
63716 KB |
Output is correct |
7 |
Correct |
54 ms |
63680 KB |
Output is correct |
8 |
Correct |
56 ms |
63680 KB |
Output is correct |
9 |
Correct |
53 ms |
63680 KB |
Output is correct |
10 |
Correct |
55 ms |
63676 KB |
Output is correct |
11 |
Correct |
56 ms |
63676 KB |
Output is correct |
12 |
Correct |
56 ms |
63680 KB |
Output is correct |
13 |
Correct |
57 ms |
63600 KB |
Output is correct |
14 |
Correct |
55 ms |
63680 KB |
Output is correct |
15 |
Correct |
57 ms |
63684 KB |
Output is correct |
16 |
Correct |
57 ms |
63672 KB |
Output is correct |
17 |
Correct |
63 ms |
63720 KB |
Output is correct |
18 |
Correct |
53 ms |
63680 KB |
Output is correct |
19 |
Correct |
52 ms |
63704 KB |
Output is correct |
20 |
Correct |
56 ms |
63692 KB |
Output is correct |
21 |
Correct |
54 ms |
63596 KB |
Output is correct |
22 |
Correct |
54 ms |
63704 KB |
Output is correct |
23 |
Correct |
56 ms |
63720 KB |
Output is correct |
24 |
Correct |
56 ms |
63680 KB |
Output is correct |
25 |
Correct |
55 ms |
63616 KB |
Output is correct |
26 |
Correct |
53 ms |
63680 KB |
Output is correct |
27 |
Correct |
53 ms |
63696 KB |
Output is correct |
28 |
Correct |
55 ms |
63616 KB |
Output is correct |
29 |
Correct |
55 ms |
63692 KB |
Output is correct |
30 |
Correct |
55 ms |
63676 KB |
Output is correct |
31 |
Correct |
54 ms |
63676 KB |
Output is correct |
32 |
Correct |
62 ms |
63720 KB |
Output is correct |
33 |
Correct |
54 ms |
63808 KB |
Output is correct |
34 |
Correct |
53 ms |
63676 KB |
Output is correct |
35 |
Correct |
53 ms |
63724 KB |
Output is correct |
36 |
Correct |
52 ms |
63680 KB |
Output is correct |
37 |
Correct |
53 ms |
63592 KB |
Output is correct |
38 |
Correct |
52 ms |
63636 KB |
Output is correct |
39 |
Correct |
53 ms |
63716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
63704 KB |
Output is correct |
2 |
Correct |
51 ms |
63700 KB |
Output is correct |
3 |
Correct |
52 ms |
63680 KB |
Output is correct |
4 |
Correct |
54 ms |
63576 KB |
Output is correct |
5 |
Correct |
54 ms |
63680 KB |
Output is correct |
6 |
Correct |
56 ms |
63716 KB |
Output is correct |
7 |
Correct |
54 ms |
63680 KB |
Output is correct |
8 |
Correct |
56 ms |
63680 KB |
Output is correct |
9 |
Correct |
53 ms |
63680 KB |
Output is correct |
10 |
Correct |
55 ms |
63676 KB |
Output is correct |
11 |
Correct |
56 ms |
63676 KB |
Output is correct |
12 |
Correct |
56 ms |
63680 KB |
Output is correct |
13 |
Correct |
57 ms |
63600 KB |
Output is correct |
14 |
Correct |
55 ms |
63680 KB |
Output is correct |
15 |
Correct |
57 ms |
63684 KB |
Output is correct |
16 |
Correct |
57 ms |
63672 KB |
Output is correct |
17 |
Correct |
63 ms |
63720 KB |
Output is correct |
18 |
Correct |
53 ms |
63680 KB |
Output is correct |
19 |
Correct |
52 ms |
63704 KB |
Output is correct |
20 |
Correct |
56 ms |
63692 KB |
Output is correct |
21 |
Correct |
54 ms |
63596 KB |
Output is correct |
22 |
Correct |
54 ms |
63704 KB |
Output is correct |
23 |
Correct |
56 ms |
63720 KB |
Output is correct |
24 |
Correct |
56 ms |
63680 KB |
Output is correct |
25 |
Correct |
55 ms |
63616 KB |
Output is correct |
26 |
Correct |
53 ms |
63680 KB |
Output is correct |
27 |
Correct |
53 ms |
63696 KB |
Output is correct |
28 |
Correct |
55 ms |
63616 KB |
Output is correct |
29 |
Correct |
55 ms |
63692 KB |
Output is correct |
30 |
Correct |
55 ms |
63676 KB |
Output is correct |
31 |
Correct |
54 ms |
63676 KB |
Output is correct |
32 |
Correct |
62 ms |
63720 KB |
Output is correct |
33 |
Correct |
54 ms |
63808 KB |
Output is correct |
34 |
Correct |
53 ms |
63676 KB |
Output is correct |
35 |
Correct |
53 ms |
63724 KB |
Output is correct |
36 |
Correct |
52 ms |
63680 KB |
Output is correct |
37 |
Correct |
53 ms |
63592 KB |
Output is correct |
38 |
Correct |
52 ms |
63636 KB |
Output is correct |
39 |
Correct |
53 ms |
63716 KB |
Output is correct |
40 |
Correct |
53 ms |
63572 KB |
Output is correct |
41 |
Correct |
54 ms |
63680 KB |
Output is correct |
42 |
Correct |
53 ms |
63680 KB |
Output is correct |
43 |
Correct |
52 ms |
63680 KB |
Output is correct |
44 |
Correct |
53 ms |
63700 KB |
Output is correct |
45 |
Correct |
56 ms |
63680 KB |
Output is correct |
46 |
Correct |
57 ms |
63764 KB |
Output is correct |
47 |
Correct |
55 ms |
63664 KB |
Output is correct |
48 |
Correct |
54 ms |
63640 KB |
Output is correct |
49 |
Correct |
55 ms |
63692 KB |
Output is correct |
50 |
Correct |
55 ms |
63664 KB |
Output is correct |
51 |
Correct |
56 ms |
63668 KB |
Output is correct |
52 |
Correct |
55 ms |
63612 KB |
Output is correct |
53 |
Correct |
56 ms |
63736 KB |
Output is correct |
54 |
Correct |
55 ms |
63616 KB |
Output is correct |
55 |
Correct |
55 ms |
63700 KB |
Output is correct |
56 |
Correct |
55 ms |
63720 KB |
Output is correct |
57 |
Correct |
53 ms |
63716 KB |
Output is correct |
58 |
Correct |
66 ms |
63988 KB |
Output is correct |
59 |
Correct |
91 ms |
63896 KB |
Output is correct |
60 |
Correct |
60 ms |
63752 KB |
Output is correct |
61 |
Correct |
79 ms |
63920 KB |
Output is correct |
62 |
Correct |
64 ms |
63720 KB |
Output is correct |
63 |
Correct |
78 ms |
63896 KB |
Output is correct |
64 |
Correct |
83 ms |
63936 KB |
Output is correct |
65 |
Correct |
90 ms |
63856 KB |
Output is correct |
66 |
Correct |
88 ms |
63808 KB |
Output is correct |
67 |
Correct |
86 ms |
63808 KB |
Output is correct |
68 |
Correct |
90 ms |
63808 KB |
Output is correct |
69 |
Correct |
86 ms |
63808 KB |
Output is correct |
70 |
Correct |
67 ms |
63908 KB |
Output is correct |
71 |
Correct |
53 ms |
63700 KB |
Output is correct |
72 |
Correct |
53 ms |
63604 KB |
Output is correct |
73 |
Correct |
55 ms |
63680 KB |
Output is correct |
74 |
Correct |
56 ms |
63688 KB |
Output is correct |
75 |
Correct |
55 ms |
63756 KB |
Output is correct |
76 |
Correct |
57 ms |
63616 KB |
Output is correct |
77 |
Correct |
54 ms |
63628 KB |
Output is correct |
78 |
Correct |
51 ms |
63604 KB |
Output is correct |
79 |
Correct |
54 ms |
63696 KB |
Output is correct |
80 |
Correct |
54 ms |
63680 KB |
Output is correct |
81 |
Correct |
54 ms |
63692 KB |
Output is correct |
82 |
Correct |
55 ms |
63720 KB |
Output is correct |
83 |
Correct |
55 ms |
63628 KB |
Output is correct |
84 |
Correct |
57 ms |
63660 KB |
Output is correct |
85 |
Correct |
56 ms |
63680 KB |
Output is correct |
86 |
Correct |
53 ms |
63692 KB |
Output is correct |
87 |
Correct |
52 ms |
63680 KB |
Output is correct |
88 |
Correct |
54 ms |
63716 KB |
Output is correct |
89 |
Correct |
53 ms |
63680 KB |
Output is correct |
90 |
Correct |
53 ms |
63724 KB |
Output is correct |
91 |
Correct |
56 ms |
63680 KB |
Output is correct |
92 |
Correct |
53 ms |
63680 KB |
Output is correct |
93 |
Correct |
52 ms |
63684 KB |
Output is correct |
94 |
Correct |
56 ms |
63632 KB |
Output is correct |
95 |
Correct |
81 ms |
63804 KB |
Output is correct |
96 |
Correct |
84 ms |
63900 KB |
Output is correct |
97 |
Correct |
80 ms |
63836 KB |
Output is correct |
98 |
Correct |
89 ms |
63804 KB |
Output is correct |
99 |
Correct |
73 ms |
63904 KB |
Output is correct |
100 |
Correct |
52 ms |
63600 KB |
Output is correct |
101 |
Correct |
52 ms |
63680 KB |
Output is correct |
102 |
Correct |
68 ms |
63904 KB |
Output is correct |
103 |
Correct |
83 ms |
63984 KB |
Output is correct |
104 |
Correct |
76 ms |
63808 KB |
Output is correct |
105 |
Correct |
78 ms |
63900 KB |
Output is correct |
106 |
Correct |
71 ms |
63844 KB |
Output is correct |
107 |
Correct |
52 ms |
63676 KB |
Output is correct |
108 |
Correct |
52 ms |
63784 KB |
Output is correct |
109 |
Correct |
53 ms |
63896 KB |
Output is correct |
110 |
Correct |
60 ms |
63808 KB |
Output is correct |
111 |
Correct |
53 ms |
63784 KB |
Output is correct |
112 |
Correct |
53 ms |
63808 KB |
Output is correct |
113 |
Correct |
54 ms |
63880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
63680 KB |
Output is correct |
2 |
Correct |
53 ms |
63684 KB |
Output is correct |
3 |
Correct |
52 ms |
63700 KB |
Output is correct |
4 |
Correct |
53 ms |
63584 KB |
Output is correct |
5 |
Correct |
167 ms |
80188 KB |
Output is correct |
6 |
Correct |
188 ms |
83964 KB |
Output is correct |
7 |
Correct |
134 ms |
74180 KB |
Output is correct |
8 |
Correct |
191 ms |
84032 KB |
Output is correct |
9 |
Correct |
69 ms |
66752 KB |
Output is correct |
10 |
Correct |
195 ms |
83936 KB |
Output is correct |
11 |
Correct |
184 ms |
84776 KB |
Output is correct |
12 |
Correct |
181 ms |
84300 KB |
Output is correct |
13 |
Correct |
183 ms |
84644 KB |
Output is correct |
14 |
Correct |
189 ms |
83948 KB |
Output is correct |
15 |
Correct |
235 ms |
83980 KB |
Output is correct |
16 |
Correct |
191 ms |
84024 KB |
Output is correct |
17 |
Correct |
202 ms |
83968 KB |
Output is correct |
18 |
Correct |
52 ms |
63704 KB |
Output is correct |
19 |
Correct |
53 ms |
63680 KB |
Output is correct |
20 |
Correct |
52 ms |
63604 KB |
Output is correct |
21 |
Correct |
52 ms |
63816 KB |
Output is correct |
22 |
Correct |
53 ms |
63796 KB |
Output is correct |
23 |
Correct |
52 ms |
63668 KB |
Output is correct |
24 |
Correct |
52 ms |
63688 KB |
Output is correct |
25 |
Correct |
52 ms |
63644 KB |
Output is correct |
26 |
Correct |
62 ms |
63764 KB |
Output is correct |
27 |
Correct |
63 ms |
63884 KB |
Output is correct |
28 |
Correct |
57 ms |
63900 KB |
Output is correct |
29 |
Correct |
53 ms |
63880 KB |
Output is correct |
30 |
Correct |
55 ms |
63848 KB |
Output is correct |
31 |
Correct |
53 ms |
63880 KB |
Output is correct |
32 |
Correct |
52 ms |
63692 KB |
Output is correct |
33 |
Correct |
186 ms |
83964 KB |
Output is correct |
34 |
Correct |
186 ms |
83944 KB |
Output is correct |
35 |
Correct |
190 ms |
84672 KB |
Output is correct |
36 |
Correct |
186 ms |
84036 KB |
Output is correct |
37 |
Correct |
237 ms |
84088 KB |
Output is correct |
38 |
Correct |
175 ms |
84032 KB |
Output is correct |
39 |
Correct |
53 ms |
63680 KB |
Output is correct |
40 |
Correct |
130 ms |
75404 KB |
Output is correct |
41 |
Correct |
186 ms |
84064 KB |
Output is correct |
42 |
Correct |
181 ms |
84648 KB |
Output is correct |
43 |
Correct |
190 ms |
83932 KB |
Output is correct |
44 |
Correct |
233 ms |
84056 KB |
Output is correct |
45 |
Correct |
173 ms |
84052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
63612 KB |
Output is correct |
2 |
Correct |
52 ms |
63680 KB |
Output is correct |
3 |
Correct |
53 ms |
63680 KB |
Output is correct |
4 |
Correct |
431 ms |
72992 KB |
Output is correct |
5 |
Correct |
1412 ms |
84032 KB |
Output is correct |
6 |
Correct |
749 ms |
67024 KB |
Output is correct |
7 |
Correct |
1551 ms |
83932 KB |
Output is correct |
8 |
Correct |
930 ms |
70636 KB |
Output is correct |
9 |
Correct |
1325 ms |
83948 KB |
Output is correct |
10 |
Execution timed out |
4089 ms |
84776 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
63612 KB |
Output is correct |
2 |
Correct |
52 ms |
63680 KB |
Output is correct |
3 |
Correct |
53 ms |
63680 KB |
Output is correct |
4 |
Correct |
431 ms |
72992 KB |
Output is correct |
5 |
Correct |
1412 ms |
84032 KB |
Output is correct |
6 |
Correct |
749 ms |
67024 KB |
Output is correct |
7 |
Correct |
1551 ms |
83932 KB |
Output is correct |
8 |
Correct |
930 ms |
70636 KB |
Output is correct |
9 |
Correct |
1325 ms |
83948 KB |
Output is correct |
10 |
Execution timed out |
4089 ms |
84776 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
63584 KB |
Output is correct |
2 |
Correct |
52 ms |
63700 KB |
Output is correct |
3 |
Execution timed out |
4078 ms |
80440 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |