[UOJ] #217. 【UNR #1】奇怪的线段树

题解见大佬博客

我的丑陋代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
inline int read()
{
int x;char c;
while((c=getchar())<''||c>'');
for(x=c-'';(c=getchar())>=''&&c<='';)x=x*+c-'';
return x;
}
#define MN 8000
#define MV 16000
#define ME 56000
#define INF 0x3FFFFFFF
struct edge{int nx,t,l,w;}e[ME*+];
int S=MV+,T=MV+,h[MV+],en=,d[MV+],q[MV+],qn,c[MV+];
int cnt=,L[MN+],R[MN+],u[MN+],z[MN+],fa[MN+];
inline void ins(int x,int y,int l,int w)
{
e[++en]=(edge){h[x],y,l,w};h[x]=en;
e[++en]=(edge){h[y],x,,};h[y]=en;
}
bool build(int k,int l,int r)
{
u[k]=read();
if(k>&&u[k]&&!u[fa[k]])exit(*puts("OwO"));
if(l<r)
{
int mid;
fa[L[k]=++cnt]=k;build(L[k],l,mid=read());
fa[R[k]=++cnt]=k;build(R[k],mid+,r);
z[L[k]]=R[k];
ins(k,L[k],,INF);
}
L[k]=l;R[k]=r;
}
bool bfs()
{
int i,j;
memset(d,,sizeof(d));
for(d[q[i=qn=]=S]=;i<=qn;++i)for(j=c[q[i]]=h[q[i]];j;j=e[j].nx)
if(e[j].w&&!d[e[j].t])d[q[++qn]=e[j].t]=d[q[i]]+;
return d[T];
}
int dfs(int x,int r)
{
if(x==T)return r;
int k,u=;
for(int&i=c[x];i;i=e[i].nx)if(e[i].w&&d[e[i].t]==d[x]+)
{
k=dfs(e[i].t,r-u<e[i].w?r-u:e[i].w);
u+=k;e[i].w-=k;e[i^].w+=k;
if(u==r)return u;
}
return d[x]=,u;
}
int main()
{
int n=read(),i,j;
build(,,n);
for(i=;i<*n;++i)if(u[i])
{
ins(S,i,,INF);
for(j=i;++j<*n;)if(L[i]<=L[j]&&R[i]>=R[j]&&u[j])u[i]=;
ins(i,i+*n,u[i],INF);
ins(i+*n,T,,INF);
for(j=i;++j<*n;)if(R[i]+==L[j]&&z[i]!=j){ins(i+*n,j,,INF);break;}
}
for(i=;i<=T;++i)for(j=h[i];j;j=e[j].nx)d[i]-=e[j].l,d[e[j].t]+=e[j].l,e[j].w-=e[j].l;
for(S+=,T+=,i=;i<S;++i)
{
if(d[i]<)ins(i,T,,-d[i]);
if(d[i]>)ins(S,i,,d[i]);
}
while(bfs())dfs(S,INF);
ins(T-,S-,,INF);
while(bfs())dfs(S,INF);
for(i=h[S-];i;i=e[i].nx)if(e[i].t==T-)printf("%d",e[i].w);
}
上一篇:oracle疑难杂症问题


下一篇:window.open参数设置及如何全屏显示(转)