Count ordered sequences of length $N$ such that each element $i$ is in the range $[a_i, b_i]$

60 Views Asked by At

We have given two arrays $A$ and $B$, both of length $N$, we want to count number of strictly increasing sequences of length $N$, and each element of the sequences should be in range $[A_i, B_i]$.

For example if $n = 2$, $A=\{1, 3\}, B = \{2, 4\}$. The result should be $4$, the sequences are $\{1, 3\}, \{1, 4\}, \{2, 3\}, \{2,4\}$.

I think that this can be solved in $O(N \cdot Max), Max \text{ - maximum element of B }$ memory and time complexity. If we define $f(i, j) - \text{ count of sequences of length i with last element j } = \sum_{a_i}^{j-1} f(i-1, k)$.

Can we do better than this, becouse $Max$ can be very big.