#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N=4e5+5;
int n,m,a[N],b[N],l[N],r[N];
ll s[N];
struct data
{
int a,l,r;
}val[N];
bool lf(data p,data q)
{
return p.l<q.l;
}
struct SegmentTree
{
ll ST[4*N];
ll lazy[4*N];
void dolazy(int id,int l,int r)
{
if (lazy[id]==0) return;
ST[id]+=lazy[id];
if (l!=r)
{
lazy[id*2]+=lazy[id];
lazy[id*2+1]+=lazy[id];
}
lazy[id]=0;
}
void update_range(int id,int l,int r,int L,int R,ll val)
{
dolazy(id,l,r);
if (r<L||R<l) return;
if(L<=l&&r<=R)
{
ST[id]+=val;
if (l!=r)
{
lazy[id*2]+=val;
lazy[id*2+1]+=val;
}
return;
}
int mid=(l+r)/2;
update_range(id*2,l,mid,L,R,val);
update_range(id*2+1,mid+1,r,L,R,val);
ST[id]=min(ST[id*2],ST[id*2+1]);
}
void update_pos(int id,int l,int r,int pos,ll val)
{
dolazy(id,l,r);
if(l>pos||r<pos) return ;
if(l==r)
{
ST[id]=min(ST[id],1ll*val);
return ;
}
int mid=(l+r)/2;
update_pos(id*2,l,mid,pos,val);
update_pos(id*2+1,mid+1,r,pos,val);
ST[id]=min(ST[id*2],ST[id*2+1]);
}
ll get (int id,int l,int r,int L,int R)
{
dolazy(id,l,r);
if (r<L||R<l) return 1e18;
if (L<=l&&r<=R) return ST[id];
int mid=(l+r)/2;
return min(get (id*2,l,mid,L,R),get (id*2+1,mid+1,r,L,R));
}
}ST1,ST2;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m;
for(int i=1;i<=n+m-1;i++) cin>>a[i];
for(int i=1;i<=n+m-1;i++) cin>>b[i];
vector<pii>pointL,pointR;
for(int i=m;i>=1;i--) pointL.push_back(mp(1,i));
for(int i=2;i<=n;i++) pointL.push_back(mp(i,1));
for(int i=1;i<=n;i++) pointR.push_back(mp(i,m));
for(int i=m-1;i>=1;i--) pointR.push_back(mp(n,i));
for(int i=0;i<n+m-1;i++)
{
l[i+1]=pointL[i].fi+pointL[i].se-1;
r[i+1]=pointR[i].fi+pointR[i].se-1;
//cout<<l[i]<<' '<<r[i]<<'\n';
}
vector<int>order;
for(int i=1;i<=n+m-1;i++) if(i%2==0) order.push_back(b[i]);
for(int i=1;i<=n+m-1;i++)
{
if(l[i]%2==0)
{
l[i]/=2;
r[i]/=2;
}
else
{
l[i]=(l[i]+1)/2+order.size();
r[i]=(r[i]+1)/2+order.size();
}
}
for(int i=1;i<=n+m-1;i++) if(i%2) order.push_back(b[i]);
for(int i=1;i<=n+m-1;i++) b[i]=order[i-1];
/*
for(int i=1;i<=n+m-1;i++) cout<<b[i]<<' ';
cout<<'\n';
for(int i=1;i<=n+m-1;i++) cout<<l[i]<<' '<<r[i]<<'\n';
*/
for(int i=1;i<=n+m-1;i++) val[i]={a[i],l[i],r[i]};
n=n+m-1;
for(int i=1;i<=n;i++) s[i]=s[i-1]+b[i];
sort(val+1,val+n+1,lf);
/*cout<<'\n';
for(int i=1;i<=n;i++) cout<<val[i].a<<' '<<val[i].l<<' '<<val[i].r<<'\n';
for(int i=1;i<=n;i++) cout<<b[i]<<' ';
cout<<'\n';*/
ST1.update_range(1,0,n,1,n,1e16);
ST2.update_range(1,0,n,1,n,1e16);
for(int i=0;i<n;i++)
{
int curL=val[i+1].l;
int curR=val[i+1].r;
ll mn=ST1.get(1,0,n,0,curL-1);
ll in_range_mn=ST2.get(1,0,n,curL,curR);
///chose a[i]
ST1.update_range(1,0,n,0,curR,val[i+1].a);
ST2.update_range(1,0,n,0,curR,val[i+1].a);
/// not chose a[i]
/// type 1:
ST1.update_pos(1,0,n,curR,mn+s[curR]-s[curL-1]);
ST2.update_pos(1,0,n,curR,mn-s[curL-1]);
/// type2:
ST1.update_pos(1,0,n,curR,in_range_mn+s[curR]);
ST2.update_pos(1,0,n,curR,in_range_mn);
//cout<<ST1.get(1,0,n,2,2)<<' '<<ST1.get(1,0,n,6,6)<<'\n';
}
cout<<ST1.get(1,0,n,0,n);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
396 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
396 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
396 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
7 ms |
1004 KB |
Output is correct |
31 |
Correct |
8 ms |
1064 KB |
Output is correct |
32 |
Correct |
8 ms |
1004 KB |
Output is correct |
33 |
Correct |
9 ms |
876 KB |
Output is correct |
34 |
Correct |
6 ms |
876 KB |
Output is correct |
35 |
Correct |
4 ms |
748 KB |
Output is correct |
36 |
Correct |
4 ms |
748 KB |
Output is correct |
37 |
Correct |
4 ms |
748 KB |
Output is correct |
38 |
Correct |
4 ms |
748 KB |
Output is correct |
39 |
Correct |
4 ms |
748 KB |
Output is correct |
40 |
Correct |
4 ms |
748 KB |
Output is correct |
41 |
Correct |
4 ms |
876 KB |
Output is correct |
42 |
Correct |
4 ms |
876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
495 ms |
28964 KB |
Output is correct |
2 |
Correct |
452 ms |
32740 KB |
Output is correct |
3 |
Correct |
479 ms |
32740 KB |
Output is correct |
4 |
Correct |
473 ms |
32996 KB |
Output is correct |
5 |
Correct |
476 ms |
33252 KB |
Output is correct |
6 |
Correct |
451 ms |
32228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1123 ms |
57236 KB |
Output is correct |
2 |
Correct |
1094 ms |
64980 KB |
Output is correct |
3 |
Correct |
1090 ms |
65292 KB |
Output is correct |
4 |
Correct |
1106 ms |
65248 KB |
Output is correct |
5 |
Correct |
1102 ms |
65240 KB |
Output is correct |
6 |
Correct |
1105 ms |
62324 KB |
Output is correct |
7 |
Correct |
1093 ms |
64792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
396 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
7 ms |
1004 KB |
Output is correct |
31 |
Correct |
8 ms |
1064 KB |
Output is correct |
32 |
Correct |
8 ms |
1004 KB |
Output is correct |
33 |
Correct |
9 ms |
876 KB |
Output is correct |
34 |
Correct |
6 ms |
876 KB |
Output is correct |
35 |
Correct |
4 ms |
748 KB |
Output is correct |
36 |
Correct |
4 ms |
748 KB |
Output is correct |
37 |
Correct |
4 ms |
748 KB |
Output is correct |
38 |
Correct |
4 ms |
748 KB |
Output is correct |
39 |
Correct |
4 ms |
748 KB |
Output is correct |
40 |
Correct |
4 ms |
748 KB |
Output is correct |
41 |
Correct |
4 ms |
876 KB |
Output is correct |
42 |
Correct |
4 ms |
876 KB |
Output is correct |
43 |
Correct |
495 ms |
28964 KB |
Output is correct |
44 |
Correct |
452 ms |
32740 KB |
Output is correct |
45 |
Correct |
479 ms |
32740 KB |
Output is correct |
46 |
Correct |
473 ms |
32996 KB |
Output is correct |
47 |
Correct |
476 ms |
33252 KB |
Output is correct |
48 |
Correct |
451 ms |
32228 KB |
Output is correct |
49 |
Correct |
1123 ms |
57236 KB |
Output is correct |
50 |
Correct |
1094 ms |
64980 KB |
Output is correct |
51 |
Correct |
1090 ms |
65292 KB |
Output is correct |
52 |
Correct |
1106 ms |
65248 KB |
Output is correct |
53 |
Correct |
1102 ms |
65240 KB |
Output is correct |
54 |
Correct |
1105 ms |
62324 KB |
Output is correct |
55 |
Correct |
1093 ms |
64792 KB |
Output is correct |
56 |
Correct |
499 ms |
32228 KB |
Output is correct |
57 |
Correct |
478 ms |
32356 KB |
Output is correct |
58 |
Correct |
462 ms |
32788 KB |
Output is correct |
59 |
Correct |
470 ms |
31460 KB |
Output is correct |
60 |
Correct |
483 ms |
32916 KB |
Output is correct |
61 |
Correct |
467 ms |
32740 KB |
Output is correct |
62 |
Correct |
477 ms |
31460 KB |
Output is correct |
63 |
Correct |
497 ms |
32740 KB |
Output is correct |
64 |
Correct |
500 ms |
32740 KB |
Output is correct |
65 |
Correct |
555 ms |
33380 KB |
Output is correct |
66 |
Correct |
631 ms |
35300 KB |
Output is correct |
67 |
Correct |
712 ms |
36992 KB |
Output is correct |
68 |
Correct |
1067 ms |
62552 KB |
Output is correct |
69 |
Correct |
1094 ms |
63960 KB |
Output is correct |
70 |
Correct |
885 ms |
57688 KB |
Output is correct |