| Task: | Company Queries II |
| Sender: | aalto26gh_031 |
| Submission time: | 2026-10-19 15:38:20 +0300 |
| Language: | Python3 (PyPy3) |
| Status: | READY |
| Result: | TIME LIMIT EXCEEDED |
| 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.87 s | details |
| #7 | ACCEPTED | 0.53 s | details |
| #8 | ACCEPTED | 0.70 s | details |
| #9 | TIME LIMIT EXCEEDED | -- | details |
| #10 | ACCEPTED | 0.89 s | details |
| #11 | ACCEPTED | 0.04 s | details |
| #12 | TIME LIMIT EXCEEDED | -- | details |
Code
import sys
inp = sys.stdin.read().split()
n = int(inp[0])
q=int(inp[1])
boss=[0] + [int(i) for i in inp[2:1+n]]
maxpower = 18 #set maxpower to 18 for preprocessing since in the worst case n = 200 000 which is 2^17..,
up = [[0]* maxpower for _ in range(n+1)] #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[i][0] = boss
depth[i] += depth[boss] +1
tracker+=1
for j in range(1,maxpower):
for i in range(1,n+1):
#i represents the employee
#j how far up we're looking
up[i][j] = up[up[i][j-1]][j-1]
#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[a][j]
if a == b:
return a
for j in range(maxpower-1,-1,-1):
if up[a][j] != up[b][j] :
a = up[a][j]
b = up[b][j]
return up[a][0]
#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: TIME LIMIT EXCEEDED
| 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: 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: TIME LIMIT EXCEEDED
| 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) |
