this problem was asked in the last week of code of hackerrank, I am having a hard time understanding the editorial can anyone help me with it?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
this problem was asked in the last week of code of hackerrank, I am having a hard time understanding the editorial can anyone help me with it?
Название |
---|
There are 3 cases for each query:
X = = Y or both of X, Y are not presented in array — all segments are good for us — it is N * (N - 1) / 2.
Only one of X or Y is not presented. Let's X is presented and Y not. So you need to calculate number of segments where frequency(l, r, X) = 0.
It's obvious that these segments placed between positions of X (we can imagine fictive X position before start of array and after it's end).
If there D elements between two positions of X — there are D * (D + 1) / 2 segments with freq(x) = 0 (any pair of these elements).
We can precalculate this value for every presented value with total complexity O(N).
Both X and Y are presented in array. Let's imagine two arrays of length N prefX[] and prefY[]: prefX[i] is number of X on segment [1..i] (prefY[i] is the same for Y).
So when segment [l..r] contains equal number of X and Y?
When prefX[r] - prefX[l - 1] = = prefY[r] - prefY[l - 1] — simply calculates number of X and Y on the segment.
But we can rewrite it: prefY[r] - prefX[r] = = prefY[l - 1] - prefX[l - 1].
If we have a lot of time — we can calculate all differences in O(N) per query and calculate frequency of each difference (remember about prefY[0] - prefX[0] = = 0 — there aren't any X or Y before array).
For some difference D with frequency freq[D] we should add to answer freq[D] * (freq[D] - 1) / 2 — each pair of indexes, but not the same.
Observation: difference is changed only when we meet X or Y in the array.
So we can iterate over all positions of X and Y in the sorted order (it can be done with 'merge' technique from mergesort in O(freq(X) + freq(Y)) with O(N) preprocessing positions of all elements.
At each position we change prefX or prefY, so current difference D = prefY - prefX is changed too.
After that we can update answer on the spot — we need add to answer freq[D] before updating it with current index (we add to answer all segments with end at this index).
Or we can do second cycle and add our formula value for each difference that we meet in first time.
Anyway, we do it with O(freq(X) + freq(Y)) complexity for any pair (X, Y).
It can be easily proved that total complexity for each possible pair (X, Y) when both are presented in the array is O(N2).
So we have solution with O(Q + N2) complexity.