Submission details
Task:Company Queries II
Sender:aalto26gh_031
Submission time:2026-10-19 15:41:38 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#1--details
#2--details
#3--details
#4--details
#5--details
#6--details
#7--details
#8--details
#9--details
#10--details
#11--details
#12--details

Code

import sys
inp = sys.stdin.read().split()
n = int(inp[0])
q=int(inp[1])


maxpower = 18 #set maxpower to 18 for preprocessing since in the worst case n = 200 000 which is 2^17.., 
up = [[0]* maxpower for _ in range(n+1)] #stores for each employee, his superior at leevel k if there is one
depth = [0]*(n+1)

tracker = 2
for i in range(2,n+1):
    boss = int(inp[i])
    up[i][0] = boss 
    depth[i] += depth[boss] +1    
    tracker+=1


for j in range(1,maxpower):
    for i in range(1,n+1):        
        #i represents the employee
        #j how far up we're looking
        up[i][j] = up[up[i][j-1]][j-1]

#now we need to find the lowest common ancestors for each query



def get_lca(a,b):
    if depth[a] < depth[b]:
        a, b = b, a

    #once we have the deeper node, we gotta make sure they're on the same level
    diff = depth[a] - depth[b]

    for j in range(maxpower):
            #for each jump (power of 2) , shift the bits to check if the bit is activated. if it is we jump by that much
            #goal here is to bring a up to the same level as b
            while (diff >> j) & 1:
                a = up[a][j]
    if a == b:
         return a
    for j in range(maxpower-1,-1,-1):
         if up[a][j] != up[b][j] :
              a = up[a][j]
              b = up[b][j]

    return up[a][0]

    #now we're gonna make both nnodes climb until we found a 

output = []
for _ in range(q):
    a = int(inp[tracker])
    b = int(inp[tracker+1])
    tracker+=2
    output.append(str(get_lca(a,b)))
sys.stdout.write('\n'.join(output) + '\n')

Test details

Test 1

Verdict:

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
(empty)

Test 2

Verdict:

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
(empty)

Test 3

Verdict:

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
(empty)

Test 4

Verdict:

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
(empty)

Test 5

Verdict:

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
(empty)

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
(empty)

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
(empty)

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
(empty)

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
(empty)

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
(empty)

Test 11

Verdict:

input
2 4
1
1 1
1 2
2 1
...

correct output
1
1
1
2

user output
(empty)

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
(empty)