結果

問題 No.728 ギブ and テイク
ユーザー mugen_1337mugen_1337
提出日時 2020-09-16 23:43:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 837 ms / 3,000 ms
コード長 4,103 bytes
コンパイル時間 3,420 ms
コンパイル使用メモリ 240,484 KB
実行使用メモリ 226,152 KB
最終ジャッジ日時 2023-09-04 04:13:30
合計ジャッジ時間 10,822 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 35 ms
15,376 KB
testcase_14 AC 53 ms
18,992 KB
testcase_15 AC 23 ms
10,140 KB
testcase_16 AC 49 ms
18,288 KB
testcase_17 AC 44 ms
17,332 KB
testcase_18 AC 525 ms
134,320 KB
testcase_19 AC 572 ms
139,272 KB
testcase_20 AC 722 ms
211,224 KB
testcase_21 AC 591 ms
143,548 KB
testcase_22 AC 219 ms
63,028 KB
testcase_23 AC 127 ms
37,028 KB
testcase_24 AC 437 ms
121,624 KB
testcase_25 AC 442 ms
120,976 KB
testcase_26 AC 209 ms
61,840 KB
testcase_27 AC 837 ms
226,152 KB
testcase_28 AC 475 ms
225,280 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}
 
struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;

template<typename T1,typename T2>
ostream &operator<<(ostream &os,const pair<T1,T2>&p){
    os<<p.first<<" "<<p.second;
    return os;
}
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}

template<typename T1,typename T2>
istream &operator>>(istream &is,pair<T1,T2>&p){
    is>>p.first>>p.second;
    return is;
}

template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

template<typename Tx,typename Ty>
struct FractionalCascading{
    vector<vector<Ty>> seg;
    vector<vector<int>> L,R;
    vector<Tx> xs;
    int sz;
    // O(nlogn)
    FractionalCascading(vector<pair<Tx,Ty>> ps){
        sort(begin(ps),end(ps));
        if(ps.size()) xs.push_back(ps[0].first);
        for(auto &p:ps)if(xs.back()!=p.first)xs.push_back(p.first);
        xs.push_back(numeric_limits<Tx>::max());

        sz=1;
        while(sz<(int)xs.size()) sz<<=1;
        seg.resize(2*sz);
        L.resize(2*sz);
        R.resize(2*sz);
        int pos=0;
        for(auto &p:ps){
            if(xs[pos]!=p.first) pos++;
            seg[pos+sz].push_back(p.second);
        }
        for(int k=sz-1;k>0;k--){
            seg[k].resize(seg[2*k].size()+seg[2*k+1].size());
            L[k].resize(seg[k].size()+1);
            R[k].resize(seg[k].size()+1);

            merge(begin(seg[2*k]),end(seg[2*k]),begin(seg[2*k+1]),end(seg[2*k+1]),begin(seg[k]));
            int li=0,ri=0;
            for(int i=0;i<(int)seg[k].size();i++){
                L[k][i]=li,R[k][i]=ri;
                if(li<(int)seg[2*k].size() and seg[2*k][li]==seg[k][i]) li++;
                else                                                    ri++;
            }
            L[k][seg[k].size()]=(int)seg[2*k].size();
            R[k][seg[k].size()]=(int)seg[2*k+1].size();
        }
    }
    int sub(int a,int b,int lw,int hi,int k,int l,int r){
        if(r<=a or b<=l) return 0;
        if(a<=l and r<=b) return hi-lw;
        return sub(a,b,L[k][lw],L[k][hi],2*k,l,(l+r)/2)+sub(a,b,R[k][lw],R[k][hi],2*k+1,(l+r)/2,r);
    }
    // [le,ri), [lw,hi)
    int count(Tx le,Tx ri,Ty lw,Ty hi){
        int lwid=lower_bound(begin(seg[1]),end(seg[1]),lw)-begin(seg[1]);
        int hiid=lower_bound(begin(seg[1]),end(seg[1]),hi)-begin(seg[1]);
        int leid=lower_bound(begin(xs),end(xs),le)-begin(xs);
        int riid=lower_bound(begin(xs),end(xs),ri)-begin(xs);
        return sub(leid,riid,lwid,hiid,1,0,sz);
    }
};

void solve(){
    int n,q;cin>>n>>q;
    vector<int>c(n);
    cin>>c;
    vector<pair<int,int>> d;
    map<int,int> pre;
    rep(i,n){
        if(pre.count(c[i])) d.push_back({i,pre[c[i]]});
        pre[c[i]]=i;
    }
    FractionalCascading<int,int> seg(d);
    while(q--){
        int l,r;cin>>l>>r;l--;
        int ans=r-l-seg.count(l,r,l,r);
        cout<<ans<<"\n";
    }
}

void solve_give_and(){
    int n;cin>>n;
    ll a[n],l[n],r[n];
    rep(i,n) cin>>a[i];
    rep(i,n) cin>>l[i]>>r[i];

    vector<pair<ll,ll>> ps;
    rep(i,n) ps.push_back({i,a[i]+r[i]});
    FractionalCascading<ll,ll> seg(ps);

    ll ans=0;
    rep(i,n){
        int idx=lower_bound(a,a+n,a[i]-l[i])-a;
        // [idx,i)
        // [a[i],INF)
        ans+=seg.count(idx,i,a[i],LINF);
    }
    cout<<ans<<endl;
}

signed main(){
    // solve();
    solve_give_and();
    return 0;
}
0