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
# | User | Rating |
---|---|---|
1 | tourist | 3856 |
2 | jiangly | 3747 |
3 | orzdevinwang | 3706 |
4 | jqdai0815 | 3682 |
5 | ksun48 | 3591 |
6 | gamegame | 3477 |
7 | Benq | 3468 |
8 | Radewoosh | 3462 |
9 | ecnerwala | 3451 |
10 | heuristica | 3431 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | -is-this-fft- | 162 |
3 | Dominater069 | 160 |
4 | Um_nik | 158 |
5 | atcoder_official | 156 |
6 | adamant | 152 |
6 | djm03178 | 152 |
8 | Qingyu | 149 |
8 | luogu_official | 149 |
10 | awoo | 147 |
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
Name |
---|
(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.net/contestInvitation/0c55b0f44793caa990ef4f0bf1d13711ef2481d1
I assume that u will have repetitive queries about the MEX of the array that you have. The naïve approach of recalculating MEX each time is what's getting a TLE since it's O(n^2). We can easily avoid "recalculating" by keeping in mind that adding elements will never decrease the MEX. So we keep a separate array of Boolean numbers, and a counter that moves forward while the number it's at is true in the array. and even though this is going to be implemented using a "while" loop nested in your for loop, this nested loop will visit each value at most once, so the overall complexity is going to be O(n).
set mex = x, sort the vector (dont need to if its a set), then increment mex while mex is found in the vector/set