From d99b517ed02dff19644eac454475b164ea9a43ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sun, 17 Apr 2022 14:02:35 +0200 Subject: [PATCH] only update removed node if not covered anymore --- main.cpp | 4 ++-- segment_tree.h | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index a7b7703..c89a89a 100644 --- a/main.cpp +++ b/main.cpp @@ -22,10 +22,10 @@ int main() { srand(987192345); add_rect(0,0,5,4); - check_area(20); + // check_area(20); add_rect(3,2,6,6); - check_area(28); +// check_area(28); add_rect(1,-2,8,4); check_area(52); diff --git a/segment_tree.h b/segment_tree.h index 2ee53d3..2d399e3 100644 --- a/segment_tree.h +++ b/segment_tree.h @@ -72,7 +72,9 @@ void SegmentTree::add_coverage(Index node_idx) { void SegmentTree::remove_coverage(Index node_idx) { --(_nodes[node_idx].coverage); - update_covered_length(node_idx); + if(!_nodes[node_idx].covered()) { + update_covered_length(node_idx); + } } Index SegmentTree::left_child_idx(Index node_idx) { @@ -84,6 +86,7 @@ Index SegmentTree::right_child_idx(Index node_idx) { } void SegmentTree::update_covered_length(Index node_idx) { + assert(!_nodes[node_idx].covered()); if (is_leaf(node_idx)) { _nodes[node_idx].covered_length = 0; } else {