4. Let $a_{1}, a_{2}, \cdots, a_{20} \in\{1,2, \cdots, 5\}, b_{1}, b_{2}, \cdots, b_{20} \in\{1,2, \cdots, 10\}$, and the set $X=$ $\left\{(i, j) \mid 1 \leqslant i<j \leqslant 20,\left(a_{i}-a_{j}\right)\left(b_{i}-b_{j}\right)<0\right\}$. Find the maximum number of elements in $X$.
Consider a set of positive integers $\left\{a_{1}, a_{2}, \cdots, a_{20}, b_{1}, b_{2}, \cdots, b_{20}\right\}$.
For $k=1,2,3,4,5$, let $a_{1}, a_{2}, \cdots, a_{20}$ have $t_{k}$ numbers with value $k$. According to the definition of $X$, when $a_{i}=a_{j}$, $(i, j) \notin X$, so there are at least $\sum_{k=1}^{5} C_{t_{k}}^{2}$ pairs $(i, j)$ not in $X$.
Notice that $\sum_{k=1}^{5} t_{k}=20$. By the Cauchy-Schwarz inequality, we have
$$
\sum_{k=1}^{5} C_{t_{k}}^{2}=\frac{1}{2}\left(\sum_{k=1}^{5} t_{k}^{2}-\sum_{k=1}^{5} t_{k}\right) \geqslant \frac{1}{2}\left[\frac{1}{5}\left(\sum_{k=1}^{5} t_{k}\right)^{2}-\sum_{k=1}^{5} t_{k}\right]=30,
$$

Thus, the number of elements in $X$ is at most $C_{20}^{2}-30=190-30=160$.
On the other hand, take $a_{4 k-3}=a_{4 k-2}=a_{4 k-1}=a_{4 k}=k, k=1,2, \cdots, 5$;
$b_{i}=6-a_{i}, i=1,2, \cdots, 20$, then for any $1 \leqslant i<j \leqslant 20$,
$\left(a_{i}-a_{j}\right)\left(b_{i}-b_{j}\right)=\left(a_{i}-a_{j}\right)\left[\left(6-a_{i}\right)-\left(6-a_{j}\right)\right]=-\left(a_{i}-a_{j}\right)^{2} \leqslant 0$, equality holds if and only if $a_{i}=a_{j}$, which happens exactly $5 C_{4}^{2}=30$ times. In this case, the number of elements in $X$ is 160. In summary, the maximum number of elements in $X$ is 160.