# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1099006 | thangdz2k7 | Railway Trip (JOI17_railway_trip) | C++17 | 94 ms | 18768 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author : thembululquaUwU
// 3.9.2024
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define endl '\n'
using namespace std;
using ll = long long;
using ii = pair <int, int>;
using vi = vector <int>;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int LG = 20;
void maxl(auto &a, auto b) {a = max(a, b);}
void minl(auto &a, auto b) {a = min(a, b);}
int n, k, q, a[N];
int l[N][LG], r[N][LG];
void solve(){
cin >> n >> k >> q;
for (int i = 1; i <= n; ++ i) cin >> a[i];
l[1][0] = 1;
for (int i = 2; i <= n; ++ i){
l[i][0] = i - 1;
while (a[i] > a[l[i][0]]) l[i][0] = l[l[i][0]][0];
}
r[n][0] = n;
for (int i = n - 1; i >= 1; -- i){
r[i][0] = i + 1;
while (a[i] > a[r[i][0]]) r[i][0] = r[r[i][0]][0];
}
for (int lg = 1; lg < LG; ++ lg){
for (int i = 1; i <= n; ++ i){
int L = l[i][lg - 1], R = r[i][lg - 1];
l[i][lg] = min(l[L][lg - 1], l[R][lg - 1]);
r[i][lg] = max(r[L][lg - 1], r[R][lg - 1]);
}
}
while (q --){
int u, v; cin >> u >> v;
if (u > v) swap(u, v); int ans = 0;
int L = u, R = u;
for (int lg = LG - 1; lg >= 0; -- lg){
if (max(r[L][lg], r[R][lg]) < v){
ans += (1 << lg);
tie(L, R) = make_pair(min(l[L][lg], l[R][lg]), max(r[L][lg], r[R][lg]));
}
}
u = R;
L = v, R = v;
for (int lg = LG - 1; lg >= 0; -- lg){
if (min(l[L][lg], l[R][lg]) > u){
ans += (1 << lg);
tie(L, R) = make_pair(min(l[L][lg], l[R][lg]), max(r[L][lg], r[R][lg]));
}
}
cout << ans << endl;
}
}
int main(){
if (fopen("pqh.inp", "r")){
freopen("pqh.inp", "r", stdin);
freopen("pqh.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1; // cin >> t;
while (t --) solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |