Soon the Feb long challenge starts, and I decided to share the most amazing things for me in this blog:
**1.**Min_25’s blog: https://min-25.hatenablog.com the ideas are of high-level there
**2.**The k-th ordered statistics queries with an update of the next type: l r x y
For i=l..r
If a[i] == x
a[i] = y
a[i], y <= N
**3.**My problem idea:
Given a permutation of length N and Q queries to it:
Swap(p[x], p[y])
Tell how many pairs of elements between l and r have differs with the value equals to K
So far… The solution for the second problem is SQRT-decomposition
Let’s split our array into \sqrt{N} blocks, each of the length \sqrt{N}. And for each block let’s store own sqrt-decomposition. Do you see how the queries and updates can be processed?
Let’s notice that all elements are unique, what it gives us
Subproblem 1:
K is fixed, the answer is no more than N, we can store the values = 1 in the points (X, Y), where |p[x] - p[y]| = K. Do you see how to do updates and queries in quick O(log^2) time?
Consider the summation on sub-matrix[L..R, L..R]
Subproblem 2:
Imagine we solve the problem without updates, then if we somehow know all the elements in the range l..r then we can find the answer by (set \& (set < < k)).count() if you’re familiar with C++ bitset. We can find the set with the Mo’s algorithm, don’t forget that there is boosted Mo’s algorithm with Hilbert order.
And updates can be handled with Mo’s algorithm with updates:
So, the total complexity is O(Q (N^(2/3) + N/64)), I haven’t seen such complexities before
Feel free to ask questions, I can add links to the algorithms I mentioned. Also, to explain the things more clearly.
@whzzt suggested simple O(Q sqrt(N) + Q N / 64) solution for the third problem.
Let’s store the bitsets of all elements on the prefixes, i.e. the set of first \sqrt{n} elements, the first 2*\sqrt{n} elements and so on.
When you need to answer the query, just take 2 bitsets, operate with not more than 2*\sqrt{n} elements. Fir the update we should go with updating all of the prefix bitsets.
So, query is K*(the \ size \ of \ block)+N/64, and update is N/K,