Task: | Company Queries II |
Sender: | esya_rae |
Submission time: | 2024-10-23 22:02:06 +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 | TIME LIMIT EXCEEDED | -- | details |
#7 | ACCEPTED | 0.61 s | details |
#8 | ACCEPTED | 0.97 s | details |
#9 | ACCEPTED | 0.87 s | details |
#10 | ACCEPTED | 0.81 s | details |
#11 | ACCEPTED | 0.04 s | details |
#12 | ACCEPTED | 0.81 s | details |
Code
import math import sys sys.setrecursionlimit(10**6) input = sys.stdin.readline def dfs(v, d): global g, nd, r nd.append((v, d)) r[v] = len(nd)-1 for w in g[v]: dfs(w, d + 1) nd.append((v, d)) def build_tree_min(): global nd, min_tree, k for i in range(m): min_tree[k + i] = (nd[i][1], i) for i in range(k - 1, 0, -1): left, right = min_tree[2 * i], min_tree[2 * i + 1] if left[0] < right[0]: min_tree[i] = left else: min_tree[i] = right def answer_min(a, b): global min_tree, k res = math.inf value = -1 a += k b += k while a <= b: if a % 2 == 1: z, v = min_tree[a] if res > z: value = v res = z a += 1 if b % 2 == 0: z, v = min_tree[b] if res > z: value = v res = z b -= 1 a //= 2 b //= 2 return res, value n, q = map(int, input().split()) nd = [] g = [[] for _ in range(n)] r = [-1] * n x = list(map(int, input().split())) for i in range(n - 1): g[x[i] - 1].append(i + 1) dfs(0, 0) m = len(nd) k = 2 ** math.ceil(math.log(m, 2)) min_tree = [(math.inf, -1)] * (2 * k) build_tree_min() # print(k, min_tree) output = [] for i in range(q): a, b = map(int, input().split()) a -= 1 b -= 1 v, w = answer_min(min(r[a], r[b]), max(r[a], r[b])) output.append(str(nd[w][0] + 1)) 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: 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: 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 ... Truncated |
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 ... Truncated |
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 ... Truncated |
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 ... Truncated |
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 ... Truncated |