| Task: | Company Queries II |
| Sender: | francden |
| Submission time: | 2025-10-13 15:57:35 +0300 |
| Language: | Python3 (PyPy3) |
| Status: | READY |
| Result: | RUNTIME ERROR |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.04 s | details |
| #2 | RUNTIME ERROR | 0.07 s | details |
| #3 | ACCEPTED | 0.04 s | details |
| #4 | ACCEPTED | 0.04 s | details |
| #5 | RUNTIME ERROR | 0.07 s | details |
| #6 | TIME LIMIT EXCEEDED | -- | details |
| #7 | RUNTIME ERROR | 0.63 s | details |
| #8 | RUNTIME ERROR | 0.98 s | details |
| #9 | RUNTIME ERROR | 0.71 s | details |
| #10 | RUNTIME ERROR | 0.80 s | details |
| #11 | ACCEPTED | 0.04 s | details |
| #12 | RUNTIME ERROR | 0.79 s | details |
Code
import sys
sys.setrecursionlimit(1000000)
input = sys.stdin.readline
n, q = [int(x) for x in input().split()]
ancestors = [int(x) for x in input().split()]
reverse_ancestors = [[] for _ in range(n + 1)]
for index, value in enumerate(ancestors):
reverse_ancestors[value].append(index + 2)
depth = [0] * (n + 1)
for i in range(2, n + 1):
depth[i] = depth[ancestors[i - 2]] + 1
L = []
nodes = []
def search(node):
children = reverse_ancestors[node]
L.append((depth[node], node))
nodes.append(node)
for child in children:
search(child)
L.append((depth[node], node))
nodes.append(node)
search(1)
indexs = [-1] * (n + 1)
for index, node in enumerate(nodes):
if indexs[node] == -1:
indexs[node] = index
min_list = []
min_list.append(L)
j = 1
power = 2
while power <= n :
i=0
curr_list = []
while i + power - 1 < 2*n-1:
curr_list.append(min(min_list[-1][i],min_list[-1][i+j],key= lambda x : x[0]))
i+=1
j=power
power *=2
if curr_list :
min_list.append(curr_list)
def mini(a, b):
if b < a:
a, b = b, a
if a == b:
return L[a]
length = b - a + 1
exp = 0
while (1 << (exp + 1)) <= length:
exp += 1
power = 1 << exp
left = min_list[exp][a]
right = min_list[exp][b - power + 1]
return left if left[0] < right[0] else right
for _ in range(q):
a, b = [int(x) for x in input().split()]
print(mini(indexs[a], indexs[b])[1])
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: RUNTIME ERROR
| 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 ... |
Error:
Traceback (most recent call last):
File "input/code.py", line 81, in <module>
print(mini(indexs[a], indexs[b])[1])
File "input/code.py", line 74, in mini
left = min_list[exp][a]
IndexError: list index out of rangeTest 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: RUNTIME ERROR
| 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 |
Error:
Traceback (most recent call last):
File "input/code.py", line 81, in <module>
print(mini(indexs[a], indexs[b])[1])
File "input/code.py", line 74, in mini
left = min_list[exp][a]
IndexError: list index out of rangeTest 6
Verdict: TIME LIMIT EXCEEDED
| 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: RUNTIME ERROR
| 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 ... |
Error:
Traceback (most recent call last):
File "input/code.py", line 81, in <module>
print(mini(indexs[a], indexs[b])[1])
File "input/code.py", line 74, in mini
left = min_list[exp][a]
IndexError: list index out of rangeTest 8
Verdict: RUNTIME ERROR
| 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 81, in <module>
print(mini(indexs[a], indexs[b])[1])
File "input/code.py", line 74, in mini
left = min_list[exp][a]
IndexError: list index out of rangeTest 9
Verdict: RUNTIME ERROR
| 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 ... |
Error:
Traceback (most recent call last):
File "input/code.py", line 81, in <module>
print(mini(indexs[a], indexs[b])[1])
File "input/code.py", line 74, in mini
left = min_list[exp][a]
IndexError: list index out of rangeTest 10
Verdict: RUNTIME ERROR
| 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 ... |
Error:
Traceback (most recent call last):
File "input/code.py", line 81, in <module>
print(mini(indexs[a], indexs[b])[1])
File "input/code.py", line 74, in mini
left = min_list[exp][a]
IndexError: list index out of rangeTest 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: RUNTIME ERROR
| 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 |
Error:
Traceback (most recent call last):
File "input/code.py", line 81, in <module>
print(mini(indexs[a], indexs[b])[1])
File "input/code.py", line 74, in mini
left = min_list[exp][a]
IndexError: list index out of range