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 segment tree #539

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

Conversation

sak-codes
Copy link
Contributor

References to other Issues or PRs or Relevant literature

Brief description of what is fixed or changed

Other comments

@codecov
Copy link

codecov bot commented Jun 29, 2023

Codecov Report

Merging #539 (069b5df) into main (8f419fd) will decrease coverage by 1.743%.
Report is 15 commits behind head on main.
The diff coverage is 71.014%.

Additional details and impacted files
@@              Coverage Diff              @@
##              main      #539       +/-   ##
=============================================
- Coverage   98.528%   96.785%   -1.743%     
=============================================
  Files           32        34        +2     
  Lines         4010      4418      +408     
=============================================
+ Hits          3951      4276      +325     
- Misses          59       142       +83     
Files Changed Coverage Δ
...ucts/miscellaneous_data_structures/segment_tree.py 82.327% <70.542%> (-14.869%) ⬇️
...tructs/miscellaneous_data_structures/algorithms.py 94.000% <77.777%> (-0.792%) ⬇️

... and 14 files with indirect coverage changes

Impacted file tree graph

Copy link
Member

@czgdp1807 czgdp1807 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run same tests of OneDimensionalArraySegmentTree with the new one. Put all tests into common function and pass objects of OneDimensionalArraySegmentTree and OneDimensionalArraySegmentTreeLazy to the common function.

@sak-codes
Copy link
Contributor Author

I am trying to design the method parameters such that I can generalize the implementation but since lazy updates for each of the different applications have different implementation, I am unable to find a common design.

@czgdp1807
Copy link
Member

lazy updates for each of the different applications have different implementation, I am unable to find a common design.

Share some examples so that we can figure something out.

@sak-codes
Copy link
Contributor Author

Suppose we are solving an range sum update problem where we want to increment all the elements in the range $[i, j]$ with some number x. This is okay and well generalized for j != i. But when we want to change some element at index $i$, then if we use the same above approach then we need to pass by y=x-cur_value[i], then during the update we will have cur_value[i] += y. So user will get confuse when to use y and when to use x.

self._update_range(node.right, lazy_node.right, mid + 1, end, l, r, value)
node.data = self._func((node.left.data, node.right.data))

def update_range(self, start, end, value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def update_range(self, start, end, value):
def update(self, start, end, value):

self._update_range(self._root, self._lazy_node, 0, len(self._array) - 1,
start, end, value)

def update(self, index, value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def update(self, index, value):
def __setitem__(self, index, value):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for the parent class.

Copy link
Member

@czgdp1807 czgdp1807 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think right now the case is update for a single value and update_range for a range. I think we discussed to use __setitem__ to update a single value. However, for a range we will use update. So please do it.

@sak-codes
Copy link
Contributor Author

So you want me to rename update_range to update?

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