Skip to content

Commit

Permalink
Add Interval Container
Browse files Browse the repository at this point in the history
  • Loading branch information
baluteshih committed Oct 13, 2023
1 parent b57d4c2 commit 58a7b84
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions codebook/3_Data_Structure/IntervalContainer.cpp
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);
}
2 changes: 2 additions & 0 deletions codebook/content.tex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ \subsection{NumberofMaximalClique*} % test by POJ 2989
\section{Data Structure}
\subsection{Discrete Trick}
\lstinputlisting{3_Data_Structure/discrete_trick.cpp}
\subsection{Interval Container*} % test by kactl stress test
\lstinputlisting{3_Data_Structure/IntervalContainer.cpp}
\subsection{Leftist Tree}
\lstinputlisting{3_Data_Structure/Leftist_Tree.cpp}
\subsection{Heavy light Decomposition}
Expand Down

0 comments on commit 58a7b84

Please sign in to comment.