-
Notifications
You must be signed in to change notification settings - Fork 0
perf: Optimize DynamicSegmentTree performance #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
607736c
chore: add clangd configuration
mdonaka 02fbb89
perf: rewrite DynamicSegmentTree with pointer-based recursive impleme…
mdonaka d6caa1e
[auto-verifier] verify commit 02fbb899b1ed4c4811cf6b7daa228b886bf3fe50
web-flow 089fe96
refactor: use smart pointers and restore constexpr in DynamicSegmentTree
github-actions[bot] a140f29
[auto-verifier] verify commit 089fe96d27af83d668efd95d2c9d579a89c4627c
web-flow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| InlayHints: | ||
| Enabled: No | ||
| ParameterNames: No | ||
| DeducedTypes: No | ||
| Designators: No |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| #pragma once | ||
|
|
||
| #include <deque> | ||
| #include <memory> | ||
| #include <ostream> | ||
| #include <unordered_map> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
|
|
@@ -12,72 +12,83 @@ namespace mtd { | |
| template <monoid Monoid, int size = static_cast<int>(1e9 + 1)> | ||
| class DynamicSegmentTree { | ||
| private: | ||
| std::unordered_map<int, Monoid> m_node; | ||
| using S = decltype(Monoid().m_val); | ||
|
|
||
| constexpr auto _get(int i) const { | ||
| return (m_node.find(i) == m_node.end()) ? Monoid() : m_node.at(i); | ||
| } | ||
| struct Node { | ||
| Monoid val; | ||
| Node* left; | ||
| Node* right; | ||
| Node() : val(Monoid()), left(nullptr), right(nullptr) {} | ||
| explicit Node(const Monoid& v) : val(v), left(nullptr), right(nullptr) {} | ||
| }; | ||
|
|
||
| template <class Lambda> | ||
| constexpr auto _update_op(int itr, Monoid&& val, const Lambda& op) { | ||
| int i = itr + size - 1; | ||
| m_node[i] = op(_get(i), std::forward<decltype(val)>(val)); | ||
| while (i) { | ||
| i = (i - 1) >> 1; | ||
| m_node[i] = _get((i << 1) | 1).binaryOperation(_get((i + 1) << 1LL)); | ||
| } | ||
| std::deque<Node> m_nodes; | ||
| Node* m_root; | ||
|
|
||
| Node* _get_node() { | ||
| m_nodes.emplace_back(); | ||
| return &m_nodes.back(); | ||
| } | ||
|
|
||
| constexpr auto _query(int _l, int _r) const { | ||
| _l = std::max(_l, 0); | ||
| _r = std::min(_r, size - 1); | ||
| auto l = _l + size; | ||
| int r = _r + size; | ||
| auto lm = Monoid(); | ||
| auto rm = Monoid(); | ||
| while (l <= r) { | ||
| if (l & 1) { | ||
| lm = lm.binaryOperation(_get(l - 1)); | ||
| ++l; | ||
| } | ||
| if (!(r & 1)) { | ||
| rm = _get(r - 1).binaryOperation(rm); | ||
| --r; | ||
| } | ||
| l >>= 1, r >>= 1; | ||
| } | ||
| return lm.binaryOperation(rm); | ||
| Node* _get_node(const Monoid& val) { | ||
| m_nodes.emplace_back(val); | ||
| return &m_nodes.back(); | ||
| } | ||
|
|
||
| constexpr auto _construct(const std::vector<S>& vec) { | ||
| for (unsigned int i = 0; i < vec.size(); ++i) { | ||
| m_node[i + size - 1] = Monoid(vec[i]); | ||
| template <class Lambda> | ||
| void _update_op(Node*& node, int l, int r, int pos, const Monoid& val, | ||
| const Lambda& op) { | ||
| if (!node) node = _get_node(); | ||
| if (r - l == 1) { | ||
| node->val = op(node->val, val); | ||
| return; | ||
| } | ||
| for (int i = size - 2; i >= 0; --i) { | ||
| m_node[i] = _get((i << 1) | 1).binaryOperation(_get((i + 1) << 1)); | ||
| int mid = l + (r - l) / 2; | ||
| if (pos < mid) { | ||
| _update_op(node->left, l, mid, pos, val, op); | ||
| } else { | ||
| _update_op(node->right, mid, r, pos, val, op); | ||
| } | ||
| Monoid left_val = node->left ? node->left->val : Monoid(); | ||
| Monoid right_val = node->right ? node->right->val : Monoid(); | ||
| node->val = left_val.binaryOperation(right_val); | ||
| } | ||
|
|
||
| Monoid _query(Node* node, int l, int r, int ql, int qr) const { | ||
| if (!node || r <= ql || qr <= l) return Monoid(); | ||
| if (ql <= l && r <= qr) return node->val; | ||
| int mid = l + (r - l) / 2; | ||
| return _query(node->left, l, mid, ql, qr) | ||
| .binaryOperation(_query(node->right, mid, r, ql, qr)); | ||
| } | ||
|
|
||
| public: | ||
| constexpr DynamicSegmentTree() {} | ||
| constexpr DynamicSegmentTree() : m_root(nullptr) {} | ||
|
|
||
| template <class Lambda> | ||
| constexpr auto update_op(int itr, Monoid&& val, const Lambda& op) { | ||
| return _update_op(itr, std::forward<Monoid>(val), op); | ||
| void update_op(int pos, const Monoid& val, const Lambda& op) { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. constexpr消さず全部付け直して |
||
| _update_op(m_root, 0, size, pos, val, op); | ||
| } | ||
|
|
||
| void update(int pos, const Monoid& val) { | ||
| update_op(pos, val, | ||
| [](const Monoid&, const Monoid& m2) { return m2; }); | ||
| } | ||
| constexpr auto update(int itr, Monoid&& val) { | ||
| return update_op(itr, std::forward<Monoid>(val), | ||
| [](const Monoid&, const Monoid& m2) { return m2; }); | ||
|
|
||
| void add(int pos, const Monoid& val) { | ||
| update_op(pos, val, [](const Monoid& m1, const Monoid& m2) { | ||
| return Monoid(m1.m_val + m2.m_val); | ||
| }); | ||
| } | ||
| constexpr auto add(int itr, Monoid&& val) { | ||
| return update_op(itr, std::forward<Monoid>(val), | ||
| [](const Monoid& m1, const Monoid& m2) { | ||
| return Monoid(m1.m_val + m2.m_val); | ||
| }); | ||
|
|
||
| S query(int l, int r) const { | ||
| if (l > r) return Monoid().m_val; | ||
| return _query(m_root, 0, size, l, r + 1).m_val; | ||
| } | ||
|
|
||
| S query_all() const { | ||
| return m_root ? m_root->val.m_val : Monoid().m_val; | ||
| } | ||
| constexpr auto query(int l, int r) const { return _query(l, r).m_val; } | ||
| constexpr auto query_all() const { return m_node[0].m_val; } | ||
| }; | ||
|
|
||
| } // namespace mtd | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これvectorで良い