【bzoj3218】 a + b Problem
http://www.lydsy.com/JudgeOnline/problem.php?id=3218 (题目链接)
题意
给${n}$个格子涂白或黑色,白则${w_i}$,黑则${b_i}$的好看度,若对于黑格${i}$存在:${白格j,使得1 \leq j<i,l_i \leq a_j \leq r_i}$,则损失${p_i}$,问最大的好看度。
Solution
不会。。上题解:PoPoQQQ
指针的主席树看得我眼泪掉下来啊T_T,完全不会指针啊T_T
细节
当主席树建树递归到最后一层时,记得将之前的${i-1}$号主席树上连向该节点的边继承过来。(不好描述,已在代码中标记)
代码
- // bzoj3218
- #include<algorithm>
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- #include<cstdio>
- #include<cmath>
- #include<queue>
- #define LL long long
- #define inf 2147483640
- #define Pi acos(-1.0)
- #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
- using namespace std;
- const int maxn=5010,maxm=1000010;
- struct edge {int to,next,w;}e[maxm];
- struct Segtree {int ls,rs;}tr[maxm];
- int head[maxm],d[maxm];
- int cnt=1,es,et;
- int n,L[maxn],R[maxn],a[maxn],tmp[maxm];
- int size,T[maxm];
- LL ans;
- void link(int u,int v,int w) {
- e[++cnt]=(edge){v,head[u],w};head[u]=cnt;
- e[++cnt]=(edge){u,head[v],0};head[v]=cnt;
- }
- bool bfs() {
- memset(d,-1,sizeof(d));
- queue<int> q;q.push(es);d[es]=0;
- while (!q.empty()) {
- int x=q.front();q.pop();
- for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]<0) {
- d[e[i].to]=d[x]+1;
- q.push(e[i].to);
- }
- }
- return d[et]>0;
- }
- int dfs(int x,int f) {
- if (x==et || f==0) return f;
- int w,used=0;
- for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]==d[x]+1) {
- w=dfs(e[i].to,min(e[i].w,f-used));
- used+=w;
- e[i].w-=w;e[i^1].w+=w;
- if (used==f) return used;
- }
- if(!used) d[x]=-1;
- return used;
- }
- void Dinic() {
- while (bfs())
- ans-=dfs(es,inf);
- }
- void insert(int k,int l,int r,int i) {
- if (l>R[i] || r<L[i]) return;
- if (L[i]<=l && r<=R[i]) {link(k+et,n+i,inf);return;}
- int mid=(l+r)>>1;
- if (tr[k].ls) insert(tr[k].ls,l,mid,i);
- if (tr[k].rs) insert(tr[k].rs,mid+1,r,i);
- }
- void build(int &rt,int k,int l,int r,int i) {
- rt=++size;
- if (l==r) {
- link(i,rt+et,inf);
- if (k) link(k+et,rt+et,inf); //细节
- return;
- }
- int mid=(l+r)>>1;
- if (a[i]<=mid) tr[rt].rs=tr[k].rs,build(tr[rt].ls,tr[k].ls,l,mid,i);
- else tr[rt].ls=tr[k].ls,build(tr[rt].rs,tr[k].rs,mid+1,r,i);
- if (tr[rt].ls) link(tr[rt].ls+et,rt+et,inf);
- if (tr[rt].rs) link(tr[rt].rs+et,rt+et,inf);
- }
- int main() {
- scanf("%d",&n);
- es=2*n+1;et=es+1;
- int tot=0;
- for (int b,w,p,i=1;i<=n;i++) {
- scanf("%d%d%d%d%d%d",&a[i],&b,&w,&L[i],&R[i],&p);
- tmp[++tot]=a[i];tmp[++tot]=L[i];tmp[++tot]=R[i];
- link(es,i,w);
- link(i,et,b);
- link(i+n,i,p);
- ans+=w+b;
- }
- sort(tmp+1,tmp+1+tot);
- tot=unique(tmp+1,tmp+1+tot)-tmp-1;
- for (int i=1;i<=n;i++) {
- a[i]=lower_bound(tmp+1,tmp+1+tot,a[i])-tmp;
- L[i]=lower_bound(tmp+1,tmp+1+tot,L[i])-tmp;
- R[i]=lower_bound(tmp+1,tmp+1+tot,R[i])-tmp;
- if (i>1) insert(T[i-1],1,tot,i);
- build(T[i],T[i-1],1,tot,i);
- }
- Dinic();
- printf("%lld",ans);
- return 0;
- }
【bzoj3218】 a + b Problem的更多相关文章
- 【BZOJ3218】a + b Problem 可持久化线段树优化建图
[BZOJ3218]a + b Problem 题解:思路很简单,直接最小割.S->i,容量为Bi:i->T,容量为Wi:所有符合条件的j->new,容量inf:new->i, ...
- 【BZOJ-3218】a+b Problem 最小割 + 可持久化线段树
3218: a + b Problem Time Limit: 20 Sec Memory Limit: 40 MBSubmit: 1320 Solved: 498[Submit][Status] ...
- 【bzoj3218】a+b Problem 最小割+主席树
数据范围:$n≤5000$,$a,l,r≤10^9$,$b,w,p≤2\times 10^5$. 我们考虑一种暴力的最小割做法: 首先令$sum=\sum\limits_{i=1}^{n} b_i+w ...
- 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划
[BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...
- 洛谷P1919 【模板】A*B Problem升级版 题解(FFT的第一次实战)
洛谷P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 刚学了FFT,我们来刷一道模板题. 题目描述 给定两个长度为 n 的两个十进制数,求它们的乘积. n<=100000 如 ...
- 【BZOJ1000】A+B Problem ★BZOJ1000题达成★
[BZOJ1000]A+B Problem Description 输入两个数字,输出它们之和 Input 一行两个数字A,B(0<=A,B<100) Output 输出这两个数字之和 S ...
- 【题解】CF45G Prime Problem
[题解]CF45G Prime Problem 哥德巴赫板子题? \(\frac{n(n+1)}{2}\)若是质数,则不需要分了. 上式 若是奇数,那么拆成2和另一个数. 上式 若是偶数吗,直接\(O ...
- 【题解】P4137 Rmq Problem(莫队)
[题解]P4137 Rmq Problem(莫队) 其实这道题根本就不用离散化! 因为显然有\(mex\)值是\(\le 2\times 10^5\)的,所以对于大于\(2\times 10^5\)的 ...
- 【HDU1402】【FFT】A * B Problem Plus
Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to e ...
随机推荐
- SPM FDR校正
来源: http://blog.sciencenet.cn/blog-479412-572049.html,http://52brain.com/thread-15512-1-1.html SPM8允 ...
- 添加Distributor失败
上周做了一个case,客户无法为SQL Server instance配置remote distributor. 下面分享一下排查问题的过程,希望对您排查类似的问题所有帮助. 客户的环境中的SQL S ...
- Windows Phone App Studio 无码开发手机应用
上周微软发布了一款基于Web的Windows Phone应用开发工具 "Windows Phone App Studio".它与大家熟知Visual Studio的最大不同之处是W ...
- Generate Parentheses
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- codevs3143 二叉树的序遍历
难度等级:白银 3143 二叉树的序遍历 题目描述 Description 求一棵二叉树的前序遍历,中序遍历和后序遍历 输入描述 Input Description 第一行一个整数n,表示这棵树的节点 ...
- ASP.NET MVC 教程-MVC简介
ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务器脚本创建网页和网站的开发框架. ASP.NET 支持三种不同的开发模式:Web Pages(Web 页面).MVC(Mode ...
- 学习Python的三种境界
前言 王国维在<人间词话>中将读书分为了三种境界:"古今之成大事业.大学问者,必经过三种之境界:'昨夜西风凋碧树,独上高楼,望尽天涯路'.此第一境也.'衣带渐宽终不悔,为伊消得人 ...
- 订餐系统之定时器Timer不定时
经过几天漫长的问题分析.处理.测试.验证,定时器Timer终于定时了,于是开始了这篇文章,希望对还在纠结于“定时器Timer不定时”的同学有所帮助,现在的方案,在系统日志中会有警告,如果您有更好的方案 ...
- 链接错误-库冲突(libcmt.lib和libcmtd.lib)
在同一个项目中,所有的源文件必须链接相同的C运行时库.如果某一文件用了Multithreaded DLL版本,而其他文件用了Single-Threaded或者Multithreaded版本的库,也就是 ...
- 【Zeyphr】保存json到数据库
方法一: public int SaveJob(JObject data) { var formWrapper = RequestWrapper.Instance().LoadSettingXmlSt ...