Submission details
Task:Company Queries II
Sender:francden
Submission time:2025-10-13 15:43:20 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#10.07 sdetails
#20.06 sdetails
#30.06 sdetails
#40.07 sdetails
#50.07 sdetails
#60.16 sdetails
#70.13 sdetails
#80.16 sdetails
#90.16 sdetails
#100.16 sdetails
#110.07 sdetails
#120.16 sdetails

Code

import sys
sys.setrecursionlimit(300000)
input = sys.stdin.readline

n, q = map(int, input().split())
boss = [0] + [int(x) for x in input().split()]

# Build adjacency list
children = [[] for _ in range(n + 1)]
for i in range(2, n + 1):
    children[boss[i]] .append(i)

# Euler tour and depth arrays
euler = []
depth = [0] * (n + 1)
first = [0] * (n + 1)

def dfs(u, d):
    depth[u] = d
    first[u] = len(euler)
    euler.append(u)
    for v in children[u]:
        dfs(v, d + 1)
        euler.append(u)

dfs(1, 0)

m = len(euler)
log = [0] * (m + 1)
for i in range(2, m + 1):
    log[i] = log[i // 2] + 1

# Build Sparse Table
K = log[m] + 1
st = [[0] * m for _ in range(K)]
st[0] = euler.copy()

for k in range(1, K):
    for i in range(m - (1 << k) + 1):
        a = st[k - 1][i]
        b = st[k - 1][i + (1 << (k - 1))]
        st[k][i] = a if depth[a] < depth[b] else b

def lca(a, b):
    i, j = first[a], first[b]
    if i > j:
        i, j = j, i
    k = log[j - i + 1]
    x = st[k][i]
    y = st[k][j - (1 << k) + 1]
    return x if depth[x] < depth[y] else y

# Answer queries
for _ in range(q):
    a, b = map(int, input().split())
    print(lca(a, b))

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 11, in <module>
    children[boss[i]] .append(i)
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
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 11, in <module>
    children[boss[i]] .append(i)
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
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 11, in <module>
    children[boss[i]] .append(i)
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
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 11, in <module>
    children[boss[i]] .append(i)
IndexError: list index out of range

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 11, in <module>
    children[boss[i]] .append(i)
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 11, in <module>
    children[boss[i]] .append(i)
IndexError: list index out of range

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)

Error:
Traceback (most recent call last):
  File "input/code.py", line 11, in <module>
    children[boss[i]] .append(i)
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 11, in <module>
    children[boss[i]] .append(i)
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
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 11, in <module>
    children[boss[i]] .append(i)
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
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 11, in <module>
    children[boss[i]] .append(i)
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
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 11, in <module>
    children[boss[i]] .append(i)
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
(empty)

Error:
Traceback (most recent call last):
  File "input/code.py", line 11, in <module>
    children[boss[i]] .append(i)
IndexError: list index out of range