Number of ordered quadruples How many ordered tuples $(x_1,x_2,x_3,x_4)$ exist such that
$$
\begin{align}
L_1\le & x_1\le R_1 \\
L_2\le & x_2\le R_2 \\
L_3\le & x_3\le R_3 \\
L_4\le & x_4\le R_4 \\
x_1 & \neq x_2 \\
x_2 & \neq x_3 \\
x_3 & \neq x_4 \\
x_4 & \neq x_1
\end{align}
$$
$
x_1, x_2, x_3, x_4, L_1, R_1, L_2, R_2, L_3, R_3, L_4, R_4  \in  \mathbf I
$
 A: With $\left(i, j\right) \in I = \left\{\left(1,2\right),
\left(2,3\right), \left(3,4\right), \left(4,1\right)\right\}$, let
$A_{ij}$ be the set of all tuples with the $i^{th}$ and $j^{th}$
coefficients the same.
Start with the set of all possible tuples within the ranges
$\left[L_{i}, R_{i}\right]$ including the "matching neighbour" ones
that we should be excluding. This set has cardinality
$\prod_{i=1}^{4}{(R_i - L_i + 1)}$.
From this set we need to exclude members of set $A_{12}\cup A_{23}
\cup A_{34} \cup A_{41}$, which contains the tuples with a matching
neighbour. Use the inclusion-exclusion principle to enumerate this
set:
\begin{eqnarray*}
\vert{ \bigcup_{\left(i,j\right)\in I}{A_{ij}}}\vert &=&
\sum_{\left(i,j\right)\in I}{\vert A_{ij}\vert} \\
 &-& \sum_{}{\vert
A_{ij}\cap A_{kl}\vert } \\
&+& \sum_{}{\vert A_{ij}\cap A_{kl}\cap A_{mn}\vert } \\
&-& \vert A_{12}\cap A_{23}\cap A_{34}\cap A_{41}\vert
\end{eqnarray*}
We have:
\begin{eqnarray*}
\vert A_{ij}\vert &=& X_{ij}\left(R_k - L_k + 1\right)\left(R_l -
L_l + 1\right)\qquad\mbox{ where }
\left(k, l\right) \in I, \mbox{ and } k,l\notin \left\{i, j\right\} \\
&&\qquad\qquad\qquad\mbox{and } X_{ij} = \vert\left[ L_i,
R_i\right]\cap\left[ L_j, R_j\right]\vert\\ &&\mbox{(i.e. number of
common integers in coefficients $i$ and $j$).}
\end{eqnarray*}
\begin{eqnarray*}
\vert A_{ij}\cap A_{jk}\vert &=& X_{ijk}\left(R_l - L_l +
1\right)\qquad\mbox{where } l\notin \left\{i, j, k\right\}
\\
&&\qquad\qquad\qquad\mbox{and } X_{ijk} = \vert\left[ L_i,
R_i\right]\cap\left[ L_j, R_j\right]\cap\left[ L_k, R_k\right]\vert
\end{eqnarray*}
$\vert A_{ij}\cap A_{kl}\vert = X_{ij}X_{kl}\qquad$where $k,l\notin
\left\{i, j\right\}$
$\vert A_{ij}\cap A_{jk}\cap A_{kl}\vert = X_{ijkl}\qquad$ where
$X_{ijkl} = \vert\left[ L_i, R_i\right]\cap\left[ L_j,
R_j\right]\cap\left[ L_k, R_k\right]\cap\left[ L_l, R_l\right]\vert$
$\vert A_{12}\cap A_{23}\cap A_{34}\cap A_{41}\vert =
X_{ijkl}\qquad$ since any member of set $A_{ij}\cap A_{jk}\cap
A_{kl}$ is also a member of $A_{li}$.
So the number needed is:
\begin{eqnarray*}
\prod_{i=1}^{4}{(R_i - L_i + 1)} &-& \sum_{}{X_{ij}\left(R_k - L_k +
1\right)\left(R_l - L_l + 1\right)}\qquad\left[\mbox{4 summands} \right]\\
&+& \sum_{}{X_{ijk}\left(R_l - L_l + 1\right)} +
\sum_{}{X_{ij}X_{kl}}\qquad\left[\mbox{4 and 2 summands} \right] \\
&-& \sum_{}{X_{ijkl}}\qquad\left[\mbox{4 summands} \right]\\
&+& X_{1234}.
\end{eqnarray*}
A simple example: Let $L_i = 0, R_i = 1$ for $i = 1,2,3,4$.
The only valid tuples: $\left(0,1,0,1\right)$ and
$\left(1,0,1,0\right)$ so we expect answer to be $2$.
We have $X_{ij} = X_{ijk} = X_{ijkl} = 2$.
The formula above gives:
\begin{eqnarray*}
Ans &=& 2^4 - 4\left(2\times2\times2\right) + 4\left(2\times2\right)
+ 2\left(2\times2\right) - 4\times2 + 2\\
&=& 16 - 32 + 16 + 8 - 8 + 2\\
&=& 2
\end{eqnarray*}
