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>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const int N = 1e5+5;
ll a[N], b[N];
vector<int> stka, stkb;
inline bool chk(ll a[], int x, int y, int z)
{ return !((z-y)*(a[y]-a[x]) < (y-x)*(a[z]-a[y])); }
inline bool chk2(int x, int y, int l, int r)
{ return (r-l)*(a[y]-a[x]) < (y-x)*(b[r]-b[l]); }
int main(void)
{ IO
ll n, i, m, j, ans=0;
cin >> n >> m;
for (i=1; i <= n; ++i) cin >> a[i];
for (i=1; i <= m; ++i) cin >> b[i];
for (i=1; i <= n; ++i)
{
while (SZ(stka) >= 2 && chk(a, stka[SZ(stka)-2], stka[SZ(stka)-1], i))
stka.pop_back();
stka.pb(i);
}
for (i=1; i <= m; ++i)
{
while (SZ(stkb) >= 2 && chk(b, stkb[SZ(stkb)-2], stkb[SZ(stkb)-1], i))
stkb.pop_back();
stkb.pb(i);
}
for (i=0, j=0; i < SZ(stka)-1 || j < SZ(stkb)-1; )
{
if (i < SZ(stka)-1 && j < SZ(stkb)-1)
{
if (chk2(stka[i], stka[i+1], stkb[j], stkb[j+1]))
{
ans += (stka[i+1]-stka[i]) * b[stkb[j]];
++i;
} else {
ans += (stkb[j+1]-stkb[j]) * a[stka[i]];
++j;
}
} else if (i < SZ(stka)-1) {
ans += (stka[i+1]-stka[i]) * b[stkb[j]];
++i;
} else { // if j < SZ(stkb)-1
ans += (stkb[j+1]-stkb[j]) * a[stka[i]];
++j;
}
}
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... |