#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,k,q;
cin >> n >> k >> q;
vector<int> l(n+1);
vector<vector<int>> idxes(k+1);
for(int i=1;i<=n;i++)cin>>l[i];
vector<vector<int>> adj(n+1);
stack<pair<int,int>> s;
for(int i=1;i<=n;i++) {
while(!s.empty() and s.top().first<l[i])s.pop();
if(!s.empty()) {
adj[s.top().second].emplace_back(i);
adj[i].emplace_back(s.top().second);
}
s.emplace(l[i],i);
}
while(!s.empty())s.pop();
for(int i=n;i;i--){
while(!s.empty() and s.top().first<l[i])s.pop();
if(!s.empty()) {
adj[s.top().second].emplace_back(i);
adj[i].emplace_back(s.top().second);
}
s.emplace(l[i],i);
}
for(int i=1;i<=q;i++) {
int a,b;
cin >> a >> b;
vector<bool> visited(n+1);
queue<pair<int,int>> qu;
qu.emplace(-1,a);
while(!qu.empty()) {
auto [dist,idx] = qu.front();qu.pop();
if(visited[idx])continue;
visited[idx]=true;
if(idx==b) {
cout << dist << '\n';
break;
}
for(int&x:adj[idx])if(!visited[x])qu.emplace(dist+1,x);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
95 ms |
10128 KB |
Output is correct |
3 |
Correct |
90 ms |
10076 KB |
Output is correct |
4 |
Correct |
97 ms |
10076 KB |
Output is correct |
5 |
Correct |
97 ms |
10076 KB |
Output is correct |
6 |
Correct |
109 ms |
10400 KB |
Output is correct |
7 |
Correct |
112 ms |
13344 KB |
Output is correct |
8 |
Correct |
41 ms |
12388 KB |
Output is correct |
9 |
Correct |
45 ms |
12700 KB |
Output is correct |
10 |
Correct |
54 ms |
10332 KB |
Output is correct |
11 |
Correct |
92 ms |
12380 KB |
Output is correct |
12 |
Correct |
89 ms |
10844 KB |
Output is correct |
13 |
Correct |
94 ms |
10324 KB |
Output is correct |
14 |
Correct |
89 ms |
12372 KB |
Output is correct |
15 |
Correct |
94 ms |
11100 KB |
Output is correct |
16 |
Correct |
86 ms |
10532 KB |
Output is correct |
17 |
Correct |
115 ms |
13520 KB |
Output is correct |
18 |
Correct |
110 ms |
13468 KB |
Output is correct |
19 |
Correct |
71 ms |
13352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2054 ms |
10324 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2037 ms |
13684 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |