madn's blog

By madn, history, 2 years ago, In English,

I've been recently thinking about this problem:

Given array of n integers and q queries( l, r pairs ). For each query you need to count number of inversions( i, j pairs such that i < j and a[i] > a[j] ) in subarray from l to r. And you have to answer the queries online(you're not given all queries beforehand).

I was able to come up with a solution that i'd describe below, but I'd like to know if it's possible to solve the problem with better complexity.

So, my solution:

1) preprocessing

Let's divide our array into sqrt(n) blocks, each sized sqrt(n), and merge-sort elements inside each, counting number of inversions in it. Then, for each pair of blocks, count number of inversions such that first element lies within first block and second lies within second block. We can do this using binary search, because each block is sorted already. After that, for each sqrt-block-range( [l, r] range such that l is a left border of one block and r is a right border of other block ) we'd find number of inversions in it by summing precalculated values( inversions in each block and inversions in pairs of blocks). There are around n pairs and ranges so we can do all preprocessing in O(n * sqrt(n) * log(n)). We also need to build merge-sort tree on the array for queries.

2) queries

We already know answer for sqrt-block-range that lies within query range. Then we just need to count inversions that include elements outside sqrt-block-range. There no more than 2 * sqrt of such elements, so we can do this with binary search in merge-sort tree in O(sqrt(n) * log^2(n)), or in O(sqrt(n) * log(n)) with partial cascading.

Overall complexity: O((n + q) * sqrt(n) * log(n)) with partial cascading.

So what are your thoughts, can it be solved faster?

P.S. Sorry for my poor English.

 
 
 
 
  • Vote: I like it
  • +19
  • Vote: I do not like it

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by madn (previous revision, new revision, compare).

»
2 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Edit: I missed something so this was wrong XD.

  • »
    »
    2 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    It is O(N lg N) for each query, because you merge sorting a segment of array, is not it?

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      yeah I had a mistake in my solution, I didn't mean to do it repeatedly for every query, but I definitely missed something. oops :P

»
10 months ago, # |
  Vote: I like it +13 Vote: I do not like it

yes, it can be (with something of classic sqrt-decomp and MO's algo)

1) divide array to blocks of size

2) calculate answers for all segments [L, r] where L — start of block and r — arbitrary index. Similary for all segments [l, R], R — end of the block. It needs two tables with memory.

3) answer on query [l, r]: find L and R such l ≤ L ≤ R ≤ r. preanswer is invs[L][r] + invs[l][R] - invs[L][R]. remains add inversions i, j such l ≤ i < L ≤ R < j ≤ r (on corners). it can be done with two pointers if you have sorted elements on each block.

So, 2) can be done in time (yes i not described it, try to think about dynamic prog) and 3) in per query