Submission details
Task:Company Queries II
Sender:aalto26gh_041
Submission time:2026-10-20 00:45:04 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.04 sdetails
#3ACCEPTED0.04 sdetails
#4ACCEPTED0.04 sdetails
#5ACCEPTED0.04 sdetails
#6--details
#7ACCEPTED0.54 sdetails
#8ACCEPTED0.89 sdetails
#9--details
#10--details
#11ACCEPTED0.04 sdetails
#12--details

Code

import sys

def solve():
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    n = int(input_data[0])
    q = int(input_data[1])
    
    # Adjacency list for building depths
    adj = [[] for _ in range(n + 1)]
    
    # Store binary lifting table: up[u][i] = 2^i-th ancestor of u
    # Max depth < 200,000, so 2^18 (262,144) is enough
    LOGN = 19
    up = [[0] * LOGN for _ in range(n + 1)]
    depth = [0] * (n + 1)
    
    ptr = 2
    for i in range(2, n + 1):
        parent = int(input_data[ptr])
        ptr += 1
        adj[parent].append(i)
        up[i][0] = parent

    # Iterative BFS to calculate depths and binary lifting table
    queue = [1]
    depth[1] = 0
    
    # Process tree level by level
    head = 0
    while head < len(queue):
        u = queue[head]
        head += 1
        
        # Fill binary lifting entries for node u
        for k in range(1, LOGN):
            prev = up[u][k - 1]
            if prev != 0:
                up[u][k] = up[prev][k - 1]
            else:
                up[u][k] = 0
                
        for v in adj[u]:
            depth[v] = depth[u] + 1
            queue.append(v)
            
    # LCA Query function
    def get_lca(a, b):
        if depth[a] < depth[b]:
            a, b = b, a
            
        # 1. Lift 'a' to the same depth as 'b'
        diff = depth[a] - depth[b]
        for k in range(LOGN):
            if (diff >> k) & 1:
                a = up[a][k]
                
        if a == b:
            return a
            
        # 2. Lift both nodes together until they are just below the LCA
        for k in range(LOGN - 1, -1, -1):
            if up[a][k] != up[b][k]:
                a = up[a][k]
                b = up[b][k]
                
        return up[a][0]

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

if __name__ == '__main__':
    solve()

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:

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

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

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)