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

Segment Tree size is too small #82

Open
bogoconic1 opened this issue Jan 3, 2023 · 5 comments
Open

Segment Tree size is too small #82

bogoconic1 opened this issue Jan 3, 2023 · 5 comments

Comments

@bogoconic1
Copy link

Describe the bug
Copied LazySegmentTree.py template to solve this problem, but when submitting gave Runtime Error On Test 5
https://codeforces.com/contest/1549/submission/186542539

After debugging, realised that size of self._lazy and self._data is too small. Changed it from 2*_size to 4*_size and the code ACs
https://codeforces.com/contest/1549/submission/186543644

Expected behaviour
Should not throw an index out of bounds error

Additional context
Submitted a pull request for this issue #81

Thanks

@Mukundan314
Copy link
Collaborator

Mukundan314 commented Jan 3, 2023

Specific testcase your code fails for is:

2
1 2

In this testcase your code queries the segment tree for [1, 1) which is an empty range, both SegmentTree and LazySegmentTree don't support empty ranges.

@Mukundan314
Copy link
Collaborator

We can probably update SegmentTree and LazySegmentTree to return default for empty ranges.

cc: @cheran-senthil

@Mukundan314
Copy link
Collaborator

Actually it seems I was a bit mistaken its seems that SegmentTree and LazySegmentTree do support empty ranges. Its that your codes queries outside the range of the array.

@bjorn-martinsson
Copy link
Collaborator

The issue is that if len (the variable self._len) is a power of 2, then query(len, len) or query(len, len, value) will fail.

This is arguably not a bug, but it is a werid edge case. The reason why the functions query(start, end) and add(start, end, value) get index out of bound is that the starting value start is assumed to be in the range [0,self._len). If we want both functions to support empty ranges, I think that a good fix would be to put an if check for start == stop (or start >= stop if we want to be 100% consistent with the other pyrival segment tree implementations) in the beginning of both query and add.

Btw @bogoconic1, it is a lot easier to debug something if you post a runable python script that triggers the bug instead of linking to a codeforces submission. Please try to do so in the future =)

@bogoconic1
Copy link
Author

Thanks, will do so next time

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

No branches or pull requests

3 participants