Submission details
Task:Company Queries II
Sender:aalto26gh_006
Submission time:2026-10-20 13:16:59 +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
#6ACCEPTED0.68 sdetails
#7ACCEPTED0.41 sdetails
#80.51 sdetails
#9ACCEPTED0.69 sdetails
#100.62 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.75 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:69:9: warning: unused variable 'max_k' [-Wunused-variable]
   69 |     int max_k = pow(2, max_k_power);
      |         ^~~~~

Code

#include <iostream>
#include <vector>
#include <cmath>
#include <bits/stdc++.h>
#include <string>

using namespace std;

int succ(int node, int depth, vector<pair<int, int>> & tree)
{
    if(depth == 1)
    {
        return tree[node].first;
    }
    else
    {
        return succ(succ(node, depth/2, tree), depth/2, tree);
    }
}

void preproc(int max_k_power, vector<vector<int>> & ancestors, vector<pair<int, int>> & tree, vector<int> & depth)
{
    int n_employees = tree.size() - 1;
    for(int i = 1; i <= max_k_power; ++i)
    {
        for(int e = 1; e <= n_employees; ++e)
        {
            ancestors[e][i] = ancestors[ancestors[e][i-1]][i-1];
        }
    }
}

int calc_LCA(int & node1, int & node2, vector<vector<int>> & ancestors, vector<pair<int, int>> & tree, vector<int> & depth)
{
    if(node1 == node2) return node1;
    if(tree[node1].second < tree[node2].second)
    {
        swap(node1, node2);
    }
    int n_employees = tree.size() - 1;
    int current_diff = tree[node1].second - tree[node2].second;
    for(int i = ceil(log2(n_employees)); i >= 0; --i)
    {
        if(pow(2, i) <= current_diff)
        {
            node1 = ancestors[node1][i];
            current_diff = current_diff - pow(2,i);
        }
    }
    if(node1 == node2) return node1;
    for(int i = ceil(log2(n_employees)); i >= 0; --i)
    {
        if(ancestors[node1][i] != ancestors[node2][i])
        {
            node1 = ancestors[node1][i];
            node2 = ancestors[node2][i];
        }
    }
    return ancestors[node1][0];
    
}

int main()
{
    long long n_employees;
    long long n_queries;
    cin >> n_employees >> n_queries;
    int max_k_power = floor(log2(n_employees));
    int max_k = pow(2, max_k_power);

    vector<vector<int>> ancestors(n_employees + 1, vector<int>(max_k_power + 1, 0));
    vector<pair<int, int>> tree(n_employees + 1);
    vector<int> depth(n_employees + 1, -1);
    depth[1] = 0;
    tree[1] = {0, 0};
    for(int i = 2; i <= n_employees; ++i)
    {
        int a;
        cin >> a;
        tree[i] = {a, tree[a].second + 1};
        ancestors[i][0] = a;
    }

    preproc(max_k_power, ancestors, tree, depth);

    string bin = "";
    for(int i = 0; i < n_queries; ++i)
    {
        int a, b;
        cin >> a >> b;
        bin.append(to_string(calc_LCA(a, b, ancestors, tree, depth)) + '\n');
    }
    cout << bin;

}

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:

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

Feedback: Incorrect character on line 210 col 1: expected "10", got "0"

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:

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

Feedback: Incorrect character on line 66585 col 1: expected "11199", got "0"

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