Submission #445458

#TimeUsernameProblemLanguageResultExecution timeMemory
445458grt3D Histogram (COCI20_histogram)C++17
20 / 110
2579 ms42444 KiB
#include <bits/stdc++.h> #define ST first #define ND second #define PB push_back using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 200 * 1000 + 10; int n; int a[nax], b[nax], f[nax], g[nax]; pair<int, ll> lines[nax]; ll ans; vector<pair<int, ll>>T[(1 << 19)]; int R; int roz[(1<<19)]; bool intersect(pair<int,ll>&x, pair<int,ll>&y, pair<int,ll>&z) { return (ll)(z.ST - x.ST) * (y.ND - x.ND) - (ll)(y.ST - x.ST)*(z.ND - x.ND) <= 0; } void construct(int l, int r) { for(int i = l; i <= r; ++i) { T[i + R].clear(); T[i + R].PB(lines[i]); } l += R; r += R; while((l>>1)) { l >>= 1; r >>= 1; for(int i = l; i <= r; ++i) { int &v = i; T[v].clear(); auto &nw = T[v]; for(auto &x : T[(v<<1)^1]) { if((int)nw.size() == 0) { nw.PB(x); continue; } if(nw.back().ST == x.ST) continue; while((int)nw.size() >= 2 && intersect(nw.back(), x, nw[(int)nw.size() - 2])) { nw.pop_back(); } nw.PB(x); } for(auto &x : T[(v<<1)]) { if((int)nw.size() == 0) { nw.PB(x); continue; } if(nw.back().ST == x.ST) continue; while((int)nw.size() >= 2 && intersect(nw.back(), x, nw[(int)nw.size() - 2])) { nw.pop_back(); } nw.PB(x); } } } } ll get_mx(vector<pair<int,ll>>&vec, int x) { while((int)vec.size() >= 2 && (ll)vec.back().ST * x + vec.back().ND <= (ll)vec[(int)vec.size() - 2].ST * x + vec[(int)vec.size() - 2].ND) { vec.pop_back(); } return (ll)vec.back().ST * x + vec.back().ND; } ll ask(int l, int r, int x) { l += R; r += R; ll w = max(get_mx(T[l], x), get_mx(T[r], x)); while((l>>1) != (r>>1)) { if((l&1)==0) w = max(w, get_mx(T[l^1], x)); if(r&1) w = max(w, get_mx(T[r^1], x)); l >>= 1; r >>= 1; } return w; } void rec(int l, int r) { int mid = ((l + r) >> 1); if(l <= mid - 1) rec(l, mid - 1); if(mid + 1 <= r) rec(mid + 1, r); f[mid] = a[mid]; g[mid] = b[mid]; lines[mid] = {g[mid], (ll)g[mid] * mid}; for(int i = mid - 1; i >= l; --i) { f[i] = min(f[i + 1], a[i]); g[i] = min(g[i + 1], b[i]); lines[i] = {g[i], (ll)g[i] * (-i + 1)}; } for(int i = mid + 1; i <= r; ++i) { f[i] = min(f[i - 1], a[i]); g[i] = min(g[i - 1], b[i]); lines[i] = {g[i], (ll)g[i] * i}; } construct(mid, r); lines[mid] = {g[mid], (ll)g[mid] * (-mid + 1)}; int lp, rp, l2, r2; rp = r; for(lp = l; lp <= mid; ++lp) { while(rp >= mid && (f[lp] > f[rp] || g[lp] > g[rp])) { rp--; } if(rp >= mid) { ans = max(ans, (ll)f[lp] * g[lp] * (rp - lp + 1)); } } lp = l; for(rp = r; rp >= mid; rp--) { while(lp <= mid && (f[rp] > f[lp] || g[rp] > g[lp])) { lp++; } if(lp <= mid) { ans = max(ans, (ll)f[rp] * g[rp] * (rp - lp + 1)); } } rp = r; r2 = r; for(lp = l; lp <= mid; lp++) { while(rp >= mid && (f[lp] > f[rp])) { rp--; // erase } while(r2 - 1 >= mid && (g[lp] >= g[r2 - 1])) { r2--; // add } if(rp >= mid && g[lp] >= g[r2] && r2 <= rp) { // [r2, rp] ans = max(ans, ask(r2, rp, -lp + 1) * f[lp]); } } construct(l, mid); lp = l; l2 = l; for(rp = r; rp >= mid; rp--) { while(lp <= mid && (f[rp] > f[lp])) { lp++; } while(l2 + 1 <= mid && (g[rp] >= g[l2 + 1])) { l2++; // add } if(lp <= mid && g[rp] >= g[l2] && lp <= l2) { // [r2, rp] ans = max(ans, ask(lp, l2, rp) * f[rp]); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; } R = 1; while(R < n) R *= 2; for(int i = 0; i < n; ++i) { T[i+R].reserve(1); roz[i + R] = 1; } for(int i = R - 1; i >= 1; --i) { T[i].reserve(roz[2*i]+roz[2*i+1]); } rec(0, n - 1); cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...