#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define sz(a) (ll)a.size()
#define all(x) (x).begin(), (x).end()
int main() {
ll n, k, Q; cin >> n >> k >> Q;
vector<vector<ll>> adj(n);
vector<ll> l(n), a(Q), b(Q); for(auto& u : l) cin >> u;
for(ll i=0; i<Q; i++) cin >> a[i] >> b[i];
stack<ll> s;
s.push(0);
for(ll i=1; i<n; i++){
while(!s.empty() && l[s.top()]<l[i]) s.pop();
adj[i].pb(s.top());
adj[s.top()].pb(i);
s.push(i);
}
while(!s.empty()) s.pop();
s.push(n-1);
for(ll i=n-2; i>=0; i--){
while(!s.empty() && l[s.top()]<l[i]) s.pop();
adj[i].pb(s.top());
adj[s.top()].pb(i);
s.push(i);
}
for(ll i=0; i<Q; i++){
a[i]--; b[i]--;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> q;
q.push({0, a[i]});
vector<ll> dist(n, -1);
dist[a[i]]=0;
while(!q.empty()){
ll pl=q.top().S, curr=q.top().F;
q.pop();
if(dist[pl]<curr) continue;
for(auto& u : adj[pl]){
if(curr+1>=dist[u] && dist[u]!=-1) continue;
dist[u]=curr+1;
q.push({dist[u], u});
}
}
cout << dist[b[i]]-1 << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
278 ms |
10816 KB |
Output is correct |
3 |
Correct |
309 ms |
10724 KB |
Output is correct |
4 |
Correct |
328 ms |
10836 KB |
Output is correct |
5 |
Correct |
356 ms |
10884 KB |
Output is correct |
6 |
Correct |
431 ms |
11068 KB |
Output is correct |
7 |
Correct |
527 ms |
11988 KB |
Output is correct |
8 |
Correct |
94 ms |
10068 KB |
Output is correct |
9 |
Correct |
122 ms |
11204 KB |
Output is correct |
10 |
Correct |
116 ms |
10320 KB |
Output is correct |
11 |
Correct |
250 ms |
10836 KB |
Output is correct |
12 |
Correct |
256 ms |
10832 KB |
Output is correct |
13 |
Correct |
254 ms |
10576 KB |
Output is correct |
14 |
Correct |
256 ms |
10840 KB |
Output is correct |
15 |
Correct |
251 ms |
10844 KB |
Output is correct |
16 |
Correct |
255 ms |
10580 KB |
Output is correct |
17 |
Correct |
527 ms |
12024 KB |
Output is correct |
18 |
Correct |
523 ms |
12204 KB |
Output is correct |
19 |
Correct |
482 ms |
11812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2044 ms |
13444 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2069 ms |
14652 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |