This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MAXN=100005,INF=1e9;
int a[MAXN],L[MAXN],R[MAXN];
vector<int> adj[MAXN];
bool visited[MAXN];
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n,k,q,u,v;
cin >> n >> k >> q;
for(int i=1;i<=n;i++)cin >> a[i];
stack<int> kiri;
kiri.push(1);
for(int i=2;i<=n;i++){
while(a[kiri.top()]<a[i])kiri.pop();
adj[i].push_back(kiri.top());
adj[kiri.top()].push_back(i);
kiri.push(i);
}
stack<int> kanan;
kanan.push(n);
for(int i=n-1;i>=1;i--){
while(a[kanan.top()]<a[i])kanan.pop();
adj[i].push_back(kanan.top());
adj[kanan.top()].push_back(i);
kanan.push(i);
}
while(q--){
for(int i=1;i<=n;i++)visited[i]=0;
cin >> u >> v;
queue<pii> Q;
Q.push({0,u});
visited[u]=true;
while(!Q.empty()){
pii now=Q.front();
Q.pop();
if(now.second==v){
cout << now.first-1 << '\n';
break;
}
for(auto nxt : adj[now.second]){
if(!visited[nxt]){
visited[nxt]=true;
Q.push({now.first+1,nxt});
}
}
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |