oneby7

Range Queries Segment tree

610. Range Minimum with Updates

Medium

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 sets nums[i] to v.
  • [2, l, r] is a question: its answer is the smallest value among nums[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]]

Output: [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]]

Output: [-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]]

Output: [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.

nums = [4, 1, 6, 3] queries = [[1, 1, 9], [2, 0, 2]]

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109
  • 1 ≤ queries.length ≤ 105
  • queries[j].length = 3, and queries[j][0] is 1 or 2
  • For [1, i, v]: 0 ≤ i < nums.length and -109 ≤ v ≤ 109
  • For [2, l, r]: 0 ≤ l ≤ r < nums.length

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.