- Edited
You have trouble focusing on one thing for hours at a time because your body and brain didn't evolve to do that. It's extremely unnatural behavior. I used to force people to take breaks every hour, even if they thought they were in the zone. Sometimes you think you're still focused, but you start making stupid mistakes.
I also used to work with some guys who would do a set of pushups every time they started to lose focus. It worked for me as well. Stressing your muscles releases hormones that benefit concentration immediately and over time. Loads of studies have repeatedly linked exercise with increased brain function in healthy and non-healthy individuals.
Another thing to consider is that the typical typing position isn't ergonomic. I do regular posture exercises to counter the damage it does (and to deal with a bad back injury I had years ago). Hurting your body hurts your brain.
I suspect that the secret to getting things done is to break everything up into small problems that you can solve in one session -- and not give into the temptation to keep going past your endurance without a significant break.