Close the real line with a large half-circle in the upper half-plane; when deg Q ≥ deg P + 2, the arc adds nothing as the radius grows, so ∫ P/Q dx is 2πi times the upper residues.
Improper integrals over the whole real line, such as $\int_{-\infty}^\infty\frac{dx}{x^2 + 1}$, can be done with residues even when no antiderivative is in sight. The idea: close the real line into a loop with a large half circle, and use the ML inequality to show the half circle contributes nothing.
🔑 Key idea
Let $f = \frac{P}{Q}$ with polynomials $P, Q$, $\deg Q \ge \deg P + 2$, and $Q$ never $0$ on the real line. Then
$$\int_{-\infty}^{\infty}\frac{P(x)}{Q(x)}\,dx = 2\pi i\sum_{\operatorname{Im} a > 0}\operatorname{Res}\left(\frac{P}{Q}, a\right),$$
the sum over the poles in the upper half-plane only.
💡 Why it works
Let $C_R$ be the segment $[-R, R]$ followed by the upper half circle $\Gamma_R$ of radius $R$. For $R$ large, $C_R$ encloses every upper pole, so $\int_{-R}^{R} f\,dx + \int_{\Gamma_R} f\,dz = 2\pi i\sum\operatorname{Res}$. On $\Gamma_R$, $|f(z)| \le \frac{K}{R^2}$ for some constant $K$ (two degrees of decay), and the length is $\pi R$, so $\left|\int_{\Gamma_R}f\,dz\right| \le \frac{\pi K}{R} \to 0$. Letting $R \to \infty$ leaves the real integral equal to $2\pi i\sum\operatorname{Res}$.
For $\frac{1}{z^2 + 1}$ the half-disk encloses only $i$. Drag the handle outward: the arc's integral shrinks toward $0$ and the straight part approaches the whole real integral, $\pi$.
Double pole at $i$: $\operatorname{Res} = \frac{d}{dz}(z + i)^{-2}\Big|_{i} = \frac{-2}{(2i)^3} = \frac{1}{4i}$.
$2\pi i\cdot\frac{1}{4i} = \frac{\pi}{2}$.
Answer: $\frac{\pi}{2}$
Check the conditions: no real zeros of $Q$, and $\deg Q \ge \deg P + 2$.
Find the zeros of $Q$ with positive imaginary part.
Add the residues of $\frac{P}{Q}$ there and multiply by $2\pi i$.
Why two degrees? With only one degree of decay, as in $\frac{x}{x^2 + 1}$, the arc's ML bound $\frac{\pi R \cdot R}{R^2 - 1}$ does not shrink, and in fact $\int_{-\infty}^{\infty}\frac{x}{x^2 + 1}dx$ does not converge. The degree condition is what makes both the real integral and the arc argument work.
Including the poles in the lower half-plane. The closed contour (segment plus upper half circle) does not surround the lower poles, so their residues do not belong in the sum. For $\frac{1}{x^2 + 4}$, adding the residue at $-2i$ would give $0$, an impossible value for the integral of a positive function. Quick check: a positive integrand must give a positive real answer.
Practice
Pass with 3 right answers, the last 2 in a row. Each right answer earns 3 XP.