#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e5+10;
int n, q, k, d1[maxn], d2[maxn], a[maxn], nxl[maxn], nxr[maxn];
void shp1(int S) {
for(int i = 1; i <= n; i++) {
d1[i] = inf;
}
d1[S] = 0;
queue<int> q; q.push(S);
while(q.size()) {
int v = q.front();
q.pop();
if(nxl[v] != 0 && d1[nxl[v]] == inf) {
d1[nxl[v]] = d1[v]+1;
q.push(nxl[v]);
}
if(nxr[v] != 0 && d1[nxr[v]] == inf) {
d1[nxr[v]] = d1[v]+1;
q.push(nxr[v]);
}
}
}
void shp2(int S) {
for(int i = 1; i <= n; i++) {
d2[i] = inf;
}
d2[S] = 0;
queue<int> q; q.push(S);
while(q.size()) {
int v = q.front();
q.pop();
if(nxl[v] != 0 && d2[nxl[v]] == inf) {
d2[nxl[v]] = d2[v]+1;
q.push(nxl[v]);
}
if(nxr[v] != 0 && d2[nxr[v]] == inf) {
d2[nxr[v]] = d2[v]+1;
q.push(nxr[v]);
}
}
}
void solve() {
cin >> n >> k >> q;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
//find nxl
stack<pair<int,int>> st;
for(int i = 1; i <= n; i++) {
while(st.size() && st.top().fr < a[i]) {
st.pop();
}
if(st.size() != 0) {
nxl[i] = st.top().sc;
}
st.push(mp(a[i],i));
}
while(st.size()) st.pop();
for(int i = n; i >= 1; i--) {
while(st.size() && st.top().fr < a[i]) {
st.pop();
}
if(st.size() != 0) {
nxr[i] = st.top().sc;
}
st.push(mp(a[i],i));
}
while(q--) {
int u,v; cin >> u >> v;
shp1(u);
shp2(v);
int ans = inf;
for(int i = 1; i <= n; i++) {
ans = min(ans, d1[i]+d2[i]-1);
}
cout << ans << endl;
}
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
28 ms |
4564 KB |
Output is correct |
3 |
Correct |
28 ms |
4564 KB |
Output is correct |
4 |
Correct |
26 ms |
4528 KB |
Output is correct |
5 |
Correct |
23 ms |
4544 KB |
Output is correct |
6 |
Correct |
20 ms |
4544 KB |
Output is correct |
7 |
Correct |
21 ms |
4688 KB |
Output is correct |
8 |
Correct |
92 ms |
5480 KB |
Output is correct |
9 |
Correct |
57 ms |
5396 KB |
Output is correct |
10 |
Correct |
64 ms |
5668 KB |
Output is correct |
11 |
Correct |
56 ms |
5332 KB |
Output is correct |
12 |
Correct |
59 ms |
5376 KB |
Output is correct |
13 |
Correct |
66 ms |
5316 KB |
Output is correct |
14 |
Correct |
66 ms |
5460 KB |
Output is correct |
15 |
Correct |
63 ms |
5316 KB |
Output is correct |
16 |
Correct |
57 ms |
5304 KB |
Output is correct |
17 |
Correct |
22 ms |
4680 KB |
Output is correct |
18 |
Correct |
22 ms |
4776 KB |
Output is correct |
19 |
Correct |
20 ms |
4792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2077 ms |
4976 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2081 ms |
5176 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |