// 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;
pair<int,pii> f(int pos,int l,int r){
int L=pos,R=pos,ans=0;
while(1){
int LL,RR;
if(a[L]<a[R]) LL=bef[R],RR=aft[R];
else if(a[L]>a[R]) RR=aft[L],LL=bef[L];
else LL=bef[L],RR=aft[R];
if(l<LL && RR<r) L=LL,R=RR,ans++;
else break;
}
return {ans,{L,R}};
}
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]];
}
bef[0]=0, aft[n-1]=n-1;
while(q--){
int A,B; cin>>A>>B; --A,--B;
if(A>B) swap(A,B);
pair<int,pii> aa=f(A,-1,B);
pair<int,pii> bb=f(B,aa.S.S,n);
cout<<aa.F+bb.F<<"\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 |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 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 |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
11 ms |
1536 KB |
Output is correct |
3 |
Correct |
11 ms |
1536 KB |
Output is correct |
4 |
Correct |
11 ms |
1536 KB |
Output is correct |
5 |
Correct |
11 ms |
1512 KB |
Output is correct |
6 |
Correct |
12 ms |
1536 KB |
Output is correct |
7 |
Correct |
13 ms |
1536 KB |
Output is correct |
8 |
Correct |
12 ms |
1536 KB |
Output is correct |
9 |
Correct |
13 ms |
1536 KB |
Output is correct |
10 |
Correct |
12 ms |
1536 KB |
Output is correct |
11 |
Correct |
15 ms |
1536 KB |
Output is correct |
12 |
Correct |
14 ms |
1664 KB |
Output is correct |
13 |
Correct |
15 ms |
1664 KB |
Output is correct |
14 |
Correct |
13 ms |
1536 KB |
Output is correct |
15 |
Correct |
14 ms |
1600 KB |
Output is correct |
16 |
Correct |
13 ms |
1536 KB |
Output is correct |
17 |
Correct |
13 ms |
1536 KB |
Output is correct |
18 |
Correct |
13 ms |
1536 KB |
Output is correct |
19 |
Correct |
12 ms |
1536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1434 ms |
2288 KB |
Output is correct |
2 |
Correct |
1614 ms |
2248 KB |
Output is correct |
3 |
Correct |
1361 ms |
2248 KB |
Output is correct |
4 |
Correct |
1401 ms |
2192 KB |
Output is correct |
5 |
Correct |
1561 ms |
2136 KB |
Output is correct |
6 |
Correct |
1323 ms |
2220 KB |
Output is correct |
7 |
Correct |
1338 ms |
2152 KB |
Output is correct |
8 |
Execution timed out |
2028 ms |
1900 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
1784 KB |
Output is correct |
2 |
Correct |
67 ms |
1784 KB |
Output is correct |
3 |
Correct |
68 ms |
1784 KB |
Output is correct |
4 |
Correct |
64 ms |
1784 KB |
Output is correct |
5 |
Execution timed out |
2039 ms |
1972 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |