Continuous function problem on $\mathbb{R}$ Can somebody please help me with this problem?



Let $f:\mathbb{R}\rightarrow\mathbb{R}$ such that $f([a,b])$ is an interval, for all $a,b\in\mathbb{R}$ such that $a &lt; b$. Also, $f^{-1}(r)$ is closed for all $r\in\mathbb{Q}$. Show that $f$ is contiuous on $\mathbb{R}$.



Thanks in advance.
Since you’re asking for help, here is an intentionally incomplete solution. It suffices to show that $f$ is continuous on the left and on the right at every point.
 Let us show for example that $f$ is right-continuous  at a point $x\in{\mathbb R}$.

For $\delta \gt 0$, there are two nonnegative numbers (which we will denote by $A(\delta)$ and
$B(\delta)$) such that $f([x,x+\delta])=[f(x)-A(\delta),f(x)+B(\delta)]$ (why ?)
  The functions $A$ and $B$ are nonincreasing in $\delta$ (why?)
  So the limits $a=\lim_{\delta \to 0, \delta \gt 0} A(\delta)$ and $b=\lim_{\delta \to 0, \delta \gt 0} B(\delta)$ exist ; our job is to show that those limits are in fact zero.

Suppose for example that $a\gt 0$. Then there is a rational $r$ such that $f(x)-a \lt r \lt f(x)$. Show that the closed set $C=f^{-1}(r)$ must contain $x$, and derive a contradiction from that.
Since you’re asking for help, here is an intentionally incomplete solution. It suffices to show that $f$ is continuous on the left and on the right at every point.
 Let us show for example that $f$ is right-continuous  at a point $x\in{\mathbb R}$.

For $\delta \gt 0$, there are two nonnegative numbers (which we will denote by $A(\delta)$ and
$B(\delta)$) such that $f([x,x+\delta])=[f(x)-A(\delta),f(x)+B(\delta)]$ (why ?)
  The functions $A$ and $B$ are nonincreasing in $\delta$ (why?)
  So the limits $a=\lim_{\delta \to 0, \delta \gt 0} A(\delta)$ and $b=\lim_{\delta \to 0, \delta \gt 0} B(\delta)$ exist ; our job is to show that those limits are in fact zero.

Suppose for example that $a\gt 0$. Then there is a rational $r$ such that $f(x)-a \lt r \lt f(x)$. Show that the closed set $C=f^{-1}(r)$ must contain $x$, and derive a contradiction from that.