Submission #503049

# Submission time Handle Problem Language Result Execution time Memory
503049 2022-01-07T04:33:49 Z zaneyu 3D Histogram (COCI20_histogram) C++14
0 / 110
243 ms 524292 KB
/*input
6
3 1
2 1
2 2
2 3
1 1
2 2
*/
#include<bits/stdc++.h>
#include <nmmintrin.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<ll,ll>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=2e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-6;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
    out<<P.f<<' '<<P.s;
    return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
    REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?"\n":"");
    return out;
}
ll mult(ll a,ll b){
    return a*b%MOD;
}
ll mypow(ll a,ll b){
    a%=MOD;
    if(a==0) return 0;
    if(b<=0) return 1;
    ll res=1LL;
    while(b){
        if(b&1) res=(res*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return res;
}
int a[maxn],b[maxn];
ll ans=0;
int sa[maxn],sb[maxn];
struct line{
    ll m,c;
    ll eval(ll x){
        return m*x+c;
    }
    ld intersect(line x){
        return (ld)(x.c-c)/(m-x.m);
    }
}; //beware of same slope
struct cht{
    deque<line> dq;
    void add(line z){
        //CHECK THIS
        if(sz(dq) and dq.back().m==z.m) return;
        while(sz(dq)>=2 and dq.back().intersect(dq[sz(dq)-2])>=dq[sz(dq)-2].intersect(z)){
            dq.pop_back();
        }
        dq.pb(z);
    }
    ll query(ll x){
        assert(sz(dq));
        while(sz(dq)>=2 and dq[0].eval(x)<=dq[1].eval(x)) dq.pop_front();
        return dq[0].eval(x);
    }
}seg[4*maxn];
void build(int idx,int l,int r){
    seg[idx].dq.clear();
    for(int i=l;i<=r;i++) seg[idx].add((line){sb[i],-1LL*sb[i]*i});
    if(l==r) return;   
    int mid=(l+r)/2;
    build(idx*2,l,mid),build(idx*2+1,mid+1,r);
}
ll query(int idx,int l,int r,int ql,int qr,int x){
    if(r<ql or l>qr) return 0;
    if(ql<=l and r<=qr) return seg[idx].query(x);
    int mid=(l+r)/2;
    return max(query(idx*2,l,mid,ql,qr,x),query(idx*2+1,mid+1,r,ql,qr,x));
}
void rec(int l,int r){
    if(l==r){
        MXTO(ans,1LL*a[l]*b[l]);
        return;
    }
    int mid=(l+r)/2;
    int ma=INF,mb=INF;
    sa[mid+1]=sb[mid+1]=INF;
    for(int i=mid;i>=l;i--){
        sa[i]=min(sa[i+1],a[i]),sb[i]=min(sb[i+1],b[i]);
    }
    int p=mid;
    for(int i=mid+1;i<=r;i++){
        MNTO(ma,a[i]),MNTO(mb,b[i]);
        while(p>=l and (sa[p]>=ma and sb[p]>=mb)) --p;
        MXTO(ans,1LL*(i-p)*ma*mb);
    }
    ma=INF,mb=INF;
    //min a on right
    //sa[l]>pa[r] sb[l]<pb[r]
    //forms a left bound, forms a right bound
    //left bound decreases, right bound decreases 
    //pain
    int x=mid,y=mid;
    build(1,l,mid);
    for(int i=mid+1;i<=r;i++){
        MNTO(ma,a[i]),MNTO(mb,b[i]);
        while(x>=l and sa[x]>ma) --x;
        while(y>=l and sb[y]>mb) --y;
        if(x+1<=y) MXTO(ans,1LL*query(1,l,mid,x+1,y,i+1)*ma);
    }
    rec(l,mid),rec(mid+1,r);
}
int32_t main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int n;
    cin>>n;
    REP(i,n){
        cin>>a[i]>>b[i];
    }
    rec(0,n-1);
    reverse(a,a+n),reverse(b,b+n);
    rec(0,n-1);
    cout<<ans;
}
# Verdict Execution time Memory Grader output
1 Runtime error 243 ms 524292 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 243 ms 524292 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -