kindly help in problem 1677A - Tokitsukaze and Strange Inequality,i dont want to use data structure like fenwick tree.I tried to understand it from editorial but am unable to.
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
kindly help in problem 1677A - Tokitsukaze and Strange Inequality,i dont want to use data structure like fenwick tree.I tried to understand it from editorial but am unable to.
Name |
---|
Auto comment: topic has been updated by BLUmOOn (previous revision, new revision, compare).
Fix indices a and c such that $$$p_a<p_c$$$ as given in the condition. Then count the number of inversions in the range [a+1,n]. This can be precomputed for all values of a in $$$O(N^2)$$$ or can also be calculated individually for each a as you traverse using an ordered_set or segment tree in $$$O(NlogN)$$$ for each a, resulting in a total of $$$O(N^2logN)$$$. Even though, I doubt that the second approach will pass the given constraints or might just barely manage to escape TLE.
Let the number of inversions in range [i,j] be denoted by $$$Inv(i,j)$$$. Even without precomputation Inv(i,j) can be calculated in $$$O(NlogN)$$$ as mentioned above.
Now, for each index a, the Inv(a+1,n) will contain pair such that both b and d lie in range [a+1,c] or both lie in range [c,n]. Since we want to avoid those cases as it violates the condition $$$a<b<c<d$$$, we subtract $$$Inv(a+1,c)$$$ and $$$Inv(c,n)$$$ from $$$Inv(a+1,n)$$$.
Hence the final answer will be
thanks for the reply,what are u defining inv(i,j) to be?Won't subtracting inv(a+1,c) mean we are subtracting some pairs of (b,d) that would instead should have been counted.
No, if we count pairs of inversions in the range [a+1,c] that would just mean we are counting all those pairs of {b,d} which satisfy $$$b<d$$$ and $$$p_b>p_d$$$ and lie in range [a+1,c]. But this means that, $$$a<b<d<=c$$$, which violates the condition on indices.
what is inv(i,j),please define it
inv[i,j] = count of all pairs x, y in the range i, j st x < y and a[x] > a[y].
we are fixing one of the conditions iteratively (a and c) now we only need to handle b, d. we actually need inversions in st one the element of the pair lies inside the range of a and c and the other one lies beyond c. We can calc it as he clearly explained the his first commen. You only need to precompute the inversions in range. As for the preconputation you can follow a strategy of picking an inverted pair i, j and using a 2d difference array to update all the subarrays where both of these indices come. You initialise the difference array such that the prefix sum of it will add 1 to the region where its row <= i and column >= j. At the end prefix sum the entire difference array to obtain your precalculation of inversions in n^2
can u please elaborate on how u calculated inv(x,y),rest all i have understood.how can i precompute no. of pairs {x,y} in range[i,j] st x<y and a[x]>a[y]
I have explained exactly that in my comment above, anyway here 280160949 is an implementation of the problem, study the part where I use difference arrays to calculate the inversions in range.
You might wanna familiarise yourself with 2D prefix sums.