having an array $$$arr$$$ with $$$n$$$ elements we have two types of queries:
- given an index $$$idx$$$ and a value $$$val$$$, $$$arr_{idx}:=val$$$
- given a range $$$(l,r)$$$, find the $$$MEX$$$ of the range
what is the most efficient way to do this?
i'd be grateful if you could share some c++ code to do this.
thanks in advance guys.








We can use 3D MO — $$$O(n ^ {5/3})$$$.
There is also a solution $$$O(n log ^ 2 n)$$$.
If there are no change requests, then the wavelet tree in $$$O(n log n)$$$ can be used.
can you provide more info or some reference for the second solution?
I don't know the algorithm, but I know it exists.
In this blog you can find the same question
This might be helpful (it uses persistent segment trees which is an advanced topic)
thanks
For range MEX with point updates, I believe the best known solution is $$$\mathcal{O}(\log^3{n})$$$ per operation. Key observations:
Using a wavelet tree with value as the top-level dimension, we avoid an extra log factor from binary searching on value.
Let me know if you'd like a wavelet tree blog (it would explain my solution in more detail). It's my favorite data structure and I think it deserves better coverage.
yes please, would appreciate it