aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFangrui Song <i@maskray.me>2024-08-03 00:25:09 -0700
committerFangrui Song <i@maskray.me>2024-08-03 00:25:09 -0700
commit42ceb770b649c8b39b7520a5c28bb21f7c0f65df (patch)
treed386e73a5883a307a494b292c9b5ccc44184969a
parentfc1c261147275c9c2c3931077a362a20fac49020 (diff)
downloadshoka-42ceb770b649c8b39b7520a5c28bb21f7c0f65df.tar.gz
shoka-42ceb770b649c8b39b7520a5c28bb21f7c0f65df.zip
make stack smaller
-rw-r--r--cartesian.h9
1 files changed, 4 insertions, 5 deletions
diff --git a/cartesian.h b/cartesian.h
index 4108d3f..fc2780e 100644
--- a/cartesian.h
+++ b/cartesian.h
@@ -14,14 +14,13 @@ class MaxCartesianTree {
public:
explicit MaxCartesianTree(std::ranges::random_access_range auto a,
C compare = {})
- : n(std::ranges::size(a)), stack(n + 1), root{-1}, child(n) {
- int top = 0;
- stack[0] = -1;
+ : n(std::ranges::size(a)), stack(n), root{-1}, child(n) {
+ int top = -1;
for (int i = 0; i < n; i++) {
- while (~stack[top] && compare(a[stack[top]], a[i])) {
+ while (~top && compare(a[stack[top]], a[i])) {
top--;
}
- int &r = top ? child[stack[top]][1] : root;
+ int &r = ~top ? child[stack[top]][1] : root;
child[i] = {r, -1};
r = i;
stack[++top] = i;