williamfiset / williamfiset/algorithms

Generic Segment Tree Min/Max range update bugs

Open
#208 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Data Structure
Dominant language
Java
Stars
18.8k
Forks
4.5k
PR merge metrics
No merged PRs in 30d

Description

The generic segment tree implementation seems to be suffering from two issues when using multiplication range updates with min/max range queries:

  • The segment tree doesn't seem to handle overflow well (hard to avoid with multiplication updates in general), or at least the result doesn't match with the test values. This may be because the segment tree only capture the min/max value in a segment and when this value overflows the true min/max is lost -- not sure how much can be done here. Perhaps just ensure no overflow can happen in the tests?

  • Updates involving negative values aren't being handled. I'm not certain this can be handled with the current framework -- I think we may need to keep track of more information. It may be worth investigating if a shadow min segment tree (or a pair of values on the segment node) can be useful for a max segment tree for this situation (and vice versa) when we encounter a negative value. My idea is that for a max segment tree when a negative value is encountered, the maximum value would become the minimum value in a min segment tree -- i don't know if this works, but I think it's in the right direction.

    long[] ar = {2, 1, 3, 4, -1};
    GenericSegmentTree st =
        new GenericSegmentTree(
            ar,
            GenericSegmentTree.SegmentCombinationFn.MAX,
            GenericSegmentTree.RangeUpdateFn.MULTIPLICATION);

    st.rangeUpdate1(0, 4, 1);
    assertThat(st.rangeQuery1(0, 4)).isEqualTo(4);

    // Negative numbers are a known issue
    st.rangeUpdate1(0, 4, -2);
    assertThat(st.rangeQuery1(0, 4)).isEqualTo(2); // Returns -8 as max but should be 2

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the GenericSegmentTree implementation and reproduce the provided rangeUpdate1 and rangeQuery1 example. Inspect how multiplication updates maintain min/max values, then define and test the expected behavior for negative multipliers and overflow before considering a fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
data
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.