CSES - Aalto Competitive Programming 2024 - wk7 Homework - Results
Submission details
Task:Company Queries II
Sender:esya_rae
Submission time:2024-10-23 21:49:38 +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.59 sdetails
#8ACCEPTED0.96 sdetails
#9ACCEPTED0.84 sdetails
#10ACCEPTED0.81 sdetails
#11ACCEPTED0.04 sdetails
#12ACCEPTED0.83 sdetails

Code

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

def build_tree_min():
    global depths, min_tree, k
    for i in range(m):
        min_tree[k + i] = (depths[i], 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())
node_ids = []
depths = []
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, -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(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: 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