3218: a + b Problem

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

Description

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);
}

[bzoj3218]a + b Problem 网络流+主席树优化建图的更多相关文章

  1. bzoj4383 [POI2015]Pustynia 拓扑排序+差分约束+线段树优化建图

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4383 题解 暴力的做法显然是把所有的条件拆分以后暴力建一条有向边表示小于关系. 因为不存在零环 ...

  2. 【BZOJ4383】[POI2015]Pustynia 线段树优化建图

    [BZOJ4383][POI2015]Pustynia Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息,每条信息包含三个数l,r ...

  3. AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图

    AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图 链接 AtCoder 大意 在数轴上放上n个点,点i可能的位置有\(x_i\)或者\(y_i\ ...

  4. loj#2255. 「SNOI2017」炸弹 线段树优化建图,拓扑,缩点

    loj#2255. 「SNOI2017」炸弹 线段树优化建图,拓扑,缩点 链接 loj 思路 用交错关系建出图来,发现可以直接缩点,拓扑统计. 完了吗,不,瓶颈在于边数太多了,线段树优化建图. 细节 ...

  5. bzoj3073: [Pa2011]Journeys 线段树优化建图

    bzoj3073: [Pa2011]Journeys 链接 BZOJ 思路 区间和区间连边.如何线段树优化建图. 和单点连区间类似的,我们新建一个点,区间->新点->区间. 又转化成了单点 ...

  6. BZOJ 3073: [Pa2011]Journeys Dijkstra+线段树优化建图

    复习一下线段树优化建图:1.两颗线段树的叶子节点的编号是公用的. 2.每次连边是要建两个虚拟节点 $p1,p2$ 并在 $p1,p2$ 之间连边. #include <bits/stdc++.h ...

  7. BZOJ 4276 [ONTAK2015]Bajtman i Okrągły Robin 费用流+线段树优化建图

    Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1],[a[i]+1,a[i]+2],...,[b[i]-1,b[i]]这么多段长度为1时间中选出一个时间进行抢劫,并计划抢 ...

  8. 【BZOJ3218】a + b Problem 可持久化线段树优化建图

    [BZOJ3218]a + b Problem 题解:思路很简单,直接最小割.S->i,容量为Bi:i->T,容量为Wi:所有符合条件的j->new,容量inf:new->i, ...

  9. Codeforces 1045A Last chance 网络流,线段树,线段树优化建图

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1045A.html 题目传送们 - CF1045A 题意 你有 $n$ 个炮,有 $m$ 个敌人,敌人排成一 ...

随机推荐

  1. 一种保持顺序的Properties

    其实properties有没有顺序都一样 程序都能正常运行 但看着就比较闹心 所以网上找了找 还真有人给了个例子实现读Property的有序 但是删除某些属性之后 写入又有问题 会异常 后来重写了一下 ...

  2. 玩lua

    https://my.oschina.net/wangxuanyihaha/blog/186401

  3. web online ide &web online editor & web online playground & web online runtime

    web online ide &web online editor web online ide &web online editor & web online playgro ...

  4. sql语句编写 有时候一个子查询可以拆分成多个子查询

    sql语句编写 有时候一个子查询可以拆分成多个子查询

  5. 2018牛客多校第三场 C.Shuffle Cards

    题意: 给出一段序列,每次将从第p个数开始的s个数移到最前面.求最终的序列是什么. 题解: Splay翻转模板题.存下板子. #include <bits/stdc++.h> using ...

  6. JSOI2004 平衡点 / 吊打XXX [模拟退火]

    题目描述 如图:有n个重物,每个重物系在一条足够长的绳子上.每条绳子自上而下穿过桌面上的洞,然后系在一起.图中X处就是公共的绳结.假设绳子是完全弹性的(不会造成能量损失),桌子足够高(因而重物不会垂到 ...

  7. codeforces 1060 B

    https://codeforces.com/contest/1060/problem/B 题意:给你一个数C ,你要找到两个数A.B,使得A+B=C并且A的每个位的数的和最大,求最大的和是多少 题解 ...

  8. TestRedis

    import org.junit.Before; import org.junit.Test; import redis.clients.jedis.Jedis; import java.util.H ...

  9. macos装多个python

    简介 Mac包管理工具brew: 安装方法:命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Ho ...

  10. NGINX: 配置跨域请求

    说明: 内容全部来自 SegmentFault Developer Nginx 配置跨域请求 跨域请求失败, nginx 报错: 403 No 'Access-Control-Allow-Origin ...