Submission #241927

#TimeUsernameProblemLanguageResultExecution timeMemory
241927osaaateiasavtnlRailway Trip (JOI17_railway_trip)C++17
0 / 100
2092 ms8696 KiB
#include<bits/stdc++.h> using namespace std; #define ii pair <int, int> #define app push_back #define all(a) a.begin(), a.end() #define bp __builtin_popcountll #define ll long long #define mp make_pair #define f first #define s second #define Time (double)clock()/CLOCKS_PER_SEC const int N = 1e5+7; int a[N]; vector <int> g[N]; int d[N]; int dist(int S, int T) { for (int i = 0; i < N; ++i) d[i] = N; d[S] = 0; queue <int> q; q.push(S); while (q.size()) { int u = q.front(); q.pop(); if (u == T) return d[u]; for (int v : g[u]) if (d[u]+1 < d[v]) { d[v] = d[u]+1; q.push(v); } } cout << "LMAO" << endl; exit(1); } signed main() { #ifdef HOME freopen("input.txt", "r", stdin); #else #define endl '\n' ios_base::sync_with_stdio(0); cin.tie(0); #endif int n, k, q; cin >> n >> k >> q; for (int i = 1; i <= n; ++i) cin >> a[i]; while (q--) { int l, r; cin >> l >> r; if (r < l) swap(l, r); if (l == r) { cout << 0 << endl; exit(0); } vector <int> p = {l, r}; { int mx = a[l]; for (int i = l-1; i; --i) { if (a[i] >= mx) { p.app(i); mx = a[i]; } } } if (l + 1 < r) { int pos = l+1; for (int i = l + 1; i < r; ++i) if (a[i] > a[pos]) pos = i; int mx = a[l]; for (int i = l + 1; i < pos; ++i) { if (a[i] >= mx) { p.app(i); mx = a[i]; } } mx = a[r]; for (int i = r - 1; i > pos; --i) { if (a[i] >= mx) { p.app(i); mx = a[i]; } } p.app(pos); } { int mx = a[r]; for (int i = r + 1; i <= n; ++i) { if (a[i] >= mx) { p.app(i); mx = a[i]; } } } sort(all(p)); /* cout << "p : "; for (auto e : p) cout << e << ' '; cout << endl; exit(0); */ int u = -1, v = -1; for (int i = 0; i < p.size(); ++i) { if (p[i] == l) { u = i; } if (p[i] == r) { v = i; } } for (int i = 0; i < N; ++i) g[i].clear(); for (int i = 0; i < p.size(); ++i) { for (int j = i - 1; j >= 0; --j) { if (a[p[j]] >= a[p[i]]) { g[i].app(j); g[j].app(i); break; } } for (int j = i + 1; j < p.size(); ++j) { if (a[p[j]] >= a[p[i]]) { g[i].app(j); g[j].app(i); break; } } } cout << dist(u, v) - 1 << endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...