#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz
#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y)
{
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y)
{
x = y;
return true;
}
return false;
}
const int MOD = 1e9 + 7; //998244353
template<class X, class Y>
void add(X &x, const Y &y) {
x = (x + y);
if(x >= MOD) x -= MOD;
}
template<class X, class Y>
void sub(X &x, const Y &y) {
x = (x - y);
if(x < 0) x += MOD;
}
/* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
const ll INF = 1e9;
const int N = 2e5 + 10;
const int LOG = 17;
pii f[N][LOG + 2];
int jump[N][LOG + 2], jr[N][LOG + 2];
int a[N], L[N], R[N];
void init(int n, vector<int> aa) {
for(int i = 1; i <= n; i++) {
a[i] = aa[i - 1];
f[i][0] = mp(aa[i - 1], i);
}
for(int j = 1; j <= LOG; j++) {
for(int i = 1; i <= n - (1 << j) + 1; i++) {
int u = i + (1 << (j - 1));
f[i][j].se = (f[i][j - 1].fi > f[u][j - 1].fi ? f[i][j - 1].se : f[u][j - 1].se);
f[i][j].fi = max(f[i][j - 1].fi, f[u][j - 1].fi);
}
}
stack<int> st;
for(int i = 1; i <= n; i++) {
while(!st.empty() && a[st.top()] <= a[i]) st.pop();
if(!st.empty()) L[i] = st.top();
st.push(i);
}
while(!st.empty()) st.pop();
for(int i = n; i >= 1; i--) {
while(!st.empty() && a[st.top()] <= a[i]) st.pop();
if(!st.empty()) R[i] = st.top();
st.push(i);
}
for(int i = 1; i <= n; i++) {
jump[i][0] = (a[L[i]] > a[R[i]] ? L[i] : R[i]);
jr[i][0] = R[i];
}
for(int j = 1; j <= LOG; j++) {
for(int i = 1; i <= n; i++) {
int u = jump[i][j - 1];
jump[i][j] = jump[u][j - 1];
u = jr[i][j - 1];
jr[i][j] = jr[u][j - 1];
}
}
}
pii query(int L, int R) {
if(L > R) return mp(0, 0);
int x = 31 - btclz(R - L + 1);
if(f[L][x] > f[R - (1 << x) + 1][x]) return f[L][x];
else return f[R - (1 << x) + 1][x];
}
int minimum_jumps(int l, int r, int u, int v) {
++l; ++r; ++u; ++v;
if(query(r + 1, u - 1).fi >= query(u, v).fi) return -1;
int pos = -1;
int L = l, R = r;
while(L <= R) {
int mid = (L + R) >> 1;
if(query(mid, r).fi < query(u, v).fi) {
pos = mid;
R = mid - 1;
} else L = mid + 1;
}
if(pos < 0) return -1;
int ans = 0;
pos = query(pos, r).se;
for(int i = LOG; i >= 0; i--) {
if(!jump[pos][i]) continue;
if(a[jump[pos][i]] < query(r + 1, u - 1).fi) {
pos = jump[pos][i];
ans += (1 << i);
}
}
if(jump[pos][0] && a[jump[pos][0]] < query(u, v).fi) pos = jump[pos][0], ++ans;
for(int i = LOG; i >= 0; i--) {
if(jr[pos][i] && a[jr[pos][i]] <= query(r + 1, u - 1).fi) {
pos = jr[pos][i];
ans += (1 << i);
}
}
return ans + (pos < u || pos > v);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
194 ms |
50728 KB |
Output is correct |
4 |
Correct |
1072 ms |
63688 KB |
Output is correct |
5 |
Correct |
1002 ms |
32312 KB |
Output is correct |
6 |
Correct |
1285 ms |
63684 KB |
Output is correct |
7 |
Correct |
990 ms |
43608 KB |
Output is correct |
8 |
Correct |
1287 ms |
63756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
2 ms |
336 KB |
Output is correct |
6 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
99 ms |
51244 KB |
Output is correct |
6 |
Correct |
126 ms |
63640 KB |
Output is correct |
7 |
Correct |
68 ms |
32792 KB |
Output is correct |
8 |
Correct |
120 ms |
63644 KB |
Output is correct |
9 |
Correct |
10 ms |
9808 KB |
Output is correct |
10 |
Correct |
143 ms |
63688 KB |
Output is correct |
11 |
Correct |
114 ms |
63756 KB |
Output is correct |
12 |
Correct |
112 ms |
63896 KB |
Output is correct |
13 |
Correct |
113 ms |
63708 KB |
Output is correct |
14 |
Correct |
128 ms |
63704 KB |
Output is correct |
15 |
Correct |
127 ms |
64008 KB |
Output is correct |
16 |
Correct |
110 ms |
64292 KB |
Output is correct |
17 |
Correct |
114 ms |
64316 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
336 KB |
Output is correct |
20 |
Correct |
0 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
275 ms |
29220 KB |
Output is correct |
5 |
Correct |
877 ms |
63852 KB |
Output is correct |
6 |
Correct |
526 ms |
10704 KB |
Output is correct |
7 |
Correct |
982 ms |
63712 KB |
Output is correct |
8 |
Correct |
522 ms |
22092 KB |
Output is correct |
9 |
Correct |
908 ms |
63708 KB |
Output is correct |
10 |
Correct |
1140 ms |
63692 KB |
Output is correct |
11 |
Correct |
1049 ms |
64444 KB |
Output is correct |
12 |
Correct |
1115 ms |
63644 KB |
Output is correct |
13 |
Correct |
974 ms |
63640 KB |
Output is correct |
14 |
Correct |
706 ms |
63988 KB |
Output is correct |
15 |
Correct |
1016 ms |
63816 KB |
Output is correct |
16 |
Correct |
804 ms |
63832 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
0 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
21 ms |
848 KB |
Output is correct |
28 |
Correct |
20 ms |
848 KB |
Output is correct |
29 |
Correct |
20 ms |
848 KB |
Output is correct |
30 |
Correct |
21 ms |
976 KB |
Output is correct |
31 |
Correct |
15 ms |
976 KB |
Output is correct |
32 |
Correct |
0 ms |
336 KB |
Output is correct |
33 |
Correct |
70 ms |
36936 KB |
Output is correct |
34 |
Correct |
118 ms |
63644 KB |
Output is correct |
35 |
Correct |
113 ms |
63692 KB |
Output is correct |
36 |
Correct |
117 ms |
63652 KB |
Output is correct |
37 |
Correct |
127 ms |
63952 KB |
Output is correct |
38 |
Correct |
110 ms |
63904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
275 ms |
29220 KB |
Output is correct |
5 |
Correct |
877 ms |
63852 KB |
Output is correct |
6 |
Correct |
526 ms |
10704 KB |
Output is correct |
7 |
Correct |
982 ms |
63712 KB |
Output is correct |
8 |
Correct |
522 ms |
22092 KB |
Output is correct |
9 |
Correct |
908 ms |
63708 KB |
Output is correct |
10 |
Correct |
1140 ms |
63692 KB |
Output is correct |
11 |
Correct |
1049 ms |
64444 KB |
Output is correct |
12 |
Correct |
1115 ms |
63644 KB |
Output is correct |
13 |
Correct |
974 ms |
63640 KB |
Output is correct |
14 |
Correct |
706 ms |
63988 KB |
Output is correct |
15 |
Correct |
1016 ms |
63816 KB |
Output is correct |
16 |
Correct |
804 ms |
63832 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
336 KB |
Output is correct |
22 |
Correct |
2 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
336 KB |
Output is correct |
24 |
Correct |
2 ms |
336 KB |
Output is correct |
25 |
Correct |
0 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
464 KB |
Output is correct |
27 |
Correct |
21 ms |
848 KB |
Output is correct |
28 |
Correct |
20 ms |
848 KB |
Output is correct |
29 |
Correct |
20 ms |
848 KB |
Output is correct |
30 |
Correct |
21 ms |
976 KB |
Output is correct |
31 |
Correct |
15 ms |
976 KB |
Output is correct |
32 |
Correct |
0 ms |
336 KB |
Output is correct |
33 |
Correct |
70 ms |
36936 KB |
Output is correct |
34 |
Correct |
118 ms |
63644 KB |
Output is correct |
35 |
Correct |
113 ms |
63692 KB |
Output is correct |
36 |
Correct |
117 ms |
63652 KB |
Output is correct |
37 |
Correct |
127 ms |
63952 KB |
Output is correct |
38 |
Correct |
110 ms |
63904 KB |
Output is correct |
39 |
Correct |
0 ms |
336 KB |
Output is correct |
40 |
Correct |
0 ms |
336 KB |
Output is correct |
41 |
Correct |
0 ms |
336 KB |
Output is correct |
42 |
Correct |
283 ms |
29292 KB |
Output is correct |
43 |
Correct |
851 ms |
63688 KB |
Output is correct |
44 |
Correct |
637 ms |
10704 KB |
Output is correct |
45 |
Correct |
830 ms |
63644 KB |
Output is correct |
46 |
Correct |
580 ms |
22152 KB |
Output is correct |
47 |
Correct |
975 ms |
63708 KB |
Output is correct |
48 |
Correct |
1088 ms |
63640 KB |
Output is correct |
49 |
Correct |
969 ms |
64272 KB |
Output is correct |
50 |
Correct |
1006 ms |
63684 KB |
Output is correct |
51 |
Correct |
1035 ms |
63684 KB |
Output is correct |
52 |
Correct |
955 ms |
63992 KB |
Output is correct |
53 |
Correct |
939 ms |
63772 KB |
Output is correct |
54 |
Correct |
985 ms |
63788 KB |
Output is correct |
55 |
Correct |
0 ms |
336 KB |
Output is correct |
56 |
Correct |
165 ms |
63384 KB |
Output is correct |
57 |
Correct |
898 ms |
63648 KB |
Output is correct |
58 |
Correct |
476 ms |
11472 KB |
Output is correct |
59 |
Correct |
780 ms |
63712 KB |
Output is correct |
60 |
Correct |
422 ms |
22864 KB |
Output is correct |
61 |
Correct |
855 ms |
63704 KB |
Output is correct |
62 |
Correct |
1260 ms |
63816 KB |
Output is correct |
63 |
Correct |
1079 ms |
64044 KB |
Output is correct |
64 |
Correct |
1198 ms |
63816 KB |
Output is correct |
65 |
Correct |
1126 ms |
63680 KB |
Output is correct |
66 |
Correct |
1244 ms |
63992 KB |
Output is correct |
67 |
Correct |
1044 ms |
64172 KB |
Output is correct |
68 |
Correct |
766 ms |
63796 KB |
Output is correct |
69 |
Correct |
0 ms |
336 KB |
Output is correct |
70 |
Correct |
1 ms |
336 KB |
Output is correct |
71 |
Correct |
2 ms |
336 KB |
Output is correct |
72 |
Correct |
2 ms |
336 KB |
Output is correct |
73 |
Correct |
2 ms |
336 KB |
Output is correct |
74 |
Correct |
3 ms |
336 KB |
Output is correct |
75 |
Correct |
2 ms |
336 KB |
Output is correct |
76 |
Correct |
0 ms |
336 KB |
Output is correct |
77 |
Correct |
0 ms |
336 KB |
Output is correct |
78 |
Correct |
1 ms |
336 KB |
Output is correct |
79 |
Correct |
3 ms |
336 KB |
Output is correct |
80 |
Correct |
2 ms |
336 KB |
Output is correct |
81 |
Correct |
2 ms |
336 KB |
Output is correct |
82 |
Correct |
3 ms |
336 KB |
Output is correct |
83 |
Correct |
2 ms |
336 KB |
Output is correct |
84 |
Correct |
1 ms |
336 KB |
Output is correct |
85 |
Correct |
3 ms |
336 KB |
Output is correct |
86 |
Correct |
11 ms |
848 KB |
Output is correct |
87 |
Correct |
21 ms |
848 KB |
Output is correct |
88 |
Correct |
21 ms |
976 KB |
Output is correct |
89 |
Correct |
16 ms |
848 KB |
Output is correct |
90 |
Correct |
17 ms |
976 KB |
Output is correct |
91 |
Correct |
0 ms |
336 KB |
Output is correct |
92 |
Correct |
1 ms |
464 KB |
Output is correct |
93 |
Correct |
21 ms |
980 KB |
Output is correct |
94 |
Correct |
21 ms |
976 KB |
Output is correct |
95 |
Correct |
21 ms |
848 KB |
Output is correct |
96 |
Correct |
16 ms |
976 KB |
Output is correct |
97 |
Correct |
19 ms |
976 KB |
Output is correct |
98 |
Correct |
0 ms |
336 KB |
Output is correct |
99 |
Correct |
121 ms |
63584 KB |
Output is correct |
100 |
Correct |
123 ms |
63708 KB |
Output is correct |
101 |
Correct |
115 ms |
63756 KB |
Output is correct |
102 |
Correct |
131 ms |
63648 KB |
Output is correct |
103 |
Correct |
126 ms |
64012 KB |
Output is correct |
104 |
Correct |
126 ms |
63916 KB |
Output is correct |
105 |
Correct |
0 ms |
336 KB |
Output is correct |
106 |
Correct |
67 ms |
36968 KB |
Output is correct |
107 |
Correct |
135 ms |
63676 KB |
Output is correct |
108 |
Correct |
124 ms |
63692 KB |
Output is correct |
109 |
Correct |
117 ms |
63688 KB |
Output is correct |
110 |
Correct |
135 ms |
63976 KB |
Output is correct |
111 |
Correct |
119 ms |
63936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
194 ms |
50728 KB |
Output is correct |
4 |
Correct |
1072 ms |
63688 KB |
Output is correct |
5 |
Correct |
1002 ms |
32312 KB |
Output is correct |
6 |
Correct |
1285 ms |
63684 KB |
Output is correct |
7 |
Correct |
990 ms |
43608 KB |
Output is correct |
8 |
Correct |
1287 ms |
63756 KB |
Output is correct |
9 |
Correct |
0 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
336 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
2 ms |
336 KB |
Output is correct |
14 |
Incorrect |
2 ms |
336 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |