Website Summary Files Commits Branches @commit @diff @stats
path: root / Additional / Additional-2023-SI / IntervalCollection.cpp
diff options
context:
space:
mode:
Commit9eed63a37a808c59990feaaaa3e1725dfdd31f3c (patch)
Author
 Stoycho Kyosev <stoycho.kk@gmail.com>
2023-10-30 22:33:14 +0200
Committer
 Stoycho Kyosev <stoycho.kk@gmail.com>
2023-10-30 22:33:14 +0200
Filesbc22e2b8407f9207c7a5a8ef999f41446c0cee19 /Additional/Additional-2023-SI/IntervalCollection.cpp
Data-structures-and-algorithms-9eed63a37a808c59990feaaaa3e1725dfdd31f3c.tar
Data-structures-and-algorithms-9eed63a37a808c59990feaaaa3e1725dfdd31f3c.tar.gz
Data-structures-and-algorithms-9eed63a37a808c59990feaaaa3e1725dfdd31f3c.zip
Parent0f17a224d2934c748b32b908f74724b3299d263a (diff)
Add SI consultation
1 files changed, 103 insertions, 0 deletions
-rw-r--r--Additional/Additional-2023-SI/IntervalCollection.cpp103
diff --git a/Additional/Additional-2023-SI/IntervalCollection.cpp b/Additional/Additional-2023-SI/IntervalCollection.cpp
new file mode 100644
index 0000000..0ac2c76
--- /dev/null
+++ b/Additional/Additional-2023-SI/IntervalCollection.cpp
@@ -0,0 +1,103 @@
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+class IntervalCollection
+{
+ using Iterator = std::vector<std::pair<int, int>>::iterator;
+ using ConstIterator = std::vector<std::pair<int, int>>::const_iterator;
+
+ Iterator getRightmostInterval(int x)
+ {
+ std::pair<int, int> searcher = { x, x };
+ return std::lower_bound(
+ intervals.begin(),
+ intervals.end(),
+ searcher,
+ [](const std::pair<int, int>& first, const std::pair<int, int>& second) -> bool
+ {
+ return first.second < second.second;
+ }
+ );
+ }
+
+ ConstIterator getRightmostIntervalConst(int x) const
+ {
+ std::pair<int, int> searcher = { x, x };
+ return std::lower_bound(
+ intervals.begin(),
+ intervals.end(),
+ searcher,
+ [](const std::pair<int, int>& first, const std::pair<int, int>& second) -> bool
+ {
+ return first.second < second.second;
+ }
+ );
+ }
+
+public:
+ void insert(int x)
+ {
+ Iterator right = getRightmostInterval(x);
+ bool begin = (right == intervals.begin());
+
+ Iterator left = begin ? intervals.end() : right - 1;
+
+ bool needLeftMerge = !begin && (x - 1 == left->second);
+ bool needRightMerge = (right != intervals.end()) && (x + 1 == right->first);
+
+ if (right != intervals.end() && x >= right->first && x <= right->second)
+ {
+ return;
+ }
+
+ if (needLeftMerge && needRightMerge)
+ {
+ left->second = right->second;
+ intervals.erase(right);
+ }
+ else if (needLeftMerge)
+ {
+ left->second = x;
+ }
+ else if (needRightMerge)
+ {
+ right->first = x;
+ }
+ else
+ {
+ intervals.insert(right, { x, x });
+ }
+ }
+
+ bool contains(int x) const
+ {
+ ConstIterator right = getRightmostIntervalConst(x);
+ return (right != intervals.end() && x >= right->first && x <= right->second);
+ }
+
+ void print() const
+ {
+ for (const auto& x : intervals)
+ {
+ std::cout << x.first << " " << x.second << std::endl;
+ }
+ }
+private:
+ std::vector<std::pair<int, int>> intervals;
+};
+
+int main()
+{
+ IntervalCollection ic;
+
+ ic.insert(1);
+ ic.insert(3);
+ ic.insert(2);
+ ic.insert(5);
+ ic.insert(7);
+ ic.insert(4);
+ ic.insert(6);
+
+ std::cout << ic.contains(1) << " " << ic.contains(10);
+} \ No newline at end of file