Generation of W state and GHZ state of multiple atomic ensembles via a single atom in a nonresonant cavity