#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> bool cmin(T &i, T j) { return i > j ? i=j,true:false; }
template<class T> bool cmax(T &i, T j) { return i < j ? i=j,true:false; }
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
int rng() {
return abs((int)mrand());
}
// Current challenge: finish CSES problemset!!
constexpr int nax = 200200, lg = 20;
int h[nax];
int par[nax][2];
int partree[nax];
int sparse[lg][nax];
void init(int n, vector<int> H) {
for (int i=0;i<n;i++) {
h[i] = H[i]-1;
par[i][0] = par[i][1] = n;
}
for (int i=0;i<n;i++) {
sparse[0][i] = h[i];
}
for (int pw=1;pw<lg;pw++) {
for (int i=0;i<n;i++) {
sparse[pw][i] = max(sparse[pw-1][i],sparse[pw-1][min(n-1,i+(1<<(pw-1)))]);
}
}
h[n] = n;
vector<int> stk;
for (int i=0;i<n;i++) {
while(!stk.empty() && h[stk.back()] < h[i]) { stk.pop_back(); }
if (!stk.empty()) par[i][0] = stk.back();
stk.push_back(i);
}
stk.clear();
for (int i=n-1;i>=0;i--) {
while(!stk.empty() && h[stk.back()] < h[i]) { stk.pop_back(); }
if (!stk.empty()) par[i][1] = stk.back();
stk.push_back(i);
}
for (int i=0;i<n;i++) {
if (h[i] == n-1) { partree[i] = -1; continue; }
assert(h[par[i][0]] != h[par[i][1]]);
if (par[i][0] == n) {
partree[i] = par[i][1];
} else if (par[i][1] == n) {
partree[i] = par[i][0];
} else {
partree[i] = (h[par[i][0]] > h[par[i][1]] ? par[i][0] : par[i][1]);
}
}
}
int jump(int a,int b) {
if (h[a] > h[b]) { return nax; }
int x = a;
int ans = 0;
// int cnt_not_greatest = 0;
int w = -1;
assert(a!=b);
int bit = 31 - __builtin_clz(b-a+1);
if (max(sparse[bit][a],sparse[bit][b-(1<<bit)+1]) > h[b]) {
return nax;
}
while(x != b) {
if (h[par[x][0]] > h[b]) {
// cnt_not_greatest++;
x = par[x][1];
assert(w != 0);
w = 1;
ans++;
} else if (h[par[x][1]] > h[b]) {
x = par[x][0];
assert(w != 1);
w = 0;
// cnt_not_greatest++;
ans++;
} else {
x = (h[par[x][0]] > h[par[x][1]] ? par[x][0] : par[x][1]);
ans++;
}
}
// assert(cnt_not_greatest<=1);
return ans;
}
int minimum_jumps(int a,int b,int c,int d) {
int ans = nax;
for (int A=a;A<=b;A++) {
for (int C=c;C<=d;C++) {
cmin(ans,jump(A,C));
}
}
return (ans == nax ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Execution timed out |
4065 ms |
17312 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
3 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
2 ms |
556 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
37 ms |
600 KB |
Output is correct |
12 |
Correct |
20 ms |
600 KB |
Output is correct |
13 |
Correct |
8 ms |
600 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
19 ms |
600 KB |
Output is correct |
16 |
Correct |
7 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
2 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
3 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
2 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
2 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
0 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
0 ms |
600 KB |
Output is correct |
38 |
Correct |
1 ms |
600 KB |
Output is correct |
39 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
3 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
2 ms |
556 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
37 ms |
600 KB |
Output is correct |
12 |
Correct |
20 ms |
600 KB |
Output is correct |
13 |
Correct |
8 ms |
600 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
19 ms |
600 KB |
Output is correct |
16 |
Correct |
7 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
2 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
3 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
2 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
2 ms |
600 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
600 KB |
Output is correct |
35 |
Correct |
0 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
0 ms |
600 KB |
Output is correct |
38 |
Correct |
1 ms |
600 KB |
Output is correct |
39 |
Correct |
1 ms |
600 KB |
Output is correct |
40 |
Correct |
0 ms |
344 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
344 KB |
Output is correct |
44 |
Correct |
1 ms |
344 KB |
Output is correct |
45 |
Correct |
2 ms |
600 KB |
Output is correct |
46 |
Correct |
1 ms |
600 KB |
Output is correct |
47 |
Correct |
2 ms |
600 KB |
Output is correct |
48 |
Correct |
1 ms |
600 KB |
Output is correct |
49 |
Correct |
2 ms |
600 KB |
Output is correct |
50 |
Correct |
36 ms |
600 KB |
Output is correct |
51 |
Correct |
20 ms |
600 KB |
Output is correct |
52 |
Correct |
8 ms |
600 KB |
Output is correct |
53 |
Correct |
2 ms |
600 KB |
Output is correct |
54 |
Correct |
19 ms |
600 KB |
Output is correct |
55 |
Correct |
7 ms |
600 KB |
Output is correct |
56 |
Correct |
1 ms |
600 KB |
Output is correct |
57 |
Correct |
0 ms |
600 KB |
Output is correct |
58 |
Correct |
366 ms |
600 KB |
Output is correct |
59 |
Correct |
620 ms |
600 KB |
Output is correct |
60 |
Correct |
38 ms |
600 KB |
Output is correct |
61 |
Correct |
650 ms |
600 KB |
Output is correct |
62 |
Correct |
13 ms |
596 KB |
Output is correct |
63 |
Correct |
587 ms |
600 KB |
Output is correct |
64 |
Execution timed out |
4022 ms |
600 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1049 ms |
16700 KB |
Output is correct |
6 |
Execution timed out |
4034 ms |
20560 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
148 ms |
9640 KB |
Output is correct |
5 |
Correct |
647 ms |
20560 KB |
Output is correct |
6 |
Correct |
417 ms |
3928 KB |
Output is correct |
7 |
Correct |
633 ms |
20700 KB |
Output is correct |
8 |
Correct |
367 ms |
7512 KB |
Output is correct |
9 |
Correct |
620 ms |
20560 KB |
Output is correct |
10 |
Execution timed out |
4003 ms |
21708 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
148 ms |
9640 KB |
Output is correct |
5 |
Correct |
647 ms |
20560 KB |
Output is correct |
6 |
Correct |
417 ms |
3928 KB |
Output is correct |
7 |
Correct |
633 ms |
20700 KB |
Output is correct |
8 |
Correct |
367 ms |
7512 KB |
Output is correct |
9 |
Correct |
620 ms |
20560 KB |
Output is correct |
10 |
Execution timed out |
4003 ms |
21708 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Execution timed out |
4065 ms |
17312 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |