I would like to find ways I can look up the smallest element bigger than X that doesnt exist in a vector/set, without having to deal with TLE risks, if that is possible
I would like to find ways I can look up the smallest element bigger than X that doesnt exist in a vector/set, without having to deal with TLE risks, if that is possible
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | jiangly | 3810 |
| 2 | Benq | 3676 |
| 3 | Kevin114514 | 3655 |
| 4 | maroonrk | 3463 |
| 5 | strapple | 3390 |
| 6 | Um_nik | 3387 |
| 7 | tourist | 3384 |
| 8 | heuristica | 3322 |
| 9 | turmax | 3319 |
| 10 | jiangbowen | 3291 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | nik_exists | 150 |
| 2 | maspy | 150 |
| 4 | AmShZ | 143 |
| 5 | Um_nik | 142 |
| 6 | Errichto | 139 |
| 7 | adamant | 137 |
| 8 | maroonrk | 133 |
| 9 | BledDest | 132 |
| 10 | qwexd | 129 |
| Название |
|---|



(omg fz blog!) i guess you can binary search on a segment tree that have vec[i] as indices and 1 as value, the condition of the bs would be
if(m-i+1 < query(i, m)) go left else go right, this should work, solg^2(n)per query andlg(n)updates, if you hate segment tree (*get some help*) ig you can do prefix sum + coordinate compression but that's confusing to implement, interested if a better solution is possible!Bruh what
potato chipusu
You can also do it in O( log N ) (WALKING ON SEGMENT TREE)
can you elaborate more on how to do that?
EDIT: ok nvm i see it! very neat :) you check if left child = r-l+1, and go to right child else you traverse to left and repeat until you hit some node and that'll be your mex, very neat! O(logn)
Exactly.
If there are insertion, deletion operations on our vector:
Let n = upper bound on answer.
We can maintain a set of numbers not present in the given vector, let's denote it as S. Finding the answer will be the same as finding the lowest number in S, not less than X. So the time complexity will be O(nlog(n))
If there are no insertion, deletion operations on our vector:
Let's denote the given vector as v. Let's sort v and remove duplicates, and for each x in v, precalculate answer if X = x. If X is present in v, output the value that we precalculated, otherwise output X. To precalculate answer for each x in v, we can do the following: if x+1 is present in v, then answer for x will be equal to answer for x+1, otherwise x+1 will be the answer.
Time complexity will be O((q+N)log(N)), where q is the number of queries and N is the number of elements in vector.
Build a weight segment tree which records the number of the elements that do not exist in the vector, binary search on the segment tree to find the first location that its prefix sum of the number is not $$$0$$$.
The time complexity is $$$O((n+q)\log W)$$$.
subtract x from everything and deal it like mex
lets say you have 4, 5, 6, 9, 10 in your data
we will store in map {4, 6} {9, 10}
now lets say you want >= missing key for 9
so we query <= 9 which is 9 itself in the map
then the value of it is 10, hence all numbers from 9 to 10 are present so 11 is the missing one
i have created a problem on polygon refer the first question of this mashup https://codeforces.me/contestInvitation/0c55b0f44793caa990ef4f0bf1d13711ef2481d1
didnt notice the problem, lit sir thx!
set mex = x, sort the vector (dont need to if its a set), then increment mex while mex is found in the vector/set