結果
問題 | No.704 ゴミ拾い Medium |
ユーザー |
![]() |
提出日時 | 2019-06-27 16:20:36 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 218 ms / 1,500 ms |
コード長 | 1,987 bytes |
コンパイル時間 | 1,659 ms |
コンパイル使用メモリ | 172,628 KB |
実行使用メモリ | 26,624 KB |
最終ジャッジ日時 | 2024-06-28 02:00:47 |
合計ジャッジ時間 | 9,468 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 44 |
ソースコード
#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] << endlusing 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;}