Submission #691267

#TimeUsernameProblemLanguageResultExecution timeMemory
691267amunduzbaevRailway Trip (JOI17_railway_trip)C++17
20 / 100
2086 ms11080 KiB
#include "bits/stdc++.h" using namespace std; #define ar array typedef long long ll; //~ #define int ll const int N = 2e5 + 5; vector<int> edges[N]; int a[N]; signed main(){ ios::sync_with_stdio(0); cin.tie(0); int n, k, q; cin >> n >> k >> q; vector<int> ss; for(int i=0;i<n;i++){ cin >> a[i]; while(!ss.empty() && a[ss.back()] < a[i]){ //~ edges[ss.back()].push_back(i); //~ edges[i].push_back(ss.back()); ss.pop_back(); } if(!ss.empty()){ edges[ss.back()].push_back(i); edges[i].push_back(ss.back()); } ss.push_back(i); } ss.clear(); for(int i=n-1;~i;i--){ while(!ss.empty() && a[ss.back()] < a[i]){ //~ edges[ss.back()].push_back(i); //~ edges[i].push_back(ss.back()); ss.pop_back(); } if(!ss.empty()){ edges[ss.back()].push_back(i); edges[i].push_back(ss.back()); } ss.push_back(i); } int d[n]; while(q--){ int a, b; cin >> a >> b; a--, b--; memset(d, 63, sizeof d); queue<int> q; q.push(a); d[a] = 0; while(!q.empty()){ int u = q.front(); q.pop(); for(auto x : edges[u]){ if(d[x] > d[u] + 1){ d[x] = d[u] + 1; q.push(x); } } } cout<<--d[b]<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...