Range Queries Segment tree
610. Range Minimum with Updates
You are given an array of integers nums and a list of queries. Each query is one of two kinds:
[1, i, v]is a change: it setsnums[i]tov.[2, l, r]is a question: its answer is the smallest value amongnums[l], nums[l + 1], …, nums[r]. Both ends are included, and positions count from 0.
The queries run in order, so a question sees every change made before it. Return the answers to the questions
in the order they were asked. If there are no questions, return an empty list [].
Example 1
Input: nums = [5, 8, 2, 9, 4, 7]
queries = [[2, 0, 5],
[2, 3, 5],
[1, 2, 10],
[2, 0, 5],
[1, 4, -1],
[2, 3, 4],
[2, 1, 3]]
[2,4,4,-1,8]Explanation: The whole array has minimum 2, and positions 3 to 5 hold 9, 4 and 7. Then 2 becomes 10, so the whole array now has minimum 4. Then 4 becomes -1, so positions 3 and 4 hold 9 and -1. Last, positions 1 to 3 hold 8, 10 and 9.
Example 2
Input: nums = [-1000000000]
queries = [[2, 0, 0],
[1, 0, 1000000000],
[2, 0, 0]]
[-1000000000,1000000000]Explanation: With one value, every question asks about that value. The change raises it from -109 to 109.
Example 3
Input: nums = [3, 3, 3]
queries = [[1, 1, 0],
[1, 1, 6],
[2, 0, 2],
[2, 1, 1]]
[3,6]Explanation: Position 1 changes twice, and the second change replaces the first, so it holds 6. Only the two questions give answers.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ nums.length ≤ 105-109 ≤ nums[i] ≤ 1091 ≤ queries.length ≤ 105queries[j].length = 3, andqueries[j][0]is 1 or 2- For
[1, i, v]:0 ≤ i < nums.lengthand-109 ≤ v ≤ 109 - For
[2, l, r]:0 ≤ l ≤ r < nums.length
Clarifying questions
What you would ask an interviewer before writing any code.