#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> ii;
#define FOR(i,a,b) for(int i=a; i<b; i++)
#define ROF(i,a,b) for(int i=a; i>=b; i--)
#define pb push_back
#define fi first
#define se second
// find min k so that if I change k intervals to -1, I can add +-c[i]
int main(){
ios::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n,k,q; cin>>n>>k>>q;
vi a(n); FOR(i,0,n) cin>>a[i];
vi l(n), r(n);
vi g[n];
stack<int> s;
s.push(0);
FOR(i,1,n){
while (a[s.top()] < a[i]) s.pop();
l[i] = s.top(); s.push(i);
g[l[i]].pb(i); g[i].pb(l[i]);
}
while (!s.empty()) s.pop();
s.push(n-1);
ROF(i,n-2,0){
while (a[s.top()] < a[i]) s.pop();
r[i] = s.top(); s.push(i);
g[r[i]].pb(i);
g[i].pb(r[i]);
}
vi dist;
FOR(i,0,q){
int a, b; cin>>a>>b;
a--; b--;
dist.assign(n,-1);
vi bfs; bfs.pb(a); dist[a]=0;
FOR(j,0,bfs.size()){
int u = bfs[j];
for (int v : g[u]){
if (dist[v]!=-1) continue;
dist[v] = dist[u]+1; bfs.pb(v);
}
}
cout<<dist[b]-1<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
460 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 |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
153 ms |
9980 KB |
Output is correct |
3 |
Correct |
145 ms |
9988 KB |
Output is correct |
4 |
Correct |
146 ms |
10000 KB |
Output is correct |
5 |
Correct |
146 ms |
10080 KB |
Output is correct |
6 |
Correct |
145 ms |
10356 KB |
Output is correct |
7 |
Correct |
154 ms |
10304 KB |
Output is correct |
8 |
Correct |
86 ms |
9004 KB |
Output is correct |
9 |
Correct |
71 ms |
9836 KB |
Output is correct |
10 |
Correct |
76 ms |
9324 KB |
Output is correct |
11 |
Correct |
134 ms |
10312 KB |
Output is correct |
12 |
Correct |
132 ms |
10316 KB |
Output is correct |
13 |
Correct |
135 ms |
10308 KB |
Output is correct |
14 |
Correct |
133 ms |
10312 KB |
Output is correct |
15 |
Correct |
134 ms |
10356 KB |
Output is correct |
16 |
Correct |
129 ms |
10280 KB |
Output is correct |
17 |
Correct |
151 ms |
10360 KB |
Output is correct |
18 |
Correct |
155 ms |
10360 KB |
Output is correct |
19 |
Correct |
91 ms |
9656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2102 ms |
10124 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2033 ms |
10728 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |