Submission details
Task:Company Queries II
Sender:aalto26gh_003
Submission time:2026-10-18 19:05:23 +0300
Language:C++ (C++20)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#60.35 sdetails
#70.17 sdetails
#80.22 sdetails
#90.40 sdetails
#100.33 sdetails
#11ACCEPTED0.00 sdetails
#120.43 sdetails

Code

#include <iostream>
#include <vector>

using namespace std;

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

    int n, q;
    cin >> n >> q;
    vector<int> bosses(n - 1);
    for (int i = 0; i < n - 1; i++)
        cin >> bosses[i];

    int maxk = 0;
    while ((1 << maxk) <= n)
        maxk++;
    vector<vector<int>> pre(n, vector<int>(maxk, 0));

    int k = 0;
    while (k <= maxk) {
        for (int i = 2; i < n + 1; i++) {
            if (k == 0) {
                pre[i - 1][k] = bosses[i - 2];
            } else {
                int prev = pre[i - 1][k - 1];
                pre[i - 1][k] = prev == 0 ? 0 : pre[prev - 1][k - 1];
            }
        }
        k++;
    }

    vector<int> depth(n + 1, 0);
    for (int i = 2; i < n + 1; i++)
        depth[i] = depth[bosses[i - 2]] + 1;

    int a, b;
    for (int dc = 0; dc < q; dc++) {
        cin >> a >> b;

        if (depth[a] < depth[b]) {
            int tmp = a;
            a = b;
            b = tmp;
        }
        int diff = depth[a] - depth[b];

        int j = 0;
        while (diff > 0) {
            if (diff % 2 != 0)
                a = pre[a - 1][j];

            diff /= 2;
            j++;
        }

        if (a != b) {
            for (int k1 = maxk; k1 >= 0; k1--) {
                if (pre[a - 1][k1] != pre[b - 1][k1]) {
                    a = pre[a - 1][k1];
                    b = pre[b - 1][k1];
                }
            }
            a = pre[a - 1][0];
        }
        cout << a << "\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:

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

Error:
free(): invalid pointer

Test 7

Verdict:

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

Error:
free(): invalid pointer

Test 8

Verdict:

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

Error:
free(): invalid pointer

Test 9

Verdict:

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

Error:
free(): invalid pointer

Test 10

Verdict:

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

Error:
free(): invalid pointer

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:

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

Error:
free(): invalid pointer