Problem: MEX Sequence
time limit per test: 2 seconds
memory limit per test: 512 megabytes
You are given an array $$$a$$$ of length $$$n$$$. Define an infinite sequence $$$s$$$ as follows:
For $$$1 \leq i \leq n: s_i = a_i$$$
For $$$i \gt n$$$: $$$s_i$$$ = $$$\operatorname{mex}(s_{i-1}, s_{i-2}, ..., s_{i-n})$$$
You are given $$$q$$$ queries. In each query, you are given an integer $$$k$$$, and you have to determine $$$s_k$$$.
Input
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$, the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$q$$$ $$$(1 \leq n, q \leq 2 \cdot 10^5)$$$, the size of the array and the number of queries, respectively.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(0 \leq a_i \leq n)$$$ — the elements of the array $$$a$$$.
The third line of each test case contains $$$q$$$ integers $$$k_1, k_2, ..., k_q$$$ $$$(1 \leq k_i \leq 10^{18})$$$, where $$$k_i$$$ is the value of $$$k$$$ for the $$$i$$$-th query.
It's guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
Output
For each test case, output a line with $$$q$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th query.
Please try to solve the question first before seeing the solution unless you're new!
If you get stuck, I recommend you see this problem from CSES then solve this one.
Feel free to rate the problem from 1 to 10!
Update: Shoutout to BLOBVISGOD to be the first solver of the problem!
Update: The editorial is here!









If k>2N then its easy to solve it with a formula otherwise you can just bruteforce first 2N elements before the queries and output a[K]
No bro, brute forcing to finding the mex till $$$2n$$$ elements takes $$$O(n^2)$$$ time because each computation can take $$$O(n)$$$. You need to use some data structure to speed this up to $$$O(log n)$$$ or $$$O(1)$$$ per computation.
How does your "easy" formula work, and how do you brute force efficiently (n=1e5)?
My solution is as follows: First observe that s_{i+1},...,s_{i+n} cannot be equal to s_{i}, and furthermore for i>n, we have s_{i} <= n, since it is the mex of n numbers. Hence for i>n, the sequence is periodic with period n+1, and s_{n+1},...,s_{2n+2} are a permutation of 0,...,n.
To find this permutation, we can use an std::set that stores the numbers from 0,...,n that do not occur in our 'current' window of n elements. we can find the MEX by doing *set.begin(), and update the set by adding numbers s_{i-n} to it if it is the last occurrence of s_{i-n} in the interval, and removing s_{i} from the set, since after the interval moves one place to the right it will contain s_{i}.
I am not sure if there is an easier way? what is your solution?
Use ordered set and binary search for the first value i such that order_of_key(i) doesnt equal i and then you know the mex is i. Now you continue this until all values from 0-N are filled then you will notice that the Mex will just increase by one each time
that sounds significantly more annoying to implement, and is slower (O(n log(n)^2) instead of O(n log(n))
I think you can optimize it and use dsu instead of ordered set where you draw an edge to the next element that didnt appear yet and once u add an element i just draw an edge between that and find(i+1).
This will be O(N*alpha(n))
You also need to delete numbers, I am not sure how DSU can handle that. Can you maybe implement it?
I thought you get the MEX of everything before i not just the N numbers before it
what is bro yapping about :skull:
Technically, if you want a ""faster"" solution, you can use https://en.wikipedia.org/wiki/Van_Emde_Boas_tree . Then it is O(n loglog(n)) :)
can you share some resources , where I can read that ?
Hi! is this your chess profile?
Of course!
Come on! Please give a code solution quickly. I'll only count that. Hopefully you give one in C++ so that I can understand your code since I don't do Python and other languages.
aight, calm down:
Either I understood the task wrong or your code is completely wrong because every testcase I tried your code didnt work on
Bro the answer is $$$0$$$, sorry.
The sequence is $$$1, 2, 3, 0, 2, 4, 1, 5, 3, 0, 2, 4, 1, 5, 3, 0$$$
$$$16$$$th element is $$$0$$$.
Code output is also 0.
He thought u take the mex of all the elements before I not just the N elements before it
Following is my approach: Simply find the MEX of the initial array, and that will be the element at the (n)th index (0-based) , these elements keep repeating, therefore element at index k is the same as the element at index k%(n+1)
UPD : I assumed that the elements were distinct , my bad
that does not work if the elements a_1,...,a_n are not unique, i.e. a_i=a_j for i,j <= n.
Yes I assumed they were distinct, my bad
Your code fails for the following test case:
Your output is 2 but expected is 5 because the sequence is:
This means that the answer is the 5th term of the repetition = (k — n) % (n + 1) = 312779 % 6 = 5. Hence, the answer is the 5th term of the repeating part which is 3 1 4 0 5 2 which is 5. The correct code was written by BLOBVISGOD which outputs 5 for this case.
I have a solution that solves for arbitrarily large $$$A_i$$$, of course if $$$A_i$$$ is like $$$\le 10^{100}$$$ then we got to do bignum calculation and it would be really annoying. If $$$k \le n$$$ we simple output $$$A_n$$$. Otherwise, WLOG, supposed $$$A$$$ is sorted, then $$$S_{n + 1 + [0, A_1)}$$$ would be $$$[0, A_1)$$$, and $$$S_{n + [A_1, A_2 - 1)}$$$ would be $$$[A_1 + 1, A_2)$$$, and so on.
Bro if $$$a_i \gt n$$$ it just doesn't matter since the MEX is at most $$$n$$$. If the value $$$k$$$ of the query is at most $$$n$$$ just output the value itself else take all values greater than $$$n$$$ to be $$$n$$$ and calculate the MEX normally like the solution.
Also, you will lose generality because the sequence values depend on the last $$$n$$$ terms which may not necessarily be sorted. Thus, the WLOG part is also wrong according to me.
Oh sorry, I didn't read the statement carefully. It says $$$S_i$$$ is the MEX of last $$$n$$$ terms, not all the previous terms. Otherwise my solution would be correct. Sorry about that man. If so then we can just maintain a set to track the missing element from $$$0$$$ to $$$n - 1$$$, and compute the smallest one for each element from $$$n + 1$$$ to $$$2n$$$.
you are 12 years old ?!!!
Yes, that is right!
More specifically, I was born on 17 February 2013.
Cool. keep going bro, please don't get distracted in future. Good luck!