牛客多校第四场sequence C (线段树+单调栈)

时间:2021-11-09 05:29:40

牛客多校第四场sequence C (线段树+单调栈)

传送门:https://ac.nowcoder.com/acm/contest/884/C

题意:

求一个$$ \max {1 \leq l \leq r \leq n}\left{\min \left(a{l \dots r}\right) \times \operatorname{sum}\left(b_{l \dots r}\right)\right} $$

题解:

枚举最小值

最大值可能有两种情况:两个正数相乘,两个负数相乘,我们先讨论正数相乘的情况

对于当前的\(a_i\)我们可以利用单调栈找到左边第一个比他小的数\(a_l\)和右边第一个比他小的数\(a_r\)

那么在区间\([l+1,r-1]\)的区间最小值就是\(a_i\)

我们现在已知\(a_i\)在区间内已经是最小的了,所以为了得到最大值,我们需要得到\(sum(b_{l...r})\)的最大值

将区间分为两个部分\([l+1,i]和[i,r-1]\)那么\(sum(b_{l..r})\)的最大值就是右边部分最大的前缀和减去左边部分最小的前缀和即可

对于负数相乘的情况

\(sum(b_{l...r})\)的最大值就是右边部分的最小的前缀和减去左边部分的最大前缀和即可

代码:

/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting          
*          ┃   ┃ 神兽保佑,代码无bug
*          ┃   ┃           
*          ┃   ┃       
*          ┃   ┃
*          ┃   ┃           
*          ┃   ┗━━━┓
*          ┃       ┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 佛祖保佑 永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e6 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
double ans = 1.0;
while(b) {
if(b % 2)ans = ans * a;
a = a * a;
b /= 2;
} return ans;
}
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
int stal[maxn], star[maxn], sta[maxn];
LL a[maxn];
LL b[maxn];
LL Max[maxn << 2];
LL Min[maxn << 2];
LL sum[maxn];
void push_up(int rt) {
Max[rt] = max(Max[ls], Max[rs]);
Min[rt] = min(Min[ls], Min[rs]);
}
void build(int l, int r, int rt) {
Max[rt] = 0;
Min[rt] = INF;
if(l == r) {
Max[rt] = sum[l];
Min[rt] = sum[l];
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
push_up(rt);
} LL query_max(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return Max[rt];
}
int mid = (l + r) >> 1;
LL ans = 0;
if(L <= mid) {
ans = max(ans, query_max(L, R, lson));
}
if(R > mid) {
ans = max(ans, query_max(L, R, rson));
}
return ans;
}
LL query_min(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return Min[rt];
}
int mid = (l + r) >> 1;
LL ans = INF;
if(L <= mid) {
ans = min(ans, query_min(L, R, lson));
}
if(R > mid) {
ans = min(ans, query_min(L, R, rson));
}
return ans;
} int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
sum[0] = 0;
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for(int i = 1; i <= n; i++) {
scanf("%lld", &b[i]);
sum[i] = sum[i - 1] + b[i];
}
LL ans = -INF;
int l = 0, r = 0;
while(l <= n && r <= n) {
while(a[l] <= 0 & l <= n) l++;
if(a[l] > 0) {
r = l;
while(a[r + 1] > 0) r++;
int top = 0;
sta[0] = l - 1;
for(int i = l; i <= r; i++) {
while(top > 0 && a[sta[top]] > a[i]) {
star[sta[top]] = i;//可以往右走的最远距离
top--;
}
sta[++top] = i;
stal[i] = sta[top - 1];//可以往左走的最远距离 }
while(top > 0) {
star[sta[top]] = r + 1;
top--;
}
for(int i = l; i <= r; i++) {
ans = max(ans, (sum[star[i] - 1] - sum[stal[i]]) * a[i]);
}
} else {
r = l;
}
l = r + 1;
}
build(0, n, 1);
for(int i = 1; i <= n; i++) {
if(a[i] < 0) {
LL maxx = query_max(0, i - 1, 0, n, 1);
LL minn = query_min(i, n, 0, n, 1);
ans = max(ans, (minn - maxx) * a[i]);
}
}
printf("%lld\n", ans);
return 0;
}