結果

問題 No.1965 Heavier
ユーザー HaaHaa
提出日時 2022-06-03 22:25:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 657 ms / 2,000 ms
コード長 2,733 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 184,536 KB
実行使用メモリ 27,672 KB
最終ジャッジ日時 2023-10-21 02:02:00
合計ジャッジ時間 13,825 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 468 ms
27,672 KB
testcase_07 AC 482 ms
27,672 KB
testcase_08 AC 487 ms
27,672 KB
testcase_09 AC 637 ms
27,672 KB
testcase_10 AC 634 ms
27,672 KB
testcase_11 AC 632 ms
27,672 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 657 ms
27,672 KB
testcase_16 AC 651 ms
27,672 KB
testcase_17 AC 434 ms
25,100 KB
testcase_18 AC 621 ms
27,408 KB
testcase_19 AC 504 ms
27,144 KB
testcase_20 AC 515 ms
27,408 KB
testcase_21 AC 468 ms
26,616 KB
testcase_22 AC 459 ms
26,684 KB
testcase_23 AC 287 ms
16,396 KB
testcase_24 AC 385 ms
25,892 KB
testcase_25 AC 441 ms
27,672 KB
testcase_26 AC 431 ms
27,672 KB
testcase_27 AC 457 ms
27,672 KB
testcase_28 AC 457 ms
27,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
template<typename T> bool chmax(T &x, const T &y) {return (x<y)?(x=y,true):false;};
template<typename T> bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;};
constexpr ll MOD=1000000007;
constexpr ll INF=2e18;

template<typename X>
struct Segtree{
    using F=function<X(X,X)>;
    int n; F f; X ex;
    vector<X> dat;
    Segtree(int n_, F f_, X ex_):f(f_), ex(ex_){
        n=1;
        while(n_>n){n*=2;}
        dat.assign(2*n,ex);
    }
    void set(int i, X x){
        dat[i+n-1]=x;
    }
    void build(){
        for(int k=n-2;k>=0;k--) 
            dat[k]=f(dat[2*k+1],dat[2*k+2]);
    }
    void update(int i, X x){
        i+=n-1;
        dat[i]=x;
        while(i>0){
            i=(i-1)/2;
            dat[i]=f(dat[i*2+1],dat[i*2+2]);
        }
    }
    X query(int a, int b){
        return query_sub(a,b,0,0,n);
    }
    X query_sub(int a, int b, int k, int l, int r){
        if(r<=a||b<=l)
            return ex;
        else if(a<=l&&r<=b)
            return dat[k];
        else{
            X vl=query_sub(a,b,k*2+1,l,(l+r)/2);
            X vr=query_sub(a,b,k*2+2,(l+r)/2,r);
            return f(vl,vr);
        }
    }
};

P f(P l, P r){
    if(l.first-l.second>r.first-r.second)
        return l;
    else
        return r;
}

P g(P l, P r){
    if(l.first+l.second>r.first+r.second)
        return l;
    else
        return r;
}

int main(){
    int n; cin >> n;
    VI a(n), b(n);
    REP(i,n) cin >> a[i];
    REP(i,n) cin >> b[i];
    Segtree<P> segx(n,f,P{-INF,INF});
    Segtree<P> segy(n,g,P{-INF,-INF});
    vector<P> p(n);
    REP(i,n) p[i]={b[i],i};
    sort(ALL(p));
    VI idx(n);
    REP(i,n) idx[p[i].second]=i;
    int r=0;
    ll ans=0;
    REP(i,n-1){
        for(int j=r;j<=n;j++){
            if(j==n){
                r=j;
                ans+=j-i-1;
                break;
            }
            if(j-i>=1){
                bool ng=0;
                P resx=segx.query(0,idx[j]);
                if(resx.first+b[j]>=a[j]+resx.second)
                    ng=1;
                P resy=segy.query(idx[j],n);
                if(resy.first+resy.second>=a[j]+b[j])
                    ng=1;
                if(ng){
                    r=j;
                    ans+=j-i-1;
                    break;
                }
            }
            segx.update(idx[j],P{a[j],b[j]});
            segy.update(idx[j],P{a[j],b[j]});
        }
        segx.update(idx[i],P{-INF,INF});
        segy.update(idx[i],P{-INF,-INF});
    }
    cout << ans << endl;
    return 0;
}
0