Submission details
Task:Company Queries II
Sender:azeaus1
Submission time:2025-10-20 11:51:55 +0300
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.22 sdetails
#7ACCEPTED0.19 sdetails
#8ACCEPTED0.23 sdetails
#9ACCEPTED0.21 sdetails
#10ACCEPTED0.20 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.22 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:41:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for (int i = 0; i < visited_nbr.size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~~~~~
input/code.cpp:47:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |         for (int b = 0; b + length <= visited_nbr.size(); ++b) {
      |                         ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~

Code

#include <bits/stdc++.h>
using namespace std;

int n, q;
vector<vector<int>> tree;
vector<int> visited_nbr, visited_depth, first;
vector<vector<pair<int,int>>> min_queries;

void dfs(int x, int d) {
    first[x] = visited_nbr.size();
    visited_nbr.push_back(x);
    visited_depth.push_back(d);
    for (int node : tree[x]) {
        dfs(node, d+1);
        visited_nbr.push_back(x);
        visited_depth.push_back(d);
    }
}

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

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

    tree.assign(n, {});
    for (int i = 1; i < n; i++)
        tree[boss[i-1]-1].push_back(i);

    visited_nbr.clear();
    visited_depth.clear();
    first.assign(n, 0);
    dfs(0, 0);

    int m = floor(log2(visited_nbr.size()))+1;

    min_queries.assign(m, vector<pair<int,int>>(visited_nbr.size()));

    for (int i = 0; i < visited_nbr.size(); ++i)
        min_queries[0][i] = {visited_depth[i], i};

    for (int a = 1; a < m; ++a) {
        int length = 1 << a;
        int half = length >> 1;
        for (int b = 0; b + length <= visited_nbr.size(); ++b) {
            auto left = min_queries[a - 1][b];
            auto right = min_queries[a - 1][b + half];
            min_queries[a][b] = (left.first <= right.first) ? left : right;
        }
    }

    auto compute_minimum = [&](int a, int b) -> int {
        int length = b-a+1;
        int k = 31-__builtin_clz(length);
        auto left = min_queries[k][a];
        auto right = min_queries[k][b-(1 << k)+1];
        auto res = (left.first <= right.first) ? left : right;
        return visited_nbr[res.second];
    };

    while (q--) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        int idxA = first[a];
        int idxB = first[b];
        if (idxA > idxB) swap(idxA, idxB);
        cout << compute_minimum(idxA, idxB) + 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
...