Submission #959204

#TimeUsernameProblemLanguageResultExecution timeMemory
959204AbitoRailway Trip (JOI17_railway_trip)C++17
20 / 100
2091 ms14936 KiB
#include <bits/stdc++.h> #define F first #define S second #define pb push_back #define ppb pop_back #define ep insert #define endl '\n' #define elif else if #define pow pwr #define sqrt sqrtt #define int long long #define ll long long typedef unsigned long long ull; using namespace std; const int N=2e5+5; int a[N],n,q,k,dis[N]; vector<int> adj[N]; void bfs(int s){ for (int i=1;i<=n;i++) dis[i]=-1; dis[s]=0; queue<int> q; q.push(s); while (!q.empty()){ int x=q.front(); q.pop(); for (auto u:adj[x]){ if (dis[u]==-1){ dis[u]=dis[x]+1; q.push(u); } } }return; } int32_t main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); cin>>n>>k>>q; for (int i=1;i<=n;i++) cin>>a[i]; stack<int> s; for (int i=1;i<=n;i++){ while (!s.empty() && a[i]>a[s.top()]) s.pop(); if (!s.empty()) adj[i].pb(s.top()),adj[s.top()].pb(i); s.push(i); } while (!s.empty()) s.pop(); for (int i=n;i>0;i--){ while (!s.empty() && a[i]>a[s.top()]) s.pop(); if (!s.empty()) adj[i].pb(s.top()),adj[s.top()].pb(i); s.push(i); } while (q--){ int x,y; cin>>x>>y; bfs(x); cout<<dis[y]-1<<endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...