思路
设 表示最后一个满足 且 的模板,显然要使 号点灌到水那么就要把 以后的地方全都淹没到 为止,所以 。
怎么求 呢?显然,若 ,则 。所以,我们只要从 开始,一路往上跳,直到 为止。然后把 存下来输出就行。
代码
# 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;
}