公式

D - Prefix Bubble Sort 解説 by evima


For operation \(k\), the integers included in the first \(k\) elements of \(P\) are referred to as “target integers.” Additionally, for \(P_i = x\), the number of \(j\) that satisfy \(P_j > P_i\) and \(j < i\) is referred to as the “contribution of \(x\) to the inversion number.”

When operation \(k\) is performed, for a target integer \(x\), the number of integers greater than \(x\) to its left (if they exist) will always decrease by \(1\) due to the operation. Also, since \(A\) is non-decreasing, once \(x\) becomes a target integer, it will always be a target integer in subsequent operations. Therefore, if \(x\) first becomes a target integer at the \(l_x\)-th operation, and the number of integers greater than \(x\) to its left in the initial state is \(inv_x\), then in each of the \(l_x\)-th to \(\min(M, l_x + inv_x)\)-th operations, the contribution of \(x\) to the inversion number decreases by \(1\), and beyond that, the contribution of \(x\) to the inversion number does not change.

From the above, if we can find \(l_x\) and \(inv_x\) for each \(x\), the change in the inversion number by each operation can be found by accumulating adjacent differences. \(l_x\) and \(inv_x\) can be found in \(O(N \log M)\) and \(O(N \log N)\) time, respectively, using binary search and a Fenwick tree.

投稿日時:
最終更新: