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>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;
typedef pair<int,int> pii;
pii operator+(pii a,pii b){
return {a.fs+b.fs,a.sc+b.sc};
}
pii operator-(pii a,pii b){
return {a.fs-b.fs,a.sc-b.sc};
}
int dot(pii a,pii b){
return a.fs*b.fs+a.sc*b.sc;
}
int cross(pii a,pii b){
return a.fs*b.sc-b.fs*a.sc;
}
int sign(int x){
if(x==0){
return 0;
}
if(x>0){
return 1;
}
else{
return -1;
}
}
int ori(pii a,pii b,pii c){
return sign(cross(b-a,c-a));
}
long double slope(pii a,pii b){
return (b.sc-a.sc)*1.0/(1.0*(b.fs-a.fs));
}
signed main(){
AquA;
int n,m;
cin >> n >> m;
vector<int> a(n),b(m);
for(int i=0;i<n;i++){
cin >> a[i];
}
for(int j=0;j<m;j++){
cin >> b[j];
}
vector<pii> c,d;
for(int i=0;i<n;i++){
while(c.size()>=2 && ori(c[c.size()-2],c[c.size()-1],{i,a[i]})<=0){
c.pop_back();
}
c.push_back({i,a[i]});
}
for(int i=0;i<m;i++){
while(d.size()>=2 && ori(d[d.size()-2],d[d.size()-1],{i,b[i]})<=0){
d.pop_back();
}
d.push_back({i,b[i]});
}
int ans=0;
int e=c.size(),f=d.size();
for(int h=0,w=0;h<e-1 || w<f-1;){
if(w!=f-1 && (h==e-1 || slope(c[h],c[h+1])>slope(d[w],d[w+1]))){
ans+=c[h].sc*(d[w+1].fs-d[w].fs);
w++;
}
else{
ans+=d[w].sc*(c[h+1].fs-c[h].fs);
h++;
}
}
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... |