膜拜PoPoQQQ的题解:http://blog.csdn.net/PoPoQQQ/article/details/42557217
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#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<r){
int u=Q[++l];
for (int p=head[u];p;p=G[p].next)
if (G[p].f && dis[V]==-1){
dis[V]=dis[u]+1; Q[++r]=V;
if (V==T) return 1;
}
}
return 0;
}
int cur[N];
ll dfs(int u,ll flow){
if (u==T) return flow;
ll used=0,now;
for (int p=cur[u];p;p=G[p].next)
{
cur[u]=p;
if (G[p].f && dis[V]==dis[u]+1)
{
now=dfs(V,min(flow-used,(ll)G[p].f));
G[p].f-=now,G[p^1].f+=now;
used+=now; if (flow==used) break;
}
}
if (!used) dis[u]=-1;
return used;
}
ll Dinic(){
ll ret=0;
while (bfs())
memcpy(cur,head,sizeof(cur)),ret+=dfs(S,1LL<<60);
return ret;
}
}
const int N=5005,M=1000005;
int root[N],ncnt;
int ls[M],rs[M];
int cur;
inline void Add(int &x,int y,int l,int r,int t){
using namespace DINIC;
x=++ncnt; int mid=(l+r)>>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;
}