@@ -65,7 +65,7 @@ class Heap {
65
65
heap_size = 0 ;
66
66
for (int i = 0 ; i < n; ++i) {
67
67
vec[i].second = -1 ;
68
- if (vec[i].first > moved_dist) {
68
+ if (vec[i].first - eps > moved_dist) {
69
69
vec[i].second = heap_size;
70
70
heap[heap_size++] = {vec[i].first , i};
71
71
}
@@ -83,7 +83,8 @@ class Heap {
83
83
return heap[0 ];
84
84
}
85
85
86
- void remove (int index) { // takes the index from the heap
86
+ void remove (int index) { // takes the index from the vec
87
+ index = vec[index ].second ;
87
88
if (index == -1 ) {
88
89
return ;
89
90
}
@@ -96,24 +97,26 @@ class Heap {
96
97
}
97
98
98
99
void insert (const std::pair<NT, int > val) {
100
+ vec[val.second ].second = heap_size;
101
+ vec[val.second ].first = val.first ;
99
102
heap[heap_size++] = val;
100
103
siftUp (heap_size - 1 );
101
104
}
102
105
103
106
void change_val (const int & index, const NT& new_val, const NT& moved_dist) { // takes the index from the vec
104
- if (new_val < moved_dist) { // should not be inserted into the heap
105
- remove (vec[index ].second );
106
- } else { // should be inserted into the heap
107
+ if (new_val < moved_dist - eps) {
108
+ vec[index ].first = new_val;
109
+ remove (index );
110
+ } else {
107
111
if (vec[index ].second == -1 ) {
108
- vec[index ].second = heap_size;
109
112
insert ({new_val, index });
110
113
} else {
111
114
heap[vec[index ].second ].first = new_val;
115
+ vec[index ].first = new_val;
112
116
siftDown (vec[index ].second );
113
117
siftUp (vec[index ].second );
114
118
}
115
119
}
116
- vec[index ].first = new_val;
117
120
}
118
121
};
119
122
0 commit comments