Machine Learning
Contest: Codeforces Round 466 (Div. 2)
Difficulty rating: 2600
Tags: brute force, data structures

You come home and fell some unpleasant smell. Where is it coming from?

You are given an array a. You have to answer the following queries:

1. You are given two integers l and r. Let ci be the number of occurrences of i in al: r, where al: r is the subarray of a from l-th element to r-th inclusive. Find the Mex of {c0, c1, ..., c109}
2. You are given two integers p to x. Change ap to x.

The Mex of a multiset of numbers is the smallest non-negative integer not in the set.

Note that in this problem all elements of a are positive, which means that c0 = 0 and 0 is never the answer for the query of the second type.

Input format:
The first line of input contains two integers n and q (1 ≤ n, q ≤ 100 000) — the length of the array and the number of queries respectively.

The second line of input contains n integers — a1, a2, ..., an (1 ≤ ai ≤ 109).

Each of the next q lines describes a single query.

The first type of query is described by three integers ti = 1, li, ri, where 1 ≤ li ≤ ri ≤ n — the bounds of the subarray.

The second type of query is described by three integers ti = 2, pi, xi, where 1 ≤ pi ≤ n is the index of the element, which must be changed and 1 ≤ xi ≤ 109 is the new value.

Output format:
For each query of the first type output a single integer  — the Mex of {c0, c1, ..., c109}.

Constraints:
Time limit: 4.0 s
Memory limit: 512.0 MB

Note:
The subarray of the first query consists of the single element — 1.

The subarray of the second query consists of four 2s, one 3 and two 1s.

The subarray of the fourth query consists of three 1s, three 2s and one 3.

Example input:
10 4
1 2 3 1 1 2 2 2 9 9
1 1 1
1 2 8
2 7 1
1 2 8

Example output:
2
3
2

Editorial:
At first let's find out minimum length of array, such that answer for it is c. For it on this segment there should be a number that has one occurrence, some number that has two occurrences, etc. Length of this segment will be $$1 + 2 + 3 + \cdots + c = \frac{c(c+1)}{2}$$. That's why answer won't exceed $$2 \cdot \sqrt{n}$$ for any query.
Let's compress numbers in such a way that numbers from array and from queries will be in range from 1 to n + q. Obviously this modification won't change answer, but now elements won't exceed $$n + q$$.
Suppose for interval $${ l ; r }$$ we know number $$\text{cnt}_i$$ of occurrences of each number and number of occurrences of each number of occurrences $$\hat{\mathrm{size}}_{i}$$. Then we can find Mex of this set in $$O(\sqrt{n})$$. Moreover it's easy to update arrays $$\text{cnt}_i$$ and $$\hat{\mathrm{size}}_{i}$$ for segments $${ \left[ l - 1 ; r \right] }$$, $${\bigl [}l+1;r{\bigr ]}$$, $${\bigl [}l;r-1{\bigr ]}$$ and $${\bigl [}l;r+1{\bigr ]}$$ in O(1) time.
We will represent each query as tuple of integers $$(t,l,r)$$ where t — is a number of change queries before this, and segment $${ l ; r }$$. It's easy to see that t can also be changed by one in $${\mathcal{O}}(1)$$ time. Because of $$q \approx n$$ will replace $$q$$ to $$n.$$ in following part of editorial.
Let $$P = n^{\frac{2}{3}}$$ and let's sort queries by triples $$(\lfloor \frac { t } { P } \rfloor, \lfloor \frac { l } { P } \rfloor, r )$$ and will answer queries in this order. Border $$\pi$$ will be increased by $${\mathcal{O}}(n)$$ for each sqaure of size $$\frac{n^2}{P^2} = \frac{n^2}{n^\frac{4}{3}} = n^\frac{2}{3}$$ $$\text{The image appears to be blurred, but it seems to contain a mathematical expression or a symbol that is not clearly discernible due to the blurriness. If there was intended text or a specific formula, it would need to be re-rendered with clearer imagery.}$$ sum of movements of right border is $${\mathcal O}(n^{\frac{5}{3}})$$. For each query borders ? and $$\text{The image does not contain any text or LaTeX formulas. It appears to be an abstract, blurred image with no discernible content.}$$ are moved by not more than $$O(n^{\frac{2}{3}})$$ $$\text{The image appears to be blurred, but it seems to contain a mathematical expression or a symbol that is not clearly discernible due to the blurriness. If there was intended text or a specific formula, it would need to be re-rendered with clearer imagery.}$$ sum of movements is $${\mathcal O}(n^{\frac{5}{3}})$$.
Solution has time complexity $${\mathcal O}(n^{\frac{5}{3}})$$.