結果

問題 No.2332 Make a Sequence
ユーザー cureskolcureskol
提出日時 2023-05-28 14:52:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 342 ms / 2,000 ms
コード長 13,304 bytes
コンパイル時間 4,210 ms
コンパイル使用メモリ 255,992 KB
実行使用メモリ 27,620 KB
最終ジャッジ日時 2023-08-27 10:47:09
合計ジャッジ時間 20,295 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 148 ms
10,752 KB
testcase_08 AC 28 ms
6,472 KB
testcase_09 AC 113 ms
9,680 KB
testcase_10 AC 141 ms
8,404 KB
testcase_11 AC 93 ms
6,724 KB
testcase_12 AC 177 ms
12,484 KB
testcase_13 AC 177 ms
12,508 KB
testcase_14 AC 177 ms
12,468 KB
testcase_15 AC 177 ms
12,552 KB
testcase_16 AC 173 ms
12,476 KB
testcase_17 AC 176 ms
12,584 KB
testcase_18 AC 175 ms
12,636 KB
testcase_19 AC 178 ms
12,540 KB
testcase_20 AC 181 ms
12,656 KB
testcase_21 AC 184 ms
12,560 KB
testcase_22 AC 189 ms
12,540 KB
testcase_23 AC 189 ms
12,684 KB
testcase_24 AC 184 ms
12,720 KB
testcase_25 AC 187 ms
12,564 KB
testcase_26 AC 185 ms
12,776 KB
testcase_27 AC 193 ms
13,092 KB
testcase_28 AC 191 ms
13,264 KB
testcase_29 AC 188 ms
13,260 KB
testcase_30 AC 192 ms
13,060 KB
testcase_31 AC 192 ms
13,092 KB
testcase_32 AC 214 ms
16,540 KB
testcase_33 AC 209 ms
17,228 KB
testcase_34 AC 210 ms
16,964 KB
testcase_35 AC 208 ms
17,352 KB
testcase_36 AC 207 ms
16,980 KB
testcase_37 AC 213 ms
17,996 KB
testcase_38 AC 210 ms
17,756 KB
testcase_39 AC 215 ms
18,396 KB
testcase_40 AC 209 ms
17,896 KB
testcase_41 AC 208 ms
16,592 KB
testcase_42 AC 261 ms
23,788 KB
testcase_43 AC 264 ms
23,124 KB
testcase_44 AC 258 ms
22,828 KB
testcase_45 AC 267 ms
23,324 KB
testcase_46 AC 263 ms
23,232 KB
testcase_47 AC 340 ms
25,596 KB
testcase_48 AC 341 ms
25,536 KB
testcase_49 AC 341 ms
27,620 KB
testcase_50 AC 342 ms
25,604 KB
testcase_51 AC 337 ms
26,444 KB
testcase_52 AC 198 ms
15,700 KB
testcase_53 AC 196 ms
13,332 KB
testcase_54 AC 196 ms
12,996 KB
testcase_55 AC 199 ms
15,476 KB
testcase_56 AC 202 ms
15,372 KB
testcase_57 AC 194 ms
12,576 KB
testcase_58 AC 197 ms
12,480 KB
testcase_59 AC 194 ms
12,556 KB
testcase_60 AC 193 ms
12,644 KB
testcase_61 AC 197 ms
12,484 KB
testcase_62 AC 294 ms
24,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region template
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
 
using ll=long long;
 
#define overload2(a,b,c,...) c
#define overload3(a,b,c,d,...) d
#define overload4(a,b,c,d,e,...) e
#define overload5(a,b,c,d,e,f,...) f
 
#define TYPE1(T) template<typename T>
#define TYPE2(T,U) template<typename T,typename U>
#define TYPE(...) overload2(__VA_ARGS__,TYPE2,TYPE1)(__VA_ARGS__)
#define TYPES1(T) template<typename... T>
#define TYPES2(H,T) template<typename H,typename... T>
#define TYPES(...) overload2(__VA_ARGS__,TYPES2,TYPES1)(__VA_ARGS__)
 
#define REP4(i,s,n,d) for(int i=(s);i<(n);i+=(d))
#define REP3(i,s,n) REP4(i,s,n,1)
#define REP2(i,n) REP3(i,0,n)
#define REP1(n) REP2(tomato,n)
#define REP(...) overload4(__VA_ARGS__,REP4,REP3,REP2,REP1)(__VA_ARGS__)
 
#define RREP4(i,n,s,d) for(int i=(n)-1;i>=(s);i-=(d))
#define RREP3(i,n,s) RREP4(i,n,s,1)
#define RREP2(i,n) RREP3(i,n,0)
#define RREP1(n) RREP2(tomato,n)
#define RREP(...) overload4(__VA_ARGS__,RREP4,RREP3,RREP2,RREP1)(__VA_ARGS__)

#define AFOR4(a,b,c,d,v) for(auto&[a,b,c,d]:v)
#define AFOR3(a,b,c,v) for(auto&[a,b,c]:v)
#define AFOR2(a,b,v) for(auto&[a,b]:v)
#define AFOR1(a,v) for(auto&a:v)
#define AFOR(...) overload5(__VA_ARGS__,AFOR4,AFOR3,AFOR2,AFOR1)(__VA_ARGS__)

#define CFOR4(a,b,c,d,v) for(const auto&[a,b,c,d]:v)
#define CFOR3(a,b,c,v) for(const auto&[a,b,c]:v)
#define CFOR2(a,b,v) for(const auto&[a,b]:v)
#define CFOR1(a,v) for(const auto&a:v)
#define CFOR(...) overload5(__VA_ARGS__,CFOR4,CFOR3,CFOR2,CFOR1)(__VA_ARGS__)

#define PFOR(v) for(bool f=true; (f ? exchange(f,false) : next_permutation(ALL(v)));)
 
 
TYPES(T) void input(T&... a){ (cin>>...>>a); }
#define DECLARE(T,...) T __VA_ARGS__;input(__VA_ARGS__);
#define INT(...) DECLARE(int,__VA_ARGS__)
#define STR(...) DECLARE(string,__VA_ARGS__)
#define LL(...) DECLARE(long long,__VA_ARGS__)
#define CHR(...) DECLARE(char,__VA_ARGS__)
#define DBL(...) DECLARE(double,__VA_ARGS__)
#define VI(n,v) vector<int> v(n);cin>>v;
#define VLL(n,v) vector<ll> v(n);cin>>v;
#define VS(n,s) vector<string> s(n);cin>>s;
 
TYPE(T) istream&operator>>(istream&is,vector<T>&v){
  for(auto&a:v)cin>>a;
  return is;
}
TYPE(T) ostream&operator<<(ostream&os,const vector<T>&v){
  if(&os==&cerr)os<<"[";
  REP(i,v.size()){
    os<<v[i];
    if(i+1<v.size())os<<(&os==&cerr?",":" ");
  }
  if(&os==&cerr)os<<"]";
  return os;
}
TYPE(T,S) istream&operator>>(istream&is,pair<T,S>&p){
  cin>>p.first>>p.second;
  return is;
}

#ifdef __DEBUG
 #include <debug>
#else
 #define debug(...) void(0)
#endif

void print(){ cout << '\n'; }
TYPES(T,Ts) void print(const T& a,const Ts&... b){
  cout<<a;
  (cout<<...<<(cout<< ' ',b));
  cout << '\n';
}
 
#pragma endregion template

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;

using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) \
  for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T, typename U>
T ceil(T x, U y) {
  return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U>
T floor(T x, U y) {
  return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template <typename T, typename U>
T SUM(const vector<U> &A) {
  T sum = 0;
  for (auto &&a: A) sum += a;
  return sum;
}

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  assert(!que.empty());
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  assert(!que.empty());
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
  }
  return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
  FOR(iter) {
    double x = (ok + ng) / 2;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
  }
  return (ok + ng) / 2;
}

template <class T, class S>
inline bool chmax(T &a, const S &b) {
  return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
  return (a > b ? a = b, 1 : 0);
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
  int N = A.size();
  vector<T> B(N + 1);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
  vector<int> ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
  vc<T> B(len(I));
  FOR(i, len(I)) B[i] = A[I[i]];
  return B;
}
#line 1 "convex/dynamic_lichao.hpp"
// 直線追加かつ非永続なら空間 Q でよい。
// 関数は ll -> T。[L, R) 上 f が overflow しないように注意。
// evaluate を書き変えると、totally monotone な関数群にも使える
template <typename T, bool PERSISTENT, int NODES, bool MINIMIZE>
struct Dynamic_LiChao_Tree {
  using FUNC = pair<T, T>;
  vc<FUNC> funcs;

  static inline T evaluate(FUNC &f, ll x) { return f.fi * x + f.se; }

  struct Node {
    int fid;
    Node *l, *r;
  };

  Node *pool;
  int pid;
  ll L, R;

  using np = Node *;

  Dynamic_LiChao_Tree(ll L, ll R) : pid(0), L(L), R(R) {
    pool = new Node[NODES];
  }

  void reset() {
    funcs.clear();
    pid = 0;
  }

  np new_root() { return nullptr; }

  np new_node() {
    pool[pid].fid = -1;
    pool[pid].l = nullptr, pool[pid].r = nullptr;
    return &(pool[pid++]);
  }

  np add_line(np root, FUNC f) {
    int fid = len(funcs);
    funcs.eb(f);
    if (!root) root = new_node();
    return add_line_rec(root, fid, L, R);
  }

  // [xl, xr)
  np add_segment(np root, ll xl, ll xr, FUNC f) {
    int fid = len(funcs);
    funcs.eb(f);
    if (!root) root = new_node();
    return add_segment_rec(root, xl, xr, fid, L, R);
  }

  // (値・関数番号)
  pair<T, int> query(np root, ll x) {
    assert(L <= x && x < R);
    if (!root) {
      if (MINIMIZE) return {infty<T>, -1};
      if (!MINIMIZE) return {-infty<T>, -1};
    }
    return query_rec(root, x, L, R);
  }

private:
  np copy_node(Node *c) {
    if (!c || !PERSISTENT) return c;
    pool[pid].fid = c->fid;
    pool[pid].l = c->l, pool[pid].r = c->r;
    return &(pool[pid++]);
  }

  inline T evaluate_inner(int fid, ll x) {
    if (fid == -1) { return (MINIMIZE ? infty<T> : -infty<T>); };
    return evaluate(funcs[fid], x);
  }

  np add_segment_rec(np c, ll xl, ll xr, int fid, ll node_l, ll node_r) {
    chmax(xl, node_l), chmin(xr, node_r);
    if (xl >= xr) return c;
    if (node_l < xl || xr < node_r) {
      c = copy_node(c);
      ll node_m = (node_l + node_r) / 2;
      if (!c->l) c->l = new_node();
      if (!c->r) c->r = new_node();
      c->l = add_segment_rec(c->l, xl, xr, fid, node_l, node_m);
      c->r = add_segment_rec(c->r, xl, xr, fid, node_m, node_r);
      return c;
    }
    return add_line_rec(c, fid, node_l, node_r);
  }

  np add_line_rec(np c, int fid, ll node_l, ll node_r) {
    int gid = c->fid;
    T fl = evaluate_inner(fid, node_l), fr = evaluate_inner(fid, node_r - 1);
    T gl = evaluate_inner(gid, node_l), gr = evaluate_inner(gid, node_r - 1);
    bool bl = (MINIMIZE ? fl < gl : fl > gl);
    bool br = (MINIMIZE ? fr < gr : fr > gr);
    if (bl && br) {
      c = copy_node(c);
      c->fid = fid;
      return c;
    }
    if (!bl && !br) { return c; }

    c = copy_node(c);
    ll node_m = (node_l + node_r) / 2;
    auto fm = evaluate_inner(fid, node_m), gm = evaluate_inner(gid, node_m);
    bool bm = (MINIMIZE ? fm < gm : fm > gm);
    if (bm) {
      c->fid = fid;
      if (bl) {
        if (!c->r) c->r = new_node();
        c->r = add_line_rec(c->r, gid, node_m, node_r);
      } else {
        if (!c->l) c->l = new_node();
        c->l = add_line_rec(c->l, gid, node_l, node_m);
      }
    }
    if (!bm) {
      if (!bl) {
        if (!c->r) c->r = new_node();
        c->r = add_line_rec(c->r, fid, node_m, node_r);
      } else {
        if (!c->l) c->l = new_node();
        c->l = add_line_rec(c->l, fid, node_l, node_m);
      }
    }
    return c;
  }

  pair<T, int> query_rec(np c, ll x, ll node_l, ll node_r) {
    int fid = c->fid;
    pair<T, int> res = {evaluate_inner(fid, x), fid};
    ll node_m = (node_l + node_r) / 2;
    if (x < node_m && c->l) {
      pair<T, int> res1 = query_rec(c->l, x, node_l, node_m);
      res = (MINIMIZE ? min(res, res1) : max(res, res1));
    }
    if (x >= node_m && c->r) {
      pair<T, int> res1 = query_rec(c->r, x, node_m, node_r);
      res = (MINIMIZE ? min(res, res1) : max(res, res1));
    }
    return res;
  }
};

#include <atcoder/string>
using namespace atcoder;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  INT(n,m);
  VLL(n,a);
  VLL(m,b);
  VLL(m,c);
  
  vi ab(n+m);
  REP(i,n)ab[i]=a[i];
  REP(j,m)ab[n+j]=b[j];

  Dynamic_LiChao_Tree<ll, 0, 15'000'000, 1> X(-infty<ll>, infty<ll>);
  using np=decltype(X)::np;
  np root = X.new_root();
  root = X.add_segment(root,0,1,{1,0});

  auto z=z_algorithm(ab);

  REP(j,m){
    auto [fval,fid]=X.query(root,j);
    debug(j,fval,fid);
    if(fid==-1)continue;
    int r=min(z[n+j],n);
    debug(r);
    if(r==0)continue;
    root = X.add_segment(root,j+1,j+r+1,{c[j],fval-j*c[j]});
  }
  auto [fval,fid]=X.query(root,m);
  if(fid==-1)cout<<-1<<'\n';
  else cout<<fval<<'\n';
}
0