Importance of halogens and nitrogen in degree of unsaturation
So here is the method I learned years ago without memorizing an awkward formula. The concept is to reduce the molecular formula to a hydrocarbon and compare it to the most saturated hydrocarbon bearing the same number of carbon atoms.  Consider the infamous free-base, hydroxychloroquine, $\ce{C18H26ClN3O}$.
 
The following atom replacements may be done in any order. Ignore divalent atoms ($\ce{O,S}$). The formula is now $\ce{C18H26ClN3}$. Replace all halogens with an equal number of hydrogens $\Rightarrow \ce{C18H27N3}$. Substitute $\ce{CH}$ for each nitrogen or trivalent atom $\Rightarrow \ce{C21H30}$. The most saturated $\ce{C21}$ hydrocarbon is $\ce{C21H44}$. Now do the math: $\ce{C21H44}$ - $\ce{C21H30}$ = $\ce{H14}$. Now divide 14 by 2. The Degree of Unsaturation is 7. 
As to the question as to why oxygen is dropped from the equation, if the oxygen in hydroxychloroquine weren't there, the degree of unsaturation would still be the same. For example, the Degrees of Unsaturation of tetrahydrofuran and cyclobutane are the same, D.U. = 1.