Proof of the theorem. Let $\Omega\subset\mathbb{R}$ be the union of all open intervals $(a,b)\subset\mathbb{R}$ such that $f|_{(a,b)}$ is a polynomial. The set $\Omega$ is open, so $$ \Omega=\bigcup_{i=1}^\infty (a_i, b_i)\, , \qquad \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (1) $$$$ \Omega=\bigcup_{i=1}^N (a_i, b_i)\, , \qquad \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (1) $$ where $a_i<b_i$ and $(a_i, b_i)\cap (a_j, b_j) = \emptyset$ for $i\neq j$, $1\leq N\leq\infty$. Observe that $f|_{(a_i, b_i)}$ is a polynomial (Why?)*. We want to prove that $\Omega=\mathbb{R}$. First we will prove that $\overline{\Omega}=\mathbb{R}$. To this end it suffices to prove that for any interval $[a,b]$, $a<b$ we have $[a,b]\cap\Omega\neq\emptyset$. Let $$ E_n=\{x\in\mathbb{R}:\, f^{(n)}(x)=0\}\, . $$ The sets $E_n\cap [a,b]$ are closed and $$ [a,b]=\bigcup_{n=0}^\infty E_n\cap [a,b]\, . $$ Since $[a,b]$ is complete, it follows from the Baire theorem that for some $n$ the set $E_n\cap [a,b]$ has nonempty interior (in the topology of $[a,b]$), so there is $(c,d)\subset E_n\cap [a,b]$ such that $f^{(n)}=0$ on $(c,d)$. Accordingly $f$ is a polynomial on $(c,d)$ and hence $$ (c,d)\subset\Omega\cap [a,b]\neq\emptyset. $$ The set $X=\mathbb{R}\setminus\Omega$ is closed and hence complete. It remains to prove that $X=\emptyset$. Suppose not. Observe that every point $x\in X$ is an accumulation point of the set, i.e. there is a sequence $x_i\in X$, $x_i\neq x$, $x_i\to x$. Indeed, otherwise $x$ would be an isolated point, i.e. there would be two intervals $$ (a,x),\, (x,b)\subset\Omega,\ x\not\in\Omega\, . \qquad \ \ \ \ \ \ \ \ \ \ \ \ (2) $$ The function $f$ restricted to each of the two intervals is a polynomial, say of degrees $n_1$ and $n_2$. If $n>\max\{ n_1, n_2\}$, then $f^{(n)}=0$ on $(a,x)\cup (x,b)$. Since $f^{(n)}$ is continuous on $(a,b)$, it must be zero on the entire interval and hence $f$ is a polynomal of degree $\leq n-1$ on $(a,b)$, so $(a,b)\subset\Omega$ which contradicts (2).