[bzoj3218]a + b Problem 网络流+主席树优化建图

时间:2021-08-16 20:10:20

3218: a + b Problem

Time Limit: 20 Sec  Memory Limit: 40 MB
Submit: 2229  Solved: 836
[Submit][Status][Discuss]

Description

[bzoj3218]a + b Problem  网络流+主席树优化建图

[bzoj3218]a + b Problem  网络流+主席树优化建图

Input

 

Output

 

Sample Input

10
0 1 7 3 9 2
7 4 0 9 10 5
1 0 4 2 10 2
7 9 1 5 7 2
6 3 5 3 6 2
6 6 4 1 8 1
6 1 6 0 6 5
2 2 5 0 9 3
5 1 3 0 2 5
5 6 7 1 1 2

Sample Output

55

HINT

 

Source

此题是一个选择性问题,明显是用网络流来解决。

考虑没有p限制,这是一个最大权闭合子图问题。

从S像每一个i连一条bi的边,从i向T连一条wi的边,跑最小割即可。

对于p限制,我们要体现出要是没割wi且存在一个aj使得他在li-ri之间,他就要变得奇怪。

所以我们可以将i拆成两个点i,i+n。

从i向i+n连一条容量为pi的边,从i+n向每一个满足条件的j连一条inf的边。

由于n为5000,考虑使用主席树优化建图即可解决。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define maxm 1000005
#define maxn 5005
#define inf 999999999
using namespace std;
inline int read() {
int x=,f=;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-;
for(;isdigit(ch);ch=getchar()) x=x*+ch-'';
return x*f;
}
int ans=,n;
struct Edge {int to,nxt,c;}e[maxm*];
int head[maxn*],cnt;
inline void add(int u,int v,int c) {e[cnt].nxt=head[u];e[cnt].to=v;e[cnt].c=c;head[u]=cnt++;swap(u,v);c=;e[cnt].nxt=head[u];e[cnt].to=v;e[cnt].c=c;head[u]=cnt++;}
int a[maxn],b[maxn],w[maxn],l[maxn],r[maxn],p[maxn],hsh[maxn*],sum;
int S,T,sz;
struct seg {int l,r;}t[maxn*];
int rt[maxn*];
inline void query(int l,int r,int x,int L,int R,int point) {
if(!x||L>r||R<l) return;
if(L<=l&&R>=r) {add(point,x,inf);return;}
int mid=(l+r)>>;
if(L<=mid) query(l,mid,t[x].l,L,R,point);
if(R>mid) query(mid+,r,t[x].r,L,R,point);
return;
}
inline void insert(int l,int r,int &x,int pre,int pos,int point) {
x=++sz;
if(l==r&&pre>) add(x,pre,inf);
if(l==r) {add(x,point,inf);return;}
t[x]=t[pre];
int mid=(l+r)>>;
if(pos<=mid) insert(l,mid,t[x].l,t[pre].l,pos,point);
else insert(mid+,r,t[x].r,t[pre].r,pos,point);
if(t[x].l) add(x,t[x].l,inf);
if(t[x].r) add(x,t[x].r,inf);
}
int q[maxn*],dis[maxn*];
inline bool bfs() {
for(int i=;i<=sz;i++) dis[i]=-;
int hd=,tl=;
q[]=T;dis[T]=;
while(hd!=tl) {
int now=q[hd++];if(hd==) hd=;
for(int i=head[now];i>=;i=e[i].nxt) {
int to=e[i].to;if(dis[to]>-||!e[i^].c) continue;
dis[to]=dis[now]-;q[tl++]=to;
if(tl==) tl=;
}
}
return dis[S]>-;
}
inline int dfs(int x,int mxf) {
if(!mxf||x==T) return mxf;
int nf=;
for(int i=head[x];i>=;i=e[i].nxt) {
int to=e[i].to;
if(e[i].c&&dis[to]==dis[x]+) {
int f=dfs(to,min(mxf,e[i].c));
mxf-=f;e[i].c-=f;
e[i^].c+=f;nf+=f;
if(!mxf) break;
}
}
if(!nf) dis[x]=-;
return nf;
}
inline void dinic() {while(bfs()) ans-=dfs(S,inf);}
int main() {
memset(head,-,sizeof(head));
n=read();S=*n+,T=*n+;sz=T;
for(int i=;i<=n;i++) {
a[i]=read(),b[i]=read(),w[i]=read(),l[i]=read(),r[i]=read(),p[i]=read();
hsh[++sum]=a[i];hsh[++sum]=l[i];hsh[++sum]=r[i];
ans+=w[i]+b[i];
}
sort(hsh+,hsh+sum+);sum=unique(hsh+,hsh+sum+)-hsh-;
for(int i=;i<=n;i++) {
a[i]=lower_bound(hsh+,hsh+sum+,a[i])-hsh;
l[i]=lower_bound(hsh+,hsh+sum+,l[i])-hsh;
r[i]=lower_bound(hsh+,hsh+sum+,r[i])-hsh;
add(S,i,b[i]);add(i,i+n,p[i]);add(i,T,w[i]);
}
for(int i=;i<=n;i++) {
if(i>) query(,sum,rt[i-],l[i],r[i],i+n);
insert(,sum,rt[i],rt[i-],a[i],i);
}
dinic();printf("%d\n",ans);
}