CSES - Aalto Competitive Programming 2024 - wk4 - Homework - Results
Submission details
Task:Dynamic Range Minimum Queries
Sender:Mojojijo
Submission time:2024-09-24 21:55:54 +0300
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2--details

Code

def dyn_range_mn_queries(n, q, arr, barr):
    tree = [0] * (4 * n)

    def build(node, start, end):
        if start == end:
            tree[node] = arr[start]
        else:
            mid = (start + end) // 2
            build(2 * node + 1, start, mid)
            build(2 * node + 2, mid + 1, end)
            tree[node] = min(tree[2 * node + 1], tree[2 * node + 2])

    def update(node, start, end, idx, val):
        if start == end:
            tree[node] = val
        else:
            mid = (start + end) // 2
            if start <= idx <= mid:
                update(2 * node + 1, start, mid, idx, val)
            else:
                update(2 * node + 2, mid + 1, end, idx, val)
            tree[node] = min(tree[2 * node + 1], tree[2 * node + 2])

    def query_tree(node, start, end, l, r):
        if r < start or end < l:
            return float('inf')
        if l <= start and end <= r:
            return tree[node]
        mid = (start + end) // 2
        left_min = query_tree(2 * node + 1, start, mid, l, r)
        right_min = query_tree(2 * node + 2, mid + 1, end, l, r)
        return min(left_min, right_min)

    build(0, 0, n - 1)
    
    for query in barr:
        if query[0] == 1:
            _, k, u = query
            update(0, 0, n - 1, k - 1, u)
        else:
            _, a, b = query
            result = query_tree(0, 0, n - 1, a - 1, b - 1)
            print(result)

n, q = map(int, input().split())
arr = list(map(int, input().split()))

barr = []
for _ in range(q):
    query = list(map(int, input().split()))
    barr.append(query)

dyn_range_mn_queries(n, q, arr, barr)


# Input:
# 8 4
# 3 2 4 5 1 1 5 3
# 2 1 4
# 2 5 6
# 1 2 3
# 2 1 4
# Output:
# 2
# 1
# 3

Test details

Test 1

Verdict: ACCEPTED

input
8 80
7 6 4 6 2 9 4 8
2 1 1
2 1 2
2 1 3
...

correct output
7
6
4
4
2
...

user output
7
6
4
4
2
...
Truncated

Test 2

Verdict:

input
200000 200000
398739055 65343131 699208332 3...

correct output
28609
129890
20378
20378
311522
...

user output
(empty)