Submission #549339

#TimeUsernameProblemLanguageResultExecution timeMemory
549339LoboRailway Trip (JOI17_railway_trip)C++17
20 / 100
2081 ms5668 KiB
#include<bits/stdc++.h> using namespace std; const long long inf = (long long) 1e18 + 10; const int inf1 = (int) 1e9 + 10; #define int long long #define dbl long double #define endl '\n' #define sc second #define fr first #define mp make_pair #define pb push_back #define all(x) x.begin(), x.end() const int maxn = 1e5+10; int n, q, k, d1[maxn], d2[maxn], a[maxn], nxl[maxn], nxr[maxn]; void shp1(int S) { for(int i = 1; i <= n; i++) { d1[i] = inf; } d1[S] = 0; queue<int> q; q.push(S); while(q.size()) { int v = q.front(); q.pop(); if(nxl[v] != 0 && d1[nxl[v]] == inf) { d1[nxl[v]] = d1[v]+1; q.push(nxl[v]); } if(nxr[v] != 0 && d1[nxr[v]] == inf) { d1[nxr[v]] = d1[v]+1; q.push(nxr[v]); } } } void shp2(int S) { for(int i = 1; i <= n; i++) { d2[i] = inf; } d2[S] = 0; queue<int> q; q.push(S); while(q.size()) { int v = q.front(); q.pop(); if(nxl[v] != 0 && d2[nxl[v]] == inf) { d2[nxl[v]] = d2[v]+1; q.push(nxl[v]); } if(nxr[v] != 0 && d2[nxr[v]] == inf) { d2[nxr[v]] = d2[v]+1; q.push(nxr[v]); } } } void solve() { cin >> n >> k >> q; for(int i = 1; i <= n; i++) { cin >> a[i]; } //find nxl stack<pair<int,int>> st; for(int i = 1; i <= n; i++) { while(st.size() && st.top().fr < a[i]) { st.pop(); } if(st.size() != 0) { nxl[i] = st.top().sc; } st.push(mp(a[i],i)); } while(st.size()) st.pop(); for(int i = n; i >= 1; i--) { while(st.size() && st.top().fr < a[i]) { st.pop(); } if(st.size() != 0) { nxr[i] = st.top().sc; } st.push(mp(a[i],i)); } while(q--) { int u,v; cin >> u >> v; shp1(u); shp2(v); int ans = inf; for(int i = 1; i <= n; i++) { ans = min(ans, d1[i]+d2[i]-1); } cout << ans << endl; } } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); // freopen("in.in", "r", stdin); // freopen("out.out", "w", stdout); int tt = 1; // cin >> tt; while(tt--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...