On closed days, when neither person covers the range alone, a pair works if begins_b <= reach_a + 1
When neither person covers the range alone, reach_a is at most end - 1 and begins_b - 1 is at least start. Then the window is just (begins_b - 1, reach_a), and the whole test is one comparison: begins_b <= reach_a + 1. The second person has to be back by the day after the first one's run ends.
Building each person's runs is O(d log d) for d free days. Then each pair is O(1), so all pairs cost O(m²) instead of O(m² L²).
Here it is measured over every ordered pair on a 30-day rotation, days 15 through 44. Each engineer, drawn with random.Random(5), is free from day 1 through a random day c from 1 to 60. They're free again from a random day between c and 61 through day 60:
| Engineers | Ordered pairs | Pairs that work | Day by day | reach_and_begin, then handoff_days |
|---|---|---|---|---|
| 50 | 2,450 | 996 | 26.9 ms | 0.72 ms |
| 100 | 9,900 | 4,160 | 117.2 ms | 2.67 ms |
| 200 | 39,800 | 17,711 | 475.6 ms | 9.53 ms |
The fast column includes computing every engineer's reach and begins, with the reach_and_begin you wrote.
In the editor, RB holds each person's (reach, begins) for the week from day 1 through day 7. Write working_pairs(rb, end): every ordered pair that splits the range by the one comparison, leaving out anyone who covers it alone.