跳到正文

题解

AT_abc359_e 题解

Vitamin_B约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年6月25日
收录
2025年12月12日
更新
2026年10月31日
篇幅
约 1 分钟
获赞
1

版本

  1. v1当前

思路

设 xx 表示最后一个满足 hx≥hih_x\ge h_i 且 x<ix<i 的模板,显然要使 ii 号点灌到水那么就要把 xx 以后的地方全都淹没到 hih_i 为止,所以 ansi=ansx+(i−x)×hians_i=ans_x+(i-x)\times h_i。

怎么求 xx 呢?显然,若 hj<hih_j<h_i,则 xi≥xjx_i\ge x_j。所以,我们只要从 i−1i-1 开始,一路往上跳,直到 hj≥hih_j\ge h_i 为止。然后把 xi,ansix_i,ans_i 存下来输出就行。

代码

C++
# 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;
}
原文