#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
const int lg = 17;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, K, Q;
cin >> N >> K >> Q;
vi L(1+N);
for(int i = 1; i <= N; i++)
cin >> L[i];
int lh[1+lg][1+N], rh[1+lg][1+N];
lh[0][1] = 1;
for(int i = 2; i <= N; i++)
{
lh[0][i] = i-1;
while(L[lh[0][i]] < L[i])
lh[0][i] = lh[0][ lh[0][i] ];
}
rh[0][N] = N;
for(int i = N-1; i >= 1; i--)
{
rh[0][i] = i+1;
while(L[rh[0][i]] < L[i])
rh[0][i] = rh[0][ rh[0][i] ];
}
for(int e = 1; e <= lg; e++)
{
for(int i = 1; i <= N; i++)
{
lh[e][i] = min(lh[e-1][lh[e-1][i]], lh[e-1][rh[e-1][i]]);
rh[e][i] = max(rh[e-1][rh[e-1][i]], rh[e-1][lh[e-1][i]]);
}
}
// cerr << "done\n";
for(int q = 1; q <= Q; q++)
{
int a, b;
cin >> a >> b;
if(a > b)
swap(a, b);
int res = 0;
int la = a, ra = a;
for(int e = lg; e >= 0; e--)
{
if(max(rh[e][la], rh[e][ra]) < b)
{
res += (1<<e);
int nla = min(lh[e][la], lh[e][ra]);
int nra = max(rh[e][la], rh[e][ra]);
la = nla;
ra = nra;
}
}
int lb = b, rb = b;
for(int e = lg; e >= 0; e--)
{
if(min(lh[e][lb], lh[e][rb]) > ra)
{
res += (1<<e);
int nlb = min(lh[e][lb], lh[e][rb]);
int nrb = max(rh[e][lb], rh[e][rb]);
lb = nlb;
rb = nrb;
}
}
cout << res << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
452 KB |
Output is correct |
2 |
Correct |
17 ms |
14932 KB |
Output is correct |
3 |
Correct |
18 ms |
15064 KB |
Output is correct |
4 |
Correct |
18 ms |
15060 KB |
Output is correct |
5 |
Correct |
18 ms |
15016 KB |
Output is correct |
6 |
Correct |
19 ms |
15100 KB |
Output is correct |
7 |
Correct |
21 ms |
15380 KB |
Output is correct |
8 |
Correct |
16 ms |
14932 KB |
Output is correct |
9 |
Correct |
23 ms |
15308 KB |
Output is correct |
10 |
Correct |
18 ms |
15156 KB |
Output is correct |
11 |
Correct |
19 ms |
15316 KB |
Output is correct |
12 |
Correct |
20 ms |
15300 KB |
Output is correct |
13 |
Correct |
19 ms |
15304 KB |
Output is correct |
14 |
Correct |
19 ms |
15392 KB |
Output is correct |
15 |
Correct |
19 ms |
15268 KB |
Output is correct |
16 |
Correct |
19 ms |
15300 KB |
Output is correct |
17 |
Correct |
20 ms |
15308 KB |
Output is correct |
18 |
Correct |
22 ms |
15308 KB |
Output is correct |
19 |
Correct |
20 ms |
15360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
149 ms |
16564 KB |
Output is correct |
2 |
Correct |
147 ms |
16524 KB |
Output is correct |
3 |
Correct |
146 ms |
16656 KB |
Output is correct |
4 |
Correct |
149 ms |
16684 KB |
Output is correct |
5 |
Correct |
126 ms |
16648 KB |
Output is correct |
6 |
Correct |
127 ms |
16584 KB |
Output is correct |
7 |
Correct |
154 ms |
16540 KB |
Output is correct |
8 |
Correct |
170 ms |
16696 KB |
Output is correct |
9 |
Correct |
259 ms |
16716 KB |
Output is correct |
10 |
Correct |
218 ms |
16756 KB |
Output is correct |
11 |
Correct |
224 ms |
16764 KB |
Output is correct |
12 |
Correct |
207 ms |
16700 KB |
Output is correct |
13 |
Correct |
233 ms |
16716 KB |
Output is correct |
14 |
Correct |
180 ms |
16660 KB |
Output is correct |
15 |
Correct |
109 ms |
16460 KB |
Output is correct |
16 |
Correct |
135 ms |
16504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
16720 KB |
Output is correct |
2 |
Correct |
92 ms |
16764 KB |
Output is correct |
3 |
Correct |
103 ms |
16712 KB |
Output is correct |
4 |
Correct |
92 ms |
16676 KB |
Output is correct |
5 |
Correct |
194 ms |
16660 KB |
Output is correct |
6 |
Correct |
146 ms |
16968 KB |
Output is correct |
7 |
Correct |
179 ms |
17092 KB |
Output is correct |
8 |
Correct |
176 ms |
16848 KB |
Output is correct |
9 |
Correct |
180 ms |
16960 KB |
Output is correct |
10 |
Correct |
163 ms |
17024 KB |
Output is correct |
11 |
Correct |
146 ms |
16972 KB |
Output is correct |
12 |
Correct |
172 ms |
17020 KB |
Output is correct |
13 |
Correct |
161 ms |
17016 KB |
Output is correct |
14 |
Correct |
166 ms |
17016 KB |
Output is correct |
15 |
Correct |
174 ms |
17012 KB |
Output is correct |
16 |
Correct |
171 ms |
16972 KB |
Output is correct |
17 |
Correct |
143 ms |
16996 KB |
Output is correct |
18 |
Correct |
139 ms |
16980 KB |
Output is correct |
19 |
Correct |
133 ms |
17036 KB |
Output is correct |
20 |
Correct |
97 ms |
16832 KB |
Output is correct |
21 |
Correct |
80 ms |
16716 KB |
Output is correct |
22 |
Correct |
84 ms |
16844 KB |
Output is correct |
23 |
Correct |
83 ms |
16712 KB |
Output is correct |