-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bit01 bug; add erasable_mono_pairheap; add lazymap/lazyumap/lazys…
…et/lazyuset;
- Loading branch information
old-yan
committed
Oct 26, 2024
1 parent
6bd6da1
commit 77a75dd
Showing
55 changed files
with
2,682 additions
and
111 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,102 @@ | ||
/* | ||
最后修改: | ||
20241020 | ||
测试环境: | ||
gcc11.2,c++11 | ||
clang22.0,C++11 | ||
msvc14.2,C++14 | ||
*/ | ||
#ifndef __OY_ERASABLEMONOPAIRHEAP__ | ||
#define __OY_ERASABLEMONOPAIRHEAP__ | ||
|
||
#include "MonoPairHeap.h" | ||
|
||
namespace OY { | ||
namespace EMONOPH { | ||
using size_type = MONOPH::size_type; | ||
template <typename Tp> | ||
struct AddCommutativeGroup { | ||
using value_type = Tp; | ||
static Tp identity() { return Tp{}; } | ||
static Tp op(const Tp &x, const Tp &y) { return x + y; } | ||
static Tp inverse(const Tp &x) { return -x; } | ||
}; | ||
template <typename Tp> | ||
struct BitxorCommutativeGroup { | ||
using value_type = Tp; | ||
static Tp identity() { return Tp{}; } | ||
static Tp op(const Tp &x, const Tp &y) { return x ^ y; } | ||
static Tp inverse(const Tp &x) { return x; } | ||
}; | ||
template <typename CommutativeGroup> | ||
struct Trait { | ||
using group = CommutativeGroup; | ||
using value_type = typename group::value_type; | ||
using sum_type = typename MONOPH::Has_Sum_Type<group, value_type>::type; | ||
static constexpr bool has_op = MONOPH::Has_Op<group, sum_type>::value; | ||
}; | ||
template <typename CommutativeGroup, bool HasOp = Trait<CommutativeGroup>::has_op> | ||
struct HeapBase : Trait<CommutativeGroup> { | ||
typename Trait<CommutativeGroup>::sum_type m_prod; | ||
HeapBase() : m_prod(CommutativeGroup::identity()) {} | ||
}; | ||
template <typename CommutativeGroup> | ||
struct HeapBase<CommutativeGroup, false> : Trait<CommutativeGroup> {}; | ||
template <typename CommutativeGroup, typename Compare = std::less<typename CommutativeGroup::value_type>, template <typename> typename BufferType = VectorBufferWithCollect> | ||
class Heap : HeapBase<CommutativeGroup> { | ||
public: | ||
using typename HeapBase<CommutativeGroup>::group; | ||
using typename HeapBase<CommutativeGroup>::value_type; | ||
using typename HeapBase<CommutativeGroup>::sum_type; | ||
using HeapBase<CommutativeGroup>::has_op; | ||
using heap_type = Heap<CommutativeGroup, Compare, BufferType>; | ||
using inner_type = MONOPH::Heap<MONOPH::NoOp<value_type>, Compare, BufferType>; | ||
using buffer_type = typename inner_type::buffer_type; | ||
static void _reserve(size_type capacity) { inner_type::_reserve(capacity); } | ||
private: | ||
mutable inner_type m_data, m_lazy; | ||
size_type m_sz; | ||
public: | ||
Heap() : m_sz() {} | ||
void clear() { | ||
m_data.clear(), m_lazy.clear(), m_sz = 0; | ||
if constexpr (has_op) this->m_prod = group::identity(); | ||
} | ||
size_type size() const { return m_sz; } | ||
bool empty() const { return !m_sz; } | ||
void push(const value_type &x) { | ||
m_data.push(x), m_sz++; | ||
if constexpr (has_op) this->m_prod = group::op(this->m_prod, x); | ||
} | ||
void erase(const value_type &x) { | ||
m_lazy.push(x), m_sz--; | ||
if constexpr (has_op) this->m_prod = group::op(this->m_prod, group::inverse(x)); | ||
} | ||
void pop() { | ||
while (!m_lazy.empty() && !Compare()(m_lazy.top(), m_data.top())) m_data.pop(), m_lazy.pop(); | ||
if constexpr (has_op) this->m_prod = group::op(this->m_prod, group::inverse(m_data.top())); | ||
m_data.pop(), m_sz--; | ||
} | ||
const value_type &top() const { | ||
while (!m_lazy.empty() && !Compare()(m_lazy.top(), m_data.top())) m_data.pop(), m_lazy.pop(); | ||
return m_data.top(); | ||
} | ||
void join(heap_type &rhs) { | ||
if (!rhs.empty()) { | ||
m_data.join(rhs.m_data), m_lazy.join(rhs.m_lazy), rhs.m_sz = 0; | ||
if constexpr (has_op) this->m_prod = group::op(this->m_prod, rhs.m_prod), rhs.m_prod = group::identity(); | ||
} | ||
} | ||
void join(heap_type &&rhs) { join(rhs); } | ||
const sum_type &query_all() const { return this->m_prod; } | ||
}; | ||
} | ||
template <typename Tp, typename Compare = std::less<Tp>, template <typename> typename BufferType = VectorBufferWithCollect> | ||
using ErasableMonoPairHeap = EMONOPH::Heap<MONOPH::NoOp<Tp>, Compare, BufferType>; | ||
template <typename Tp, typename Compare = std::less<Tp>> | ||
using VectorErasableMonoSumPairHeap = EMONOPH::Heap<EMONOPH::AddCommutativeGroup<Tp>, Compare>; | ||
template <typename Tp, typename Compare = std::less<Tp>> | ||
using VectorErasableMonoBitXorPairHeap = EMONOPH::Heap<EMONOPH::BitxorCommutativeGroup<Tp>, Compare>; | ||
} | ||
|
||
#endif |
This file contains 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,52 @@ | ||
### 一、模板类别 | ||
|
||
数据结构:可删除元素的配对堆。 | ||
|
||
练习题目: | ||
|
||
1. [3321. 计算子数组的 x-sum II](https://leetcode.cn/problems/find-x-sum-of-all-k-long-subarrays-ii) | ||
|
||
|
||
### 二、模板功能 | ||
|
||
本模板为支持维护信息的堆。同时,借助懒删除技术,实现了堆中元素的删除。 | ||
|
||
与 `MonoPairHeap` 不同的是,本模板要求传入的代数结构为交换群,需要存在逆元。只有通过差分才可以实时维护堆的信息。 | ||
|
||
需要注意的是, `erase` 函数需要保证,每次传入的元素必须仍在堆中,未被删除。 | ||
|
||
在接口上,并无特殊之处。 | ||
|
||
|
||
### 三、模板示例 | ||
|
||
```c++ | ||
#include "DS/ErasableMonoPairHeap.h" | ||
#include "IO/FastIO.h" | ||
|
||
void test() { | ||
OY::VectorErasableMonoSumPairHeap<int> S; | ||
S.push(100); | ||
S.push(400); | ||
S.push(200); | ||
S.push(300); | ||
S.push(500); | ||
S.erase(200); | ||
S.erase(400); | ||
cout << "size = " << S.size() << endl; | ||
S.pop(); | ||
cout << "sum = " << S.query_all() << endl; | ||
} | ||
|
||
int main() { | ||
test(); | ||
} | ||
``` | ||
|
||
``` | ||
#输出如下 | ||
size = 3 | ||
sum = 400 | ||
``` | ||
|
This file contains 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 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 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 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 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 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 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 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
Oops, something went wrong.