<span style="font-size: 1em; line-height: 1.4em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgb(255, 255, 255);">Would you want to fight against bears riding horses? Me neither.</span>
Limak is a grizzly bear. He is general of the dreadful army of Bearland. The most important part of an army is cavalry of course.
Cavalry of Bearland consists of n warriors and n horses. i-th warrior has strength wi and i-th horse has strength hi. Warrior together with his horse is called a unit. Strength of a unit is equal to multiplied strengths of warrior and horse. Total strength of cavalry is equal to sum of strengths of all n units. Good assignment of warriors and horses makes cavalry truly powerful.
Initially, i-th warrior has i-th horse. You are given q queries. In each query two warriors swap their horses with each other.
General Limak must be ready for every possible situation. What if warriors weren't allowed to ride their own horses? After each query find the maximum possible strength of cavalry if we consider assignments of all warriors to all horses that no warrior is assigned to his own horse (it can be proven that for n ≥ 2 there is always at least one correct assignment).
Note that we can't leave a warrior without a horse.
The first line contains two space-separated integers, n and q (2 ≤ n ≤ 30 000, 1 ≤ q ≤ 10 000).
The second line contains n space-separated integers, w1, w2, ..., wn (1 ≤ wi ≤ 106) — strengths of warriors.
The third line contains n space-separated integers, h1, h2, ..., hn (1 ≤ hi ≤ 106) — strengths of horses.
Next q lines describe queries. i-th of them contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi), indices of warriors who swap their horses with each other.
Print q lines with answers to queries. In i-th line print the maximum possible strength of cavalry after first i queries.
4 2 1 10 100 1000 3 7 2 5 2 4 2 4
5732 7532
3 3 7 11 5 3 2 1 1 2 1 3 2 3
44 48 52
7 4 1 2 4 8 16 32 64 87 40 77 29 50 11 18 1 5 2 7 6 2 5 6
9315 9308 9315 9315
Clarification for the first sample:
Horses: 3 7 2 5
After first query situation looks like the following:
Horses: 3 5 2 7
We can get 1·2 + 10·3 + 100·7 + 1000·5 = 5732 (note that no hussar takes his own horse in this assignment).
After second query we get back to initial situation and optimal assignment is 1·2 + 10·3 + 100·5 + 1000·7 = 7532.
Clarification for the second sample. After first query:
Horses: 2 3 1
Optimal assignment is 7·1 + 11·2 + 5·3 = 44.
Then after second query 7·3 + 11·2 + 5·1 = 48.
Finally 7·2 + 11·3 + 5·1 = 52.
题意:给你n个人n匹马,都有各自的价值,人骑马的价值之和等于人的价值乘以马的价值,唯一的要求是第i个人不能骑第i匹马。求最大价值。中间还有交换。
做法:假如没有第i个人不能骑第i匹马的限制的话。那么直接sort一发按顺序匹配就好了。但是有了这个限制之后。按顺序匹配的过程中就需要有交换。因为这个交换不会换到距离超过2的位置。所以直接暴力处理就好。把所有的状态用数字表示出来。建一个二维矩阵用线段树来搞。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <time.h>
#include <vector>
#include <stack>
using namespace std;
#define inf 99999999999999999LL
__int64 c[100000][6][6];
pair<__int64 ,int>war[33333],hor[33333];
int n,q,h[33333],rr[33333];
void run(__int64 a[6][6],int pos)
{
__int64 e[6];
int o=h[war[pos].second];
e[1]=(pos>2&&o!=hor[pos-2].second)?war[pos].first*hor[pos-2].first:-inf;
e[2]=(pos>1&&o!=hor[pos-1].second)?war[pos].first*hor[pos-1].first:-inf;
e[3]=(pos>0&&o!=hor[pos].second)?war[pos].first*hor[pos].first:-inf;
e[4]=(pos<n&&o!=hor[pos+1].second)?war[pos].first*hor[pos+1].first:-inf;
e[5]=(pos<n-1&&o!=hor[pos+2].second)?war[pos].first*hor[pos+2].first:-inf;
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
a[i][j]=-inf;
a[0][0]=e[3]; a[0][1]=e[4]; a[0][2]=e[5];
a[1][0]=e[2]; a[1][3]=e[4]; a[1][4]=e[5];
a[2][1]=e[2]; a[2][3]=e[3]; a[2][5]=e[5];
a[3][0]=e[1]; a[4][1]=e[1]; a[5][3]=e[1];
}
void push_up(__int64 a[6][6],__int64 b[6][6],__int64 d[6][6])
{
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
a[i][j]=-inf;
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
for(int k=0;k<6;k++)
{
a[i][j]=max(a[i][j],b[i][k]+d[k][j]);
}
}
void build(int t,int l,int r)
{
int e[6];
if(l==r)
{
run(c[t],l);
return ;
}
int mid=l+r>>1;
build(t<<1,l,mid);
build(t<<1|1,mid+1,r);
push_up(c[t],c[t<<1],c[t<<1|1]);
}
void update(int t,int l,int r,int pos)
{
if(l==r)
{
run(c[t],l);
return ;
}
int mid=l+r>>1;
if(pos<=mid)update(t<<1,l,mid,pos);
else update(t<<1|1,mid+1,r,pos);
push_up(c[t],c[t<<1],c[t<<1|1]);
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
{
scanf("%I64d",&war[i].first);
war[i].second=i;
}
for(int i=1;i<=n;i++)
{
scanf("%I64d",&hor[i].first);
hor[i].second=i;
h[i]=i;
}
sort(war+1,war+1+n);
sort(hor+1,hor+1+n);
for(int i=1;i<=n;i++)
{
rr[war[i].second]=i;
}
build(1,1,n);
for(int i=1;i<=q;i++)
{
int u,v;
scanf("%d%d",&u,&v);
swap(h[u],h[v]);
if(u!=v)
{
update(1,1,n,rr[u]);
update(1,1,n,rr[v]);
}
printf("%I64d\n",c[1][0][0]);
}
}