#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
int a[100001],b[100001];
struct pt{
ll x,y;
int id;
pt(ll _x,ll _y,int _id): x(_x),y(_y),id(_id){}
bool operator<(const pt &t) const {
if(x * t.y == y * t.x)return id > t.id;
return (x * t.y) < (y * t.x);
}
//bool operator==(const pt &t) const {return x==t.x && y==t.y && id==t.id;}
};
int main()
{
owo
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<m;i++)cin>>b[i];
set<pt>h,w;
set<int>row,col;
for(int i=0;i<n;i++)row.insert(i);
for(int i=1;i<n;i++)h.insert(pt(a[i]-a[i-1],1,i));
for(int i=0;i<m;i++)col.insert(i);
for(int i=1;i<m;i++)w.insert(pt(b[i]-b[i-1],1,i));
ll ans = 0;
int cx = n-1,cy = m-1;
while(cx && cy){
auto ith = h.end();
auto itw = w.end();
ith--;
itw--;
//cout<<ith->x<<" "<<ith->y<<" "<<ith->id<<" "<<ans<<'\n';
//cout<<itw->x<<" "<<itw->y<<" "<<itw->id<<" "<<ans<<'\n';
if(*itw < *ith){
pt v = *ith;
h.erase(ith);
auto it = row.lb(v.id);
int lf = -1,mid,rg = -1;
mid = *it;
if(it != row.begin()){
it--;
lf = *it;
it++;
}
it++;
if(it != row.end())rg = *it;
if(lf != -1)h.erase(pt(a[mid] - a[lf],mid-lf,mid));
if(rg != -1)h.erase(pt(a[rg] - a[mid],rg-mid,rg));
if(lf != -1 && rg != -1)h.insert(pt(a[rg] - a[lf],rg-lf,rg));
row.erase(mid);
if(cx == mid){
auto i = row.end();
i--;
ans+=(cx - *i) * 1LL * b[cy];
cx = *i;
}
}else{
pt v = *itw;
w.erase(itw);
auto it = col.lb(v.id);
int lf = -1,mid,rg = -1;
mid = *it;
if(it != col.begin()){
it--;
lf = *it;
it++;
}
it++;
if(it != col.end())rg = *it;
if(lf != -1)w.erase(pt(b[mid] - b[lf],mid-lf,mid));
if(rg != -1)w.erase(pt(b[rg] - b[mid],rg-mid,rg));
if(lf != -1 && rg != -1)w.insert(pt(b[rg] - b[lf],rg-lf,rg));
col.erase(mid);
if(cy == mid){
auto i = col.end();
i--;
ans+=(cy - *i) * 1LL * a[cx];
cy = *i;
}
//cout<<v.x<<" "<<v.y<<" "<<v.id<<" "<<ans<<'\n';
//cout<<cx<<" "<<cy<<'\n';
}
}
ans+=cy*1LL*a[0];
ans+=cx*1LL*b[0];
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
488 KB |
Output is correct |
13 |
Correct |
1 ms |
480 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
328 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
119 ms |
9584 KB |
Output is correct |
5 |
Correct |
68 ms |
8916 KB |
Output is correct |
6 |
Correct |
39 ms |
4052 KB |
Output is correct |
7 |
Correct |
467 ms |
23008 KB |
Output is correct |
8 |
Correct |
430 ms |
23012 KB |
Output is correct |
9 |
Correct |
454 ms |
23064 KB |
Output is correct |
10 |
Correct |
451 ms |
23012 KB |
Output is correct |
11 |
Correct |
141 ms |
22964 KB |
Output is correct |
12 |
Correct |
447 ms |
23008 KB |
Output is correct |
13 |
Correct |
454 ms |
23008 KB |
Output is correct |
14 |
Correct |
260 ms |
23008 KB |
Output is correct |
15 |
Correct |
151 ms |
23004 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
488 KB |
Output is correct |
13 |
Correct |
1 ms |
480 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
328 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
119 ms |
9584 KB |
Output is correct |
33 |
Correct |
68 ms |
8916 KB |
Output is correct |
34 |
Correct |
39 ms |
4052 KB |
Output is correct |
35 |
Correct |
467 ms |
23008 KB |
Output is correct |
36 |
Correct |
430 ms |
23012 KB |
Output is correct |
37 |
Correct |
454 ms |
23064 KB |
Output is correct |
38 |
Correct |
451 ms |
23012 KB |
Output is correct |
39 |
Correct |
141 ms |
22964 KB |
Output is correct |
40 |
Correct |
447 ms |
23008 KB |
Output is correct |
41 |
Correct |
454 ms |
23008 KB |
Output is correct |
42 |
Correct |
260 ms |
23008 KB |
Output is correct |
43 |
Correct |
151 ms |
23004 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
136 ms |
11192 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
10 ms |
1348 KB |
Output is correct |
58 |
Correct |
33 ms |
3584 KB |
Output is correct |
59 |
Correct |
433 ms |
24940 KB |
Output is correct |
60 |
Correct |
422 ms |
24940 KB |
Output is correct |
61 |
Correct |
436 ms |
24984 KB |
Output is correct |
62 |
Correct |
404 ms |
24916 KB |
Output is correct |
63 |
Correct |
126 ms |
24764 KB |
Output is correct |
64 |
Correct |
437 ms |
24900 KB |
Output is correct |
65 |
Correct |
439 ms |
24960 KB |
Output is correct |
66 |
Correct |
152 ms |
24912 KB |
Output is correct |
67 |
Correct |
157 ms |
25056 KB |
Output is correct |