PrepStack · Markdown HTML

Sweep line with sorted events

Geometry, sweep, simulation, and parsing · HTML

Formal shape

Each object emits start/end events; sort by coordinate and tie rule, and the state after x remains constant until the next coordinate.

Recognition signals

False friends

SignalWhy it is different
Original event order is semantically requiredIf events cannot be reordered by coordinate, sorting changes the process; use direct simulation.
The remaining dimension is dynamically complexA sweep removes one dimension only; the other may still need a segment tree.

Core invariant

After all events at x are applied, active exactly represents objects present on the open span (x,next_x).

State, transition, and processing order

ItemDefinition
StateSorted (x,delta) events, active count or set, objective accumulator, and previous coordinate.
TransitionFirst settle the span from prev to x using old active, then combine every delta at x under one tie policy.
Frontier / orderThe sweep coordinate separates finalized left space from unprocessed right space; state changes only at events.

Python skeleton

def max_overlap(half_open_intervals):
    events = []
    for left, right in half_open_intervals:
        if left < right:
            events.append((left, 1))
            events.append((right, -1))
    active = best = 0
    # For [left,right), endings at x happen before starts at x.
    for _, delta in sorted(events, key=lambda e: (e[0], e[1])):
        active += delta
        best = max(best, active)
    return best

def covered_length(intervals):
    events = sorted((x,d) for l,r in intervals for x,d in ((l,1),(r,-1)) if l < r)
    active = total = 0; prev = None
    for x, delta in events:
        if prev is not None and active: total += x-prev
        active += delta; prev = x
    return total

Correctness sketch

  1. Activity can change only at an endpoint.
  2. No event lies between adjacent coordinates, so old active is valid across the whole span.
  3. Summing all deltas at x produces the exact state for the next open span.
  4. Every event span is visited once, so peaks and lengths are neither missed nor duplicated.

Complexity

TimeSpaceParameters
O(E log E)O(E)E events, usually a constant multiple of object count

Edge cases

Error patterns

PatternWhy it fails / fix
Wrong same-point orderFor half-open intervals, endings precede starts.
Updating before settling lengthThis applies the new state to the span on the left.
No explicit tie rulePeak counts then depend accidentally on sort stability.

Selection and exclusion rules

RelationTemplateBoundary
analogyMerge-sort cross-half countingOne dimension is sorted while a structure maintains relations in another.
active structureSegment tree range aggregationThe sweep maintains dynamic range summaries

Original micro-example

PartDetail
ResultMaximum overlap is 2.
ScenarioHalf-open intervals [1,4), [2,5), and [4,6).
WalkthroughAt coordinate 4 the first interval ends before the third begins, preventing a false count of 3.

Recall prompts

My notes