Cod sursa(job #3121421)

Utilizator HandoMihnea-Vicentiu Hando Data 12 aprilie 2023 17:52:53
Problema Lowest Common Ancestor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.47 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ar array
#define vt vector
#define pq priority_queue
#define pu push
#define pub push_back
#define em emplace
#define emb emplace_back
#define mt make_tuple

#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define allp(x, l, r) x.begin() + l, x.begin() + r
#define len(x) (int)x.size()
#define uniq(x) unique(all(x)), x.end()

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <class T, size_t N>
void re(array <T, N>& x);
template <class T> 
void re(vt <T>& x);

template <class T> 
void re(T& x) {
    cin >> x;
}

template <class T, class... M> 
void re(T& x, M&... args) {
    re(x); re(args...);
}

template <class T> 
void re(vt <T>& x) {
    for(auto& it : x)
        re(it);
}

template <class T, size_t N>
void re(array <T, N>& x) {
    for(auto& it : x)
        re(it);
}

template <class T, size_t N>
void wr(array <T, N> x);
template <class T> 
void wr(vt <T> x);

template <class T> 
void wr(T x) {
    cout << x;
}

template <class T, class ...M>  void wr(T x, M... args) {
    wr(x), wr(args...);
}

template <class T> 
void wr(vt <T> x) {
    for(auto it : x)
        wr(it, ' ');
}

template <class T, size_t N>
void wr(array <T, N> x) {
    for(auto it : x)
        wr(it, ' ');
}

template<class T, class... M>
auto mvt(size_t n, M&&... args) {
    if constexpr(sizeof...(args) == 1)
        return vector<T>(n, args...);
    else
        return vector(n, mvt<T>(args...));
}

void set_fixed(int p = 0) {
    cout << fixed << setprecision(p);
}

void set_scientific() {
    cout << scientific;
}

inline void Open(const string Name) {
    #ifndef ONLINE_JUDGE
        (void)!freopen((Name + ".in").c_str(), "r", stdin);
        (void)!freopen((Name + ".out").c_str(), "w", stdout);
    #endif
}

const int K = 18;

vt <vt <int>> adj;
vt <int> euler, first, dst;

void dfs(int u, int depth = 0) {
    dst[u] = depth;
    first[u] = len(euler);
    euler.emb(u);
    for (int v : adj[u]) {
        dfs(v, depth + 1);
        euler.emb(u);
    }
}

void solve() {
    int n, q; re(n, q);
    adj = mvt<int>(n, 0, 0);
    first = dst = mvt <int>(n, -1);
    for (int v = 1; v < n; ++v) {
        int u; re(u);
        --u;
        adj[u].emb(v);
    }

    dfs(0);

    vt <vt <int>> st(K, vt <int>(len(euler)));
    auto get_min = [&](int i, int j) {
        if (dst[st[i][j]] < dst[st[i][j + (1 << i)]]) {
            return st[i][j];
        }
        return st[i][j + (1 << i)];
    };

    st[0] = euler;
    for (int i = 1; i < K; ++i)
        for (int j = 0; j + (1 << (i - 1)) < len(euler); ++j) {
            st[i][j] = get_min(i - 1, j);
        }

    while (q--) {
        int u, v; re(u, v);
        --u, --v;
        int l = first[u], r = first[v];
        if (l > r) {
            swap(l, r);
        }

        int k = 31 - __builtin_clz(r - l + 1), ans = -1;
        if (dst[st[k][l]] < dst[st[k][r - (1 << k) + 1]]) {
            ans = st[k][l];
        } else {
            ans = st[k][r - (1 << k) + 1];
        }
        wr(ans + 1, '\n');
    }

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    Open("lca");

    int t = 1;
    for(;t;t--) {
        solve();
    }
    
    return 0;
}