5. For the inequality about $x$ $\frac{x^{2}+\left(2 a^{2}+2\right) x-a^{2}+4 a-7}{x^{2}+\left(a^{2}+4 a-5\right) x-a^{2}+4 a-7}<0$, the solution set is the union of some intervals, and the sum of the lengths of these intervals is less than 4. Find the range of real number $a$.
(2001 Shanghai High School Competition Question)
5. Consider the relationship between the coefficients of the quadratic terms in the numerator and denominator on the left side of the inequality:
$$
\left(a^{2}+4 a-5\right)-\left(2 a^{2}+2\right)=-a^{2}+4 a-7 \text {. }
$$

Let the roots of the quadratic equations in $x$ be:
$$
x^{2}+\left(2 a^{2}+2\right) x-a^{2}+4 a-7=0, \quad x^{2}+\left(a^{2}+4 a-5\right) x-a^{2}+4 a-7=0
$$
with roots $x_{1}$ and $x_{2}\left(x_{1}<x_{2}\right)$, and $x_{3}$ and $x_{4}\left(x_{3}<x_{4}\right)$, respectively.
Note that $x_{1} x_{2}=x_{3} x_{4}=-a^{2}+4 a-7=-a(a-2)^{2}-3<0$,
$$
\left(x_{1}+x_{2}\right)-\left(x_{3}+x_{4}\right)=\left(a^{2}+4 a-5\right)-\left(2 a^{2}+2\right)=-a^{2}+4 a-7<0 \text {, }
$$

Therefore, the order of $x_{1}, x_{2}, x_{3}, x_{4}$ is $x_{1}<x_{3}<x_{2}<x_{4}$.
Thus, the solution set of the original inequality is $\left(x_{1}, x_{3}\right) \cup\left(x_{2}, x_{4}\right)$.
By the problem, $\left(x_{3}-x_{1}\right)+\left(x_{4}-x_{2}\right) \geqslant 4$, which means $a^{2}-4 a+7 \geqslant 4$.
Solving this, we get $a \leqslant 1$ and $a \geqslant 3$.
Therefore, $a \in(-\infty, 1] \cup[3,+\infty)$.