結果

問題 No.2332 Make a Sequence
ユーザー m_99m_99
提出日時 2023-05-28 14:54:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 310 ms / 2,000 ms
コード長 3,337 bytes
コンパイル時間 4,097 ms
コンパイル使用メモリ 235,264 KB
実行使用メモリ 24,136 KB
最終ジャッジ日時 2023-08-27 10:52:38
合計ジャッジ時間 24,057 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 230 ms
19,936 KB
testcase_08 AC 70 ms
4,624 KB
testcase_09 AC 183 ms
13,180 KB
testcase_10 AC 189 ms
19,188 KB
testcase_11 AC 128 ms
11,964 KB
testcase_12 AC 280 ms
23,552 KB
testcase_13 AC 280 ms
22,720 KB
testcase_14 AC 279 ms
24,136 KB
testcase_15 AC 280 ms
23,460 KB
testcase_16 AC 281 ms
23,724 KB
testcase_17 AC 280 ms
23,348 KB
testcase_18 AC 279 ms
23,184 KB
testcase_19 AC 282 ms
23,348 KB
testcase_20 AC 281 ms
23,280 KB
testcase_21 AC 279 ms
23,224 KB
testcase_22 AC 280 ms
23,920 KB
testcase_23 AC 281 ms
22,788 KB
testcase_24 AC 279 ms
23,132 KB
testcase_25 AC 281 ms
23,888 KB
testcase_26 AC 282 ms
23,672 KB
testcase_27 AC 280 ms
23,188 KB
testcase_28 AC 281 ms
23,776 KB
testcase_29 AC 279 ms
23,200 KB
testcase_30 AC 280 ms
23,540 KB
testcase_31 AC 279 ms
23,684 KB
testcase_32 AC 281 ms
24,092 KB
testcase_33 AC 281 ms
23,260 KB
testcase_34 AC 281 ms
23,344 KB
testcase_35 AC 282 ms
23,684 KB
testcase_36 AC 282 ms
23,352 KB
testcase_37 AC 282 ms
23,308 KB
testcase_38 AC 283 ms
23,656 KB
testcase_39 AC 282 ms
23,924 KB
testcase_40 AC 280 ms
23,464 KB
testcase_41 AC 278 ms
23,364 KB
testcase_42 AC 288 ms
23,584 KB
testcase_43 AC 288 ms
23,948 KB
testcase_44 AC 286 ms
23,444 KB
testcase_45 AC 281 ms
23,420 KB
testcase_46 AC 287 ms
23,468 KB
testcase_47 AC 309 ms
23,576 KB
testcase_48 AC 309 ms
23,388 KB
testcase_49 AC 309 ms
23,968 KB
testcase_50 AC 309 ms
22,996 KB
testcase_51 AC 310 ms
22,776 KB
testcase_52 AC 282 ms
23,348 KB
testcase_53 AC 281 ms
23,480 KB
testcase_54 AC 280 ms
23,428 KB
testcase_55 AC 283 ms
23,988 KB
testcase_56 AC 281 ms
23,612 KB
testcase_57 AC 256 ms
23,400 KB
testcase_58 AC 253 ms
23,800 KB
testcase_59 AC 252 ms
23,500 KB
testcase_60 AC 253 ms
22,868 KB
testcase_61 AC 252 ms
23,568 KB
testcase_62 AC 300 ms
23,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001

template <typename T>
struct Z_algorithm{
	vector<int> A;
	Z_algorithm(T &s){
		A.resize(s.size(),0);
		A[0] = s.size();
		int i=1,j=0;
		while(i<s.size()){
			while (i+j<s.size()&&s[j]==s[i+j])j++;
			A[i] = j;
			if(j==0){
				i++;
				continue;
			}
			int k = 1;
			while(i+k<s.size()&&k+A[k]<j){
				A[i+k] = A[k];
				k++;
			}
			i+=k;
			j-=k;
		}
	}
	//[0,S.size)と[pos,S.size)の最長共通接頭辞の長さ
	int get_length(int pos){
		return A[pos];
	}
	
};

/*
	iv...Inf
	s...if maxCHT, set -1
*/
template <typename T>
struct CHT{

	vector<T> v;
	vector<T> a,b;
	int n;
	long long sign;
	
	T init_value;
	
	CHT(vector<T> &x,T iv,long long s=1){
		init_value = iv;
		sign = s;
		v = x;
		sort(v.begin(),v.end());
		int temp = distance(v.begin(),unique(v.begin(),v.end()));
		v.resize(temp);
		n=1;
		while(true){
			if(n>=v.size())break;
			n<<=1;
		}
		
		T t = v.back();
		
		v.resize(n,t+1);
		a.resize(2*n,0);
		b.resize(2*n,init_value);
	}
		
	void add(T A,T B,int start){
		int now = start;
		
		while(true){
			int ll = (now<<(__builtin_clz(now)-__builtin_clz(n)))-n;
			int rr = ll + (n>>(31-__builtin_clz(now))) -1;
			int m = (ll+rr)>>1;
			bool fl = (A*v[ll]+B)<(a[now]*v[ll]+b[now]);
			bool fm = (A*v[m]+B)<(a[now]*v[m]+b[now]);
			bool fr = (A*v[rr]+B)<(a[now]*v[rr]+b[now]);

			if(fl&&fr){
				a[now]=A;
				b[now]=B;
				break;
			}
			if(!fl&&!fr){
				break;
			}
			if(fm){
				swap(A,a[now]);
				swap(B,b[now]);
				swap(fl,fr);
			}
			if(fl){
				now<<=1;
			}
			else{
				now<<=1;
				now++;
			}
		}
	}

	void add_line(T A,T B){
		A *= sign;
		B *= sign;
		add(A,B,1);
	}
	
	void add_segment(T A,T B,T L,T R){
		L = distance(v.begin(),lower_bound(v.begin(),v.end(),L));
		R = distance(v.begin(),lower_bound(v.begin(),v.end(),R));
		if(R==L)return;
		L+=n;
		R+=n;
		
		while(true){
			if(L&1){
				add(A,B,L++);
			}
			if(R&1){
				add(A,B,--R);
			}
			if(L>=R)break;
			L>>=1;R>>=1;
		}
	}
	
	T query(T x){
		int now = n + distance(v.begin(),lower_bound(v.begin(),v.end(),x));
		T ret = init_value;
		while(true){
			ret = min(ret,(a[now]*x+b[now]));
			now>>=1;
			if(now<=0)break;
		}
		return ret*sign;
	}
	
	void show(){
		int n = 1;
		for(int i=1;i<a.size();i++){
			for(int j=0;j<n;j++){
				if(j!=0)cout<<' ';
				cout<<a[i+j];
			}
			cout<<endl;
			i+=n-1;
			n*=2;
		}
	}
	
};

int main(){
	
	int N,M;
	cin>>N>>M;
	
	vector<int> a(N);
	rep(i,N)cin>>a[i];
	
	a.push_back(-1);
	rep(i,M){
		int t;
		cin>>t;
		a.push_back(t);
	}
	
	Z_algorithm<vector<int>> Z(a);
	
	vector<long long> c(M);
	rep(i,M)cin>>c[i];
	
	vector<long long> dp(M+1,Inf64);
	vector<long long> t(M+5);
	rep(i,M+5)t[i] = i;
	
	CHT<long long> C(t,Inf64);
	
	rep(i,M+1){
		if(i==0)dp[i] = 0;
		else{
			//cout<<C.query(i)<<endl;
			dp[i] = min(dp[i],C.query(i));
		}
		if(dp[i]!=Inf64&&i!=M){
			long long b = dp[i];
			b -= c[i] * i;
			//cout<<c[i]<<','<<b<<','<<i<<','<<Z.get_length(N+1+i)<<endl;
			C.add_segment(c[i],b,i,i+Z.get_length(N+1+i)+1);
			//C.add_line(c[i],b);
		}
	}
	long long ans = dp.back();
	if(ans>=Inf64)ans = -1;
	cout<<ans<<endl;
	return 0;
}
0