Submission #792946

#TimeUsernameProblemLanguageResultExecution timeMemory
792946hugo_pmRailway Trip (JOI17_railway_trip)C++17
100 / 100
302 ms30004 KiB
#include <bits/stdc++.h> #define int long long using namespace std; #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define rep(i, a, b) for(int i = (a); i < (b); i++) #define sz(v) ((int)((v).size())) template<typename T> void chmax(T &x, const T &v) { if (x < v) x = v; } template<typename T> void chmin(T &x, const T &v) { if (x > v) x = v; } using pii = pair<int, int>; using vi = vector<int>; string to_string(string s) { return s; } template <typename T> string to_string(T v) { bool first = true; string res = "["; for (const auto &x : v) { if (!first) res += ", "; first = false; res += to_string(x); } res += "]"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } void dbg_out() { cout << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << to_string(H); dbg_out(T...); } #ifdef DEBUG #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif const int MAX_N = 1e5, LG = 17; int level[MAX_N]; int lft[LG][MAX_N], rgt[LG][MAX_N]; pii f(int L, int R, int bit) { return make_pair( min(lft[bit][L], lft[bit][R]), max(rgt[bit][L], rgt[bit][R]) ); } signed main() { ios::sync_with_stdio(false); cin.tie(0); int N, K, Q; cin >> N >> K >> Q; rep(i, 0, N) cin >> level[i]; lft[0][0] = 0; rep(i, 1, N) { int j = i-1; while (level[j] < level[i]) { j = lft[0][j]; } lft[0][i] = j; } rgt[0][N-1] = N-1; for (int i = N-2; i >= 0; --i) { int j = i+1; while (level[j] < level[i]) { j = rgt[0][j]; } rgt[0][i] = j; } rep(bit, 0, LG-1) { rep(i, 0, N) { tie(lft[bit+1][i], rgt[bit+1][i]) = f(lft[bit][i], rgt[bit][i], bit); } } rep(iReq, 0, Q) { int A, B; cin >> A >> B; --A, --B; if (A > B) swap(A, B); int jumps = 0; int LA = A, RA = A; for (int bit = LG-1; bit >= 0; --bit) { auto [newLA, newRA] = f(LA, RA, bit); if (newRA < B) { tie(LA, RA) = tie(newLA, newRA); jumps += (1 << bit); } } int LB = B, RB = B; for (int bit = LG-1; bit >= 0; --bit) { auto [newLB, newRB] = f(LB, RB, bit); if (newLB > RA) { tie(LB, RB) = tie(newLB, newRB); jumps += (1 << bit); } } if (RA != LB) jumps++; cout << jumps - 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...