I try to understand the following steps from this lecture, (at 59:00 min, I simplified a bit here).
Let $f,g:\mathbb{R} \rightarrow \mathbb{R}$ be some density functions on $\mathbb{R}$, both with same total mass. Let further $T:\mathbb{R}\rightarrow \mathbb{R}$ be a measure preserving map, i.e.
$$\int_{T(A)^{-1}}f(x)dx = \int_A g(x)dx, \quad A \subset\mathbb{R}\text{ measurable}.$$ We assume that $T$ is optimal in the sense that $$\int_\mathbb{R} (x-T(x))^2f(x)dx\leq\int_\mathbb{R} (x-\tilde{T}(x))^2f(x)dx $$ holds for all measure preserving $\tilde{T}$. Expanding the square terms in the integrals, we get
$$ \int_\mathbb{R} (x^2 -2xT(x)+T(x)^2)f(x)dx\leq\int_\mathbb{R} (x^2 -2x\tilde{T}(x)+\tilde{T}(x)^2)f(x)dx. $$ It is now claimed that the inequality only holds due to the middle term, i.e. $$\int_\mathbb{R}-2xT(x)f(x)dx\leq\int_\mathbb{R} -2x\tilde{T}(x)f(x)dx$$
while the other two are equal. But I wonder how $$\int_\mathbb{R}T(x)^2f(x)dx=\int_\mathbb{R} \tilde{T}(x)^2f(x)dx$$ can be true. The argument is said to be measure preservation. But I dont see why.
I think one way to see why the equality holds is to apply a change of variables twice. (See the voice "main property" in https://en.wikipedia.org/wiki/Pushforward_measure). You can then easily conclude that $$\int_\mathbb{R} T(x)^2 f(x)dx = \int_{T(\mathbb{R})} y^2 g(y)dy=\int_\mathbb{R} \tilde T(x)^2 f(x)dx.$$ (For this to work it is important to know that $T$ and $\tilde T$ are both measure preserving with respect to the same measures. In this case the measures are given by the density functions $f$ and $g$.)