Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

The find_first and find_last methods returned the same value (n and -1 respectively) both when no position satisfied the condition and when boundary conditions were met, creating ambiguity for callers.

Changes

  • Modified return type from int to optional<int> for find_first and find_last in:
    • structure/segment-tree/segment-tree.hpp
    • structure/segment-tree/lazy-segment-tree.hpp
  • Return nullopt when condition is never satisfied or parameters are out of bounds
  • Return integer value when a valid position is found

Usage

auto seg = SegmentTree(monoid, v);

// Before: ambiguous - both return n
auto pos1 = seg.find_first(0, check);  // condition satisfied at n
auto pos2 = seg.find_first(0, check);  // condition never satisfied

// After: unambiguous
auto pos = seg.find_first(0, check);
if (pos.has_value()) {
  // found at pos.value()
} else {
  // not found
}
Original prompt

This section details on the original issue you should resolve

<issue_title>セグ木の二分探索</issue_title>
<issue_description>find_first
右端が n で条件を満たした場合は n を返す
ところで、条件を満たさなかった場合も n を返しており、n+1 か -1 を返した方がいいかも</issue_description>

Comments on the Issue (you are @copilot in this section)

@ei1333 一周回って optional を返すか

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits November 20, 2025 18:23
Co-authored-by: ei1333 <11004413+ei1333@users.noreply.github.com>
Co-authored-by: ei1333 <11004413+ei1333@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix return value of find_first in segment tree binary search Return optional<int> from segment tree binary search methods Nov 20, 2025
Copilot AI requested a review from ei1333 November 20, 2025 18:29
ei1333

This comment was marked as duplicate.

ei1333

This comment was marked as duplicate.

@ei1333 ei1333 marked this pull request as ready for review November 21, 2025 02:06
@ei1333 ei1333 merged commit e09e370 into master Nov 21, 2025
24 checks passed
@ei1333 ei1333 deleted the copilot/fix-find-first-return-value branch November 21, 2025 02:07
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