답안 #503088

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
503088 2022-01-07T08:01:12 Z zaneyu 3D Histogram (COCI20_histogram) C++14
110 / 110
1096 ms 409000 KB
/*input
5
10 3
6 5
4 4
6 3
6 7

*/
#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[3*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]);
        //cout<<ma<<mb<<'\n';
        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);
    REP(i,n) swap(a[i],b[i]);
    rec(0,n-1);
	reverse(a,a+n),reverse(b,b+n);
	rec(0,n-1);
    cout<<ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 244 ms 404300 KB Output is correct
2 Correct 245 ms 404184 KB Output is correct
3 Correct 226 ms 404164 KB Output is correct
4 Correct 228 ms 404164 KB Output is correct
5 Correct 250 ms 404284 KB Output is correct
6 Correct 229 ms 404304 KB Output is correct
7 Correct 250 ms 404284 KB Output is correct
8 Correct 235 ms 404200 KB Output is correct
9 Correct 228 ms 404288 KB Output is correct
10 Correct 238 ms 404392 KB Output is correct
11 Correct 226 ms 404244 KB Output is correct
12 Correct 244 ms 404204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 244 ms 404300 KB Output is correct
2 Correct 245 ms 404184 KB Output is correct
3 Correct 226 ms 404164 KB Output is correct
4 Correct 228 ms 404164 KB Output is correct
5 Correct 250 ms 404284 KB Output is correct
6 Correct 229 ms 404304 KB Output is correct
7 Correct 250 ms 404284 KB Output is correct
8 Correct 235 ms 404200 KB Output is correct
9 Correct 228 ms 404288 KB Output is correct
10 Correct 238 ms 404392 KB Output is correct
11 Correct 226 ms 404244 KB Output is correct
12 Correct 244 ms 404204 KB Output is correct
13 Correct 973 ms 409000 KB Output is correct
14 Correct 1096 ms 408372 KB Output is correct
15 Correct 797 ms 408296 KB Output is correct
16 Correct 851 ms 408376 KB Output is correct
17 Correct 974 ms 408380 KB Output is correct
18 Correct 982 ms 408612 KB Output is correct
19 Correct 947 ms 408584 KB Output is correct
20 Correct 1000 ms 408508 KB Output is correct
21 Correct 839 ms 408296 KB Output is correct
22 Correct 911 ms 408480 KB Output is correct
23 Correct 317 ms 404568 KB Output is correct
24 Correct 703 ms 407744 KB Output is correct