树状数组简单粗暴,快速,简洁
x&-x 是x的二进制表示最后数起第一个1的位置
核心代码:
求和函数:
int getsum(int x)
{
int ans=0;
while(x)
{
ans+=sum[x];
x-=x&-x
}
return ans;
}
更新值函数
void add(int x,int w)
{
while(x<=n)
{
sum[x]+=w;
x+=x&-x;
}
}
注意点:
1. int 可能会溢出
2.最好数组从下表1开始
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
网络流:
与网络流有关的算法:
ek算法核心代码:
说明:
采用bfs增广
const int MAXN = 10000;
const int inf = 0x3f3f3f3f;
int maxflow;
flow[MAXN];
void bfs()
{
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
q.push(s);
flow[s]=inf;
vis[s]=1;
int u;
while(q.size()&&vis[f]==0)
{
u=q.top();
q.pop();
for(v=1;v<=n;v++)
{
if(vis[v]==0&&a[u][v])
{
flow[v]=min(flow[u],a[u][v]);
pre[v]=u;
}
}
}
return ;
}
void max_flow()
{
maxflow=0;
while(1)
{
bfs();
if(flow[f]==inf)
{
break;
}
int t=f;
while(pre[t])
{
a[pre[t]][t]-=flow[f];
a[t][pre[t]]+=flow[f];
}
maxflow+=flow[f];
}
}