#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define ROF(i, r, l) for(int i = (r) - 1; i >= (l); --i)
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define pb push_back
#define eb emplace_back
#define sz(v) (int)v.size()
#define sum_of(v) accumulate(all(v), 0ll)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(x) "[" #x " = " << (x) << "]"
template<typename T>
bool minimize(T& a, const T& b){
if(a > b) return a = b, true;
return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b) return a = b, true;
return false;
}
using ll = long long;
using db = double;
using ld = long double;
using ull = unsigned long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vc = vector<char>;
using vl = vector<ll>;
using vd = vector<db>;
using vpi = vector<pi>;
using vpl = vector<pl>;
void setIO(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
#endif // LOCAL
}
struct point{
ll x, y;
point() : x(0), y(0) {}
point(ll x, ll y) : x(x), y(y) {}
point& operator += (const point& o){
x += o.x; y += o.y;
return *this;
}
point& operator -= (const point& o){
x -= o.x; y -= o.y;
return *this;
}
friend bool operator == (const point& a, const point& b){ return a.x == b.x && a.y == b.y; }
friend bool operator != (const point& a, const point& b){ return a.x != b.x || a.y != b.y; }
friend point operator + (point a, const point& b){ return a += b; }
friend point operator - (point a, const point& b){ return a -= b; }
friend ostream& operator << (ostream& out, const point& p){ return out << '(' << p.x << ',' << p.y << ')'; }
};
ll cross(const point& a, const point& b){
return a.x * b.y - a.y * b.x;
}
ll cross3(const point& a, point b, point c){
b -= a; c -= a;
return cross(b, c);
}
vector<point> construct_lower_hull(vector<point> pts){
vector<point> hull;
for(auto p : pts){
// cout << p << ' ';
while(sz(hull) > 1 && cross3(hull[sz(hull)-2], hull.back(), p) <= 0) hull.pop_back();
hull.pb(p);
}
cout << '\n';
return hull;
}
signed main(){
setIO();
int N, M;
cin >> N >> M;
vector<point> vertical, horizontal;
FOR(i, 0, N){
int c; cin >> c;
vertical.pb(point(i, c));
}
FOR(i, 0, M){
int c; cin >> c;
horizontal.pb(point(i, c));
}
vertical = construct_lower_hull(vertical);
horizontal = construct_lower_hull(horizontal);
int i = 0, j = 0;
ll ans = 0;
// for(auto it : vertical) cout << it << ' '; cout << '\n';
// for(auto it : horizontal) cout << it << ' '; cout << '\n';
while(i+1 < sz(vertical) || j+1 < sz(horizontal)){
// cout << vertical[i].x << ' ' << horizontal[j].x << '\n';
if(i+1 == sz(vertical)){
assert(j+1 < sz(horizontal));
ans += 1LL * (horizontal[j+1].x - horizontal[j].x) * vertical[i].y;
++j;
continue;
}
if(j+1 == sz(horizontal)){
assert(i+1 < sz(vertical));
ans += 1LL * (vertical[i+1].x - vertical[i].x) * horizontal[j].y;
++i;
continue;
}
ll A = (vertical[i+1].x - vertical[i].x), B = (horizontal[j].y - horizontal[j+1].y);
ll C = (horizontal[j+1].x - horizontal[j].x), D = (vertical[i].y - vertical[i+1].y);
if(A * B - C * D < 0){ //should go vertically first
ans += 1LL * (vertical[i+1].x - vertical[i].x) * horizontal[j].y;
++i;
} else{ //should go horizontally first
ans += 1LL * (horizontal[j+1].x - horizontal[j].x) * vertical[i].y;
++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... |