There is a list of key vs value pair.size = n. No I have been given a key I have to find the max value from the list such that its key is less than the given key.
If there are q queries I can do this in O(qn). Is there a faster way to do this.
There is a list of key vs value pair.size = n. No I have been given a key I have to find the max value from the list such that its key is less than the given key.
If there are q queries I can do this in O(qn). Is there a faster way to do this.
| # | User | Rating |
|---|---|---|
| 1 | Kevin114514 | 3722 |
| 2 | Benq | 3676 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3408 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3196 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | maroonrk | 153 |
| 2 | Qingyu | 152 |
| 3 | nik_exists | 151 |
| 4 | AmShZ | 147 |
| 4 | maspy | 147 |
| 6 | Um_nik | 142 |
| 7 | Errichto | 139 |
| 8 | adamant | 135 |
| 8 | BledDest | 135 |
| 10 | AksLolCoding | 132 |
| Name |
|---|



Let each key be an index of the corresponding value. Store the values in a segment tree for maximum. Then, if you need to find the maximum value with key < k you just need to query maximum value on the segment [0;k - 1]. Probably, you may need to compress key values so they would fit the range [0;n].
If you could answer the queries offline (i.e. you know all the queries before you began to answer them), just sort the queries and key-value pairs by key value and use the fact that the answer for the i - th query in the sorted order is the maximum of the answer of the (i - 1) - th query and all the values having their key between this two queries.
Hi, queries are online and queries are of the form key,value which on evaluation gets added to the list.
Could you share a link to the problem?
If I am right understood the problem, you should use the Fenwick tree with maximum.