結果

問題 No.2332 Make a Sequence
ユーザー owo_zzz
提出日時 2025-01-30 02:46:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 303 ms / 2,000 ms
コード長 7,415 bytes
コンパイル時間 1,768 ms
コンパイル使用メモリ 156,096 KB
実行使用メモリ 53,860 KB
最終ジャッジ日時 2025-01-30 02:47:07
合計ジャッジ時間 17,323 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 61
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:247:16: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  247 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:248:16: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  248 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:252:16: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  252 |         freopen(task1".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:253:16: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  253 |         freopen(task1".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

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

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <unordered_set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#include <cstring>
#include <unordered_map>
#include <cmath>
#include <array>
#include <cassert>
#include <random>
#include <chrono>

using namespace std;

#define          BIT(i,j)  (((i)>>(j))&1LL)
#define           MASK(i)  (1LL<<(i))
#define            ALL(x)  (x).begin(),(x).end()
#define             SZ(x)  (int)(x).size()
#define                fi  first
#define                se  second
#define               ull  unsigned long long
#define                ll  long long
#define                ld  long double
#define                vi  vector<int>
#define               vvi  vector<vi>
#define              vvvi  vector<vvi>
#define               pii  pair<int,int>
#define              vpii  vector<pii>
#define             vvpii  vector<vpii>
#define              endl  "\n"
#define                MP  make_pair
#define               int  ll

//------------------------------------------------------------------------------------------------//
template<typename T1, typename T2> bool mini(T1 &a, T2 b){if(a>b){a=b;return true;}return false;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b){if(a<b){a=b;return true;}return false;}
template<typename T> T gcd(T a, T b) { while(b) { a %= b; swap(a,b); } return a; }
template<typename T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
//-----------------------------------------------------------------------------------------------//
const  ll   LINF = 1e18;
const int    INF = 1e18;
const int    LOG = 20;
const int   MAXN = 5e5+7;
const int      N = 1e2+3;
const int   MOD1 = 1e14+13;
const int   MOD2 = 5e13+777;
const int   BASE = 4e5+1;
const  ld    EPS = 1e-9;
const  ld     PI = acos(-1);
const int OFFSET = 1e3;
//------------------------------------------------------------------------------------------------//

struct Line {
  ll m, c;
  ll eval(ll x) {
    return m * x + c;
  }
};

struct node {
  Line line;
  node* left = nullptr;
  node* right = nullptr;
  node(Line line) : line(line) {}
  void add(Line nw, int l, int r, int L, int R) {
    if (l > r || r < L || l > R) return;
    int m = (l + 1 == r ? l : (l + r) / 2);
    if (l >= L and r <= R) {
      bool lef = nw.eval(l) < line.eval(l);
      bool mid = nw.eval(m) < line.eval(m);
      if (mid) swap(line, nw);
      if (l == r) return;
      if (lef != mid) {
        if (left == nullptr) left = new node(nw);
        else left -> add(nw, l, m, L, R);
      }
      else {
        if (right == nullptr) right = new node(nw);
        else right -> add(nw, m + 1, r, L, R);
      }
      return;
    }
    if (max(l, L) <= min(m, R)) {
      if (left == nullptr) left = new node({0, INF});
      left -> add(nw, l, m, L, R);
    }
    if (max(m + 1, L) <= min(r, R)) {
      if (right == nullptr) right = new node ({0, INF});
      right -> add(nw, m + 1, r, L, R);
    }
  }
  ll get(ll x, int l, int r, int L, int R) {
    if (l > r || r < L || l > R) return INF;
    int m = (l + 1 == r ? l : (l + r) / 2);
    if (l >= L and r <= R) {
      ll ans = line.eval(x);
      if (l < r) {
        if (x <= m && left != nullptr) ans = min(ans, left -> get(x, l, m, L, R));
        if (x > m && right != nullptr) ans = min(ans, right -> get(x, m + 1, r, L, R));
      }
      return ans;
    }
    ll ans = INF;
    if (max(l, L) <= min(m, R)) {
      if (left == nullptr) left = new node({0, INF});
      ans = min(ans, left -> get(x, l, m, L, R));
    }
    if (max(m + 1, L) <= min(r, R)) {
      if (right == nullptr) right = new node ({0, INF});
      ans = min(ans, right -> get(x, m + 1, r, L, R));
    }
    return ans;
  }
};

struct LiChaoTree {
  int L, R;
  node* root;
  LiChaoTree() : L(numeric_limits<int>::min() / 2), R(numeric_limits<int>::max() / 2), root(nullptr) {}
  LiChaoTree(int L, int R) : L(L), R(R) {
    root = new node({0, INF});
  }
  void add_line(Line line) {
    root -> add(line, L, R, L, R);
  }
  // y = mx + b: x in [l, r]
  void add(Line line, int l, int r) {
    root -> add(line, L, R, l, r);
  }
  ll get(ll x) {
    return root -> get(x, L, R, L, R);
  }
  ll get(ll x, int l, int r) {
    return root -> get(x, l, r, L, R);
  }
};

int a[MAXN],b[MAXN],c[MAXN],dp[MAXN];
int POW1[MAXN],POW2[MAXN];
int h1[MAXN][2],h2[MAXN][2];

pii geta(int l, int r){
    int res1 = h1[r][0]-1LL*POW1[r-l+1]*h1[l-1][0];
    int res2 = h2[r][0]-1LL*POW2[r-l+1]*h2[l-1][0];
    return {res1,res2};
}

pii getb(int l, int r){
    int res1 = h1[r][1]-1LL*POW1[r-l+1]*h1[l-1][1];
    int res2 = h2[r][1]-1LL*POW2[r-l+1]*h2[l-1][1];
    return {res1,res2};
}

void solve(){
    int n,m; cin >> n >> m;
    POW1[0] = POW2[0] = 1;
    for(int i = 1; i<=max(n,m); i++){
        POW1[i] = 1LL*POW1[i-1]*BASE;
        POW2[i] = 1LL*POW2[i-1]*BASE;
    }
    vi compress;
    for(int i = 1; i<=n; i++){
        cin >> a[i];
        compress.push_back(a[i]);
    }
    for(int i = 1; i<=m; i++){
        cin >> b[i];
        compress.push_back(b[i]);
    }
    sort(ALL(compress));
    compress.resize(unique(ALL(compress))-compress.begin());
    for(int i = 1; i<=n; i++){
        a[i] = upper_bound(ALL(compress),a[i])-compress.begin();
        h1[i][0] = (1LL*h1[i-1][0]*BASE+a[i]);
        h2[i][0] = (1LL*h2[i-1][0]*BASE+a[i]);
    }
    for(int i = 1; i<=m; i++){
        b[i] = upper_bound(ALL(compress),b[i])-compress.begin();
        h1[i][1] = (1LL*h1[i-1][1]*BASE+b[i]);
        h2[i][1] = (1LL*h2[i-1][1]*BASE+b[i]);
    }
    // return ;
    LiChaoTree f(1,m);
    for(int i = 0; i<m; i++) cin >> c[i];
    for(int i = 1; i<=m; i++) dp[i] = LINF;
    dp[0] = 0;
    // f.add({c[0],0},1,m);
    f.add_line({100000,INF});
    for(int i = 0; i<=m; i++){
        if(i) dp[i] = f.get(i,1,m);
        int l = 1,r = min(n,m-i),res = 0;
        // cerr << l << " " << r << endl;
        while(l<=r){
            int mid = (l+r)>>1;
            if(geta(1,mid)==getb(i+1,i+mid)){
                res = mid;
                l = mid+1;
            }else{
                r = mid-1;
            }
        }
        if(res){
            f.add({c[i],dp[i]-1LL*c[i]*i},i+1,i+res);
            // cout << i+1 << " " << i+res << endl;
            // cerr << res << " ";
        }
        // cerr << dp[i] << " ";
    }
    // dp[i] = min(dp[j]+c[j]*(i-j))
    if(dp[m]>=LINF) dp[m] = -1;
    cout << dp[m];
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #define task "test"
    if(fopen(task".inp","r")){
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    #define task1 "nothing"
    if(fopen(task1".inp","r")){
        freopen(task1".inp","r",stdin);
        freopen(task1".out","w",stdout);
    }

    int test = 1;

    while(test--){
        solve();
    }
    cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n";
    return 0;
}

/**  /\_/\
 *  (= ._.)
 *  / >TL \>AC
**/
0