Submission details
Task:Company Queries II
Sender:francden
Submission time:2025-10-13 15:15:19 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.07 sdetails
#20.07 sdetails
#30.07 sdetails
#40.04 sdetails
#50.07 sdetails
#60.79 sdetails
#70.59 sdetails
#80.92 sdetails
#90.77 sdetails
#100.76 sdetails
#110.07 sdetails
#120.77 sdetails

Code

import sys
sys.setrecursionlimit(100000)
n,q = [int(x) for x in input().split()]

ancestors = [int(x) for x in input().split()]

reverse_ancestors = [[]]

for i in range(n):
    reverse_ancestors.append([])

for index,value in enumerate(ancestors):
    reverse_ancestors[value].append(index+2) 

depth = [0,1]
for i in range(2,n+1):
    depth.append(depth[ancestors[i-2]]+1)

L=[]
nodes = []
def search(node):
    children = reverse_ancestors[node]

    L.append((depth[node],node))
    nodes.append(node)

    for child in children:
        search(child)
        L.append((depth[node],node))
        nodes.append(node)
search(1)
indexs=[0 for i in range(n+1)]
for index,node in enumerate(nodes):
    if indexs[node]==0:
        indexs[node]=index

min_list = []
min_list.append(L)

j = 1
power = 2

while power <= n :
    i=0
    curr_list = []
    while i + power - 1 < 2*n-1:
        curr_list.append(min(min_list[-1][i],min_list[-1][i+power-j],key= lambda x : x[0]))
        i+=1
    j=power
    power *=2
    if curr_list :
        min_list.append(curr_list)

def mini(a,b):
    if a == b :
        return L[a]
    k = b-a+1

    # find the biggest power of 2 below k
    power = 1
    i = 0
    while k > power :
        power *=2
        i+=1
   
    if k == power:
        return(min_list[i][a])
    
    return min(min_list[i-1][a],min_list[i-1][b-power//2],key= lambda x : x[0])

for querie in range(q):
    a,b = [int(x) for x in input().split()]
    print(mini(indexs[a],indexs[b])[1])

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
6
8
3
2
8
...

Error:
Traceback (most recent call last):
  File "input/code.py", line 73, in <module>
    print(mini(indexs[a],indexs[b])[1])
  File "input/code.py", line 67, in mini
    return(min_list[i][a])
IndexError: list index out of range

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

Error:
Traceback (most recent call last):
  File "input/code.py", line 73, in <module>
    print(mini(indexs[a],indexs[b])[1])
  File "input/code.py", line 69, in mini
    return min(min_list[i-1][a],min_list[i-1][b-power//2],key= lambda x : x[0])
IndexError: list index out of range

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

Error:
Traceback (most recent call last):
  File "input/code.py", line 73, in <module>
    print(mini(indexs[a],indexs[b])[1])
  File "input/code.py", line 69, in mini
    return min(min_list[i-1][a],min_list[i-1][b-power//2],key= lambda x : x[0])
IndexError: list index out of range

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

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
1

Error:
Traceback (most recent call last):
  File "input/code.py", line 73, in <module>
    print(mini(indexs[a],indexs[b])[1])
  File "input/code.py", line 69, in mini
    return min(min_list[i-1][a],min_list[i-1][b-power//2],key= lambda x : x[0])
IndexError: list index out of range

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 31, in <module>
    search(1)
  File "input/code.py", line 28, in search
    search(child)
  File "input/code.py", line 28, in search
    search(child)
  File "input/code.py", line 28, in search
    search(child)
  [Previous line repeated 158551 more times]
  File "input/code.py", line 22, in search
    children = reverse_ancestors[node]
RecursionError: maximum recursion depth exceeded

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
1

Error:
Traceback (most recent call last):
  File "input/code.py", line 73, in <module>
    print(mini(indexs[a],indexs[b])[1])
  File "input/code.py", line 69, in mini
    return min(min_list[i-1][a],min_list[i-1][b-power//2],key= lambda x : x[0])
IndexError: list index out of range

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 73, in <module>
    print(mini(indexs[a],indexs[b])[1])
  File "input/code.py", line 69, in mini
    return min(min_list[i-1][a],min_list[i-1][b-power//2],key= lambda x : x[0])
IndexError: list index out of range

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
633

Error:
Traceback (most recent call last):
  File "input/code.py", line 73, in <module>
    print(mini(indexs[a],indexs[b])[1])
  File "input/code.py", line 69, in mini
    return min(min_list[i-1][a],min_list[i-1][b-power//2],key= lambda x : x[0])
IndexError: list index out of range

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

Error:
Traceback (most recent call last):
  File "input/code.py", line 73, in <module>
    print(mini(indexs[a],indexs[b])[1])
  File "input/code.py", line 69, in mini
    return min(min_list[i-1][a],min_list[i-1][b-power//2],key= lambda x : x[0])
IndexError: list index out of range

Test 11

Verdict:

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

correct output
1
1
1
2

user output
1

Error:
Traceback (most recent call last):
  File "input/code.py", line 73, in <module>
    print(mini(indexs[a],indexs[b])[1])
  File "input/code.py", line 69, in mini
    return min(min_list[i-1][a],min_list[i-1][b-power//2],key= lambda x : x[0])
IndexError: list index out of range

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
27468

Error:
Traceback (most recent call last):
  File "input/code.py", line 73, in <module>
    print(mini(indexs[a],indexs[b])[1])
  File "input/code.py", line 69, in mini
    return min(min_list[i-1][a],min_list[i-1][b-power//2],key= lambda x : x[0])
IndexError: list index out of range