이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |