[bzoj3218]a + b Problem 网络流+主席树优化建图
3218: a + b Problem
Time Limit: 20 Sec Memory Limit: 40 MB
Submit: 2229 Solved: 836
[Submit][Status][Discuss]
Description
.jpg)
.jpg)
Input
Output
Sample Input
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
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 网络流+主席树优化建图的更多相关文章
- bzoj4383 [POI2015]Pustynia 拓扑排序+差分约束+线段树优化建图
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4383 题解 暴力的做法显然是把所有的条件拆分以后暴力建一条有向边表示小于关系. 因为不存在零环 ...
- 【BZOJ4383】[POI2015]Pustynia 线段树优化建图
[BZOJ4383][POI2015]Pustynia Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息,每条信息包含三个数l,r ...
- AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图
AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图 链接 AtCoder 大意 在数轴上放上n个点,点i可能的位置有\(x_i\)或者\(y_i\ ...
- loj#2255. 「SNOI2017」炸弹 线段树优化建图,拓扑,缩点
loj#2255. 「SNOI2017」炸弹 线段树优化建图,拓扑,缩点 链接 loj 思路 用交错关系建出图来,发现可以直接缩点,拓扑统计. 完了吗,不,瓶颈在于边数太多了,线段树优化建图. 细节 ...
- bzoj3073: [Pa2011]Journeys 线段树优化建图
bzoj3073: [Pa2011]Journeys 链接 BZOJ 思路 区间和区间连边.如何线段树优化建图. 和单点连区间类似的,我们新建一个点,区间->新点->区间. 又转化成了单点 ...
- BZOJ 3073: [Pa2011]Journeys Dijkstra+线段树优化建图
复习一下线段树优化建图:1.两颗线段树的叶子节点的编号是公用的. 2.每次连边是要建两个虚拟节点 $p1,p2$ 并在 $p1,p2$ 之间连边. #include <bits/stdc++.h ...
- 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时间中选出一个时间进行抢劫,并计划抢 ...
- 【BZOJ3218】a + b Problem 可持久化线段树优化建图
[BZOJ3218]a + b Problem 题解:思路很简单,直接最小割.S->i,容量为Bi:i->T,容量为Wi:所有符合条件的j->new,容量inf:new->i, ...
- Codeforces 1045A Last chance 网络流,线段树,线段树优化建图
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1045A.html 题目传送们 - CF1045A 题意 你有 $n$ 个炮,有 $m$ 个敌人,敌人排成一 ...
随机推荐
- SSH答疑解惑系列(二)——java.lang.reflect.InvocationTargetException异常
在项目中遇到了invocationTargetException的问题,在这里跟大家分享一下. 报错信息如下: 使用反射时,比如执行invoke方法,如果被反射执行的方法体抛出了Exception,这 ...
- bootsrap 上传插件fileinput 简单使用
1.安装 下载fileinput文件,载入对应的css+js文件,如下: <link href="css/bootstrap.min.css" rel="style ...
- [剑指Offer] 9.变态跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. [思路1]每个台阶都有跳与不跳两种可能性(最后一个台阶除外),最后一个台阶必 ...
- 【题解】ZJOI2007报表统计
洛谷传送门 主要思路大概也是差不多的,对于两种询问分别用线段树与平衡树来维护. 1.MIN_SORT_GAP:显然平衡树简单操作,来一发前驱.后继即可. 2.MIN_GAP:这一个我用的是线段树:可以 ...
- [洛谷P3946] ことりのおやつ(小鸟的点心)
题目大意:最短路,第$i$个点原有积雪$h_i$,极限雪高$l_i$(即雪超过极限雪高就不可以行走),每秒降雪$q$,ことり速度为$1m/s$,若时间大于$g$,则输出$wtnap wa kotori ...
- 算法学习——kruskal重构树
kruskal重构树是一个比较冷门的数据结构. 其实可以看做一种最小生成树的表现形式. 在普通的kruskal中,如果一条边连接了在2个不同集合中的点的话,我们将合并这2个点所在集合. 而在krusk ...
- [Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LA4273 Post Offices
题目戳这里. 村庄排序.状态\(f[j][i]\)表示考虑前\(i\)个村庄,造\(j\)个邮局且\(i\)造了邮局的最小代价.我们用\(Lb_i,Rb_i\)表示在第\(i\)个村庄造邮局,邮局最左 ...
- [COGS 622] [NOIP2011] 玛雅游戏 模拟
整个模拟的关键除了打出来就是一个剪枝:对于两个左右相邻的块你不用再走←,因为走→是等效的 #include<cstdio> #include<cstring> #include ...
- Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem
E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...