-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b57d4c2
commit 58a7b84
Showing
2 changed files
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* Add and remove intervals from a set of disjoint intervals. | ||
* Will merge the added interval with any overlapping intervals in the set when adding. | ||
* Intervals are [inclusive, exclusive). */ | ||
set<pii>::iterator addInterval(set<pii>& is, int L, int R) { | ||
if (L == R) return is.end(); | ||
auto it = is.lower_bound({L, R}), before = it; | ||
while (it != is.end() && it->X <= R) { | ||
R = max(R, it->Y); | ||
before = it = is.erase(it); | ||
} | ||
if (it != is.begin() && (--it)->Y >= L) { | ||
L = min(L, it->X); | ||
R = max(R, it->Y); | ||
is.erase(it); | ||
} | ||
return is.insert(before, pii(L, R)); | ||
} | ||
void removeInterval(set<pii>& is, int L, int R) { | ||
if (L == R) return; | ||
auto it = addInterval(is, L, R); | ||
auto r2 = it->Y; | ||
if (it->X == L) is.erase(it); | ||
else (int&)it->Y = L; | ||
if (R != r2) is.emplace(R, r2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters