shivankjha46's blog

By shivankjha46, 11 months ago, In English

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!

Solution

Full text and comments »

  • Vote: I like it
  • +11
  • Vote: I do not like it