Submission #1322717

#TimeUsernameProblemLanguageResultExecution timeMemory
1322717TymondRailway Trip (JOI17_railway_trip)C++20
100 / 100
148 ms27040 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

struct pair_hash{
  size_t operator()(const pair<int,int>&x)const{
    return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
  }
};

const int INF = 1e9 + 7;
const int MAXN = 1e5 + 7;
const int MAXK = 18;
int A[MAXN];
int mx[MAXN][MAXK];
pii jump[MAXN][MAXK];
int id[MAXN];
int lg[MAXN];
int n, K, q;

void preprocessing(){
    lg[1] = 0;
    for(int i = 2; i < MAXN; i++){
        lg[i] = lg[i / 2] + 1;
    }

    map<int, int> cnt;
    for(int i = 1; i <= n; i++){
        id[i] = cnt[A[i]]++;
        jump[i][0] = mp(i, i);
        mx[i][0] = A[i];
    }

    for(int j = 1; j < MAXK; j++){
        for(int i = 1; i <= n; i++){
            mx[i][j] = max(mx[i][j - 1], mx[min(n, i + (1 << (j - 1)))][j - 1]);
        }
    }

    vi vec = {};
    for(int i = 1; i <= n; i++){
        while(sz(vec) && A[i] > A[vec.back()]){
            vec.pop_back();
        }

        if(sz(vec)){
            jump[i][0].fi = vec.back();
        }
        vec.pb(i);
    }
    vec = {};
    for(int i = n; i >= 1; i--){
        while(sz(vec) && A[i] > A[vec.back()]){
            vec.pop_back();
        }

        if(sz(vec)){
            jump[i][0].se = vec.back();
        }
        vec.pb(i);
    }

    for(int j = 1; j < MAXK; j++){
        for(int i = 1; i <= n; i++){
            int x = jump[i][j - 1].fi;
            int y = jump[i][j - 1].se;
            jump[i][j] = mp(min(jump[x][j - 1].fi, jump[y][j - 1].fi), max(jump[x][j - 1].se, jump[y][j - 1].se));
        }
    }
}

int query(int l, int p){
    return max(mx[l][lg[p - l + 1]], mx[p - (1 << lg[p - l + 1]) + 1][lg[p - l + 1]]);
}

pair<int, pii> Jump(pii x, int val){
    int res = 0;
    for(int j = MAXK - 1; j >= 0; j--){
        int a = min(jump[x.fi][j].fi, jump[x.se][j].fi);
        int b = max(jump[x.fi][j].se, jump[x.se][j].se);
        if(max(A[a], A[b]) < val){
            x = mp(a, b);
            res += (1 << j);
        }
    }

    if(max(A[x.fi], A[x.se]) < val){
        int j = 0;
        int a = min(jump[x.fi][j].fi, jump[x.se][j].fi);
        int b = max(jump[x.fi][j].se, jump[x.se][j].se);
        res++;
        x = mp(a, b);
    }
    return mp(res, x);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n >> K >> q;
    for(int i = 1; i <= n; i++){
        cin >> A[i];
    }

    preprocessing();
    while(q--){
        int a, b;
        cin >> a >> b;

        if(b < a){
            swap(a, b);
        }

        int m = query(a, b);
        int ans = INF;
        for(int j = 0; j <= 1; j++){
            //skaczesz do m + j
            pair<int, pii> x = Jump(mp(a, a), m + j);
            pair<int, pii> y = Jump(mp(b, b), m + j);

            if(!j){
                int p = -1;
                for(auto e : {x.se.se, x.se.fi}){
                    if(A[e] == m){
                        p = id[e];
                        break;
                    }
                }

                int d = INF;
                for(auto e : {y.se.fi, y.se.se}){
                    if(A[e] == m){
                        d = id[e];
                        break;
                    }
                }

                ans = x.fi + y.fi + d - p;
                continue;
            }

            int f = 1 - (x.se.fi == y.se.fi || x.se.se == y.se.fi || x.se.fi == y.se.se || x.se.se == y.se.se);
            ans = min(ans, x.fi + y.fi + f);
        }

        ans--;
        cout << ans << '\n';
    }

    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...