AT_abc359_e 题解
思路
设
怎么求
代码
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int n, h[200005], lst[200005];
ll ans[200005];
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> n;
h[0] = 1e9 + 5, ans[0] = 1;
for (int i = 1; i <= n; ++ i) {
cin >> h[i];
lst[i] = i - 1;
while (h[i] > h[lst[i]])
lst[i] = lst[lst[i]];
cout << (ans[i] = ans[lst[i]] + (ll) (i - lst[i]) * h[i]) << ' ';
}
return 0;
}