// High above the clouds there is a rainbow...
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn=1e5+10;
int a[maxn],bef[maxn],aft[maxn],n;
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int k,q;cin>>n>>k>>q;
for(int i=0;i<n;i++){
cin>>a[i], --a[i];
a[i]=min(a[i],k-1);
}
for(int i=0;i<n;i++){
bef[i]=i-1;
while(bef[i]>=0 && a[bef[i]]<a[i])
bef[i]=bef[bef[i]];
}
for(int i=n-1;i>=0;i--){
aft[i]=i+1;
while(aft[i]<n && a[aft[i]]<a[i])
aft[i]=aft[aft[i]];
}
while(q--){
pii A,B; cin>>A.F>>B.F; A.S=--A.F, B.S=--B.F;
if(A.F>B.F) swap(A,B);
int ans=0;
while(max(A.F,B.F)>min(A.S,B.S)){
int numa= max(a[A.F],a[A.S]), numb= max(a[B.F],a[B.S]);
if(numa>numb) swap(A,B);
ans++;
if(a[A.F]<a[A.S]) A={bef[A.S],aft[A.S]};
else if(a[A.F]>a[A.S]) A={bef[A.F],aft[A.F]};
else A={bef[A.F],aft[A.S]};
}
cout<<ans-1<<"\n";
}
return 0;
}
// Deathly mistakes:
// * Read the problem curfully.
// * Check maxn.
// * Overflows.
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
396 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
10 ms |
1764 KB |
Output is correct |
3 |
Correct |
11 ms |
1736 KB |
Output is correct |
4 |
Correct |
11 ms |
1792 KB |
Output is correct |
5 |
Correct |
11 ms |
1784 KB |
Output is correct |
6 |
Correct |
11 ms |
1920 KB |
Output is correct |
7 |
Correct |
13 ms |
2040 KB |
Output is correct |
8 |
Correct |
13 ms |
1920 KB |
Output is correct |
9 |
Correct |
13 ms |
2048 KB |
Output is correct |
10 |
Correct |
13 ms |
1920 KB |
Output is correct |
11 |
Correct |
13 ms |
2048 KB |
Output is correct |
12 |
Correct |
13 ms |
2176 KB |
Output is correct |
13 |
Correct |
13 ms |
2048 KB |
Output is correct |
14 |
Correct |
13 ms |
2048 KB |
Output is correct |
15 |
Correct |
14 ms |
2048 KB |
Output is correct |
16 |
Correct |
14 ms |
2084 KB |
Output is correct |
17 |
Correct |
15 ms |
2048 KB |
Output is correct |
18 |
Correct |
12 ms |
2068 KB |
Output is correct |
19 |
Correct |
12 ms |
2092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1588 ms |
3516 KB |
Output is correct |
2 |
Correct |
1812 ms |
3448 KB |
Output is correct |
3 |
Correct |
1591 ms |
3480 KB |
Output is correct |
4 |
Correct |
1633 ms |
3496 KB |
Output is correct |
5 |
Correct |
1563 ms |
3488 KB |
Output is correct |
6 |
Correct |
1515 ms |
3464 KB |
Output is correct |
7 |
Correct |
1558 ms |
3484 KB |
Output is correct |
8 |
Execution timed out |
2029 ms |
2776 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
3552 KB |
Output is correct |
2 |
Correct |
61 ms |
3548 KB |
Output is correct |
3 |
Correct |
66 ms |
3620 KB |
Output is correct |
4 |
Correct |
65 ms |
3568 KB |
Output is correct |
5 |
Execution timed out |
2056 ms |
2520 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |