In both cases (switch open/closed), in the steady state, current flows only through the lower branch, and none through the upper branch. There is no electrical connection through the capacitors. So the 9V emf of the cell is divided between the 2 resistors in the ratio of 3:6 - ie 3V and 6V.
The difference between the two cases is that when the switch is closed the potential at X must be the same as at Y, so that the PDs across the capacitors are 3V and 6V respectively. Whereas when the switch is open the potential at X is not constrained, the only constraint is that the total PD across the two capacitors in series is 9V.
The charges on the capacitors are different in the 2 cases, and this difference in charge has to come through the connection XY. So it is simply a question of calculating the charges on the two capacitors in each case, then finding the difference.
When switch S is open the 2 capacitors are in series. The total capacitance is $C=C_1C_2/(C_1+C_2)=18/9=2\mu F$. The total charge is $Q=CV=2\mu F \times 9V=18\mu C$. The charges are the same on each capacitor because the two inner plates are connected and isolated : the total charge on these two plates was initially zero (before the cell was connected), and there is no path by which charge can get onto or leave them (because switch S is open). The charges on the plates in $\mu C$ are are shown in the upper half of the diagram below.
When switch S is closed the potential across each capacitor is the same as across the corresponding resistor, ie 3V and 6V. So the charges are $3\mu F \times 3V=9\mu C$ and $6\mu F \times 6V=36\mu C$. The charges on each plate are now as shown in the lower half of the diagram below.
Initially (S open) the total charge on the inner plates was zero. (It was not actually necessary to calculate them individually to know that the sum would be zero.) Finally (S closed) the total charge on the inner plates is $-9+36=+27\mu C$. This charge must have come through the switch, ie from Y to X.