1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| #include <bits/stdc++.h> using namespace std;
#define pii pair <int, int> #define fi first #define se second #define ll long long #define INFLL 0x3f3f3f3f3f3f3f3f #define N 3000010 int n, a[N]; ll b[N];
struct Cartesian_Tree { struct node { int id, val, fa; int son[2]; node() {} node (int id, int val, int fa) : id(id), val(val), fa(fa) { son[0] = son[1] = 0; } }t[N]; int root; pii b[N]; void init() { t[0] = node(0, -1e9, 0); } void build(int n, int *a) { for (int i = 1; i <= n; ++i) { t[i] = node(i, a[i], 0); } for (int i = 1; i <= n; ++i) { int k = i - 1; while (t[k].val > t[i].val) { k = t[k].fa; } t[i].son[0] = t[k].son[1]; t[k].son[1] = i; t[i].fa = k; t[t[i].son[0]].fa = i; } root = t[0].son[1]; } int DFS(int u) { if (!u) return 0; int lsze = DFS(t[u].son[0]); int rsze = DFS(t[u].son[1]); b[t[u].id] = pii(lsze, rsze); return lsze + rsze + 1; } }CT;
struct SEG { struct node { ll Max, Min; node() { Max = -INFLL; Min = INFLL; } node operator + (const node &other) const { node res = node(); res.Max = max(Max, other.Max); res.Min = min(Min, other.Min); return res; } }t[N << 2], resl, resr; void build(int id, int l, int r) { if (l == r) { t[id] = node(); t[id].Max = t[id].Min = b[l]; return; } int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); t[id] = t[id << 1] + t[id << 1 | 1]; } node query(int id, int l, int r, int ql, int qr) { if (l >= ql && r <= qr) { return t[id]; } int mid = (l + r) >> 1; node res = node(); if (ql <= mid) res = res + query(id << 1, l, mid, ql, qr); if (qr > mid) res = res + query(id << 1 | 1, mid + 1, r, ql, qr); return res; } }seg;
namespace IO { const int S=(1<<20)+5; char buf[S],*H,*T; inline char Get() { if(H==T) T=(H=buf)+fread(buf,1,S,stdin); if(H==T) return -1;return *H++; } inline int read() { int x=0,fg=1;char c=Get(); while(!isdigit(c)&&c!='-') c=Get(); if(c=='-') fg=-1,c=Get(); while(isdigit(c)) x=x*10+c-'0',c=Get(); return x*fg; } inline void reads(char *s) { char c=Get();int tot=0; while(c<'a'||c>'z') c=Get(); while(c>='a'&&c<='z') s[++tot]=c,c=Get(); s[++tot]='\0'; } char obuf[S],*oS=obuf,*oT=oS+S-1,c,qu[55];int qr; inline void flush(){fwrite(obuf,1,oS-obuf,stdout);oS=obuf;} inline void putc(char x){*oS++ =x;if(oS==oT) flush();} template <class I>inline void print(I x) { if(!x) putc('0'); if(x<0) putc('-'),x=-x; while(x) qu[++qr]=x%10+'0',x/=10; while(qr) putc(qu[qr--]); putc('\n'); } inline void prints(const char *s) { int len=strlen(s); for(int i=0;i<len;i++) putc(s[i]); putc('\n'); } inline void printd(int d,double x) { long long t=(long long)floor(x); print(t);putc('.');x-=t; while(d--) { double y=x*10;x*=10; int c=(int)floor(y); putc(c+'0');x-=floor(y); } } }using namespace IO;
int main() { n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); b[0] = 0; for (int i = 1; i <= n; ++i) { b[i] = read(); b[i] += b[i - 1]; } CT.init(); CT.build(n, a); CT.DFS(CT.root); ll res = -INFLL; seg.build(1, 0, n); for (int i = 1; i <= n; ++i) { int l = i - CT.b[i].fi, r = i + CT.b[i].se; seg.resl = seg.query(1, 0, n, l - 1, i - 1); seg.resr = seg.query(1, 0, n, i, r); res = max(res, 1ll * a[i] * (seg.resr.Max - seg.resl.Min)); res = max(res, 1ll * a[i] * (seg.resr.Min - seg.resl.Max)); } printf("%lld\n", res); return 0; }
|