You are not logged in. Please login at to post your questions!


Minimum number query

I know how to solve it for a problem which handles two queries 1)update an index to a new value 2)minimum element in a range.(using segment tree)

But how to handle for the case like minimum in a range which is greater than K which is some number given in the query

If anyone knows it using fenwick tree/segment tree please explain it clearly.

For a segment tree we will build the tree first but here we dont know the value of k because k is given in the queries .So how to build and update in this case ?

asked 06 Jan, 04:04

beginner_1111's gravatar image

accept rate: 13%

edited 06 Jan, 07:17

I think you can apply a Merge Sort Tree.

I solved a similar problem a few days ago where I had to find the value closest to k in a given range [L, R]. The problem was LRQUER.

In your case you can do the following:

Instead of storing a single value in any node, you can store a range. This range will be the sorted list of the values stored in its children. Then, while querying, you will query in this range. For a single query [L, R], you can find the upper_bound() and then return the minimum of all the values found like this.

Edit: To handle point updates, we'll have to update the corresponding parent nodes too. We can update a node in $O(log(n))$ time by searching for the previous element and then inserting a new element, using something like std::set (because we have to store the elements in sorted order) in C++. If duplicate elements are allowed, then we can use std::multiset in C++.

Now, there can be at most $O(log(n))$ updates in a single update operation because the depth of the tree will be of the order of $log(n)$, making the complexity $O(log^2(n))$.


answered 06 Jan, 13:54

shubhambhattar's gravatar image

accept rate: 23%

edited 06 Jan, 19:53

@shubhambhattar but how to handle point updates?

(06 Jan, 19:25) beginner_11114★

@beginner_1111 Updated my answer.

(06 Jan, 19:54) shubhambhattar3★

@shubhambhattar sir i have gone through your AC code for LRQUER but why you have multiplied every array element by 2 and then found the answer accordingly i did it without multiplying by 2 but got WA on half of the subtasks

(07 Jan, 09:33) divik5444★

@divik544 It was done so that I could avoid floating point calculations. If you remember, we had to divide the expression by $2$. Multiplying everything by $2$ helped me with that.

(07 Jan, 13:33) shubhambhattar3★

Thanks sir i got it now :)

(07 Jan, 14:44) divik5444★

how can i return index of minimum elemnet from segment tree?


answered 09 Nov, 00:44

loooll11lll's gravatar image

accept rate: 0%

edited 09 Nov, 00:44

when keepning minimum value in the node keep the index too

(09 Nov, 08:12) sonu_6284★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 06 Jan, 04:04

question was seen: 587 times

last updated: 09 Nov, 08:12