#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
typedef unsigned long long ull;
using namespace std;
const int N=2e5+5;
int a[N],n,q,k,dis[N];
vector<int> adj[N];
void bfs(int s){
for (int i=1;i<=n;i++) dis[i]=-1;
dis[s]=0;
queue<int> q;
q.push(s);
while (!q.empty()){
int x=q.front();
q.pop();
for (auto u:adj[x]){
if (dis[u]==-1){
dis[u]=dis[x]+1;
q.push(u);
}
}
}return;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>k>>q;
for (int i=1;i<=n;i++) cin>>a[i];
stack<int> s;
for (int i=1;i<=n;i++){
while (!s.empty() && a[i]>a[s.top()]) s.pop();
if (!s.empty()) adj[i].pb(s.top()),adj[s.top()].pb(i);
s.push(i);
}
while (!s.empty()) s.pop();
for (int i=n;i>0;i--){
while (!s.empty() && a[i]>a[s.top()]) s.pop();
if (!s.empty()) adj[i].pb(s.top()),adj[s.top()].pb(i);
s.push(i);
}
while (q--){
int x,y;
cin>>x>>y;
bfs(x);
cout<<dis[y]-1<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6796 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
141 ms |
13912 KB |
Output is correct |
3 |
Correct |
142 ms |
13916 KB |
Output is correct |
4 |
Correct |
151 ms |
13996 KB |
Output is correct |
5 |
Correct |
152 ms |
14008 KB |
Output is correct |
6 |
Correct |
165 ms |
14140 KB |
Output is correct |
7 |
Correct |
155 ms |
14464 KB |
Output is correct |
8 |
Correct |
51 ms |
13148 KB |
Output is correct |
9 |
Correct |
57 ms |
14200 KB |
Output is correct |
10 |
Correct |
54 ms |
13404 KB |
Output is correct |
11 |
Correct |
146 ms |
13988 KB |
Output is correct |
12 |
Correct |
136 ms |
13904 KB |
Output is correct |
13 |
Correct |
156 ms |
13916 KB |
Output is correct |
14 |
Correct |
140 ms |
13996 KB |
Output is correct |
15 |
Correct |
138 ms |
13988 KB |
Output is correct |
16 |
Correct |
137 ms |
13944 KB |
Output is correct |
17 |
Correct |
147 ms |
14400 KB |
Output is correct |
18 |
Correct |
151 ms |
14492 KB |
Output is correct |
19 |
Correct |
79 ms |
14160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2091 ms |
14092 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2007 ms |
14936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |