[主席树优化建图 最小割] BZOJ 3218 a + b Problem

膜拜PoPoQQQ的题解:http://blog.csdn.net/PoPoQQQ/article/details/42557217


#include
#include
#include
#include
#define cl(x) memset(x,0,sizeof(x))
#define V G[p].v
using namespace std;
typedef long long ll;
 
inline char nc()
{
    static char buf[100000],*p1=buf,*p2=buf;
    if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
    return *p1++;
}
 
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;
}
 
namespace DINIC{
    const int N=210005;
    const int M=1001005;
    struct edge{
        int u,v,f;
        int next;
    }G[M];
    int head[N],inum=1;
    inline void add(int u,int v,int f,int p){
        G[p].u=u; G[p].v=v; G[p].f=f; G[p].next=head[u]; head[u]=p;
    }
    inline void link(int u,int v,int f){
        add(u,v,f,++inum); add(v,u,0,++inum);
    }
    int S,T;
    int dis[N];
    int Q[N],l,r;
    inline bool bfs(){
        memset(dis,-1,sizeof(dis)); l=r=-1;
        Q[++r]=S; dis[S]=1;
        while (l>1;
    if (y) link(y+T,x+T,1<<30);
    link(::cur,x+T,1<<30);
    if (l==r) return;
    if (t<=mid)
        rs[x]=rs[y],Add(ls[x],ls[y],l,mid,t);
    else
        ls[x]=ls[y],Add(rs[x],rs[y],mid+1,r,t);
}
 
inline void Query(int x,int l,int r,int L,int R){
    using namespace DINIC;
    if (!x || x==root[0]) return;
    if (L==l && r==R) {
        link(x+T,::cur,1<<30);
        return;
    }
    int mid=(l+r)>>1;
    if (R<=mid)
        Query(ls[x],l,mid,L,R);
    else if (L>mid)
        Query(rs[x],mid+1,r,L,R);
    else
        Query(ls[x],l,mid,L,mid),Query(rs[x],mid+1,r,mid+1,R);
}
 
int n;
ll ans;
 
int main()
{
    int a,b,w,l,r,p;
    using namespace DINIC;
    read(n); S=n*2+1; T=n*2+2; root[0]=++ncnt; ls[root[0]]=rs[root[0]]=root[0];
    for (int i=1;i<=n;i++)
    {
        read(a); read(b); read(w); read(l); read(r); read(p);
        link(S,i<<1,w);
        link(i<<1,T,b);
        ::cur=i<<1; Add(root[i],root[i-1],0,1000000000,a);
        ::cur=(i<<1)-1; Query(root[i-1],0,1000000000,l,r);
        link((i<<1)-1,i<<1,p);
        ans+=w+b;
    }
    ans-=Dinic();
    printf("%lld\n",ans);
    return 0;
}


你可能感兴趣的:(网络流,主席树)