【BZOJ-3218】a+b Problem 最小割 + 可持久化线段树
3218: a + b Problem
Time Limit: 20 Sec Memory Limit: 40 MB
Submit: 1320 Solved: 498
[Submit][Status][Discuss]
Description
.jpg)
.jpg)
Input
Output
Sample Input
Sample Output
HINT
Source
Solution
这道题非常吼啊!!
最小割的解法想一想还是比较容易想到的。
最大化价值的方法可以看成总和减去最小化损失。
暴力的解法就是$<S,i,b[i]>$割掉表示选白色,$<i',T,w[i]>$割掉表示选黑色,$<i,i',p[i]>$表示如果$i$和$i'$不同则付出代价,$<i',j,INF>$表示和$j$是不会被割的。
这样直接求出$mincut$,然后答案就是$\sum(b[i]+w[i])-mincut$。
但是这样的边数是$N^{2}$的,最后会被卡,所以要去优化。
这类情况的优化比较明显的就是影响的范围是一段区间,所以比较常见的是用线段树的节点去表示对一个区间的影响,来达到优化的目的,即连$<i',seg[l,r],INF>$。
首先肯定是线段树中的$<fa,son[0/1],INF>$,这里叶子节点直接连所对应的点,即$<leaf_{i},i,INF>$。
对于$1<=j<i$,可以考虑把普通的线段树换成可持久化线段树即可。
这里有一个问题就是$a[i]$用同一个叶子节点表示的会出现重复,所以需要新版本的叶子连向老版本的叶子$<new,old,INF>$
这样边数和点数都是$NlogN$的,就可以通过了。
搞起来有点蛋疼...思路清晰的话不是很难写。
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
inline int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} #define MAXN 100010
#define MAXM 750010 int N,a[MAXN],b[MAXN],w[MAXN],l[MAXN],r[MAXN],p[MAXN],ans;
struct EdgeNode{
int next,to,cap;
}edge[MAXM<<1];
int head[MAXN],cnt=1;
inline void AddEdge(int u,int v,int w) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v; edge[cnt].cap=w;}
inline void InsertEdge(int u,int v,int w) {/*printf("<%d %d>\n",u,v);*/ AddEdge(u,v,w); AddEdge(v,u,0);} #define INF 0x7fffffff
int h[MAXN],S,T,cur[MAXN];
inline bool bfs()
{
queue<int>q;
for (int i=S; i<=T; i++) h[i]=-1;
h[S]=1; q.push(S);
while (!q.empty())
{
int now=q.front(); q.pop();
for (int i=head[now]; i; i=edge[i].next)
if (h[edge[i].to]==-1 && edge[i].cap)
h[edge[i].to]=h[now]+1,q.push(edge[i].to);
}
return h[T]!=-1;
} inline int dfs(int loc,int low)
{
if (loc==T) return low;
int used=0,w;
for (int i=cur[loc]; i; i=edge[i].next)
if (edge[i].cap && h[edge[i].to]==h[loc]+1)
{
w=dfs(edge[i].to,min(edge[i].cap,low-used));
edge[i].cap-=w; edge[i^1].cap+=w; used+=w;
if (used==low) return low;
if (edge[i].to) cur[loc]=i;
}
if (!used) h[loc]=-1;
return used;
} int Dinic()
{
int tmp=0;
while (bfs())
{
for (int i=S; i<=T; i++) cur[i]=head[i];
tmp+=dfs(S,INF);
}
return tmp;
} namespace PrTree{
int sz,lson[MAXM],rson[MAXM],root[MAXN];
inline void Insert(int l,int r,int &x,int y,int pos,int id)
{
x=++sz;
if (l==r) {
InsertEdge(2*N+x,id,INF);
if (y) InsertEdge(2*N+x,2*N+y,INF);
return;
}
int mid=(l+r)>>1;
lson[x]=lson[y]; rson[x]=rson[y];
if (pos<=mid) Insert(l,mid,lson[x],lson[y],pos,id);
else Insert(mid+1,r,rson[x],rson[y],pos,id);
if (lson[x]) InsertEdge(2*N+x,2*N+lson[x],INF);
if (rson[x]) InsertEdge(2*N+x,2*N+rson[x],INF);
} inline void Query(int l,int r,int L,int R,int now,int id)
{
if (!now) return;
if (L<=l && R>=r) {
InsertEdge(id,2*N+now,INF);
return;
}
int mid=(l+r)>>1;
if (L<=mid) Query(l,mid,L,R,lson[now],id);
if (R>mid) Query(mid+1,r,L,R,rson[now],id);
} }using namespace PrTree; int ls[MAXN],tot;
int main()
{
N=read();
for (int i=1; i<=N; i++) a[i]=read(),b[i]=read(),w[i]=read(),l[i]=read(),r[i]=read(),p[i]=read(),ans+=w[i]+b[i];
for (int i=1; i<=N; i++) ls[++tot]=a[i],ls[++tot]=l[i],ls[++tot]=r[i]; sort(ls+1,ls+tot+1); tot=unique(ls+1,ls+tot+1)-ls-1; for (int i=1; i<=N; i++) a[i]=lower_bound(ls+1,ls+tot+1,a[i])-ls,l[i]=lower_bound(ls+1,ls+tot+1,l[i])-ls,r[i]=lower_bound(ls+1,ls+tot+1,r[i])-ls; S=0; for (int i=1; i<=N; i++) {
PrTree::Insert(1,tot,root[i],root[i-1],a[i],i);
PrTree::Query(1,tot,l[i],r[i],root[i-1],i+N);
} T=2*N+sz+1; for (int i=1; i<=N; i++) InsertEdge(S,i,b[i]),InsertEdge(i,T,w[i]),InsertEdge(i,i+N,p[i]); printf("%d\n",ans-Dinic());
return 0;
}
【BZOJ-3218】a+b Problem 最小割 + 可持久化线段树的更多相关文章
- 【BZOJ 3218】 3218: a + b Problem(最小割+可持久化线段树)
3218: a + b Problem Time Limit: 20 Sec Memory Limit: 40 MBSubmit: 1440 Solved: 545 Description Inp ...
- BZOJ.3218.a + b Problem(最小割ISAP 可持久化线段树优化建图)
BZOJ UOJ 首先不考虑奇怪方格的限制,就是类似最大权闭合子图一样建图. 对于奇怪方格的影响,显然可以建一条边\((i\to x,p_i)\),然后由\(x\)向\(1\sim i-1\)中权值在 ...
- BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 9280 Solved: 2421 ...
- bzoj 4408: [Fjoi 2016]神秘数 数学 可持久化线段树 主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=4299 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1 ...
- 【BZOJ 3524】【Poi2014】Couriers 可持久化线段树
为什么这个主席树叫可持久化线段树,我不知道,具体得问达神.我无限T,然后DaD3zZ一针见血地指出了我的N*50爆内存导致无限编译超时O)ZO)ZO)Z真是太神啦.以图为鉴: 达神题解传送门:http ...
- BZOJ 3207: 花神的嘲讽计划Ⅰ( hash + 可持久化线段树 )
O(NK)暴力搞出所有子串的哈希值, 然后就对哈希值离散化建权值线段树, 就是主席树的经典做法了.总时间复杂度O(NK+(N+Q)logN) ----------------------------- ...
- bzoj 3218 a + b Problem(最小割+主席树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3218 [题意] 给n个格子涂白或黑色,白则wi,黑则bi的好看度,若黑格i存在: 1& ...
- [BZOJ 3218] A + B Problem 【可持久化线段树 + 网络流】
题目连接:BZOJ - 3218 题目分析 题目要求将 n 个点染成黑色或白色,那么我们可以转化为一个最小割模型. 我们规定一个点 i 最后属于 S 集表示染成黑色,属于 T 集表示染成白色,那么对于 ...
- UOJ#77. A+B Problem [可持久化线段树优化建边 最小割]
UOJ#77. A+B Problem 题意:自己看 接触过线段树优化建图后思路不难想,细节要处理好 乱建图无果后想到最小割 白色和黑色只能选一个,割掉一个就行了 之前选白色必须额外割掉一个p[i], ...
随机推荐
- [转载]详解主流浏览器多进程架构:Chrome、IE
http://www.cnbeta.com/articles/109595.htm 随着Web浏览器重要性的日益突出,恶意软件.木马.间谍软件等网络攻击也呈现逐渐的上升.而面对 如此众多的潜在威胁,为 ...
- Guava HashMultiset(MultiSet)
multiset:多重集合,和set唯一的不同是 set 集合中一个值只能出现一次,而multiset多重集合中一个值可以出现多次.一个典型的应用就是统计单词出现次数 举例: public class ...
- [译]How To Use the Linux Auditing System on CentOS 7
本文是How To Use the Linux Auditing System on CentOS 7的中文版,翻译不到之处,还请指出和多多包涵.本文并不会完全遵从原文的一些格式,而是加入自己学习的理 ...
- Oracle 修改用户名
1.开始- 运行 - 输入“CMD” 确定 2.在弹出来的窗口中,输入:SQLPLUS / AS SYSDBA 回车 3.然后,用命令修改:alter user 用户名 identified by ...
- jquery的几种ajax方式对比
jquery的几种ajax方式对比 jquery的ajax方式有如下几种: 1. $.post(url,params,callback); 2. $.getJSON(url,params,ca ...
- Wordpress页脚
<?php /** * The template for displaying the footer */ ?> <?php if ( apply_filters( 'show_fl ...
- rtop:一个通过 SSH 监控远程主机的交互式工具【转】
编译自: http://www.tecmint.com/rtop-monitor-remote-linux-server-over-ssh/ rtop[1] 是一个基于 SSH 的直接的交互式远程系统 ...
- mysql-5.7.13-winx64 免安装版配置方法
1.下载MySQL Community Server 5.7.13 解压MySQL压缩包 2.修改 my-default.ini skip-grant-tables 作用是安装好MySQL后 免密码登 ...
- Java流程控制练习--万年历
Java流程控制练习--万年历 标签: Java入坑之旅 0x01. 打印倒三角和正三角 public static void main(String[] args) { // TODO Auto-g ...
- High-Speed Tracking with Kernelized Correlation Filters
2015年的一篇论文,可参考:http://blog.csdn.net/carrierlxksuper/article/details/46461245. 另参考:http:// ...