Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 63a4f8f

Browse files
committed
add cart.cpp
1 parent cb2068b commit 63a4f8f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ AtCoder解説放送ライブラリ集
1919
|BIT|[bit.cpp](bit.cpp)|Binary Indexed Tree (Fenwick Tree)|
2020
|UnionFind|[uf.cpp](uf.cpp)|Union Find (DSU)|
2121
|CHT|[cht.cpp](cht.cpp)|Convex Hull Trick|
22+
|CartesianTree|[cart.cpp](cart.cpp)|Cartesian Tree|
2223

2324
### 数学
2425
|名前|コード|説明|

‎cart.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Cartesian Tree
2+
// https://youtu.be/XVu8-ZnuOiA?t=9291
3+
template<class T=long long>
4+
struct CartesianTree {
5+
int n, root;
6+
vector<int> l, r;
7+
CartesianTree() {}
8+
CartesianTree(const vector<T>& a, bool _max=true) {
9+
n = a.size();
10+
l = r = vector<int>(n,-1);
11+
vector<int> st;
12+
rep(i,n) {
13+
int p = -1;
14+
while (st.size() && !((a[st.back()] < a[i])^_max)) {
15+
int j = st.back(); st.pop_back();
16+
r[j] = p; p = j;
17+
}
18+
l[i] = p;
19+
st.push_back(i);
20+
}
21+
rep(i,st.size()-1) r[st[i]] = st[i+1];
22+
root = st[0];
23+
}
24+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /