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

Code

import sys
input = sys.stdin.read
data = input().split()

class SegmentTree:
    def __init__(self, data):
        self.n = len(data)
        self.tree = [0] * (2 * self.n)
        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, index, value):
        pos = index + self.n
        self.tree[pos] = value
        while pos > 1:
            pos //= 2
            self.tree[pos] = min(self.tree[2 * pos], self.tree[2 * pos + 1])

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


idx = 0
n = int(data[idx])
q = int(data[idx+1])
idx += 2
arr = list(map(int, data[idx:idx+n]))
idx += n

seg_tree = SegmentTree(arr)


output = []
for i in range(q):
    if data[idx] == '1':
        k = int(data[idx+1]) - 1
        u = int(data[idx+2])
        seg_tree.update(k, u)
        idx += 3
    elif data[idx] == '2':
        a = int(data[idx+1]) - 1
        b = int(data[idx+2]) - 1
        output.append(str(seg_tree.query(a, b)))
        idx += 3

sys.stdout.write("\n".join(output) + "\n")

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)