# [51Nod 1564 区间的价值]单调栈

## 1. 题目链接

[51Nod 1564 区间的价值]

## 3. 解题思路

• ${L}_{mi{n}_{i}}=\mathrm{m}\mathrm{i}\mathrm{n}\left\{j\right\}$$L_{min_{i}}=\mathrm{min}\{j\}$, 其中$j$$j$满足$\mathrm{\forall }k\in \left[j,i\right]$$\forall k\in[j,i]$都有${A}_{k}\ge {A}_{i}$$A_k\ge A_i$;
• ${R}_{mi{n}_{i}}=\mathrm{m}\mathrm{a}\mathrm{x}\left\{j\right\}$$R_{min_{i}}=\mathrm{max}\{j\}$, 其中$j$$j$满足$\mathrm{\forall }k\in \left[i,j\right]$$\forall k\in[i,j]$都有${A}_{k}\ge {A}_{i}$$A_k\ge A_i$;

(Orz, 数学描述起来好费劲)

## 4. 参考代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const long double eps = 1e-8;
const ll infl = 0x3f3f3f3f3f3f3f3f;

const int MAXN = 100005;
int n, A[MAXN];
ll res[MAXN];
int mi[30][MAXN], ma[30][MAXN];
int lmin[MAXN], rmin[MAXN];

int rmq_min(int l, int r) {
int w = r - l + 1, d = log2(w);
return min(mi[d][l], mi[d][r - (1 << d) + 1]);
}
int rmq_max(int l, int r) {
int w = r - l + 1, d = log2(w);
return max(ma[d][l], ma[d][r - (1 << d) + 1]);
}

int main() {
#ifdef __LOCAL_WONZY__
freopen("input-3.txt", "r", stdin);
#endif
scanf("%d", &n);
//n = 1e5;
for(int i = 1; i <= n; ++i) {
scanf("%d", &A[i]);
//A[i] = rand() + 1;
mi[0][i] = ma[0][i] = A[i];
res[i] = 0;
}
for(int j = 1; j < 30; ++j) {
int w = 1 << j;
for(int i = 1; i + w - 1 <= n; ++i) {
ma[j][i] = max(ma[j - 1][i], ma[j - 1][i + (w >> 1)]);
mi[j][i] = min(mi[j - 1][i], mi[j - 1][i + (w >> 1)]);
}
}
stack<int> stk;
for(int i = 1; i <= n; ++i) {
while(!stk.empty() && A[stk.top()] > A[i]) {
rmin[stk.top()] = i - 1;
stk.pop();
}
stk.push(i);
}
while(!stk.empty()) {
rmin[stk.top()] = n;
stk.pop();
}
for(int i = n; i >= 1; --i) {
while(!stk.empty() && A[stk.top()] > A[i]) {
lmin[stk.top()] = i + 1;
stk.pop();
}
stk.push(i);
}
while(!stk.empty()) {
lmin[stk.top()] = 1;
stk.pop();
}
for(int i = 1; i <= n; ++i) {
int w = rmin[i] - lmin[i] + 1;
res[w] = max(res[w], (ll) A[i] * rmq_max(lmin[i], rmin[i]));
}
for(int i = n; i >= 1; --i) res[i] = max(res[i], res[i + 1]);
for(int i = 1; i <= n; ++i) printf("%lld\n", res[i]);
return 0;
}