#include <bits/stdc++.h>
#define PB push_back
#define sz(x) ((ll)x.size())
using namespace std;
typedef long long ll;
const ll N = 200100;
const ll BIG = ll(4e6);
const ll oo = 2e9;
const ll OO = 1e18;
ll B[BIG], ans = 0, FRM[BIG];
ll n, a[N], b[N], pref[N], suf[N], pref2[N], suf2[N];
ll root[4 * N], ptr[4 * N];
ll LST, K[BIG], PRV[BIG];
ll NEW(ll nk, ll nb, ll wh){
assert(LST < BIG);
K[LST] = nk;
B[LST] = nb;
FRM[LST] = wh;
PRV[LST] = -1;
LST++;
return LST - 1;
}
ll get_cross_poll(ll FI, ll SE){
if (K[FI] == K[SE])
return (B[FI] > B[SE] ? -OO : OO);
ll BB = B[FI] - B[SE];
ll KK = K[SE] - K[FI];
if ((BB >= 0 && KK > 0) || (BB <= 0 && KK < 0))
return BB / KK + bool(BB % KK);
else return BB / KK;
}
void build(ll v, ll l, ll r){
{
ll tit = l;
while (tit < r && pref2[tit + 1] == pref2[l])
tit++;
root[v] = NEW(-pref2[tit], ll(pref2[tit]) * ll(tit), -OO);
ptr[v] = root[v];
for (ll it = tit + 1; it <= r; it++){
ll cur = NEW(-pref2[it], ll(pref2[it]) * ll(it), -OO);
while (PRV[ptr[v]] > 0){
ll CR = get_cross_poll(cur, ptr[v]);
if (CR > FRM[ptr[v]]) break;
ptr[v] = PRV[ptr[v]];
}
PRV[cur] = ptr[v];
FRM[cur] = get_cross_poll(cur, ptr[v]);
ptr[v] = cur;
}
}
if (l == r) return;
ll md = (l + r) >> 1;
build(v + v, l, md);
build(v + v + 1, md + 1, r);
}
ll get_best(ll v, ll tl, ll tr, ll ql, ll qr, ll X){
if (ql > qr) return -OO;
if (tl == ql && qr == tr){
while (FRM[ptr[v]] > X)
ptr[v] = PRV[ptr[v]];
return ll(K[ptr[v]]) * ll(X) + B[ptr[v]];
}
ll md = (tl + tr) >> 1;
return max(get_best(v + v, tl, md, ql, min(qr, md), X),
get_best(v + v + 1, md + 1, tr, max(md + 1, ql), qr, X));
}
void calc(ll l, ll r){
if (l == r) {
ans = max(ans, ll(a[l]) * ll(b[l]));
return;
}
ll md = (l + r) >> 1;
suf[md + 1] = suf2[md + 1] = oo;
for (ll i = md; i >= l; i--) {
suf[i] = min(suf[i + 1], a[i]);
suf2[i] = min(suf2[i + 1], b[i]);
}
pref[md] = pref2[md] = oo;
for (ll i = md + 1; i <= r; i++) {
pref[i] = min(pref[i - 1], a[i]);
pref2[i] = min(pref2[i - 1], b[i]);
}
LST = 1;
build(1, md + 1, r);
{
// min_a -- left
// min_b -- left
ll ptr1 = md, ptr2 = md;
for (ll i = md; i >= l; i--){
while (ptr1 < r && suf[i] <= pref[ptr1 + 1])
ptr1++;
while (ptr2 < r && suf2[i] <= pref2[ptr2 + 1])
ptr2++;
ll MN = min(ptr1, ptr2);
if (MN > md)
ans = max(ans, ll(suf[i]) * ll(suf2[i]) * (MN - ll(i) + 1ll));
}
}
{
// min_a -- left
// min_b -- right
ll ptr1 = md, ptr2 = md + 1;
for (ll i = md; i >= l; i--){
while (ptr1 < r && suf[i] <= pref[ptr1 + 1])
ptr1++;
while (ptr2 <= r && suf2[i] < pref2[ptr2])
ptr2++;
if (ptr1 >= ptr2)
ans = max(ans, get_best(1, md + 1, r, ptr2, ptr1, i - 1) * ll(suf[i]));
}
}
calc(l, md);
calc(md + 1, r);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n;
for (ll i = 1; i <= n; i++)
cin >> a[i] >> b[i];
calc(1, n);
reverse(a + 1, a + n + 1);
reverse(b + 1, b + n + 1);
calc(1, n);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Correct |
3 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
716 KB |
Output is correct |
4 |
Correct |
2 ms |
716 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
3 ms |
588 KB |
Output is correct |
7 |
Correct |
4 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
4 ms |
716 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
4 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Correct |
3 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
716 KB |
Output is correct |
4 |
Correct |
2 ms |
716 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
3 ms |
588 KB |
Output is correct |
7 |
Correct |
4 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
4 ms |
716 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
4 ms |
588 KB |
Output is correct |
13 |
Incorrect |
390 ms |
26896 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |