This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5;
#define ll long long
ll n, h[nx], sm, w[nx];
struct line
{
ll m, c;
line(ll m, ll c): m(m), c(c){}
ll val(ll x) {return m*x+c;}
};
struct lichaotree
{
struct node
{
line a;
node *l, *r;
node(line a):a(a), l(0), r(0) {}
};
typedef node* pnode;
pnode rt;
void update(int l, int r, pnode &t, line nw)
{
if (!t) return t=new node(nw), void();
int md=(l+r)/2;
int ls=nw.val(l)<t->a.val(l);
int ms=nw.val(md)<t->a.val(md);
int rs=nw.val(r)<t->a.val(r);
if (ms) swap(t->a, nw);
if (l==r) return;
if (ls^ms) update(l, md, t->l, nw);
if (ms^rs) update(md+1, r, t->r, nw);
}
ll query(int l, int r, pnode t, int x)
{
if (!t) return LLONG_MAX;
if (l==r) return t->a.val(x);
int md=(l+r)/2;
if (x<=md) return min(t->a.val(x), query(l, md, t->l, x));
else return min(t->a.val(x), query(md+1, r, t->r, x));
}
} s;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>h[i];
for (int i=1; i<=n; i++) cin>>w[i], sm+=w[i];
s.update(0, 1e6, s.rt, line(-2*h[1], h[1]*h[1]-w[1]));
//c.add(-2*h[1], h[1]*h[1]-w[1]);
for (int i=2; i<=n-1; i++)
{
ll tmp=s.query(0, 1e6, s.rt, h[i])+h[i]*h[i]-w[i];
//cout<<i<<' '<<tmp<<'\n';
//ll tmp=c.query(h[i])+h[i]*h[i]-w[i];
s.update(0, 1e6, s.rt, line(-2*h[i], h[i]*h[i]+tmp));
//c.add(-2*h[i], tmp+h[i]*h[i]);
}
cout<<s.query(0, 1e6, s.rt, h[n])+h[n]*h[n]-w[n]+sm;
//cout<<c.query(h[n])+h[n]*h[n]-w[n]+sm;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |