标签:网络流,树
题目
分析
小数据可以费用流来做,动态加遍,每次都要做一次增广
其实可以直接在树结构上维护残留网络,对每个节点都保存从根往下走能增广的费用最小的点,增广的时候枚举往上走了几步到LCA即可
不停修改增光路的边,并往上更新其祖先节点的子树信息
code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
#define lson (x<<1)
#define rson (x<<1|1)
#define mid ((l+r)>>1)
#define pi acos(-1)
#define mp make_pair
#define pb push_back
#define pa pair<int,int>
#define fi first
#define se second
#define lowbit(x) ((x)&(-x))
using namespace std;
inline ll read(){
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline ll getgcd(ll x,ll y){return !x?y:getgcd(y%x,x);}
inline ll getlcm(ll x,ll y){return x/getgcd(x,y)*y;}
inline ll qpow(int x,int y,int p){
ll re=1;
while(y){
if(y&1)re=(re*x)%p;
y>>=1,x=(x*x)%p;
}
return re;
}
//**********head by yjjr**********
#define inf 1e9
const int maxn=2e5+6;
int n,m,cap[maxn],pos[maxn],oc[maxn],id[maxn],ct[maxn],md[maxn],ans=0;
inline int gu(int x){return ct[x]>0?-1:1;}
inline int gd(int x){return ct[x]<0?-1:1;}
inline int Min(int &x,int y){return x>y?x=y,1:0;}
void update(int x){
if(cap[x])md[x]=0,id[x]=x;else md[x]=inf,id[x]=0;
if(lson<=n&&Min(md[x],md[lson]+gd(lson)))id[x]=id[lson];
if(rson<=n&&Min(md[x],md[rson]+gd(rson)))id[x]=id[rson];
}
inline int lca(int x,int y){while(x!=y){if(x<y)y>>=1;else x>>=1;}return x;}
int main()
{
n=read(),m=read();
rep(i,1,n)cap[i]=read();
rep(i,1,m)pos[i]=read();
rep(i,1,n)
if((i<<1)<n)oc[i]=1;else if((i<<1)==n)oc[i]=n;
dep(i,n,1)update(i);
rep(i,1,m){
int x=pos[i],y=id[x],t=md[x];
for(int p=x>>1,q=x^oc[x>>1],r=gu(x);p;r+=gu(p),q=p^oc[p>>1],p>>=1){
if(cap[p]&&Min(t,r))y=p;
if(q&&Min(t,md[q]+gd(q)+r))y=id[q];
}
ans+=t;
printf("%d ",ans);cap[y]--;update(y);
int z=lca(x,y);
for(t=x;t!=z;t>>=1)ct[t]--,update(t>>1);
for(t=y;t!=z;t>>=1)ct[t]++,update(t>>1);
for(t=z;t!=1;t>>=1)update(t>>1);
}
return 0;
}