Skip to content

Commit

Permalink
feat: mono_{stack,queue} & hoverline
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiphereth-A committed Mar 2, 2025
1 parent ca65969 commit 9f28ae5
Show file tree
Hide file tree
Showing 38 changed files with 316,441 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ notebook:
code_ext: hpp
test_ext: cpp
ds:
- mono_stack: 单调栈
code_ext: hpp
test_ext: cpp
- mono_queue: 单调队列
code_ext: hpp
test_ext: cpp
- bt_trv: 树结点常见遍历操作
code_ext: hpp
test_ext: cpp
Expand Down Expand Up @@ -455,6 +461,9 @@ notebook:
- lis: 最长上升子序列
code_ext: hpp
test_ext: cpp
- hoverline: 悬线法
code_ext: hpp
test_ext: cpp
- lcs_circ: 环序列的最长公共子序列长度
code_ext: hpp
test_ext: cpp
Expand Down
29 changes: 29 additions & 0 deletions src/code/ds/mono_queue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef TIFALIBS_DS_MONO_QUEUE
#define TIFALIBS_DS_MONO_QUEUE

#include "../util/util.hpp"

namespace tifa_libs::ds {

template <class T, class Comp = std::less<T>>
class mono_queue {
static CEXP Comp compare{};

const u32 k;
u32 i;
std::deque<std::pair<T, u32>> q;

public:
CEXPE mono_queue(u32 k) NE : k{k}, i{0} { assert(k > 0); }
// @return minimum of last k (at most) pushed elements
CEXP T CR push(cT_(T) x) NE {
if (!q.empty() && q.front().second + k == i) q.pop_front();
while (!q.empty() && compare(x, q.back().first)) q.pop_back();
q.emplace_back(x, i++);
return q.front().first;
}
};

} // namespace tifa_libs::ds

#endif
41 changes: 41 additions & 0 deletions src/code/ds/mono_stack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef TIFALIBS_DS_MONO_STACK
#define TIFALIBS_DS_MONO_STACK

#include "../util/util.hpp"

namespace tifa_libs::ds {

template <class T, class Comp = std::less<T>>
class mono_stack {
static CEXP Comp compare{};

vec<T> s;
template <bool use_bf = false>
CEXP auto find_(cT_(T) x) NE {
if CEXP (use_bf) {
auto it = s.rbegin();
while (it != s.rend() && compare(x, *it)) ++it;
return (--it).base();
} else return upper_bound(s, x, compare);
}

public:
CEXPE mono_stack() NE = default;

template <bool use_bf = false>
CEXP void pop_greater(cT_(T) x) NE { s.erase(find_<use_bf>(x), s.end()); }
CEXP void push_nocheck(cT_(T) x) NE { s.push_back(x); }
template <bool use_bf = false>
CEXP void push(cT_(T) x) NE { pop_greater<use_bf>(x), s.push_back(x); }
template <bool use_bf = false>
CEXP void insert(cT_(T) x) NE {
if (auto it = find_<use_bf>(x); it == s.end()) s.push_back(x);
else *it = x;
}
CEXP T CR top() CNE { return s.back(); }
CEXP u32 size() CNE { return (u32)s.size(); }
};

} // namespace tifa_libs::ds

#endif
24 changes: 24 additions & 0 deletions src/code/opt/hoverline.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef TIFALIBS_OPT_HOVERLINE
#define TIFALIBS_OPT_HOVERLINE

#include "../util/util.hpp"

namespace tifa_libs::opt {

// @return {l, r} s.t. $\forall x\in[l_i,r_i],~a_x\geq a_i$, $l_i$ leftmost, $r_i$ rightmost
template <class T, class C = std::less<T>>
CEXP ptt<vecu> hoverline(vec<T> CR a, C&& comp = C{}) NE {
if (a.empty()) return {{}, {}};
vecu l(a.size());
std::iota(l.begin(), l.end(), 0_u32);
auto r = l;
flt_ (u32, i, 0, (u32)a.size())
while (l[i] && !comp(a[l[i] - 1], a[i])) l[i] = l[l[i] - 1];
for (u32 i = (u32)a.size() - 2; ~i; --i)
while ((i32)r[i] < (i32)a.size() - 1 && !comp(a[r[i] + 1], a[i])) r[i] = r[r[i] + 1];
return {l, r};
}

} // namespace tifa_libs::opt

#endif
101 changes: 101 additions & 0 deletions src/data/bzoj/1660/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
100
100005758
99995645
99978130
99947079
99941452
99918442
99911023
99894811
99890725
99887976
99875209
99866125
99854065
99821840
99804297
99779208
99758025
99732888
99707322
99680356
99675378
99654883
99644572
99633205
99603151
99586120
99572975
99553093
99527357
99496833
99468328
99439934
99417832
99392981
99373914
99361160
99349507
99342946
99315850
100032085
100027942
100020975
99989569
99965404
99952001
99926439
99901605
99870252
99869332
99858888
99834085
99826123
99806805
99805383
99774056
99763599
99761654
99747175
99717192
99698441
99694547
99675877
99667618
99651370
99643613
99627984
99614678
99586072
99572082
99560344
99547828
99546414
99541152
99524036
99501211
99498030
99484896
99459553
99451531
99440298
99432762
99423002
99413023
99383952
99382751
99361415
99348354
99326194
99302189
99271460
99263816
99236341
99204648
99179134
99164995
99142907
99116386
99111184
99102013
99097579
1 change: 1 addition & 0 deletions src/data/bzoj/1660/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2571
Loading

0 comments on commit 9f28ae5

Please sign in to comment.