Enumerates the locations of C, the number of methods in each enumeration is the number of 1~c-1 (A, b) multiplied by the number of C~n (C,D). The answer is summed up.
The number of 1~c-1 (A, A, b) is the same as the position of the enumeration, and then calculates the number of seconds before B is smaller than a[b], the value to be saved, the next time you enumerate C, the value plus c-1 before the number of a[c-1] is smaller than the current situation in 1~c-1 (A, b), that is, the number of =c-1, because the situation before enumeration B has been counted.
Because when you calculate C, only the number of methods smaller than C is calculated, this only satisfies the number of the two-tuple smaller than c. The two-tuple in front of c is counted as well, so add a small number to the C in front of c.
Use a tree-like array to do it. The subject n range 50000, and each number is different is critical. So we open n positions, at first each position is 0, in fact each position is not 0 is 1, because each number only one.
For example 1 3 2 4 5
First C array 0 0 0 0 0
First count, then enter, because calculate a[i] before how much smaller than its number, not including its own, while the tree array calculation and the time to include its own.
I=1, the tree-like array with the sum prefix and the pre[1]=0,
At this point 0 0 0 0 0, enter 1, become 1 0 0 0 0
I=2,a[2]=3, to see 3 in front of how many number, that is to see the C array of 3 positions in front of how many 1, 1 means that has been entered,
Found 1 0 0 0 0 The first three digits are only one 1, which is pre[2]=1 (only 1 smaller than the second number entered), after entering 3, the C array becomes 1 0 1 0 0
I=3, a[3]= 2, to see how many numbers in front of 2, that is to see the C array before 2 positions in front of how many 1, found that 10100 the first two numbers only one 1, that is pre[3]=1
Prefix and can be calculated, then suffix and = num2[i] = n-i-a[i]+num[i]+1;