Submission details
Task:Company Queries II
Sender:luukwin
Submission time:2025-10-16 12:33:18 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.04 sdetails
#20.04 sdetails
#30.04 sdetails
#40.04 sdetails
#50.04 sdetails
#6--details
#7ACCEPTED0.76 sdetails
#8--details
#9--details
#10--details
#110.04 sdetails
#12--details

Code

import math

n, q = [int(x) for x in input().split()]

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

ancestors = [0 for _ in range(n)]
# Pre processing
for i in range(1, n):
    ancestors[i] = e[i-1]

preproccess = []
for j in range(1, n):
    if math.log2(j) % 1 == 0:
        bosses = []
        for i in range(n):
            k = j - 1
            boss = ancestors[i]
            while k != 0 and boss != 0:
                if math.log2(k) % 1 == 0 and j > i:
                    # print(i, j, k)
                    # print(preproccess)
                    boss = ancestors[preproccess[int(math.log2(k))][i] - 1]
                    # print(boss)
                    break
                boss = ancestors[boss-1]
                k -= 1
            bosses.append(boss)
        # print(j, bosses)
        preproccess.append(bosses)

output = []

def getAncestor(x, k):
    if math.log2(k) % 1 == 0: return preproccess[int(math.log2(k))][x-1]
    else:
        boss = ancestors[x-1]
        k -= 1
        if boss == 0: return boss
        while math.log2(k) % 1 != 0:
            boss = ancestors[boss - 1]
            if boss == 0: return boss
            k -= 1 
        return preproccess[int(math.log2(k))][boss-1]
    
# print("My", getAncestor(4, 1))
# Answers
for i in range(q):
    a, b = [int(x) for x in input().split()]
    bossa = []
    bossb = []
    for i in range(1, n):
        bossa.append(getAncestor(a, i))
        bossbset = getAncestor(b, i)
        # print(i, a, b, bossa, bossbset)
        if bossbset in bossa or bossbset == a: break
    output.append(str(bossbset))

print("\n".join(output))

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

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

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

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
0
2
3
0
0
...

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

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: 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
(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
0
1
0
1

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)