hello all, I wish to know how I solve this problem = link
The problem asks to count number of distinct elements in a range for many query. There can be also modification of single element. Is any online solution exist? Maybe with implicit cartesian tree? Pls help.
sorry wrong
huh? can you pls describe your solution? I dont get it sorry
sorry wrong
you really dont like to explain
no no ... I was writing the explanation but it grew very long ... anyway I remembered this discussion which is better than anything I can say http://apps.topcoder.com/forums/?module=RevisionHistory&messageID=1369039
thank you very much. that is very nice solution. it cant be solved by cartesian tree right?
This solution can't handle updates, can it?
Let's pretend that there are no updates. In this case, it is a well-known problem. We can solve it offline using a BIT in
O((n + m) log n)
time(I can elaborate on it if necessary).Here is the trick: we will not really do any updates. Instead, we will process queries in batches of size
B
(let's chooseB = sqrt(N)
).For each batch, we can do the following:
2 * B
special numbers per batch. We can handle them naively(by maintaining a set of all occurrences).The time complexity is
O(N / B * N * log N)
(for non-special numbers) +O(B * N * log n)
(for special ones) =O(N * sqrt(N) * log(N))
(I assume thatN = M
to keep it simple). It is fast enough to get accepted. Here is my code.I think Mo's Algorithm would also work for offline one. Can you please elaborate the solution with BIT?
Let's iterate over the array from left to right. If the current element occurs for the first time, we simply add one to this position in the BIT. Otherwise, we also need to subtract one from the position of its last previous occurrence. After adding the
i
-th element, we can answer all the queries which haver = i
(the answer is simply a sum of the[l, r]
range). Mo's algorithm has anO(N * sqrt(N))
time complexity, so it is not feasible in this case because the total time complexity will beO(N^2)
, which seems to be too much(I haven't tried it, though).Why is it O(n^2)? What do you mean by saying total?
UPD: Btw, thank you for clear explanation.
To handle updates, we divide queries into blocks of size
sqrt(N)
. Then we solve the problem for each block separately. If we use Mo's algorithm, we get an sqrt-decomposition inside an sqrt-decomposition. It's pretty bad. To be more precise, the termO(N / B * N * log N)
becomesO(N / B * N * sqrt(N))
=O(N^2)
.You were talking about the one with the updates. I see now.
BIT SOLUTION Can you please explain How this is to be done using MO's algo
It's the same as naive O(n^2) solution. Only difference is that you sort queries before starting. Tutorial on Mo's Algorithm
thank you very much. that was very helpful. I still wonder can there exist an nlogn or faster solution?
This problem can also be solved with 2D tree with complexity O(log2N) per query.
Link: http://codeforces.com/blog/entry/10508?#comment-158835
A funny fact: a naive
O(N * M)
solution gets accepted.