Add subtree size updating during treap split
This commit is contained in:
parent
7f528796c0
commit
02bdb353a4
1 changed files with 16 additions and 10 deletions
26
file.hpp
26
file.hpp
|
@ -28,19 +28,21 @@ class file {
|
|||
}
|
||||
|
||||
// Split treap by k-th element
|
||||
// TODO with sizes
|
||||
// Hopefully with sizes
|
||||
two_lines split(line *l, count_type k) {
|
||||
if(l == nullptr) return {nullptr, nullptr};
|
||||
|
||||
if(l->size < k) {
|
||||
auto two = split(l->right, k);
|
||||
l->right = two.first;
|
||||
return {l, two.second};
|
||||
if(get_size(l->left) > k) {
|
||||
// In the left subtree
|
||||
auto two = split(l->left, k);
|
||||
l->size -= two.first->size;
|
||||
return {two.first, l};
|
||||
}
|
||||
else {
|
||||
auto two = split(l->left, k - 1 - get_size(l->left));
|
||||
l->left = two.second;
|
||||
return {two.first, l};
|
||||
// In the right subtree
|
||||
auto two = split(l->right, k - (1+get_size(l->left)));
|
||||
l->size -= two.second->size;
|
||||
return {l, two.second};
|
||||
}
|
||||
}
|
||||
// Join two treaps
|
||||
|
@ -68,7 +70,7 @@ class file {
|
|||
if(k >= get_size(l->left))
|
||||
return find(l->left, k);
|
||||
else if(k > l->size+1)
|
||||
return find(l->right, k - 1 - get_size(l->left));
|
||||
return find(l->right, k - (1+get_size(l->left)) );
|
||||
else
|
||||
return l;
|
||||
}
|
||||
|
@ -79,7 +81,11 @@ public:
|
|||
root = nullptr;
|
||||
}
|
||||
// Don't find index k, but k-th line -> +1
|
||||
line* find(count_type k) {return find(root, k+1);}
|
||||
line* find(count_type k) {
|
||||
if(k >= root->size)
|
||||
return nullptr;
|
||||
return find(root, k+1);
|
||||
}
|
||||
|
||||
// File access
|
||||
void insert(int k, string s) {
|
||||
|
|
Loading…
Reference in a new issue