File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ AtCoder解説放送ライブラリ集
19
19
| BIT| [ bit.cpp] ( bit.cpp ) | Binary Indexed Tree (Fenwick Tree)|
20
20
| UnionFind| [ uf.cpp] ( uf.cpp ) | Union Find (DSU)|
21
21
| CHT| [ cht.cpp] ( cht.cpp ) | Convex Hull Trick|
22
+ | CartesianTree| [ cart.cpp] ( cart.cpp ) | Cartesian Tree|
22
23
23
24
### 数学
24
25
| 名前| コード| 説明|
Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments