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

Remove unnecessary calculation in lazysegtree.hpp #104

Closed
wants to merge 1 commit into from

Conversation

loooorent
Copy link

@loooorent loooorent commented Feb 2, 2021

When ((r >> i) << i) != r is true, the result of (r - 1) >> i is always the same as that of r >> i because the lower i bits are not zero.
There seems to be no reason to dare to write (r - 1) >> i.

Reference: https://twitter.com/rsk0315_h4x/status/1356532749264281600

@yosupo06
Copy link
Collaborator

yosupo06 commented Feb 3, 2021

The intention of this line is "push all parent nodes of a l-th & (r-1)-th node" and if (((r >> i) << i) != r) is just a pruning for decreasing # of push.

So push((r - 1) >> i) is more intuitive for me. Is there any realistic merit (e.g. speed) in push(r >> i)?

@loooorent
Copy link
Author

loooorent commented Feb 4, 2021

Thank you for your quick reply and explanation.
I understand.

I tried speed test on AtCoder server and Library Checker server, but this removal does not affect execution time.

I would like to ask you why, in lazysegtree.hpp,
is the line 59 ( if (((r >> i) << i) != r) push(r >> i); )
different from
the line 91 (if (((r >> i) << i) != r) push((r - 1) >> i);) ?

If the operations of these lines have the same meaning, we should unify the writing.
Or, do these operations have different meanings?

@yosupo06
Copy link
Collaborator

yosupo06 commented Feb 4, 2021

I would like to ask you why, in lazysegtree.hpp,
is the line 59 ( if (((r >> i) << i) != r) push(r >> i); )
different from
the line 91 (if (((r >> i) << i) != r) push((r - 1) >> i);) ?

Nice catch, these lines do the same operation, and we should fix this.

@loooorent
Copy link
Author

The intention of this line is "push all parent nodes of a l-th & (r-1)-th node" and if (((r >> i) << i) != r) is just a pruning for decreasing # of push.

I agree with this intension and I think we should rewrite the line 59.
I'll create new pull request.

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