Submission #1303338

#TimeUsernameProblemLanguageResultExecution timeMemory
1303338KietJRailway Trip (JOI17_railway_trip)C++20
20 / 100
2095 ms8132 KiB
#include <bits/stdc++.h> using namespace std; #define f first #define s second #define pb(x) push_back(x) #define sz(x) (int) x.size() #define all(x) x.begin(), x.end() #define un(x) (x).erase(unique(all(x)), x.end()) typedef long long ll; typedef double db; typedef pair <int, int> ii; typedef pair <ll, ll> pll; typedef vector <ll> V; const int N = 2e5 + 10; // template <class T1, class Gz> void add(T1 &x, Gz y) { x += y; if (x < 0) x += mod; if (x >= mod) x -= mod; } template <class T1, class Gz> bool maximize(T1 &x, Gz y) { if (x < y) { x = y; return true; } return false; } template <class T1, class Gz> bool minimize(T1 &x, Gz y) { if (x > y) { x = y; return true; } return false; } int n, k, q; int a[N]; vector <int> ed[N]; int bfs(int st, int en) { queue <int> q; vector <int> dis(n + 1, -1); dis[st] = 0; q.push(st); while (sz(q)) { int u = q.front(); q.pop(); for(auto v: ed[u]) { if (dis[v] == -1) { dis[v] = dis[u] + 1; q.push(v); } } } return dis[en]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> q; for(int i = 1; i <= n; i++) { cin >> a[i]; } stack <int> st; for(int u = 1; u <= n; u++) { while (sz(st) && a[st.top()] < a[u]) st.pop(); if (sz(st)) { ed[st.top()].push_back(u); ed[u].push_back(st.top()); } st.push(u); } while (sz(st)) st.pop(); for(int u = n; u >= 1; u--) { while (sz(st) && a[st.top()] < a[u]) st.pop(); if (sz(st)) { ed[st.top()].push_back(u); ed[u].push_back(st.top()); } st.push(u); } for(int i = 1; i <= q; i++) { int u, v; cin >> u >> v; cout << bfs(u, v) - 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...