| Task: | Company Queries II |
| Sender: | aalto26gh_031 |
| Submission time: | 2026-10-22 14:15:11 +0300 |
| Language: | Python3 (PyPy3) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.04 s | details |
| #2 | ACCEPTED | 0.04 s | details |
| #3 | ACCEPTED | 0.04 s | details |
| #4 | ACCEPTED | 0.04 s | details |
| #5 | ACCEPTED | 0.04 s | details |
| #6 | ACCEPTED | 0.44 s | details |
| #7 | ACCEPTED | 0.40 s | details |
| #8 | ACCEPTED | 0.46 s | details |
| #9 | ACCEPTED | 0.61 s | details |
| #10 | ACCEPTED | 0.53 s | details |
| #11 | ACCEPTED | 0.04 s | details |
| #12 | ACCEPTED | 0.58 s | details |
Code
import sys
inp = sys.stdin.read().split()
n = int(inp[0])
q=int(inp[1])
maxpower = 18 #set maxpower to 18 for preprocessing since in the worst case n = 200 000 which is 2^17..,
up = [[0] * (n + 1) for _ in range(maxpower)] #stores for each employee, his superior at leevel k if there is one
depth = [0]*(n+1)
tracker = 2
for i in range(2,n+1):
boss = int(inp[i])
up[0][i] = boss
depth[i] = depth[boss] +1
tracker+=1
for j in range(1, maxpower):
prev_level = up[j-1]
curr_level = up[j]
for i in range(1, n + 1):
curr_level[i] = prev_level[prev_level[i]]
#now we need to find the lowest common ancestors for each query
def get_lca(a,b):
if depth[a] < depth[b]:
a, b = b, a
#once we have the deeper node, we gotta make sure they're on the same level
diff = depth[a] - depth[b]
for j in range(maxpower):
#for each jump (power of 2) , shift the bits to check if the bit is activated. if it is we jump by that much
#goal here is to bring a up to the same level as b
if (diff >> j) & 1:
a = up[j][a]
if a == b:
return a
for j in range(maxpower-1,-1,-1):
if up[j][a] != up[j][b] :
a = up[j][a]
b = up[j][b]
return up[0][a]
#now we're gonna make both nnodes climb until we found a
output = []
for _ in range(q):
a = int(inp[tracker])
b = int(inp[tracker+1])
tracker+=2
output.append(str(get_lca(a,b)))
sys.stdout.write('\n'.join(output) + '\n')
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 ... |
