Submission #126542

#TimeUsernameProblemLanguageResultExecution timeMemory
126542baluteshihRailway Trip (JOI17_railway_trip)C++14
20 / 100
2065 ms8156 KiB
#include <bits/stdc++.h> #define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); #define pb push_back #define MP make_pair #define F first #define S second #define MEM(i,j) memset(i,j,sizeof i) #define ALL(v) v.begin(),v.end() #define ET cout << "\n" #define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;} using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; vector<int> G[100005]; int val[100005]; vector<int> st; queue<int> qu; int dis[100005]; int main() {jizz int n,k,q,a,b; cin >> n >> k >> q; for(int i=1;i<=n;++i) cin >> val[i]; for(int i=1;i<=n;++i) { while(!st.empty()&&val[i]>val[st.back()]) G[i].pb(st.back()),G[st.back()].pb(i),st.pop_back(); if(!st.empty()) if(val[st.back()]==val[i]) G[i].pb(st.back()),G[st.back()].pb(i),st.back()=i; else G[i].pb(st.back()),G[st.back()].pb(i),st.pb(i); else st.pb(i); } while(q--) { cin >> a >> b,fill(dis+1,dis+n+1,n+1); while(!qu.empty()) qu.pop(); qu.push(a),dis[a]=0; while(!qu.empty()) { int u=qu.front(); qu.pop(); if(u==b) break; for(int i:G[u]) if(dis[i]>dis[u]+1) dis[i]=dis[u]+1,qu.push(i); } cout << dis[b]-1 << "\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...