Submission details
Task:Company Queries II
Sender:aalto26gh_045
Submission time:2026-10-19 22:20:08 +0300
Language:Python3 (PyPy3)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.04 sdetails
#3ACCEPTED0.04 sdetails
#4ACCEPTED0.04 sdetails
#5ACCEPTED0.04 sdetails
#6ACCEPTED0.37 sdetails
#7ACCEPTED0.19 sdetails
#8ACCEPTED0.29 sdetails
#9ACCEPTED0.44 sdetails
#10ACCEPTED0.40 sdetails
#11ACCEPTED0.04 sdetails
#12ACCEPTED0.47 sdetails

Code

import sys

def main():
    data = sys.stdin.buffer.read().split()
    n, q = int(data[0]), int(data[1])

    # par[v] = boss of v; root points to itself (index 0 is a dummy)
    par = [1] * (n + 1)
    depth = [0] * (n + 1)
    for i in range(2, n + 1):
        e = int(data[i])          # e_i is data[2 + (i-2)] = data[i]
        par[i] = e
        depth[i] = depth[e] + 1   # valid because e < i

    # only as many levels as the deepest node needs
    LOG = max(1, max(depth).bit_length())

    # up[k][v] = 2^k-th ancestor of v
    up = [par]
    for _ in range(1, LOG):
        prev = up[-1]
        up.append([prev[x] for x in prev])

    out = []
    pos = n + 1                   # queries start after the n-1 parent values
    for _ in range(q):
        a = int(data[pos])
        b = int(data[pos + 1])
        pos += 2

        if depth[a] < depth[b]:
            a, b = b, a

        # lift a to b's depth
        diff = depth[a] - depth[b]
        k = 0
        while diff:
            if diff & 1:
                a = up[k][a]
            diff >>= 1
            k += 1

        if a == b:
            out.append(a)
            continue

        # lift both as high as possible without meeting
        for k in range(LOG - 1, -1, -1):
            row = up[k]
            ua = row[a]
            ub = row[b]
            if ua != ub:
                a = ua
                b = ub
        out.append(par[a])

    sys.stdout.write("\n".join(map(str, out)) + "\n")

main()

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