제출 #240039

#제출 시각아이디문제언어결과실행 시간메모리
240039eohomegrownappsBuilding Bridges (CEOI17_building)C++14
30 / 100
65 ms16308 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll INF = 1e18;
struct Line{
	ll m,c;
	Line(ll _m, ll _c){
		m=_m;c=_c;
	}
	ll operator()(ll x){
		return m*x+c;
	}
};

struct Node{
	ll s,e,m;
	Line f = Line(0,INF);
	Node *l, *r;

	Node(ll _s, ll _e){
		s=_s;e=_e;
		if (s==e){return;}
		m=(s+e)/2;
		l = new Node(s,m);
		r = new Node(m+1,e);
	}

	void insert(Line ins){
		if (s==e){
			if (ins(s)<f(s)){
				f=ins;
			}
			return;
		}
		if (ins.m==f.m){
			if (ins.c<f.c){
				f=ins;
			}
			return;
		}
		//wlog insert line has higher gradient
		if (ins.m<f.m){
			swap(ins,f);
		}
		if (ins(m)<f(m)){
			r->insert(f);
			f=ins;
		} else {
			l->insert(ins);
		}
	}

	ll query(ll x){
		if (s==e){
			return f(x);
		}
		if (x<=m){
			return min(f(x),l->query(x));
		} else {
			return min(f(x),r->query(x));
		}
	}
};

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	ll n;
	cin>>n;
	vector<ll> h(n);
	vector<ll> pref(n+1);
	for (ll i = 0; i<n; i++){
		cin>>h[i];
	}
	for (ll i = 0; i<n; i++){
		cin>>pref[i+1];
		pref[i+1]+=pref[i];
	}
	vector<ll> dp(n);
	Node *lichao = new Node(0,n-1);
	lichao->insert(Line(-2*h[0],dp[0]-pref[1]+h[0]*h[0]));
	for (ll x = 1; x<n; x++){
		ll minval = lichao->query(h[x]);
		dp[x]=h[x]*h[x]+pref[x]+minval;
		lichao->insert(Line(-2*h[x],dp[x]-pref[x+1]+h[x]*h[x]));
	}
	cout<<dp[n-1]<<'\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...