Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lazy propagation to treaps #262

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Matistjati
Copy link
Contributor

@Matistjati Matistjati commented Aug 4, 2024

KACTL's current treap implementation lacks lazy propagation (IMO, quite an important feature). Adding this is nontrivial if you don't understand the implementation. I chose to add reverse interval, as it is not uncommon, nontrivial and requires little code.

In order to reduce code size, I implemented the treap as described in cp-algorithms.

Some stats:

  • New treap with reverse: 61 LOC counting comments. ~1308 characters excluding comments
  • Old treap: 54 LOC. ~1119 chars
  • New treap without reverse or lazy propagation: 42 LOC. ~980 chars

Runs in 0.74 on substring reversals. code

Runs in 0.66 on cut and paste if we don't push, code. 0.73 if we do push.
Runs in 0.65 with the old implementation.

So performance seems to be basically the same. It may be harder to understand because of return-by-reference, but I think it should be understandable by the comments and sample usages.

If you use the treap as a set, merge has a precondition that max_val(l) < min_val(r). Should we use a line to include that comment?

If you want, I can codegolf away 2 lines related to rev without hurting readability too much.

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.

1 participant