Continuity of function with intermediate values

Suppose that $f\colon\Bbb R\to\Bbb R$ takes intermediate values, i.e. if $f(a) < x < f(b)$ for some $a,b$ then $x = f(t)$ for some $t$ between $a$ and $b$. Suppose also that the set of all $x$ such that $f(x) = r$ is closed for all $r \in\Bbb Q$. Prove that $f$ is continuous.

So I understand the Intermediate Value theorem, and generally know how to show that a function is continuous, but could I have a hint to help me get started on this question?

-
Assume that $f$ is not continuous at $x$. Find a sequence $(x_n)$ that is monotone, has limit $x$, and is such that $|f(x_n)-f(x)|>\delta$ for some $\delta>0$ and all $n$. Using this sequence, find a rational number $r\ne f(x)$ and a sequence $(y_n)$ with limit $x$ such that $f(y_n)=r$ for each $n$. This will contradict the closedness condition. – David Mitra Apr 5 '13 at 1:31

I will complete the comment given by David:

Suppose that $f$ is not continuous at some $x \in \mathbb{R}$. Then by definition of sequential continuity there exists some sequence $z_n$ with $\lim_{n \to \infty}z_n = x$ but $\lim_{n \to \infty}f(z_n) \neq f(x)$. That is, there is some $\delta > 0$ such that for any $N \in \mathbb{N}$ there is some $n > N$ such that $f(z_n) \not \in (f(x)-\delta,f(x)+\delta)$. Clearly we can pick a subsequence $z_{n_k}$ of $z_n$ such that for all $k \in \mathbb{N}$ we have $f(z_{n_k}) \not \in (f(x)-\delta,f(x)+\delta)$. From this subsequence we can pick again a subsequence $z_{n_{k_l}} =: x_l$ that is monotone, for every sequence there exists a monotone subsequnce.

Without loss of generality we can assume that $x_l$ is increasing. Because $f(x_l) \not \in (f(x)-\delta,f(x)+\delta)$ for any $l \in \mathbb{N}$ we have either $f(x_l) \leq f(x) - \delta$ or $f(x_l) \geq f(x) + \delta$ infinitely many times. Suppose its the former one (We can do this again without loss of generality). Pick some arbitrary $r \in \mathbb{Q}$ such that $r \in (f(x) - \delta, f(x))$ (in particular $r \neq f(x)$)

Then by intermediate value property of $f$ for every $l_i$ with $f(x_{l_i}) \leq f(x) - \delta$ there is some $y_i \in [x_{l_i}, x]$ such that $f(y_i) = r$. Since $\lim_{i \to \infty}x_{l_i} = x$ we also have $\lim_{i \to \infty}y_{i} = x$. But then $r = \lim_{i \to \infty}f(y_i) \neq f(\lim_{i \to \infty}y_i) = f(x)$, contradicting the closedness of $\{x|f(x) = r\}$. Therefore $f$ is continuous at $x$ and thus at every point in $\mathbb{R}$.

-

I would say that David constructed his proof because he saw the picture. When a student sees the sequence construction, however, it can appear magical and you get the feeling that, since these things don't occur to you, maybe you should switch into Renaissance Poetry.

Here is a sketch that you can picture. Consider $\limsup_{t\to 0+} f(x+t)$. Let's eliminate the possibility that $\limsup_{t\to 0+} f(x+t)> f(x)$. If it is greater, then take any rational $r$ with $$\limsup_{t\to 0+} f(x+t)> r > f(x).$$ Because of this and the intermediate value property the level set $f^{-1}(\{r\})$ must contain points in the interval $(x,x+\delta)$ for every $\delta>0$. But the level set is closed and so $f^{-1}(\{r\})$ contains $x$, which of course it doesn't.

The possibility that $\limsup_{t\to 0+} f(x+t)< f(x)$ is even easier to eliminate since that needs only the intermediate value propery (the other condition is not needed).

At this point one usually says the remaining cases are similar. (There are notorious situations where "the other cases" were not similar, but here they are.)

-