当前位置: 首页 > news >正文

c++ sparsetable 模版

闭区间查询

支持
区间最大
区间最小
区间和
区间最大下标
区间最小下标

#include <bits/stdc++.h>
using namespace std;#ifndef NO_UNIQUE_ADDRESS
#    ifdef __has_cpp_attribute
#        if __has_cpp_attribute(no_unique_address)
#            define NO_UNIQUE_ADDRESS [[no_unique_address]]
#        endif
#    endif
#    ifndef NO_UNIQUE_ADDRESS
#        define NO_UNIQUE_ADDRESS
#    endif
#endif // !NO_UNIQUE_ADDRESSnamespace details {inline unsigned int bsf32(uint32_t n) noexcept {
#if defined __GNUC__return __builtin_ctz(n);
#elif defined _MSC_VERunsigned long ans;_BitScanForward(&ans, n);return ans;
#elifstatic constexpr unsigned char table[32] = {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9,};return table[((n & -n) * 0x077CB531) >> 57];
#endif}inline unsigned int bsf64(uint64_t n) noexcept {
#if defined __GNUC__return __builtin_ctzll(n);
#elif defined _MSC_VERunsigned long ans;_BitScanForward64(&ans, n);return ans;
#elifstatic constexpr unsigned char table[64] = {0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,};return table[((n & -n) * 0x03f79d71b4ca8b09) >> 58];
#endif}inline unsigned int bsr32(uint32_t n) noexcept {
#if defined __GNUC__return 31 - __builtin_clz(n);
#elif defined _MSC_VERunsigned long ans;_BitScanReverse(&ans, n);return ans;
#eliffloat t = n;uint32_t ans;memcpy(&ans, &t, sizeof(float));return (ans >> 23 & 255) - 127;
#endif}inline unsigned int bsr64(uint64_t n) noexcept {
#if defined __GNUC__return 63 - __builtin_clzll(n);
#elif defined _MSC_VERunsigned long ans;_BitScanReverse64(&ans, n);return ans;
#eliffloat t = n;uint32_t ans;memcpy(&ans, &t, sizeof(float));return (ans >> 23 & 255) - 127;
#endif}inline unsigned int popcnt32(uint32_t n) noexcept {
#if defined __GNUC__return __builtin_popcount(n);
#elif defined _MSC_VERreturn __popcnt(n);
#elifn -= ((n >> 1) & 0x55555555);n = (n & 0x33333333) + ((n >> 2) & 0x33333333);return ((((n >> 4) + n) & 0x0f0f0f0f) * 0x01010101) >> 24;
#endif}inline unsigned int popcnt64(uint64_t n) noexcept {
#if defined __GNUC__return __builtin_popcountll(n);
#elif defined _MSC_VERreturn __popcnt64(n);
#elifn -= ((n >> 1) & 0x5555555555555555);n = (n & 0x3333333333333333) + ((n >> 2) & 0x3333333333333333);return ((((n >> 4) + n) & 0x0f0f0f0f0f0f0f0f) * 0x0101010101010101) >> 56;
#endif}inline unsigned int parity32(uint32_t n) noexcept {
#if defined __GNUC__return __builtin_parity(n);
#elif defined _MSC_VERreturn __popcnt(n) & 1;
#elifn ^= n >> 1;n ^= n >> 2;n = (n & 0x11111111) * 0x11111111;return (n >> 28) & 1;
#endif}inline unsigned int parity64(uint64_t n) noexcept {
#if defined __GNUC__return __builtin_parityll(n);
#elif defined _MSC_VERreturn __popcnt64(n) & 1;
#elifn ^= n >> 1;n ^= n >> 2;n = (n & 0x1111111111111111) * 0x1111111111111111;return (n >> 60) & 1;
#endif}template<typename I, bool B = 32 < std::numeric_limits<I>::digits>struct _BitFuncImpl {inline static unsigned int bsf(I n) noexcept { return bsf64(n); }inline static unsigned int bsr(I n) noexcept { return bsr64(n); }inline static unsigned int popcnt(I n) noexcept { return popcnt64(n); }inline static unsigned int parity(I n) noexcept { return parity64(n); }};template<typename I>struct _BitFuncImpl<I, false> {inline static unsigned int bsf(I n) noexcept { return bsf32(n); }inline static unsigned int bsr(I n) noexcept { return bsr32(n); }inline static unsigned int popcnt(I n) noexcept { return popcnt32(n); }inline static unsigned int parity(I n) noexcept { return parity32(n); }};template<typename I>inline unsigned int bsf(I n) noexcept { return _BitFuncImpl<I>::bsf(n); }template<typename I>inline unsigned int bsr(I n) noexcept { return _BitFuncImpl<I>::bsr(n); }template<typename I>inline unsigned int popcnt(I n) noexcept { return _BitFuncImpl<I>::popcnt(n); }template<typename I>inline unsigned int parity(I n) noexcept { return _BitFuncImpl<I>::parity(n); }template<typename T, size_t K = 0, bool = std::is_empty<T>::value && !std::is_final<T>::value>struct Derivable {private:T val;public:constexpr Derivable() = default;template<typename... Args>constexpr Derivable(Args&&... args) noexcept(std::is_nothrow_constructible<T, Args&&...>::value) : val(std::forward<Args>(args)...) {}inline constexpr T& value() noexcept { return val; }inline constexpr const T& value() const noexcept { return val; }};template<typename T, size_t K>struct Derivable<T, K, true> : private T {constexpr Derivable() = default;template<typename... Args>constexpr Derivable(Args&&... args) noexcept(std::is_nothrow_constructible<T, Args&&...>::value) : T(std::forward<Args>(args)...) {}inline constexpr T& value() noexcept { return *this; }inline constexpr const T& value() const noexcept { return *this; }};template<typename Alloc>struct InitializerIterator {using traits = std::allocator_traits<Alloc>;using pointer = typename traits::pointer;using value_type = typename traits::value_type;InitializerIterator(const InitializerIterator&) = delete;InitializerIterator(InitializerIterator&& other) noexcept: ptr(other.ptr), cur(other.cur), p_alloc(other.p_alloc){other.ptr = other.cur = nullptr;other.p_alloc = nullptr;}InitializerIterator(pointer ptr, Alloc& alloc) noexcept : ptr(ptr), cur(ptr), p_alloc(&alloc) {}~InitializerIterator() noexcept {if (!std::is_trivially_destructible<value_type>::value)for (pointer it = ptr;it != cur;++it)traits::destroy(*p_alloc, std::addressof(*it));}inline pointer release() noexcept {return ptr = cur;}inline InitializerIterator& operator*() noexcept { return *this; }inline InitializerIterator& operator++() noexcept { return *this; }inline InitializerIterator& operator++(int) noexcept { return *this;  }InitializerIterator& operator=(const InitializerIterator&) = delete;InitializerIterator& operator=(InitializerIterator&& other) noexcept {ptr = other.ptr;cur = other.cur;p_alloc = other.p_alloc;other.ptr = other.cur = nullptr;other.p_alloc = nullptr;return *this;}template<typename T>inline InitializerIterator& operator=(T&& val) noexcept(noexcept(std::is_nothrow_constructible<value_type, T&&>::value)) {traits::construct(*p_alloc, std::addressof(*cur), std::forward<T>(val));++cur;return *this;}private:pointer ptr, cur;Alloc* p_alloc;};
}template<typename TableIt, typename OutIt, typename Op>
inline OutIt make_sparse_table(TableIt st, size_t n, OutIt it, const Op& op = {}) {TableIt pre = std::move(st);for (size_t i = 2;i <= n;i *= 2) {for (size_t j = 0;j <= n - i;++j)*it++ = op(pre[j], pre[j + i / 2]);pre += n + 1 - i / 2;}return std::move(it);
}inline size_t sparse_table_size(size_t n) {const size_t h = details::bsr(n) + size_t(1);return n * h + h + size_t(1) - (size_t(1) << h);
}template<typename TableIt, typename Op>
inline typename std::iterator_traits<TableIt>::value_type query_sparse_table(TableIt st, size_t n, size_t l, size_t r, const Op& op = {}) {assert(l <= r && r < n);r++;const size_t h = details::bsr(r - l), ph = size_t(1) << h;const auto row = st + (n * h + h + 1 - ph);return op(row[l], row[r - ph]);
}template<typename It, typename Op>
class SparseTableView {
public:using value_type = typename std::iterator_traits<It>::value_type;using size_type = unsigned;using operator_type = Op;protected:It st;size_type n;NO_UNIQUE_ADDRESS operator_type op;public:SparseTableView(It it, size_type n, const operator_type& op = {}) : st(std::move(it)), n(n), op(op) {}inline size_type size() const noexcept {return n;}// 闭区间inline value_type query(size_type l, size_type r) const {return query_sparse_table(st, n, l, r, op);}// 闭区间inline value_type query(size_type l, size_type r, const value_type& unitary) const {return r <= l ? unitary : query(l, r);}
};template<typename T, typename Op>
class SparseTable : protected SparseTableView<T*, Op> {
private:using base_type = SparseTableView<T*, Op>;public:using typename base_type::value_type;using typename base_type::size_type;using typename base_type::operator_type;using pointer = value_type*;using reference = value_type&;using const_pointer = const value_type*;using const_reference = const value_type&;private:using allocator_type = std::allocator<value_type>;using allocator_traits = std::allocator_traits<allocator_type>;inline const operator_type& operator_() const noexcept {return this->op;}inline static allocator_type& allocator() noexcept {static allocator_type alloc{};return alloc;}struct Guard {pointer ptr;size_type len;~Guard() { if (len > 0) allocator_traits::deallocate(allocator(), ptr, len); }};inline void clean() noexcept {if (this->n > 0) {const size_type st_size = sparse_table_size(this->n);if (!std::is_trivially_destructible<value_type>::value) {const pointer end = this->st + st_size;for (pointer it = this->st;it != end;++it)allocator_traits::destroy(allocator(), it);}allocator_traits::deallocate(allocator(), this->st, st_size);}}public:template<typename It>SparseTable(It it, size_type n, const Op& op = {}): base_type(nullptr, n, op){if (n == 0) return;const size_type st_size = sparse_table_size(n);Guard guard = {allocator_traits::allocate(allocator(), st_size), st_size};details::InitializerIterator<allocator_type> initializer(guard.ptr, allocator());for (size_type i = 0;i < n;++i, ++it)*initializer++ = *it;make_sparse_table(guard.ptr, n, std::move(initializer), operator_()).release();this->st = guard.ptr;guard.len = 0;}template<typename S>SparseTable(const S& seq, const Op& op = {}) : SparseTable(seq.begin(), seq.size(), op) {}SparseTable(const SparseTable&) = delete;SparseTable(SparseTable&& other) noexcept: base_type(std::move(other)){other.st = nullptr;other.n = 0;}SparseTable& operator=(const SparseTable&) = delete;SparseTable& operator=(SparseTable&& other) noexcept {clean();base_type::operator=(std::move(other));other.st = nullptr;other.n = 0;return *this;}~SparseTable() {clean();}inline void clear() noexcept {clean();this->st = nullptr;this->n = 0;}inline SparseTableView<const_pointer, Op> view() const {return SparseTableView<const_pointer, Op>(this->st, this->n, this->op);}using base_type::size;using base_type::query;
};class Solution {
public:struct MAX {int operator()(int x, int y) const noexcept {return std::max(x, y);}};vector<int> maxSlidingWindow(vector<int>& nums, int k) {SparseTable<int, MAX> st(nums);vector<int> ans;for (size_t i = 0, n = nums.size(); i + k <= n; ++i)ans.push_back(st.query(i, i + k - 1)); // 调整为闭区间return ans;}
};template<typename T>
struct MAX {T operator()(T x, T y) const noexcept {return std::max(x, y);}
};template<typename T>
struct MIN {T operator()(T x, T y) const noexcept {return std::min(x, y);}
};template<typename T>
struct SUM {T operator()(T x, T y) const noexcept {return x + y;}
};struct MaxIndex {const vector<int>& nums;MaxIndex(const vector<int>& nums) : nums(nums) {}int operator()(int i, int j) const noexcept {return nums[i] >= nums[j] ? i : j;}
};struct MinIndex {const vector<int>& nums;MinIndex(const vector<int>& nums) : nums(nums) {}int operator()(int i, int j) const noexcept {return nums[i] <= nums[j] ? i : j;}
};template<typename T>
struct SparseTableMaxIndex {MaxIndex max_index;vector<int> indices;SparseTable<T, MaxIndex> max_st;SparseTableMaxIndex(const vector<int>& nums) : max_index(nums), indices(nums.size()),max_st((iota(indices.begin(), indices.end(), 0), indices), max_index) {}T query(int l, int r) const {return max_st.query(l, r);}
};template<typename T>
struct SparseTableMinIndex {MinIndex min_index;vector<int> indices;SparseTable<T, MinIndex> min_st;SparseTableMinIndex(const vector<int>& nums) : min_index(nums), indices(nums.size()),min_st((iota(indices.begin(), indices.end(), 0), indices), min_index) {}T query(int l, int r) const {return min_st.query(l, r);}
};template<typename T>
using SparseTableSum = SparseTable<T, SUM<T>>;template<typename T>
using SparseTableMax = SparseTable<T, MAX<T>>;template<typename T>
using SparseTableMin = SparseTable<T, MIN<T>>;int main() {vector<int> nums = {1, -1, 4, 7};// 闭区间查询SparseTableMaxIndex<int> st(nums);for (int i = 0; i < 4; i++) {for (int j = i; j <= 4; j++) {cout << st.query(i, j) << " "; // 调整为闭区间}cout << endl;}
}
#include <bits/stdc++.h>
using namespace std;#ifndef NO_UNIQUE_ADDRESS
#    ifdef __has_cpp_attribute
#        if __has_cpp_attribute(no_unique_address)
#            define NO_UNIQUE_ADDRESS [[no_unique_address]]
#        endif
#    endif
#    ifndef NO_UNIQUE_ADDRESS
#        define NO_UNIQUE_ADDRESS
#    endif
#endif // !NO_UNIQUE_ADDRESSnamespace details {inline unsigned int bsf32(uint32_t n) noexcept {
#if defined __GNUC__return __builtin_ctz(n);
#elif defined _MSC_VERunsigned long ans;_BitScanForward(&ans, n);return ans;
#elifstatic constexpr unsigned char table[32] = {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9,};return table[((n & -n) * 0x077CB531) >> 57];
#endif}inline unsigned int bsf64(uint64_t n) noexcept {
#if defined __GNUC__return __builtin_ctzll(n);
#elif defined _MSC_VERunsigned long ans;_BitScanForward64(&ans, n);return ans;
#elifstatic constexpr unsigned char table[64] = {0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,};return table[((n & -n) * 0x03f79d71b4ca8b09) >> 58];
#endif}inline unsigned int bsr32(uint32_t n) noexcept {
#if defined __GNUC__return 31 - __builtin_clz(n);
#elif defined _MSC_VERunsigned long ans;_BitScanReverse(&ans, n);return ans;
#eliffloat t = n;uint32_t ans;memcpy(&ans, &t, sizeof(float));return (ans >> 23 & 255) - 127;
#endif}inline unsigned int bsr64(uint64_t n) noexcept {
#if defined __GNUC__return 63 - __builtin_clzll(n);
#elif defined _MSC_VERunsigned long ans;_BitScanReverse64(&ans, n);return ans;
#eliffloat t = n;uint32_t ans;memcpy(&ans, &t, sizeof(float));return (ans >> 23 & 255) - 127;
#endif}inline unsigned int popcnt32(uint32_t n) noexcept {
#if defined __GNUC__return __builtin_popcount(n);
#elif defined _MSC_VERreturn __popcnt(n);
#elifn -= ((n >> 1) & 0x55555555);n = (n & 0x33333333) + ((n >> 2) & 0x33333333);return ((((n >> 4) + n) & 0x0f0f0f0f) * 0x01010101) >> 24;
#endif}inline unsigned int popcnt64(uint64_t n) noexcept {
#if defined __GNUC__return __builtin_popcountll(n);
#elif defined _MSC_VERreturn __popcnt64(n);
#elifn -= ((n >> 1) & 0x5555555555555555);n = (n & 0x3333333333333333) + ((n >> 2) & 0x3333333333333333);return ((((n >> 4) + n) & 0x0f0f0f0f0f0f0f0f) * 0x0101010101010101) >> 56;
#endif}inline unsigned int parity32(uint32_t n) noexcept {
#if defined __GNUC__return __builtin_parity(n);
#elif defined _MSC_VERreturn __popcnt(n) & 1;
#elifn ^= n >> 1;n ^= n >> 2;n = (n & 0x11111111) * 0x11111111;return (n >> 28) & 1;
#endif}inline unsigned int parity64(uint64_t n) noexcept {
#if defined __GNUC__return __builtin_parityll(n);
#elif defined _MSC_VERreturn __popcnt64(n) & 1;
#elifn ^= n >> 1;n ^= n >> 2;n = (n & 0x1111111111111111) * 0x1111111111111111;return (n >> 60) & 1;
#endif}template<typename I, bool B = 32 < std::numeric_limits<I>::digits>struct _BitFuncImpl {inline static unsigned int bsf(I n) noexcept { return bsf64(n); }inline static unsigned int bsr(I n) noexcept { return bsr64(n); }inline static unsigned int popcnt(I n) noexcept { return popcnt64(n); }inline static unsigned int parity(I n) noexcept { return parity64(n); }};template<typename I>struct _BitFuncImpl<I, false> {inline static unsigned int bsf(I n) noexcept { return bsf32(n); }inline static unsigned int bsr(I n) noexcept { return bsr32(n); }inline static unsigned int popcnt(I n) noexcept { return popcnt32(n); }inline static unsigned int parity(I n) noexcept { return parity32(n); }};template<typename I>inline unsigned int bsf(I n) noexcept { return _BitFuncImpl<I>::bsf(n); }template<typename I>inline unsigned int bsr(I n) noexcept { return _BitFuncImpl<I>::bsr(n); }template<typename I>inline unsigned int popcnt(I n) noexcept { return _BitFuncImpl<I>::popcnt(n); }template<typename I>inline unsigned int parity(I n) noexcept { return _BitFuncImpl<I>::parity(n); }template<typename T, size_t K = 0, bool = std::is_empty<T>::value && !std::is_final<T>::value>struct Derivable {private:T val;public:constexpr Derivable() = default;template<typename... Args>constexpr Derivable(Args&&... args) noexcept(std::is_nothrow_constructible<T, Args&&...>::value) : val(std::forward<Args>(args)...) {}inline constexpr T& value() noexcept { return val; }inline constexpr const T& value() const noexcept { return val; }};template<typename T, size_t K>struct Derivable<T, K, true> : private T {constexpr Derivable() = default;template<typename... Args>constexpr Derivable(Args&&... args) noexcept(std::is_nothrow_constructible<T, Args&&...>::value) : T(std::forward<Args>(args)...) {}inline constexpr T& value() noexcept { return *this; }inline constexpr const T& value() const noexcept { return *this; }};template<typename Alloc>struct InitializerIterator {using traits = std::allocator_traits<Alloc>;using pointer = typename traits::pointer;using value_type = typename traits::value_type;InitializerIterator(const InitializerIterator&) = delete;InitializerIterator(InitializerIterator&& other) noexcept: ptr(other.ptr), cur(other.cur), p_alloc(other.p_alloc){other.ptr = other.cur = nullptr;other.p_alloc = nullptr;}InitializerIterator(pointer ptr, Alloc& alloc) noexcept : ptr(ptr), cur(ptr), p_alloc(&alloc) {}~InitializerIterator() noexcept {if (!std::is_trivially_destructible<value_type>::value)for (pointer it = ptr;it != cur;++it)traits::destroy(*p_alloc, std::addressof(*it));}inline pointer release() noexcept {return ptr = cur;}inline InitializerIterator& operator*() noexcept { return *this; }inline InitializerIterator& operator++() noexcept { return *this; }inline InitializerIterator& operator++(int) noexcept { return *this;  }InitializerIterator& operator=(const InitializerIterator&) = delete;InitializerIterator& operator=(InitializerIterator&& other) noexcept {ptr = other.ptr;cur = other.cur;p_alloc = other.p_alloc;other.ptr = other.cur = nullptr;other.p_alloc = nullptr;return *this;}template<typename T>inline InitializerIterator& operator=(T&& val) noexcept(noexcept(std::is_nothrow_constructible<value_type, T&&>::value)) {traits::construct(*p_alloc, std::addressof(*cur), std::forward<T>(val));++cur;return *this;}private:pointer ptr, cur;Alloc* p_alloc;};
}template<typename TableIt, typename OutIt, typename Op>
inline OutIt make_sparse_table(TableIt st, size_t n, OutIt it, const Op& op = {}) {TableIt pre = std::move(st);for (size_t i = 2;i <= n;i *= 2) {for (size_t j = 0;j <= n - i;++j)*it++ = op(pre[j], pre[j + i / 2]);pre += n + 1 - i / 2;}return std::move(it);
}inline size_t sparse_table_size(size_t n) {const size_t h = details::bsr(n) + size_t(1);return n * h + h + size_t(1) - (size_t(1) << h);
}template<typename TableIt, typename Op>
inline typename std::iterator_traits<TableIt>::value_type query_sparse_table(TableIt st, size_t n, size_t l, size_t r, const Op& op = {}) {const size_t h = details::bsr(r - l), ph = size_t(1) << h;const auto row = st + (n * h + h + 1 - ph);return op(row[l], row[r - ph]);
}template<typename It, typename Op>
class SparseTableView {
public:using value_type = typename std::iterator_traits<It>::value_type;using size_type = unsigned;using operator_type = Op;protected:It st;size_type n;NO_UNIQUE_ADDRESS operator_type op;public:SparseTableView(It it, size_type n, const operator_type& op = {}) : st(std::move(it)), n(n), op(op) {}inline size_type size() const noexcept {return n;}inline value_type query(size_type l, size_type r) const {return query_sparse_table(st, n, l, r, op);}inline value_type query(size_type l, size_type r, const value_type& unitary) const {return r <= l ? unitary : query(l, r);}
};template<typename T, typename Op>
class SparseTable : protected SparseTableView<T*, Op> {
private:using base_type = SparseTableView<T*, Op>;public:using typename base_type::value_type;using typename base_type::size_type;using typename base_type::operator_type;using pointer = value_type*;using reference = value_type&;using const_pointer = const value_type*;using const_reference = const value_type&;private:using allocator_type = std::allocator<value_type>;using allocator_traits = std::allocator_traits<allocator_type>;inline const operator_type& operator_() const noexcept {return this->op;}inline static allocator_type& allocator() noexcept {static allocator_type alloc{};return alloc;}struct Guard {pointer ptr;size_type len;~Guard() { if (len > 0) allocator_traits::deallocate(allocator(), ptr, len); }};inline void clean() noexcept {if (this->n > 0) {const size_type st_size = sparse_table_size(this->n);if (!std::is_trivially_destructible<value_type>::value) {const pointer end = this->st + st_size;for (pointer it = this->st;it != end;++it)allocator_traits::destroy(allocator(), it);}allocator_traits::deallocate(allocator(), this->st, st_size);}}public:template<typename It>SparseTable(It it, size_type n, const Op& op = {}): base_type(nullptr, n, op){if (n == 0) return;const size_type st_size = sparse_table_size(n);Guard guard = {allocator_traits::allocate(allocator(), st_size), st_size};details::InitializerIterator<allocator_type> initializer(guard.ptr, allocator());for (size_type i = 0;i < n;++i, ++it)*initializer++ = *it;make_sparse_table(guard.ptr, n, std::move(initializer), operator_()).release();this->st = guard.ptr;guard.len = 0;}template<typename S>SparseTable(const S& seq, const Op& op = {}) : SparseTable(seq.begin(), seq.size(), op) {}SparseTable(const SparseTable&) = delete;SparseTable(SparseTable&& other) noexcept: base_type(std::move(other)){other.st = nullptr;other.n = 0;}SparseTable& operator=(const SparseTable&) = delete;SparseTable& operator=(SparseTable&& other) noexcept {clean();base_type::operator=(std::move(other));other.st = nullptr;other.n = 0;return *this;}~SparseTable() {clean();}inline void clear() noexcept {clean();this->st = nullptr;this->n = 0;}inline SparseTableView<const_pointer, Op> view() const {return SparseTableView<const_pointer, Op>(this->st, this->n, this->op);}using base_type::size;using base_type::query;
};

相关文章:

c++ sparsetable 模版

闭区间查询 支持 区间最大 区间最小 区间和 区间最大下标 区间最小下标 #include <bits/stdc.h> using namespace std;#ifndef NO_UNIQUE_ADDRESS # ifdef __has_cpp_attribute # if __has_cpp_attribute(no_unique_address) # define NO_UNIQUE_…...

创建线程池和封装锁

封装一个锁 1.封装一个Mutex class Mutex{public:Mutex(pthread_mutex_t * lock):_lock(lock){}void Lock(){pthread_mutex_lock(_lock);}void unLock(){pthread_mutex_unlock(_lock);}~Mutex(){}private:pthread_mutex_t *_lock; };2.封装一个LockGuard class LockGuard{pub…...

易图讯军用VR三维电子沙盘系统

深圳易图讯军用VR三维电子沙盘系统是一种集成了虚拟现实&#xff08;VR&#xff09;技术、三维建模技术、大数据分析、实时动态更新以及高度安全可靠的综合性军事指挥平台。该系统通过高精度三维模型真实再现战场环境&#xff0c;为指挥员提供沉浸式体验和交互操作的可能性&…...

LeetCode讲解篇之70. 爬楼梯

文章目录 题目描述题解思路题解代码题目链接 题目描述 题解思路 爬楼梯有一个规律&#xff0c;爬到第n层楼梯的方法种数 爬到第n - 1层楼梯的方法种数 爬到第n - 1层楼梯的方法种数 也就是我们爬到第n层楼梯其实是从第n - 1层楼梯向上爬1层或者是n - 2层楼梯向上爬2层转换来…...

论文写作不再难,论文初稿快速成型法!

撰写论文是每个学者的必修课&#xff0c;我非常明白撰写论文的不易。撰写过程中会遇到各种困扰&#xff0c;如思路不清晰、论证不充分、语言表达不准确等。在这里以我的经验分享给大家一个能快速完成论文初稿的秘诀“AI导师写作”&#xff0c;希望能帮助还在为论文发愁的你。 …...

linux系统,监控进程运行状态并自动重启崩溃后的进程的多种方法

系统进程运行异常崩溃后&#xff0c;自动重启的方法 有的公司&#xff0c;会写monitor守护进程&#xff0c;监视各个进程的运行状态&#xff0c;异常时&#xff0c;自动重启&#xff0c;但是这种&#xff0c;通过一个进程 监护一个进程的做法&#xff0c;不太完美&#xff0c;…...

【JavaEE初阶】深入理解不同锁的意义,synchronized的加锁过程理解以及CAS的原子性实现(面试经典题);

前言 &#x1f31f;&#x1f31f;本期讲解关于锁的相关知识了解&#xff0c;这里涉及到高频面试题哦~~~ &#x1f308;上期博客在这里&#xff1a;【JavaEE初阶】深入理解线程池的概念以及Java标准库提供的方法参数分析-CSDN博客 &#x1f308;感兴趣的小伙伴看一看小编主页&am…...

详解Redis分布式锁在SpringBoot的@Async方法中没锁住的坑

背景 Redis分布式锁很有用处&#xff0c;在秒杀、抢购、订单、限流特别是一些用到异步分布式并行处理任务时频繁的用到&#xff0c;可以说它是一个BS架构的应用中最高频使用的技术之一。 但是我们经常会碰到这样的一个问题&#xff0c;那就是我们都按照标准做了但有时运行着、…...

怎么做接口自动化测试

在分层测试的“金字塔”模型中&#xff0c;接口测试属于第二层服务集成测试范畴。相比UI层&#xff08;主要是WEB或APP&#xff09;自动化测试而言&#xff0c;接口自动化测试收益更大&#xff0c;且容易实现&#xff0c;维护成本低&#xff0c;有着更高的投入产出比&#xff0…...

网络编程(18)——使用asio协程实现并发服务器

十八、day18 到目前为止&#xff0c;我们以及学习了单线程同步/异步服务器、多线程IOServicePool和多线程IOThreadPool模型&#xff0c;今天学习如何通过asio协程实现并发服务器。 并发服务器有以下几种好处&#xff1a; 协程比线程更轻量&#xff0c;创建和销毁协程的开销较…...

Koa2项目实战2(路由管理、项目结构优化)

添加路由&#xff08;处理不同的URL请求&#xff09; 路由&#xff1a;根据不同的URL&#xff0c;调用对应的处理函数。 每一个接口服务&#xff0c;最核心的功能是&#xff1a;根据不同的URL请求&#xff0c;返回不同的数据。也就是调用不同的接口返回不同的数据。 在 Node…...

决战Linux操作系统

前言&#xff1a; 你是否也曾经为Linux所困扰过&#xff0c;在网上找的资料零零散散&#xff0c;是否学完Linux后还是懵懵懂懂&#xff0c;别怕&#xff0c;这篇博客是博主精心为你准备的&#xff0c;现在&#xff0c;就让我们一起来走进Linux的世界&#xff0c;决战Linux&…...

OceanBase 3.2.2 数据库问题处理记录

只记录OceanBase 数据库与OCP的异常处理&#xff0c;其它组件暂时不写录。 一、问题1&#xff1a; 说明&#xff1a;OMS 出现异常&#xff0c;无法访问(OB无法访问) OB数据库架构&#xff1a;1:1:1 原因&#xff1a;某一台OBserver因为内存问题&#xff0c;被服务器直接kill掉…...

HCIP--以太网交换安全(二)端口安全

端口安全 一、端口安全概述 1.1、端口安全概述&#xff1a;端口安全是一种网络设备防护措施&#xff0c;通过将接口学习的MAC地址设为安全地址防止非法用户通信。 1.2、端口安全原理&#xff1a; 类型 定义 特点 安全动态MAC地址 使能端口而未是能Stichy MAC功能是转换的…...

在 Windows 11 安卓子系统中安装 APK 的操作指南

这个软件好像不可以在纯android系统中使用&#xff08;不知道是缺了什么&#xff09;&#xff0c;其他对于android的虚拟机要不缺少必要功能组件&#xff0c;要不性能过于低下。本方法致力于在带有谷歌框架WSA中运行该APK 在 Windows 11 安卓子系统中安装 APK 的操作指南 本指…...

[C语言] 函数详解:库函数与自定义函数

文章目录 函数的概念库函数和自定义函数库函数使用库函数示例常用库函数及头文件 自定义函数自定义函数的基本结构示例&#xff1a;实现两个数的求和函数自定义函数的好处 函数的返回值有返回值的函数无返回值的函数 函数的声明与调用声明函数在另一个文件中调用函数示例&#…...

0x11 科迈 RAS系统 Cookie验证越权漏洞

参考: 科迈 RAS系统 Cookie验证越权漏洞 | PeiQi文库 (wgpsec.org)免责声明 欢迎访问我的博客。以下内容仅供教育和信息用途: 合法性:我不支持或鼓励非法活动。请确保遵守法律法规。信息准确性:尽管我尽力提供准确的信息,但不保证其完全准确或适用。使用前请自行验证。风…...

MoonBit 双周报 Vol.57:AI助手功能增强、表达式优先级调整、JS 交互优化、标准库与实验库API多项更新!

2024-10-08 IDE更新 AI Codelens支持 /generate 和 /fix 命令 /generate 命令能够提供一个通用的用以生成代码的聊天界面。 /fix 命令能够读取当前函数的错误信息给出修复建议。 MoonBit更新 调整中缀表达式和if、match、loop、while、for、try表达式的优先级, 后者这些控制…...

element ui input textarea控制显示高度

样式代码 .testPage { position: absolute; left: 0; top: 0; right: 0; bottom: 0; display: flex; height: 100%; /* 控制输入框高度 */ .el-textarea { height: 90%; ::v-deep .el-textarea__inner { height: 90%; } } }...

Chromium 中chrome.downloads扩展接口c++

一、前端chrome.downloads 使用 chrome.downloads API 以编程方式启动、监控、操作和搜索下载内容。 权限 downloads 您必须在扩展程序清单中声明 "downloads" 权限&#xff0c;才能使用此 API。 {"name": "My extension",..."permiss…...

别再混淆了!一文讲透NvDecoder里ulNumDecodeSurfaces和ulNumOutputSurfaces到底怎么用

深入解析NvDecoder&#xff1a;解码缓存与输出缓存的本质区别与实战配置 在视频处理领域&#xff0c;NVIDIA的硬件解码器&#xff08;NVDEC&#xff09;因其出色的性能和高效的资源利用率而广受开发者青睐。然而&#xff0c;对于许多中高级开发者来说&#xff0c;NvDecoder中ul…...

从开发到上线:在快马平台部署一个可商用的旗博士口播智能体

最近在做一个电商直播相关的项目&#xff0c;需要快速搭建一个智能口播文案生成工具。经过一番摸索&#xff0c;我发现用InsCode(快马)平台可以非常高效地完成从开发到上线的全流程。下面分享下我的实战经验。 项目需求分析 这个旗博士口播智能体主要面向直播运营人员&#xff…...

VS Code 效率技巧:符号导航快速定位代码

推荐阅读 技术总监悄悄秀了一把 VS Code 神技&#xff0c;被我狠狠学到了&#xff01; VS Code 又发布了一个 Agent 新玩具&#xff01; VS Code 1.110 官宣 AI 新特性&#xff1a;AI 直接调试浏览器&#xff01; VS Code 2026 效率秘籍&#xff1a;学完无敌&#xff01…...

基于Dify的AI数据采集与整理工具设计与实现

基于Dify的AI数据采集与整理工具设计与实现 1. 引言 1.1 背景与需求 在信息爆炸的时代,新闻网站、人物资料库等不断产生海量数据。传统手动采集整理方式效率低下,难以满足实时性、准确性和规模化的要求。本工具旨在利用Dify平台的强大编排能力,结合AI大语言模型(LLM)和…...

Llama-3.2V-11B-cot入门必看:Streamlit会话状态管理保障多用户隔离

Llama-3.2V-11B-cot入门必看&#xff1a;Streamlit会话状态管理保障多用户隔离 1. 项目概述 Llama-3.2V-11B-cot是基于Meta Llama-3.2V-11B-cot多模态大模型开发的高性能视觉推理工具&#xff0c;专为双卡4090环境深度优化。该工具通过Streamlit框架构建了宽屏友好的交互界面…...

从‘翻车’到稳定:手把手教你用Matlab极点配置驯服小车倒立摆(附Simulink模型)

用Matlab极点配置实现小车倒立摆的精准控制&#xff1a;从理论到Simulink实战 倒立摆系统作为控制理论中的经典案例&#xff0c;完美展现了动态系统稳定控制的挑战与魅力。想象一下&#xff0c;一根垂直向上的杆子放在移动小车上&#xff0c;任何微小的扰动都会导致杆子倾倒——…...

实战AI情感分析:基于快马平台构建电商评论智能洞察系统

最近在做一个电商数据分析项目时&#xff0c;发现人工处理海量商品评论实在太费时费力。于是尝试用AI情感分析技术来提升效率&#xff0c;在InsCode(快马)平台上快速搭建了一个评论智能分析系统。整个过程比想象中简单很多&#xff0c;分享下具体实现思路&#xff1a; 系统架构…...

【实战指南】解决Qt平台插件加载失败:从环境变量到PyQt5重装的完整方案

1. 遇到Qt平台插件加载失败&#xff1f;别慌&#xff0c;先看懂报错信息 最近在Windows上跑labelimg标注工具时&#xff0c;突然弹出一个让人头疼的错误&#xff1a; qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though…...

从硅片到电路:图解CMOS反相器的制造工艺与工作原理

从硅片到电路&#xff1a;图解CMOS反相器的制造工艺与工作原理 在半导体工业中&#xff0c;CMOS反相器作为数字电路的基本构建模块&#xff0c;其制造工艺凝聚了现代微电子技术的精华。本文将带您深入半导体fab的微观世界&#xff0c;通过工艺截面图的逐步解析&#xff0c;揭示…...

惊艳展示:MedGemma医学影像分析系统,自然语言提问生成专业报告

惊艳展示&#xff1a;MedGemma医学影像分析系统&#xff0c;自然语言提问生成专业报告 1. 引言&#xff1a;当AI能“看懂”医学影像&#xff0c;并“说”出专业见解 想象一下&#xff0c;你手里有一张肺部X光片&#xff0c;但你不是放射科医生。你看着那些黑白影像和复杂的结…...