CSES - Aalto Competitive Programming 2024 - wk4 - Homework - Results
Submission details
Task:Dynamic Range Minimum Queries
Sender:Farah
Submission time:2024-09-24 10:11:35 +0300
Language:Python3 (CPython3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2--details

Code

class SegmentTree:
    def __init__(self, data):
        self.n = len(data)
        self.tree = [0] * (2 * self.n)
        self.build(data)

    def build(self, data):
        for i in range(self.n):
            self.tree[self.n + i] = data[i]
        for i in range(self.n - 1, 0, -1):
            self.tree[i] = min(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, pos, value):
        pos += self.n
        self.tree[pos] = value
        pos //= 2
        while pos >= 1:
            self.tree[pos] = min(self.tree[2 * pos], self.tree[2 * pos + 1])
            pos //= 2

    def query(self, l, r):
        l += self.n
        r += self.n + 1
        min_val = float('inf')
        while l < r:
            if l % 2 == 1:
                min_val = min(min_val, self.tree[l])
                l += 1
            if r % 2 == 1:
                r -= 1
                min_val = min(min_val, self.tree[r])
            l //= 2
            r //= 2
        return min_val

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

seg_tree = SegmentTree(arr)

for _ in range(q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        k, u = query[1] - 1, query[2]
        seg_tree.update(k, u)
    elif query[0] == 2:
        a, b = query[1] - 1, query[2] - 1
        print(seg_tree.query(a, b))

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)