Submission #1099006

#TimeUsernameProblemLanguageResultExecution timeMemory
1099006thangdz2k7Railway Trip (JOI17_railway_trip)C++17
100 / 100
94 ms18768 KiB
// 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)

railway_trip.cpp:19:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   19 | void maxl(auto &a, auto b) {a = max(a, b);}
      |           ^~~~
railway_trip.cpp:19:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   19 | void maxl(auto &a, auto b) {a = max(a, b);}
      |                    ^~~~
railway_trip.cpp:20:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   20 | void minl(auto &a, auto b) {a = min(a, b);}
      |           ^~~~
railway_trip.cpp:20:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   20 | void minl(auto &a, auto b) {a = min(a, b);}
      |                    ^~~~
railway_trip.cpp: In function 'int main()':
railway_trip.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         freopen("pqh.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
railway_trip.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         freopen("pqh.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...