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}$号主席树上连向该节点的边继承过来。(不好描述,已在代码中标记)

代码

  1. // bzoj3218
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstdlib>
  5. #include<cstring>
  6. #include<cstdio>
  7. #include<cmath>
  8. #include<queue>
  9. #define LL long long
  10. #define inf 2147483640
  11. #define Pi acos(-1.0)
  12. #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
  13. using namespace std;
  14.  
  15. const int maxn=5010,maxm=1000010;
  16. struct edge {int to,next,w;}e[maxm];
  17. struct Segtree {int ls,rs;}tr[maxm];
  18. int head[maxm],d[maxm];
  19. int cnt=1,es,et;
  20. int n,L[maxn],R[maxn],a[maxn],tmp[maxm];
  21. int size,T[maxm];
  22. LL ans;
  23.  
  24. void link(int u,int v,int w) {
  25. e[++cnt]=(edge){v,head[u],w};head[u]=cnt;
  26. e[++cnt]=(edge){u,head[v],0};head[v]=cnt;
  27. }
  28. bool bfs() {
  29. memset(d,-1,sizeof(d));
  30. queue<int> q;q.push(es);d[es]=0;
  31. while (!q.empty()) {
  32. int x=q.front();q.pop();
  33. for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]<0) {
  34. d[e[i].to]=d[x]+1;
  35. q.push(e[i].to);
  36. }
  37. }
  38. return d[et]>0;
  39. }
  40. int dfs(int x,int f) {
  41. if (x==et || f==0) return f;
  42. int w,used=0;
  43. for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]==d[x]+1) {
  44. w=dfs(e[i].to,min(e[i].w,f-used));
  45. used+=w;
  46. e[i].w-=w;e[i^1].w+=w;
  47. if (used==f) return used;
  48. }
  49. if(!used) d[x]=-1;
  50. return used;
  51. }
  52. void Dinic() {
  53. while (bfs())
  54. ans-=dfs(es,inf);
  55. }
  56.  
  57. void insert(int k,int l,int r,int i) {
  58. if (l>R[i] || r<L[i]) return;
  59. if (L[i]<=l && r<=R[i]) {link(k+et,n+i,inf);return;}
  60. int mid=(l+r)>>1;
  61. if (tr[k].ls) insert(tr[k].ls,l,mid,i);
  62. if (tr[k].rs) insert(tr[k].rs,mid+1,r,i);
  63. }
  64. void build(int &rt,int k,int l,int r,int i) {
  65. rt=++size;
  66. if (l==r) {
  67. link(i,rt+et,inf);
  68. if (k) link(k+et,rt+et,inf); //细节
  69. return;
  70. }
  71. int mid=(l+r)>>1;
  72. if (a[i]<=mid) tr[rt].rs=tr[k].rs,build(tr[rt].ls,tr[k].ls,l,mid,i);
  73. else tr[rt].ls=tr[k].ls,build(tr[rt].rs,tr[k].rs,mid+1,r,i);
  74. if (tr[rt].ls) link(tr[rt].ls+et,rt+et,inf);
  75. if (tr[rt].rs) link(tr[rt].rs+et,rt+et,inf);
  76. }
  77.  
  78. int main() {
  79. scanf("%d",&n);
  80. es=2*n+1;et=es+1;
  81. int tot=0;
  82. for (int b,w,p,i=1;i<=n;i++) {
  83. scanf("%d%d%d%d%d%d",&a[i],&b,&w,&L[i],&R[i],&p);
  84. tmp[++tot]=a[i];tmp[++tot]=L[i];tmp[++tot]=R[i];
  85. link(es,i,w);
  86. link(i,et,b);
  87. link(i+n,i,p);
  88. ans+=w+b;
  89. }
  90. sort(tmp+1,tmp+1+tot);
  91. tot=unique(tmp+1,tmp+1+tot)-tmp-1;
  92. for (int i=1;i<=n;i++) {
  93. a[i]=lower_bound(tmp+1,tmp+1+tot,a[i])-tmp;
  94. L[i]=lower_bound(tmp+1,tmp+1+tot,L[i])-tmp;
  95. R[i]=lower_bound(tmp+1,tmp+1+tot,R[i])-tmp;
  96. if (i>1) insert(T[i-1],1,tot,i);
  97. build(T[i],T[i-1],1,tot,i);
  98. }
  99. Dinic();
  100. printf("%lld",ans);
  101. return 0;
  102. }

  

【bzoj3218】 a + b Problem的更多相关文章

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

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

  2. 【BZOJ-3218】a+b Problem 最小割 + 可持久化线段树

    3218: a + b Problem Time Limit: 20 Sec  Memory Limit: 40 MBSubmit: 1320  Solved: 498[Submit][Status] ...

  3. 【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 ...

  4. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  5. 洛谷P1919 【模板】A*B Problem升级版 题解(FFT的第一次实战)

    洛谷P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 刚学了FFT,我们来刷一道模板题. 题目描述 给定两个长度为 n 的两个十进制数,求它们的乘积. n<=100000 如 ...

  6. 【BZOJ1000】A+B Problem ★BZOJ1000题达成★

    [BZOJ1000]A+B Problem Description 输入两个数字,输出它们之和 Input 一行两个数字A,B(0<=A,B<100) Output 输出这两个数字之和 S ...

  7. 【题解】CF45G Prime Problem

    [题解]CF45G Prime Problem 哥德巴赫板子题? \(\frac{n(n+1)}{2}\)若是质数,则不需要分了. 上式 若是奇数,那么拆成2和另一个数. 上式 若是偶数吗,直接\(O ...

  8. 【题解】P4137 Rmq Problem(莫队)

    [题解]P4137 Rmq Problem(莫队) 其实这道题根本就不用离散化! 因为显然有\(mex\)值是\(\le 2\times 10^5\)的,所以对于大于\(2\times 10^5\)的 ...

  9. 【HDU1402】【FFT】A * B Problem Plus

    Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to e ...

随机推荐

  1. SPM FDR校正

    来源: http://blog.sciencenet.cn/blog-479412-572049.html,http://52brain.com/thread-15512-1-1.html SPM8允 ...

  2. 添加Distributor失败

    上周做了一个case,客户无法为SQL Server instance配置remote distributor. 下面分享一下排查问题的过程,希望对您排查类似的问题所有帮助. 客户的环境中的SQL S ...

  3. Windows Phone App Studio 无码开发手机应用

    上周微软发布了一款基于Web的Windows Phone应用开发工具 "Windows Phone App Studio".它与大家熟知Visual Studio的最大不同之处是W ...

  4. Generate Parentheses

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  5. codevs3143 二叉树的序遍历

    难度等级:白银 3143 二叉树的序遍历 题目描述 Description 求一棵二叉树的前序遍历,中序遍历和后序遍历 输入描述 Input Description 第一行一个整数n,表示这棵树的节点 ...

  6. ASP.NET MVC 教程-MVC简介

    ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务器脚本创建网页和网站的开发框架. ASP.NET 支持三种不同的开发模式:Web Pages(Web 页面).MVC(Mode ...

  7. 学习Python的三种境界

    前言 王国维在<人间词话>中将读书分为了三种境界:"古今之成大事业.大学问者,必经过三种之境界:'昨夜西风凋碧树,独上高楼,望尽天涯路'.此第一境也.'衣带渐宽终不悔,为伊消得人 ...

  8. 订餐系统之定时器Timer不定时

    经过几天漫长的问题分析.处理.测试.验证,定时器Timer终于定时了,于是开始了这篇文章,希望对还在纠结于“定时器Timer不定时”的同学有所帮助,现在的方案,在系统日志中会有警告,如果您有更好的方案 ...

  9. 链接错误-库冲突(libcmt.lib和libcmtd.lib)

    在同一个项目中,所有的源文件必须链接相同的C运行时库.如果某一文件用了Multithreaded DLL版本,而其他文件用了Single-Threaded或者Multithreaded版本的库,也就是 ...

  10. 【Zeyphr】保存json到数据库

    方法一: public int SaveJob(JObject data) { var formWrapper = RequestWrapper.Instance().LoadSettingXmlSt ...