AtCoder Regular Contest 189 (Div. 2)
A
计数
B
折叠差不变
D
观察性质暴力
#include<bits/stdc++.h> using namespace std; #define pb push_back #define endl '\n' #define LL long long const int N = 5e5 + 10; int n, a[N], l[N], r[N]; LL pre[N], suf[N], b[N]; void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; b[i] = a[i]; l[i] = i - 1; r[i] = i + 1; pre[i] = pre[i - 1] + a[i]; } for (int i = n; i >= 1; i--) { suf[i] = suf[i + 1] + a[i]; } while (1) { bool flag = 0; for (int i = 1; i <= n; i++) { while (l[i] >= 1 && b[i] > a[l[i]]) { b[i] += pre[l[i]] - pre[l[l[i]]]; l[i] = l[l[i]]; flag = 1; } } for (int i = n; i >= 1; i--) { while (r[i] <= n && b[i] > a[r[i]]) { b[i] += suf[r[i]] - suf[r[r[i]]]; r[i] = r[r[i]]; flag = 1; } } if (!flag) break; } for (int i = 1; i <= n; i++) { cout << b[i] << ' '; } } int main() { // ios::sync_with_stdio(false); // cin.tie(nullptr); // cin >> t; solve(); return 0; }
笛卡尔树(特殊)
#include<bits/stdc++.h> using namespace std; #define pb push_back #define endl '\n' #define LL long long const int N = 5e5 + 10; int n, a[N], ls[N], rs[N], k, stk[N]; LL ans[N], sum[N]; void dfs1(int x) { sum[x] = a[x]; if (ls[x]) dfs1(ls[x]); if (rs[x]) dfs1(rs[x]); if (ls[x]) sum[x] += sum[ls[x]]; if (rs[x]) sum[x] += sum[rs[x]]; } void dfs(int x, int fa) { ans[x] = sum[x]; if (fa && ans[x] > a[fa]) ans[x] = ans[fa]; if (ls[x]) dfs(ls[x], x); if (rs[x]) dfs(rs[x], x); } void solve() { int top = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; while(k > 0 && a[stk[k]] < a[i]) k--; if (k) rs[stk[k]] = i; if (k < top) ls[i] = stk[k + 1]; stk[++k] = i; top = k; } dfs1(stk[1]); dfs(stk[1], 0); a[0] = a[n + 1] = 1e9 + 7; for (int i = 1; i <= n; i++) { if (a[i - 1] < a[i] || a[i] > a[i + 1]) cout << ans[i] << ' '; else cout << a[i] << ' '; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); // cin >> t; solve(); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】