for (; j < N; j++) {
while (i < N && A[i] < B[j])
i++;
while (k < N && C[k] <= B[j])
k++;
res += ((long)i * (N - k));
}
Key: Iterating over the array B
. Because A
strictly less than B
, C
strictly greater than B
.
If A is used as the benchmark, the elements in B are found to be just greater than A, and the elements of C are just greater than B. What about now?
Example:
1 2 3
1 2 3
1 2 3
Based on 1 in A, B found 2, and C found 3. It cannot be calculated/determined in one step, that is, the elements of B have to be traversed, because the next element of B may not be smaller than the element of C at this time.
However, with B as the benchmark, it can be calculated/determined in one step. Find a number in A that is exactly smaller than B, and find a number in C that is just larger than B, and this a*c
is the result.