結果

問題 No.704 ゴミ拾い Medium
ユーザー hashiryohashiryo
提出日時 2019-06-27 16:20:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 218 ms / 1,500 ms
コード長 1,987 bytes
コンパイル時間 1,771 ms
コンパイル使用メモリ 171,784 KB
実行使用メモリ 26,648 KB
最終ジャッジ日時 2023-09-10 10:37:15
合計ジャッジ時間 11,666 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 194 ms
26,404 KB
testcase_25 AC 218 ms
26,344 KB
testcase_26 AC 207 ms
26,452 KB
testcase_27 AC 200 ms
26,548 KB
testcase_28 AC 210 ms
26,456 KB
testcase_29 AC 195 ms
26,448 KB
testcase_30 AC 201 ms
26,448 KB
testcase_31 AC 210 ms
26,516 KB
testcase_32 AC 199 ms
26,356 KB
testcase_33 AC 204 ms
26,312 KB
testcase_34 AC 183 ms
26,464 KB
testcase_35 AC 184 ms
26,448 KB
testcase_36 AC 179 ms
26,648 KB
testcase_37 AC 179 ms
26,544 KB
testcase_38 AC 183 ms
26,508 KB
testcase_39 AC 183 ms
26,352 KB
testcase_40 AC 181 ms
26,544 KB
testcase_41 AC 183 ms
26,352 KB
testcase_42 AC 186 ms
26,292 KB
testcase_43 AC 184 ms
26,480 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 164 ms
26,464 KB
testcase_47 AC 199 ms
26,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
const ll INF = LLONG_MAX/2;
const ll MOD = 1e9+7;
const double EPS=1e-12;



template <typename M>
struct SegmentTree{
    using T = typename M::T;
    int n;
    vector<T> dat;
    SegmentTree(){}
    SegmentTree(int n_){init(n_);}
    SegmentTree(const vector<T> &v){build(v);}
    void init(int n_){
        n=1;
        while(n<n_) n<<=1;
        dat.assign(n<<1,M::ti());
    }
    void build(const vector<T> &v){
        int n_=v.size();
        init(n_);
        for(int i=0;i<n_;i++) dat[n+i]=v[i];
        for(int i=n-1;i;i--)
            dat[i]=M::f(dat[(i<<1)|0],dat[(i<<1)|1]);
    }
    void set_val(int k,T x){
        dat[k+=n]=x;
        while(k>>=1)
            dat[k]=M::f(dat[(k<<1)|0],dat[(k<<1)|1]);
    }
    //[a,b)
    T query(int a,int b,int k=1,int l=0,int r=-1){
        if(r<0)r=n;
        if(r<=a||b<=l) return M::ti();
        if(a<=l&&r<=b) return dat[k];
        T vl=query(a,b,k*2,l,(l+r)/2);
        T vr=query(a,b,k*2+1,(l+r)/2,r);
        return M::f(vl,vr);
    }
    T operator[](const int &k) const {return dat[k + n];}
};

struct RminQ {
    using T = ll;
    static T ti() { return INT_MAX; }
    static T f(const T& l, const T & r) { return min(l, r); }
};

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;cin>>N;
  ll a[N];
  for(ll i=0;i<N;i++)cin>>a[i];
  ll x[N];
  for(ll i=0;i<N;i++)cin>>x[i];
  ll y[N];
  for(ll i=0;i<N;i++)cin>>y[i];
  SegmentTree<RminQ> segl(N),segr(N);
  ll dp=0;
  for(ll i=0;i<N;i++){
    segl.set_val(i,-x[i]+y[i]+dp);
    segr.set_val(i,x[i]+y[i]+dp);
    int idx=lower_bound(x,x+i+1,a[i])-x;
    dp = min(a[i]+segl.query(0,idx),-a[i]+segr.query(idx,i+1));
  }
  cout<<dp<<endl;
  return 0;
}
0