From 82a62a2fc70bdb559929c0e3bcf34f6e3b13b040 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sat, 21 Jun 2025 11:12:23 +0800 Subject: [PATCH] update --- src/6/T238883.cpp | 3 ++ src/6/T434502.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/6/T238883.cpp create mode 100644 src/6/T434502.cpp diff --git a/src/6/T238883.cpp b/src/6/T238883.cpp new file mode 100644 index 0000000..294989d --- /dev/null +++ b/src/6/T238883.cpp @@ -0,0 +1,3 @@ +int main(){ + +} \ No newline at end of file diff --git a/src/6/T434502.cpp b/src/6/T434502.cpp new file mode 100644 index 0000000..a09a901 --- /dev/null +++ b/src/6/T434502.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +using namespace std; + +typedef long long ll; +const int MAXN = 200010; + +int n; +ll a[MAXN]; +int lc[MAXN], rc[MAXN]; + +struct State { + int u; + ll base; + int state; + ll min_val; + ll new_base; + ll temp_ans; +}; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + + cin >> n; + for (int i = 0; i < n; i++) { + cin >> a[i]; + } + + vector stk; + int root = -1; + for (int i = 0; i < n; i++) { + lc[i] = rc[i] = -1; + } + + for (int i = 0; i < n; i++) { + int last = -1; + while (!stk.empty() && a[stk.back()] > a[i]) { + last = stk.back(); + stk.pop_back(); + } + if (stk.empty()) { + root = i; + } else { + rc[stk.back()] = i; + } + lc[i] = last; + stk.push_back(i); + } + + stack states; + states.push({root, 0, 0, 0, 0, 0}); + ll ans = 0; + + while (!states.empty()) { + State cur = states.top(); + states.pop(); + + if (cur.state == 0) { + cur.min_val = a[cur.u] - cur.base; + cur.new_base = cur.base; + if (cur.min_val > 0) { + cur.new_base += cur.min_val; + } + cur.state = 1; + states.push(cur); + + if (lc[cur.u] != -1) { + states.push({lc[cur.u], cur.new_base, 0, 0, 0, 0}); + } + } else if (cur.state == 1) { + cur.state = 2; + states.push(cur); + + if (rc[cur.u] != -1) { + states.push({rc[cur.u], cur.new_base, 0, 0, 0, 0}); + } + } else { + ll add = (cur.min_val > 0) ? 1 : 0; + ll total_ans = add + cur.temp_ans; + + if (states.empty()) { + ans = total_ans; + } else { + State& parent = states.top(); + parent.temp_ans += total_ans; + } + } + } + + cout << ans << endl; + return 0; +} \ No newline at end of file