#include <bits/stdc++.h>
#define int long long
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))
template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }
using pii = pair<int, int>;
using vi = vector<int>;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int MAX_N = 1e5, LG = 17;
int level[MAX_N];
int lft[LG][MAX_N], rgt[LG][MAX_N];
pii f(int L, int R, int bit) {
return make_pair(
min(lft[bit][L], lft[bit][R]),
max(rgt[bit][L], rgt[bit][R])
);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, K, Q;
cin >> N >> K >> Q;
rep(i, 0, N) cin >> level[i];
lft[0][0] = 0;
rep(i, 1, N) {
int j = i-1;
while (level[j] < level[i]) {
j = lft[0][j];
}
lft[0][i] = j;
}
rgt[0][N-1] = N-1;
for (int i = N-2; i >= 0; --i) {
int j = i+1;
while (level[j] < level[i]) {
j = rgt[0][j];
}
rgt[0][i] = j;
}
rep(bit, 0, LG-1) {
rep(i, 0, N) {
tie(lft[bit+1][i], rgt[bit+1][i]) = f(lft[bit][i], rgt[bit][i], bit);
}
}
rep(iReq, 0, Q) {
int A, B;
cin >> A >> B;
--A, --B;
if (A > B) swap(A, B);
int jumps = 0;
int LA = A, RA = A;
for (int bit = LG-1; bit >= 0; --bit) {
auto [newLA, newRA] = f(LA, RA, bit);
if (newRA < B) {
tie(LA, RA) = tie(newLA, newRA);
jumps += (1 << bit);
}
}
int LB = B, RB = B;
for (int bit = LG-1; bit >= 0; --bit) {
auto [newLB, newRB] = f(LB, RB, bit);
if (newLB > RA) {
tie(LB, RB) = tie(newLB, newRB);
jumps += (1 << bit);
}
}
if (RA != LB) jumps++;
cout << jumps - 1 << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
452 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
19 ms |
27860 KB |
Output is correct |
3 |
Correct |
21 ms |
27860 KB |
Output is correct |
4 |
Correct |
19 ms |
27976 KB |
Output is correct |
5 |
Correct |
19 ms |
27932 KB |
Output is correct |
6 |
Correct |
21 ms |
27984 KB |
Output is correct |
7 |
Correct |
23 ms |
28216 KB |
Output is correct |
8 |
Correct |
20 ms |
27828 KB |
Output is correct |
9 |
Correct |
20 ms |
28224 KB |
Output is correct |
10 |
Correct |
21 ms |
28016 KB |
Output is correct |
11 |
Correct |
22 ms |
28248 KB |
Output is correct |
12 |
Correct |
26 ms |
28188 KB |
Output is correct |
13 |
Correct |
22 ms |
28244 KB |
Output is correct |
14 |
Correct |
21 ms |
28180 KB |
Output is correct |
15 |
Correct |
22 ms |
28200 KB |
Output is correct |
16 |
Correct |
24 ms |
28196 KB |
Output is correct |
17 |
Correct |
22 ms |
28236 KB |
Output is correct |
18 |
Correct |
24 ms |
28240 KB |
Output is correct |
19 |
Correct |
21 ms |
28264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
29540 KB |
Output is correct |
2 |
Correct |
176 ms |
29472 KB |
Output is correct |
3 |
Correct |
175 ms |
29552 KB |
Output is correct |
4 |
Correct |
163 ms |
29488 KB |
Output is correct |
5 |
Correct |
166 ms |
29500 KB |
Output is correct |
6 |
Correct |
173 ms |
29516 KB |
Output is correct |
7 |
Correct |
154 ms |
29452 KB |
Output is correct |
8 |
Correct |
218 ms |
29576 KB |
Output is correct |
9 |
Correct |
243 ms |
29532 KB |
Output is correct |
10 |
Correct |
302 ms |
29696 KB |
Output is correct |
11 |
Correct |
237 ms |
29604 KB |
Output is correct |
12 |
Correct |
238 ms |
29632 KB |
Output is correct |
13 |
Correct |
253 ms |
29592 KB |
Output is correct |
14 |
Correct |
174 ms |
29484 KB |
Output is correct |
15 |
Correct |
145 ms |
29436 KB |
Output is correct |
16 |
Correct |
150 ms |
29432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
29688 KB |
Output is correct |
2 |
Correct |
120 ms |
29672 KB |
Output is correct |
3 |
Correct |
132 ms |
29512 KB |
Output is correct |
4 |
Correct |
129 ms |
29636 KB |
Output is correct |
5 |
Correct |
240 ms |
29580 KB |
Output is correct |
6 |
Correct |
190 ms |
29984 KB |
Output is correct |
7 |
Correct |
207 ms |
30004 KB |
Output is correct |
8 |
Correct |
210 ms |
29808 KB |
Output is correct |
9 |
Correct |
182 ms |
29880 KB |
Output is correct |
10 |
Correct |
188 ms |
29904 KB |
Output is correct |
11 |
Correct |
179 ms |
29776 KB |
Output is correct |
12 |
Correct |
172 ms |
29844 KB |
Output is correct |
13 |
Correct |
178 ms |
29900 KB |
Output is correct |
14 |
Correct |
179 ms |
29972 KB |
Output is correct |
15 |
Correct |
185 ms |
29900 KB |
Output is correct |
16 |
Correct |
191 ms |
29868 KB |
Output is correct |
17 |
Correct |
142 ms |
29928 KB |
Output is correct |
18 |
Correct |
183 ms |
29964 KB |
Output is correct |
19 |
Correct |
165 ms |
29940 KB |
Output is correct |
20 |
Correct |
128 ms |
29776 KB |
Output is correct |
21 |
Correct |
130 ms |
29724 KB |
Output is correct |
22 |
Correct |
116 ms |
29620 KB |
Output is correct |
23 |
Correct |
120 ms |
29604 KB |
Output is correct |