An improved nonlocal history-dependent model for gain and noise in avalanche photodiodes based on energy balance equation