CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:esya_rae
Submission time:2024-10-23 21:40:19 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.04 sdetails
#3ACCEPTED0.04 sdetails
#4ACCEPTED0.04 sdetails
#5ACCEPTED0.04 sdetails
#6--details
#7ACCEPTED0.89 sdetails
#8--details
#9--details
#10--details
#11ACCEPTED0.04 sdetails
#12--details

Code

import math
import sys
sys.setrecursionlimit(10**6)
def dfs(v, d):
    global g, visited, node_ids, depths, r
    node_ids.append(v)
    depths.append(d)
    r[v] = len(node_ids)-1
    for w in g[v]:
        if not visited[w]:
            dfs(w, d + 1)
            node_ids.append(v)
            depths.append(d)
    visited[v] = True


def build_tree_min(i):
    global depths, min_tree, k, m
    if i >= k + m:
        return math.inf, -1
    if i >= k:
        min_tree[i] = (depths[i - k], i - k)
        return min_tree[i]
    d1, i1 = build_tree_min(2 * i)
    d2, i2 = build_tree_min(2 * i + 1)
    if d1 < d2:
        min_tree[i] = (d1, i1)
    else:
        min_tree[i] = (d2, i2)

    return min_tree[i]


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())
node_ids = []
depths = []
visited = [False] * n
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(node_ids)
k = 2 ** math.ceil(math.log(m, 2))
min_tree = [math.inf] * (2 * k)
build_tree_min(1)
# 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(node_ids[w] + 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:

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:

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)

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)

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)

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:

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)