#include "bits/stdc++.h"
using namespace std;
#define ar array
#define int long long
const int N = 1e5 + 5;
vector<int> edges[N];
int a[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, k, q; cin>>n>>k>>q;
for(int i=0;i<n;i++){
cin>>a[i];
}
vector<int> st;
for(int i=0;i<n;i++){
bool is = 0;
while(!st.empty() && a[st.back()] <= a[i]){
is |= (a[st.back()] == a[i]);
edges[i].push_back(st.back());
edges[st.back()].push_back(i);
st.pop_back();
}
if(!is && !st.empty()){
edges[i].push_back(st.back());
edges[st.back()].push_back(i);
}
st.push_back(i);
}
auto bfs = [&](int a, int b){
queue<int> qq;
vector<int> d(n, n);
d[a] = 0; qq.push(a);
while(!qq.empty()){
int u = qq.front(); qq.pop();
for(auto x : edges[u]){
if(d[x] > d[u] + 1){
d[x] = d[u] + 1;
qq.push(x);
}
}
}
return d[b] - 1;
};
while(q--){
int a, b; cin>>a>>b;
a--, b--;
cout<<bfs(a, b)<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2676 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Correct |
178 ms |
9252 KB |
Output is correct |
3 |
Correct |
193 ms |
9556 KB |
Output is correct |
4 |
Correct |
220 ms |
9752 KB |
Output is correct |
5 |
Correct |
188 ms |
9884 KB |
Output is correct |
6 |
Correct |
213 ms |
10148 KB |
Output is correct |
7 |
Correct |
255 ms |
10492 KB |
Output is correct |
8 |
Correct |
68 ms |
7508 KB |
Output is correct |
9 |
Correct |
71 ms |
8500 KB |
Output is correct |
10 |
Correct |
73 ms |
8072 KB |
Output is correct |
11 |
Correct |
186 ms |
8972 KB |
Output is correct |
12 |
Correct |
150 ms |
9024 KB |
Output is correct |
13 |
Correct |
155 ms |
8984 KB |
Output is correct |
14 |
Correct |
139 ms |
9020 KB |
Output is correct |
15 |
Correct |
152 ms |
9036 KB |
Output is correct |
16 |
Correct |
146 ms |
8940 KB |
Output is correct |
17 |
Correct |
230 ms |
10556 KB |
Output is correct |
18 |
Correct |
239 ms |
10476 KB |
Output is correct |
19 |
Correct |
158 ms |
10736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2068 ms |
9680 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2067 ms |
10696 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |