結果

問題 No.704 ゴミ拾い Medium
ユーザー tjaketjake
提出日時 2018-11-10 01:53:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 432 ms / 1,500 ms
コード長 3,703 bytes
コンパイル時間 1,027 ms
コンパイル使用メモリ 96,788 KB
実行使用メモリ 35,416 KB
最終ジャッジ日時 2023-08-13 17:03:12
合計ジャッジ時間 11,988 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
15,652 KB
testcase_01 AC 4 ms
13,660 KB
testcase_02 AC 4 ms
13,728 KB
testcase_03 AC 4 ms
13,940 KB
testcase_04 AC 3 ms
13,556 KB
testcase_05 AC 4 ms
15,648 KB
testcase_06 AC 3 ms
13,832 KB
testcase_07 AC 4 ms
15,716 KB
testcase_08 AC 3 ms
13,672 KB
testcase_09 AC 4 ms
15,584 KB
testcase_10 AC 4 ms
15,772 KB
testcase_11 AC 4 ms
13,636 KB
testcase_12 AC 4 ms
15,644 KB
testcase_13 AC 4 ms
13,832 KB
testcase_14 AC 5 ms
13,696 KB
testcase_15 AC 5 ms
14,008 KB
testcase_16 AC 5 ms
13,924 KB
testcase_17 AC 5 ms
13,760 KB
testcase_18 AC 5 ms
13,792 KB
testcase_19 AC 5 ms
15,800 KB
testcase_20 AC 5 ms
15,796 KB
testcase_21 AC 5 ms
15,840 KB
testcase_22 AC 5 ms
13,988 KB
testcase_23 AC 5 ms
15,716 KB
testcase_24 AC 409 ms
32,632 KB
testcase_25 AC 410 ms
32,532 KB
testcase_26 AC 425 ms
32,616 KB
testcase_27 AC 413 ms
34,648 KB
testcase_28 AC 418 ms
35,416 KB
testcase_29 AC 417 ms
34,876 KB
testcase_30 AC 414 ms
32,580 KB
testcase_31 AC 417 ms
33,676 KB
testcase_32 AC 429 ms
35,096 KB
testcase_33 AC 432 ms
34,312 KB
testcase_34 AC 196 ms
18,872 KB
testcase_35 AC 189 ms
16,948 KB
testcase_36 AC 191 ms
16,780 KB
testcase_37 AC 189 ms
18,948 KB
testcase_38 AC 196 ms
16,856 KB
testcase_39 AC 197 ms
18,824 KB
testcase_40 AC 188 ms
16,788 KB
testcase_41 AC 197 ms
16,828 KB
testcase_42 AC 186 ms
16,820 KB
testcase_43 AC 187 ms
18,912 KB
testcase_44 AC 4 ms
13,656 KB
testcase_45 AC 3 ms
13,700 KB
testcase_46 AC 241 ms
16,876 KB
testcase_47 AC 238 ms
16,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cassert>
#include<ctime>
#include<utility>
using namespace std;

#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define absd(x) (x<0?-(x):x)
#define pow2(x) ((x)*(x))
#define rep(i,n) for(int i=0; i<n; ++i)
#define repr(i,n) for(int i=n-1; i>=0; --i)
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define replr(i,s,n) for(int i=n; i>=s; --i)
#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)
#define repe(e,obj) for(auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X,Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P> > pvqueue;

#define N 300004

class LiChaoTree {
  int n;
  ll xs[4*N];
  ll p[4*N], q[4*N];
  bool u[4*N];

  void _add_line(ll a, ll b, int k, int l, int r) {
    while(r-l > 0) {
      int m = (l + r) >> 1;
      if(!u[k]) {
        p[k] = a; q[k] = b;
        u[k] = true;
        return;
      }

      ll lx = xs[l], mx = xs[m], rx = xs[r-1];
      ll pk = p[k], qk = q[k];
      bool left = (a*lx+b < pk*lx+qk);
      bool mid = (a*mx+b < pk*mx+qk);
      bool right = (a*rx+b < pk*rx+qk);
      if(left && right) {
        p[k] = a; q[k] = b;
        return;
      }
      if(!left && !right) {
        return;
      }
      if(mid) {
        swap(p[k], a);
        swap(q[k], b);
      }
      if(left != mid) {
        k = 2*k+1; r = m;
      } else {
        k = 2*k+2; l = m;
      }
    }
  }

  ll _query(int k, ll x) {
    k += n - 1;
    ll s = u[k] ? p[k]*x+q[k] : INFLL;
    while(k > 0) {
      k = (k - 1) / 2;
      if(u[k]) {
        ll r = p[k]*x+q[k];
        s = mind(s, r);
      }
    }
    return s;
  }

public:
  LiChaoTree(int n0, ll ps[N]) {
    n = 1;
    while(n < n0) n <<= 1;

    rep(i, 2*n) u[i] = false;
    rep(i, n0) xs[i] = ps[i];
    repl(i, n0, 2*n-1) xs[i] = INF;
  }

  void add_line(ll a, ll b) {
    _add_line(a, b, 0, 0, n);
  }

  void add_segment_line(ll a, ll b, int l, int r) {
    ll l0 = l + n, r0 = r + n;
    ll s0 = l, t0 = r, sz = 1;
    while(l0 < r0) {
      if(r0 & 1) {
        --r0; t0 -= sz;
        _add_line(a, b, r0-1, t0, t0+sz);
      }
      if(l0 & 1) {
        _add_line(a, b, l0-1, s0, s0+sz);
        ++l0; s0 += sz;
      }
      l0 >>= 1; r0 >>= 1;
      sz <<= 1;
    }
  }

  ll query(int i) {
    return _query(i, xs[i]);
  }
};

int n;
ll a[N], a0[N], x[N], y[N];

set<ll> x0;
map<ll, int> z;

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> x[i];
  rep(i, n) cin >> y[i];

  rep(i, n) x0.insert(a[i]);
  int idx = 0;
  repe(v, x0) {
    a0[idx] = v;
    z[v] = idx++;
  }

  set<ll>::iterator it = x0.begin();

  LiChaoTree lct(x0.size(), a0);
  ll v = 0;
  int j = -1;

  rep(i, n) {
    while(it != x0.end() && (*it) < x[i]) {
      ++it; ++j;
    }
    lct.add_segment_line(-1, x[i]+y[i]+v, 0, j+1);
    lct.add_segment_line(1, -x[i]+y[i]+v, j+1, x0.size());
    v = lct.query(z[a[i]]);
  }
  cout << v << endl;
  return 0;
}
0