Submission details
Task:Company Queries II
Sender:ariadna.roga
Submission time:2025-10-12 23:32:36 +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.73 sdetails
#7ACCEPTED0.67 sdetails
#8ACCEPTED0.72 sdetails
#9ACCEPTED0.70 sdetails
#10ACCEPTED0.70 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.72 sdetails

Code

#include <iostream>
#include <vector>

using namespace std;

void euler_tour_dfs(
    const vector<vector<int>>& children, 
    int node, 
    int depth, 
    vector<int>& euler_tour, 
    vector<int>& depth_array, 
    vector<int>& first_occurrence) 
{
    first_occurrence[node] = euler_tour.size();
    euler_tour.push_back(node);
    depth_array.push_back(depth);
    
    for (int child : children[node]) {
        euler_tour_dfs(children, child, depth + 1, euler_tour, depth_array, first_occurrence);
        euler_tour.push_back(node);
        depth_array.push_back(depth);
    }
}

int main() {
    
    int n, q;
    cin >> n >> q;
    
    // Build tree 
    vector<vector<int>> children(n + 1);
    for (int i = 2; i <= n; i++) {
        int boss;
        cin >> boss;
        children[boss].push_back(i);
    }
    
    // Euler tour arrays
    vector<int> euler_tour;
    vector<int> depth_array;
    vector<int> first_occurrence(n + 1);
    
    // DFS to build Euler tour
    euler_tour_dfs(children, 1, 1, euler_tour, depth_array, first_occurrence);
    
    // Build sparse table 
    int m = euler_tour.size();
    int log_m = 0;
    while ((1 << log_m) <= m) log_m++;
    vector<vector<int>> sparse_table(m, vector<int>(log_m));
    
    // Initialize sparse table
    for (int i = 0; i < m; i++) {
        sparse_table[i][0] = i;
    }
    
    // Complete sparse table
    for (int j = 1; j < log_m; j++) {
        for (int i = 0; i + (1 << j) <= m; i++) {
            int left = sparse_table[i][j - 1];
            int right = sparse_table[i + (1 << (j - 1))][j - 1];
            if (depth_array[left] <= depth_array[right]) {
                sparse_table[i][j] = left;
            } else {
                sparse_table[i][j] = right;
            }
        }
    }

    for (int i = 0; i < q; ++i) {
        int a, b;
        cin >> a >> b;
        int l = first_occurrence[a];
        int r = first_occurrence[b];
        if (l > r) swap(l, r);
        
        int length = r - l + 1;
        int k = 0;
        while ((1 << (k + 1)) <= length) k++;
        
        int left_idx = sparse_table[l][k];
        int right_idx = sparse_table[r - (1 << k) + 1][k];
        
        if (depth_array[left_idx] <= depth_array[right_idx]) {
            cout << euler_tour[left_idx] << endl;
        } else {
            cout << euler_tour[right_idx] << endl;
        }
    }
}

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