This is not the answer for which you are looking, but I have two suggestions.
First, I do not think that BIT is the right data structure to use for the problem you are trying to solve. BIT's advantage is that it maintains an O(lg n) queryable prefix sum using only O(lg n) per insertion. Since you aren't inserting once your data structure is completed, BIT is not advantageous (because you could use a simple prefix sum array, which is queryable in O(1)).
Second, I have a naive algorithm that uses O(n2) time and space to construct a data structure that can find range inversions in O(1) time:
First, construct an (n X n) matrix mapping the inversions, so that mat[i][j]=1
only if i<j
and arr[i]
and arr[j]
are inverted. Then, compute a prefix sum across each row of this matrix, so that mat[i][j]
is the number of inversions involving arr[i]
in the range [i,j]
. Finally, compute a suffix sum across each column, so that mat[i][j]
is the total number of inversions in the range [i,j]
.
for i from 0 to n-2
for j from i+1 to n-1
if(arr[j] > arr[i])
mat[i][j] = 1;
for i from 0 to n-2
for j from i+1 to n-1
mat[i][j] += mat[i][j-1];
for j from n-1 to 1
for i from j-1 to 0
mat[i][j] += mat[i+1][j];
This clearly takes O(n2) time and space, but the number of inversions in any range can be queried in constant time.