## 雯舞

Love three things int he world -- Manchery

# [二分图匹配 线段树] Codeforces 573D Round #318 [RussianCodeCup Thanks-Round] (Div. 1) D. Bear and Cavalry

i—–(i+3)
i+1—(i+2)
i+2—(i)
i+3—(i+1)

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,int> abcd;

inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline void read(ll &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int N=30005;

int n; abcd a[N],b[N];
int li[N];

ll T[N<<2][3][3];

inline void Max(ll &x,ll y){ x=max(x,y); }
inline ll val(int x,int y){
return li[a[x].second]==b[y].second?-1LL<<50:a[x].first*b[y].first;
}

inline void upd(int x,int l,int r){
memset(T[x],-0x1f,sizeof(T[x]));
int m=(l+r)>>1;
for (int i1=0;i1<3;i1++)
for (int j2=0;j2<3;j2++){
Max(T[x][i1][j2],T[x<<1][i1][0]+T[x<<1|1][0][j2]);
Max(T[x][i1][j2],T[x<<1][i1][1]+T[x<<1|1][1][j2]+val(m,m+1)+val(m+1,m));
if (r-m>=2){
Max(T[x][i1][j2],T[x<<1][i1][1]+T[x<<1|1][2][j2]+val(m,m+2)+val(m+1,m)+val(m+2,m+1));
Max(T[x][i1][j2],T[x<<1][i1][1]+T[x<<1|1][2][j2]+val(m,m+1)+val(m+1,m+2)+val(m+2,m));
Max(T[x][i1][j2],T[x<<1][i1][1]+T[x<<1|1][2][j2]+val(m,m+2)+val(m+1,m+1)+val(m+2,m));
}
if (m-l+1>=2){
Max(T[x][i1][j2],T[x<<1][i1][2]+T[x<<1|1][1][j2]+val(m-1,m)+val(m,m+1)+val(m+1,m-1));
Max(T[x][i1][j2],T[x<<1][i1][2]+T[x<<1|1][1][j2]+val(m-1,m+1)+val(m,m-1)+val(m+1,m));
Max(T[x][i1][j2],T[x<<1][i1][2]+T[x<<1|1][1][j2]+val(m-1,m+1)+val(m,m)+val(m+1,m-1));
}
if (r-m>=2 && m-l+1>=2){
Max(T[x][i1][j2],T[x<<1][i1][2]+T[x<<1|1][2][j2]+val(m-1,m+1)+val(m,m-1)+val(m+1,m+2)+val(m+2,m));
Max(T[x][i1][j2],T[x<<1][i1][2]+T[x<<1|1][2][j2]+val(m-1,m)+val(m,m+2)+val(m+1,m-1)+val(m+2,m+1));
Max(T[x][i1][j2],T[x<<1][i1][2]+T[x<<1|1][2][j2]+val(m-1,m+1)+val(m,m+2)+val(m+1,m-1)+val(m+2,m));
}
}
if (r-l+1==2){
Max(T[x][0][2],0);
Max(T[x][2][0],0);
}
if (r-l+1==3 && r-m==1){
Max(T[x][0][2],val(l,l));
Max(T[x][1][2],0);
}
if (r-l+1==3 && r-m==2){
Max(T[x][2][0],val(r,r));
Max(T[x][2][1],0);
}
}

inline void Build(int x,int l,int r){
if (l==r){
memset(T[x],-0x1f,sizeof(T[x]));
T[x][0][1]=T[x][1][0]=0;
T[x][0][0]=val(l,l);
return;
}
int mid=(l+r)>>1;
Build(x<<1,l,mid); Build(x<<1|1,mid+1,r);
upd(x,l,r);
}
inline void modify(int x,int l,int r,int t){
if (l==r){
memset(T[x],-0x1f,sizeof(T[x]));
T[x][0][1]=T[x][1][0]=0;
T[x][0][0]=val(l,l);
return;
}
int mid=(l+r)>>1;
if (t<=mid) modify(x<<1,l,mid,t);
else modify(x<<1|1,mid+1,r,t);
upd(x,l,r);
}

int pos[N];

int main(){
int Q,x,y;
freopen("fantasy.in","r",stdin);
freopen("fantasy.out","w",stdout);
for (int i=1;i<=n;i++) read(a[i].first),a[i].second=i;
for (int i=1;i<=n;i++) read(b[i].first),b[i].second=i;
sort(a+1,a+n+1); sort(b+1,b+n+1);
for (int i=1;i<=n;i++) li[i]=i,pos[a[i].second]=i;
Build(1,1,n);
while (Q--){
swap(li[x],li[y]);
modify(1,1,n,pos[x]);
modify(1,1,n,pos[y]);
printf("%lld\n",T[1][0][0]);
}
return 0;
}

#### CodeForces 573D Bear and Cavalry(线段树优化dp)

2015-09-03 21:41:09

#### codeforces573D. Bear and Cavalry

2015-10-23 16:43:43

#### [线段树][矩乘][DP]Codeforces Round 573D && RussianCodeCup Thanks-Round .Bear and Cavalry

2017-07-11 17:31:10

#### Codeforces 574C Bear and Poker【思维】

2017-03-23 20:36:50

#### Codeforces 573D Bear and Cavalry

2015-09-18 20:07:40

#### Codeforces 673D Bear and Two Paths【贪心】

2017-02-12 17:28:00

#### Codeforces 903D - Almost Difference

2017-12-14 22:25:52

#### codeforces 623D

2016-04-15 14:41:16

#### codeforces 163D

2017-10-01 20:03:16

#### Codeforces Round #426 (Div. 2) D. The Bakery（DP+线段树）

2017-07-31 09:16:44