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>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
pll operator-(pll a, pll b){
return mp(a.F - b.F, a.S - b.S);
}
ll cross(pll a, pll b){
return a.F * b.S - a.S * b.F;
}
struct info{
ll y, from, v;
};
ostream& operator<<(ostream& o, info i){
return o << '(' << i.y << ',' << i.from << ',' << i.v << ')';
}
int main(){
StarBurstStream
int n, m;
cin >> n >> m;
vector<pll> a(1), b(1);
for(int i = 1; i <= n; i++){
pll p; p.F = i;
cin >> p.S;
while(a.size() >= 3 && cross(a.back() - a[a.size() - 2], p - a[a.size() - 2]) <= 0){
a.pob;
}
a.eb(p);
}
for(int i = 1; i <= m; i++){
pll p; p.F = i;
cin >> p.S;
while(b.size() >= 3 && cross(b.back() - b[b.size() - 2], p - b[b.size() - 2]) <= 0){
b.pob;
}
b.eb(p);
}
//printv(a, cerr);
//printv(b, cerr);
vector<info> d;
n = a.size(); m = b.size();
n--; m--;
d.eb(info({2, 1, a[1].S}));
/*vector<vector<ll>> dp(n + 1, vector<ll>(m + 1, MAX));
dp[1][1] = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(i == 1 && j == 1) continue;
dp[i][j] = min(dp[i - 1][j] + b[j].S * (a[i].F - a[i - 1].F), dp[i][j - 1] + a[i].S * (b[j].F - b[j - 1].F));
}
printv(dp[i], cerr);
}*/
auto calc = [&](int x, int y){
int l = 0, r = (int)d.size() - 1;
while(l < r){
int mid = (l + r + 1) / 2;
if(d[mid].y > y) r = mid - 1;
else l = mid;
}
info t = d[l];
ll p = t.v * (b[y].F - b[y - 1].F) + (b[y].S - b[y - 1].S) * (a[x].F - a[t.from].F);
return mp(p, b[y].F - b[y - 1].F);
};
for(int i = 2; i <= n; i++){
/*cerr << "before " << i << "\n";
for(int j = 2; j <= m; j++) cerr << calc(i, j) << " ";
cerr << "\n";*/
int l = 2, r = m;
while(l < r){
int mid = (l + r) / 2;
pll t = calc(i, mid);
if(t.F >= a[i].S * t.S) r = mid;
else l = mid + 1;
}
pll t = calc(i, l);
if(t.F >= a[i].S * t.S){
while(!d.empty() && d.back().y >= l) d.pob;
d.eb(info({l, i, a[i].S}));
}
/*cerr << "ok " << i << "\n";
for(int j = 2; j <= m; j++) cerr << calc(i, j) << " ";
cerr << "\n";
printv(d, cerr);*/
}
ll ans = b[1].S * (a[n].F - 1);
for(int i = 2; i <= m; i++){
pll t = calc(n, i);
ans += t.F;
}
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |