Submission details
Task:Company Queries II
Sender:aalto26gh_014
Submission time:2026-10-19 13:43:47 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.46 sdetails
#7ACCEPTED0.26 sdetails
#8ACCEPTED0.36 sdetails
#9ACCEPTED0.51 sdetails
#10ACCEPTED0.45 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.56 sdetails

Code

#include <vector>
#include <iostream>
#include <math.h>
using namespace std;

vector<vector<int>> ancestors;  // a[node][lb(x)]
vector<int> depth;
vector<bool> been;
vector<int> boss;
vector<vector<int>> children;


int ancestor(int node, int up) {
    for (int ind=0; ind<(int)ancestors[node].size(); ind++) {
        if ((1 << ind) & up) {
            node = ancestors[node][ind];
        }
    }
    return node;
}

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

    int n, q;
    cin >> n >> q;
    ancestors.resize(n);
    depth.resize(n);
    been.resize(n);
    boss.resize(n);
    children.resize(n);

    boss[0] = 0;
    depth[0] = 0;
    
    int a, b;
    for (int i=1; i<n; i++) {
        cin >> boss[i];
        boss[i]--;
        children[boss[i]].push_back(i);
    }

    vector<int> st;
    st.reserve(n);
    st.push_back(0);

    while (!st.empty()) {
        int v = st.back();
        st.pop_back();

        for (int child: children[v]) {
            depth[child] = depth[v] + 1;
            st.push_back(child);
        }
    }

    int logN = 1;
    while ((1LL << logN) <= n) logN++;
    
    // set ancestors
    for (int i=0; i<n; i++) { ancestors[i].push_back(boss[i]); }
    for (int i=0; i<logN; i++) {
        for (int j=0; j<n; j++) {
            ancestors[j].push_back(ancestors[ancestors[j][i]][i]);
        }
    }
    
    int da, db;
    for (int i=0; i<q; i++) {
        cin >> a >> b;
        a--; b--;
        da = depth[a];
        db = depth[b];
        if (da > db) swap(a, b);  // b is lower
        b = ancestor(b, abs(da - db));  // level

        if (a == b) {
            cout << a + 1 << "\n";
            continue;
        }

        for (int k=logN-1; k>=0; k--) {
            if (ancestors[a][k] != ancestors[b][k]) {
                a = ancestors[a][k];
                b = ancestors[b][k];
            }
        }
        cout << ancestor(a, 1) + 1 << "\n";
    }

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
10 10
1 2 3 4 5 6 7 8 9
6 9
8 10
10 3
...

correct output
6
8
3
1
8
...

user output
6
8
3
1
8
...

Test 2

Verdict: ACCEPTED

input
10 10
1 1 1 1 1 1 1 1 1
1 7
3 4
4 1
...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 3

Verdict: ACCEPTED

input
10 10
1 1 1 1 2 3 4 4 1
1 8
2 7
8 3
...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 4

Verdict: ACCEPTED

input
10 10
1 1 3 1 2 2 5 3 9
7 2
7 6
3 9
...

correct output
2
2
3
1
1
...

user output
2
2
3
1
1
...

Test 5

Verdict: ACCEPTED

input
10 10
1 2 3 2 5 3 2 2 4
6 1
1 3
1 9
...

correct output
1
1
1
2
2
...

user output
1
1
1
2
2
...

Test 6

Verdict: ACCEPTED

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
74862
8750
16237
72298
58111
...

user output
74862
8750
16237
72298
58111
...

Test 7

Verdict: ACCEPTED

input
200000 200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 8

Verdict: ACCEPTED

input
200000 200000
1 2 1 2 3 2 1 6 3 1 10 12 13 4...

correct output
1
2
2
2
1
...

user output
1
2
2
2
1
...

Test 9

Verdict: ACCEPTED

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
2796
633
633
151
2690
...

user output
2796
633
633
151
2690
...

Test 10

Verdict: ACCEPTED

input
200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
365
73
103
365
216
...

user output
365
73
103
365
216
...

Test 11

Verdict: ACCEPTED

input
2 4
1
1 1
1 2
2 1
...

correct output
1
1
1
2

user output
1
1
1
2

Test 12

Verdict: ACCEPTED

input
200000 200000
1 1 2 3 4 5 6 7 8 9 10 11 12 1...

correct output
27468
6353
27468
6353
6353
...

user output
27468
6353
27468
6353
6353
...