mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Updated
This commit is contained in:
parent
de595b0c19
commit
f3ba7fa90e
@ -47,16 +47,19 @@ private:
|
|||||||
curr->prop = 0;
|
curr->prop = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void construct(int i, int j, std::shared_ptr<Node> &curr) {
|
std::shared_ptr<Node> construct(int i, int j) {
|
||||||
curr = std::make_shared<Node>(Node());
|
auto newNode = std::make_shared<Node>(Node());
|
||||||
if (i == j) {
|
if (i == j) {
|
||||||
curr->val = vec[i];
|
newNode->val = vec[i];
|
||||||
} else {
|
} else {
|
||||||
int mid = i + (j - i) / 2;
|
int mid = i + (j - i) / 2;
|
||||||
construct(i, mid, curr->left);
|
auto leftt = construct(i, mid);
|
||||||
construct(mid + 1, j, curr->right);
|
auto right = construct(mid + 1, j);
|
||||||
curr->val = curr->left->val + curr->right->val;
|
newNode->val = leftt->val + right->val;
|
||||||
|
newNode->left = leftt;
|
||||||
|
newNode->right = right;
|
||||||
}
|
}
|
||||||
|
return newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Node> update(int i, int j, int l, int r, int value, std::shared_ptr<Node> const &curr) {
|
std::shared_ptr<Node> update(int i, int j, int l, int r, int value, std::shared_ptr<Node> const &curr) {
|
||||||
@ -97,34 +100,29 @@ public:
|
|||||||
n = 0;
|
n = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void construct(
|
void construct(const std::vector<int> &vec) // the segment tree will be built from the values in "vec", "vec" is 0 indexed
|
||||||
const std::vector<int> &vec) // the segment tree will be built from the values in "vec", "vec" is 0 indexed
|
|
||||||
{
|
{
|
||||||
if (vec.empty()) {
|
if (vec.empty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
n = vec.size();
|
n = vec.size();
|
||||||
this->vec = vec;
|
this->vec = vec;
|
||||||
std::shared_ptr<Node> root = nullptr;
|
auto root = construct(0, n - 1);
|
||||||
construct(0, n - 1, root);
|
|
||||||
ptrs.push_back(root);
|
ptrs.push_back(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
void update(int l, int r,
|
void update(int l, int r, int value) // all elements from index "l" to index "r" would by updated by "value", "l" and "r" are 0 indexed
|
||||||
int value) // all elements from index "l" to index "r" would by updated by "value", "l" and "r" are 0 indexed
|
|
||||||
{
|
{
|
||||||
ptrs.push_back(update(0, n - 1, l, r, value,
|
ptrs.push_back(update(0, n - 1, l, r, value,
|
||||||
ptrs[ptrs.size() - 1])); // saving the root pointer to the new segment tree
|
ptrs[ptrs.size() - 1])); // saving the root pointer to the new segment tree
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t query(int l, int r,
|
int64_t query(int l, int r, int version) // querying the range from "l" to "r" in a segment tree after "version" updates, "l" and "r" are 0 indexed
|
||||||
int version) // querying the range from "l" to "r" in a segment tree after "version" updates, "l" and "r" are 0 indexed
|
|
||||||
{
|
{
|
||||||
return query(0, n - 1, l, r, ptrs[version]);
|
return query(0, n - 1, l, r, ptrs[version]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int size() // returns the number of segment trees (versions) , the number of updates done so far = returned value - 1 ,because one of the trees is the original segment tree
|
||||||
size() // returns the number of segment trees (versions) , the number of updates done so far = returned value - 1 ,because one of the trees is the original segment tree
|
|
||||||
{
|
{
|
||||||
return ptrs.size();
|
return ptrs.size();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user