Like in this answer, you can use intersections here.
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds,calc,intersections}
\begin{document}
\begin{tikzpicture}
\draw[thick,name path=A] plot[smooth] coordinates {(0,0) (1,0.8) (2,1) (3.5,0.6) (4,0)};
\begin{scope}[on background layer]
\foreach \X [count=\Z] in {0,0.5,...,4}
{\path[name path global=v-\Z,overlay] (\X,0) -- (\X,0|-current bounding box.north);
\draw[name intersections={of=A and v-\Z,by=i-\Z},blue,fill=blue!20]
\ifnum\Z>1
let \p1=(i-\the\numexpr\Z-1),\p2=(i-\Z) in
(\X-0.5,0) rectangle (\X,{max(\y1,\y2)})
\fi;
\draw[name intersections={of=A and v-\Z,by=i-\Z},blue,fill=blue!80]
\ifnum\Z>1
let \p1=(i-\the\numexpr\Z-1),\p2=(i-\Z) in
(\X-0.5,0) rectangle (\X,{min(\y1,\y2)})
\fi;}
\end{scope}
%
\begin{scope}[yshift=-2cm]
\draw[thick,name path=B] plot[smooth] coordinates {(0,0) (1,0.8) (2,1) (3.5,0.6) (4,0)};
\begin{scope}[on background layer]
\foreach \Y [count=\Z] in {0,0.2,0.4,0.6,0.8,1}
{\path[name path global=h-\Z,overlay] (0,\Y) -- (4,\Y);
\draw[name intersections={of=B and h-\Z,by={i-\Z-1,i-\Z-2}},red,fill=red!20]
\ifnum\Z>1
let \p1=(i-\the\numexpr\Z-1\relax-1),\p2=(i-\the\numexpr\Z-1\relax-2),
\p3=(i-\Z-1),\p4=(i-\Z-2)
in
({min(\x1,\x3)},\Y-0.2) rectangle ({max(\x2,\x4)},\Y)
\fi;
\draw[name intersections={of=B and h-\Z,by={i-\Z-1,i-\Z-2}},red,fill=red!60]
\ifnum\Z>1
let \p1=(i-\the\numexpr\Z-1\relax-1),\p2=(i-\the\numexpr\Z-1\relax-2),
\p3=(i-\Z-1),\p4=(i-\Z-2)
in
({max(\x1,\x3)},\Y-0.2) rectangle ({min(\x2,\x4)},\Y)
\fi;
}
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}
