The two companies, which together spent upward of $500 million on advertising last year to try to build unique brands, had been talking about merging for months, with those talks accelerating in the past few weeks.