Submission details
Task:Company Queries II
Sender:aalto26gh_035
Submission time:2026-10-15 15:06: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.49 sdetails
#7ACCEPTED0.46 sdetails
#8ACCEPTED0.50 sdetails
#9ACCEPTED0.48 sdetails
#10ACCEPTED0.48 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.49 sdetails

Code

#include <bits/stdc++.h>

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

    int n, q;
    std::cin >> n >> q;

    std::vector<std::vector<int>> children(n + 1);
    for (int i = 2; i <= n; i++) {
        int boss;
        std::cin >> boss;
        children[boss].emplace_back(i);
    }

    std::vector<int> depth(n + 1);
    std::vector<int> first(n + 1);
    std::vector<int> order;

    std::stack<std::pair<int, int>> stack;
    stack.emplace(1, 0);

    depth[1] = 1;
    first[1] = 0;
    order.emplace_back(1);

    while (!stack.empty()) {
        const int node = stack.top().first;
        int &nextChild = stack.top().second;

        if (nextChild < static_cast<int>(children[node].size())) {
            const int child = children[node][nextChild++];
            depth[child] = depth[node] + 1;

            first[child] = static_cast<int>(order.size());
            order.emplace_back(child);
            stack.emplace(child, 0);
        } else {
            stack.pop();

            if (!stack.empty()) {
                order.emplace_back(stack.top().first);
            }
        }
    }

    const int m = static_cast<int>(order.size());

    std::vector log2(m + 1, 0);
    for (int i = 2; i <= m; i++) {
        log2[i] = log2[i / 2] + 1;
    }

    const int powers = log2[m] + 1;
    std::vector table(powers, std::vector<int>(m));

    for (int a = 0; a < m; a++) {
        table[0][a] = a;
    }

    for (int k = 1; k < powers; k++) {
        const int length = 1 << k;

        for (int a = 0; a + length <= m; a++) {
            const int b = a + length - 1;
            const int w = (b - a + 1) / 2;

            const int leftMin = table[k - 1][a];
            const int rightMin = table[k - 1][a + w];
            table[k][a] = (depth[order[leftMin]] < depth[order[rightMin]]) ? leftMin : rightMin;
        }
    }

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

        int left = first[a];
        int right = first[b];
        if (left > right) {
            std::swap(left, right);
        }

        const int k = log2[right - left + 1];

        const int leftMin = table[k][left];
        const int rightMin = table[k][right - (1 << k) + 1];

        if (depth[order[leftMin]] <= depth[order[rightMin]]) {
            std::cout << order[leftMin] << std::endl;
        } else {
            std::cout << order[rightMin] << std::endl;
        }
    }

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