Skip to content

Refactor LazySegmentTree to be easier to modify - #97

Merged
cheran-senthil merged 4 commits into
masterfrom
refactor/lazy-segtree
Sep 2, 2026
Merged

cheran-senthil merged 4 commits into
masterfrom
refactor/lazy-segtree

Conversation

@Mukundan314

Copy link
Copy Markdown
Collaborator

Haven't tested speed or correctness yet

@Mukundan314

Copy link
Copy Markdown
Collaborator Author

Example usage: https://codeforces.com/contest/2027/submission/290258327 (lazy segment tree is overkill for this problem but demonstrates the usage)

@cheran-senthil

Copy link
Copy Markdown
Owner

Updated this branch onto current master and finished the refactor around the source-specialization model demonstrated in the linked solution.

What changed:

  • preserved the existing constructor, range-add/range-min/max behavior, and verifier API
  • kept the four operation-specific hooks (_unset_lazy, _apply_to_data, _apply_to_lazy, _merge_data) and the buffer slot, so multi-field state can be implemented by editing those small sections
  • exposed apply() while retaining add() as an alias
  • added randomized tests for the default min/max behavior and a minimum-with-count specialization matching the D2 use case

Correctness:

  • full suite: 63 passed
  • the supplied D2 solver was separately checked against brute force on 48,000 random small instances

Performance on 50,000 mixed operations with n = 32768 (best of 7):

Runtime master this branch delta
CPython 3.13 0.604s 0.808s +34%
PyPy 3.11 / 7.3.23 0.0365s 0.0383s +5%

I also benchmarked runtime callbacks and a fully generic action/composition interface; those were roughly 2x or worse for the actual minimum-with-count specialization, so I left them out. Local binding of the hook methods also made PyPy slower, so the hot loops retain direct method dispatch.

@cheran-senthil
cheran-senthil marked this pull request as ready for review September 2, 2026 05:00
@cheran-senthil
cheran-senthil merged commit 2e8da0f into master Sep 2, 2026
24 checks passed
@cheran-senthil
cheran-senthil deleted the refactor/lazy-segtree branch September 2, 2026 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants