Putnam 2014 B6

Let f:[0,1]\to\mathbb{R} be a function for which there exists a constant K>0 such that |f(x)-f(y)|\le K|x-y| for all x,y\in [0,1]. Suppose also that for each rational number r\in [0,1], there exist integers a and b such that f(r)=a+br. Prove that there exist finitely many intervals I_1,\dots,I_n such that f is a linear function on each I_i and [0,1]=\bigcup_{i=1}^nI_i.