Let $f$ be a $C^\infty$ function on $(c,d)$ ,and let $O=\cup_{n\in \mathbb{Z}^+} (a_n,b_n)$ where $(a_n,b_n)$ are disjoint open interval in $(c,d)$ and $O$ is dense in $(c,d)$. Suppose for each $n\in \mathbb{Z}^+$ ,$f$ coincides with a polynomial on $(a_n,b_n)$. Is it necessary that $f$ coincide with a polynomial on $(c,d)$?

I believe the answer is "no". The key lemma is: Lemma. Let $f: [c,d] \to {\bf R}$ be smooth, let $I$ be a compact subinterval of $(c,d)$, $q$ be an interior point of $I$, let $n \geq 1$, and let $\varepsilon > 0$. Then there exists a smooth perturbation $g: [c,d] \to {\bf R}$ of $f$ which agrees with $f$ outside of $I$, is a polynomial on a neighbourhood of $q$ of degree at least $n$, but is not a polynomial on all of $I$, and differs from $f$ by at most $\varepsilon$ in $C^n[c,d]$ norm. If we apply this lemma iteratively for $n=1,2,\ldots$ with $\varepsilon = \varepsilon_n := 2^{n}$ and $n=0,1,2,\ldots$, starting with $f_0 = 0$, and setting $q = q_n$ to be the first rational (in some enumeration of the rationals) on which $f_n$ is not locally polynomial, one obtains a sequence $f_1, f_2, f_3,\ldots$ of smooth functions on $[c,d]$ which form a Cauchy sequence in $C^k$ for each $k$, and thus converge in the smooth topology to a limit $f$ which is equal to a polynomial on degree at least $n$ on an interval $I_n$, with the $I_n$ disjoint and covering all the rationals, thus dense in $[c,d]$, giving the claim. To prove the lemma, recall from the Weierstrass approximation theorem that the polynomials are dense in $C^0[c,d]$; integrating this fact repeatedly we see that they are dense in $C^n[c,d]$ as well. So we can approximate $f$ to arbitrary accuracy by a polynomial $h$ in the $C^n$ norm; by a small perturbation one can ensure that $h$ has degree at least $n$. Now using a smooth partition of unity, one can create a merged function $g$ that equals $h$ near $q$ and equals $f$, which can be made arbitrarily close in $C^n$. By modifying $g$ a little bit in $I$ away from $q$ one can ensure that $q$ is not polynomial on all of $I$. The problem here is superficially similar to that in the previous question If $f$ is infinitely differentiable then $f$ coincides with a polynomial , but the latter has qualitative control at every single point (allowing the powerful Baire category theorem to come into play), whereas here one only has qualitative control on a dense set, which is a far weaker statement, and one which allows for a great deal of flexibility. 


No. Say that a function $f(x)$ is locally a polynomial at $x_0$ if there is some neighborhood of $x_0$ upon which $f(x)$ coincides with a polynomial. In
the author constructs an example of a $C^\infty$ function that is not a polynomial but is nonetheless locally a polynomial on a dense set. Since the set of points at which a function is locally a polynomial is open, hence a countable union of disjoint intervals, the answer to your question follows. 


The existence of such functions being established by now, there remains a curiosity for the concrete situations that may generate them. Here is one, which is also interesting as an example of a $C^\infty$, nonanalytic solution, of a simple first order linear DDE. Let's consider the problem $$f'(x)=\lambda f(3x),\qquad x < \frac{1}{2} $$ where we seek for a function $f\in C^1(\mathbb{R})$ with support in $I:=[0,1]$, and with the condition $f(x)=f(1x)$. I will show that it has a simple eigenvalue $\lambda= \frac{9}{2}$, corresponding to an eigenfunction with $f(\frac{1}{2})=1$ (see the details below). Then, it is very easy to check that $f$ is locally polynomial on the complement of the Cantor set; and it is, of course, $C^\infty$. Indeed, since $f (3x)=0$ for $ x > \frac{1}{3}$, from the equation we have that $f'(x)$ vanishes identically on the interval $(\frac{1}{3},\frac{2}{3})$, so that $f$ is the constant $1$ therein. By induction, it is easy to see that $f$ is polynomial of degree $k1$ on each component of the complement of the closed set $F_k$ in $I$, inductively defined by $F_0:=I$ and $F_{k+1}:= \frac{1}{3}F_k \cup\left(\frac{1}{3}F_k + \frac{2}{3}\right)$. Note that $\{F_k\} _ {k\in\mathbb{N}}$ is the wellknown nested sequence of closed sets whose intersection is the Cantor set. $$*$$ Construction. Consider the linear operator $T$ on $L^\infty(0,1)$ (thought as subspace of $L^1(\mathbb{R})$ via extension by zero), defined by $$Tu(x)= \cases{ \int_0^{3x}u(t)\, dt\qquad \qquad 0 < x < \frac{1}{3}\\\ 0 \qquad \qquad \qquad \qquad \frac{1}{3} < x < \frac{2}{3}\\\ \int_0^{3(1x)}u(t)\, dt \qquad\frac{2}{3}< x < 1.} $$ Since $T$ and $T ^ 2$ are integral operators with nonnegative kernels, their norms are attained on the constant function $1$ on $[0,1]$. We have, by simple computations $T1(x)=3\min(x,1x)\chi _ { F _ 1 }(x) $, $\T\ = \T1\ _ \infty=1$ and $\T^2\=\T(T1)\_\infty=\frac{1}{3}$. Therefore $\rho(T)\le\frac{1}{\sqrt 3} < \frac{2}{3}$ and $I\frac{3}{2}T$ is invertible. The function $$f:= \left(I\frac{3}{2}T\right) ^ {1}\chi _ {[\frac{1}{3},\frac{2}{3}]} = \sum _ {k=0} ^ \infty \left( \frac{3}{2}\right) ^ k T ^ k \chi _ { [\frac{1}{3}, \frac{2}{3}] } $$ verifies $$f(x)=\frac{3}{2} \int_0^{3x}f(t)\; dt\qquad \mathrm{for\; all}\quad 0 < x <
\frac{1}{3}\, ,$$ and $$f(x)=f(1x)\qquad \mathrm{for\, all}\quad x\in\mathbb{R} \, .$$ The only points in $\mathbb{R}$ where the continuity of $f$ is not immediate and has to be checked, are $\frac{1}{3}$ and $\frac{2}{3}$. We have $$\int_ 0 ^ 1 f(x)dx=\int _ 0 ^ \frac{1}{3} f(x)dx+\int _ \frac{1}{3} ^ \frac{2}{3} f(x)dx+\int _ \frac{2}{3} ^ 1 f(x)dx= 2\int _ 0 ^ \frac{1}{3} f(x)dx+ \frac{1}{3}\, ,$$ and $$ 2 \int _ 0 ^ \frac{1}{3} f(x)dx = 3\int _ 0 ^ \frac{1}{3} \int _ 0 ^ {3x} f(t) dt dx= \int _ 0 ^ 1 \int _ 0 ^ x f(t) dt dx=$$ $$= \int _ 0 ^ 1 (1t) f(t) dt = \int _ 0 ^ 1 tf(1t)dt= \int _ 0 ^ 1 tf(t)dt = $$ $$ =\frac{1}{2}\left(\int _ 0 ^ 1 (1t)f(t)+ tf(t)dt\right) =\frac{1}{2} \int _ 0 ^ 1 f(t)dt\, . $$ So $$ \int _ 0 ^ 1f(t)dt = \frac{2}{3}\, ,$$
which implies the continuity of $f$ at $ \frac{1}{3}$ (and by symmetry at $
\frac{2}{3}$ ), since Here is a Pudding Function $f$, and its integral function, $F(x):=\int_0^x f(t)dt$. Note how the latter coincides with the first third of the former, up to rescaling. Simple considerations based on odd and even symmetry show that $F(1)=\frac{2}{3}$. 


The following is wrong, but the comments are really nice :) Suppose That on $(a_1, b_1)$, $f$ is a polynomial of degree $N$. This means that in its Taylor series at any point $x \in (a_1, b_1)$, every coefficiant past the $(N+1)$st vanishes. This must also be true for the border points, $x = a_1$ and $x=b_1$. However, because the intervals are dense in $O$, $a_1$ or $b_1$ lies in the closure of some other interval. The rest is something like an induction: It follows that on every set $(a_n, b_n)$, the coefficients past the $(N+1)$st vanish. This shows that $f$ is a polynomial, by the fundamental theorem of calculous. 

