【BZOJ3218】【UOJ#77】a + b Problem
题目
思路&做法
明显的最小割(其实是之前做过一道类似的题)
S向每一个格子连容量为\(b_i\)的边
每一个格子向T连容量为\(w_i\)的边
对于格子\(i\)向满足条件的格子\(j(1 \leq j < i, l_i \leq a_j \leq r_i)\)连容量为\(p_i\)的边
但是考虑到这题恶心的数据范围, 这样做很明显会TLE。
算法的瓶颈在第三步。我们发现\(j\)的范围看起来像是一个像二维偏序的东西, 于是便可以用主席树来优化一下, 对于每个节点\(i\)新建一个节点\(k\),由i向k连容量为\(p_i\)的边, 由\(k\)向主席树中\(j\)的范围的对应的节点连容量为\(inf\)的边。
代码
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 0x7F7F7F7F;
const int N = 50010, M = 1450010;
struct edge
{ int from, to, flow, cap;
edge() { }
edge(int _1, int _2, int _3, int _4) : from(_1), to(_2), flow(_3), cap(_4) { }
};
struct Dinic
{ edge edges[M];
int head[165022], nxt[M], tot;
int L, R;
inline void init()
{ memset(head, -1, sizeof(head));
tot = 0;
}
void add_edge(int x, int y, int z)
{ edges[tot] = edge(x, y, 0, z);
nxt[tot] = head[x];
head[x] = tot++;
edges[tot] = edge(y, x, 0, 0);
nxt[tot] = head[y];
head[y] = tot++;
}
int s, t;
int d[165022];
bool bfs()
{ memset(d, -1, sizeof(d));
queue<int> q;
d[s] = 0;
q.push(s);
while (!q.empty())
{ int x = q.front(); q.pop();
for (int i = head[x]; ~i; i = nxt[i])
{ edge & e = edges[i];
if (e.cap > e.flow && d[e.to] == -1)
{ d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return d[t] != -1;
}
int cur[165022];
int dfs(int x, int a)
{ if (x == t || a == 0) return a;
int flow = 0, f;
for (int & i = cur[x]; ~i; i = nxt[i])
{ edge & e = edges[i];
if (d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap-e.flow))) > 0)
{ e.flow += f;
edges[i^1].flow -= f;
a -= f;
flow += f;
if (a == 0) break;
}
}
return flow;
}
int maxflow(int _s, int _t)
{ s = _s, t = _t;
int flow = 0;
while (bfs())
{ for (int i = L; i <= R; i++)
cur[i] = head[i];
flow += dfs(s, INF);
}
return flow;
}
} dinic;
int n;
int root[N];
struct SegmentTree
{ int ls[N*20], rs[N*20], sz;
void update(int & cur, int pre, int l, int r, int p, int node)
{ cur = ++sz;
if (l == r)
{ if (pre) dinic.add_edge(cur, pre, INF);
dinic.add_edge(cur, node, INF);
return;
}
ls[cur] = ls[pre];
rs[cur] = rs[pre];
int mid = (l + r) >> 1;
if (p <= mid)
update(ls[cur], ls[pre], l, mid, p, node);
else
update(rs[cur], rs[pre], mid+1, r, p, node);
dinic.add_edge(cur, ls[cur], INF);
dinic.add_edge(cur, rs[cur], INF);
}
void link(int cur, int l, int r, int ql, int qr, int node)
{ if (ql == l && qr == r)
dinic.add_edge(node, cur, INF);
else
{ int mid = (l + r) >> 1;
if (qr <= mid)
link(ls[cur], l, mid, ql, qr, node);
else if (ql > mid)
link(rs[cur], mid+1, r, ql, qr, node);
else
link(ls[cur], l, mid, ql, mid, node),
link(rs[cur], mid+1, r, mid+1, qr, node);
}
}
} st;
int a[N], w[N], b[N], L[N], R[N], p[N];
int num[N], total;
int main()
{ scanf("%d", &n);
for (int i = 1; i <= n; i++)
{ scanf("%d %d %d %d %d %d", &a[i], &b[i], &w[i], &L[i], &R[i], &p[i]);
num[++total] = a[i];
num[++total] = L[i];
num[++total] = R[i];
}
sort(num+1, num+total+1);
total = unique(num+1, num+total+1) - num - 1;
for (int i = 1; i <= n; i++)
{ a[i] = lower_bound(num+1, num+1+total, a[i]) - num;
L[i] = lower_bound(num+1, num+1+total, L[i]) - num;
R[i] = lower_bound(num+1, num+1+total, R[i]) - num;
}
int ans = 0;
dinic.init();
int S = n + n + 1, T = n + n + 2;
st.sz = n + n + 2;
for (int i = 1; i <= n; i++)
{ dinic.add_edge(S, i, b[i]);
dinic.add_edge(i, T, w[i]);
dinic.add_edge(i, i+n, p[i]);
if (i > 1)
st.link(root[i-1], 1, total, L[i], R[i], i+n);
st.update(root[i], root[i-1], 1, total, a[i], i);
ans += b[i] + w[i];
}
dinic.L = 0, dinic.R = st.sz;
ans -= dinic.maxflow(S, T);
printf("%d\n", ans);
}
/*
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
*/
【BZOJ3218】【UOJ#77】a + b Problem的更多相关文章
- 【UOJ#77】A+B Problem
传送门 题目描述 略 Sol 看到选择黑白收益不同,然后还可能有代价. 我们想到用网络流解决,并且这应该是用总可能收益-最小割得到答案. 考虑初步建图,发现那个限制可以直接 \(n^2\) 解决. 我 ...
- 【UOJ#236】[IOI2016]railroad(欧拉回路,最小生成树)
[UOJ#236][IOI2016]railroad(欧拉回路,最小生成树) 题面 UOJ 题解 把速度看成点,给定的路段看成边,那么现在就有了若干边,然后现在要补上若干边,以及一条\([inf,\) ...
- 【UOJ#177】欧拉回路
[UOJ#177]欧拉回路 题面 UOJ 题解 首先图不连通就没啥好搞的了. 对于无向图而言,每个点度数为偶数. 对于有向图而言,每个点入度等于出度. 然后就是一本通上有的做法,直接\(dfs\)一遍 ...
- 【UOJ#311】【UNR #2】积劳成疾(动态规划)
[UOJ#311][UNR #2]积劳成疾(动态规划) UOJ Solution 考虑最大值分治解决问题.每次枚举最大值所在的位置,强制不能跨过最大值,左右此时不会影响,可以分开考虑. 那么设\(f[ ...
- 【UOJ#450】【集训队作业2018】复读机(生成函数,单位根反演)
[UOJ#450][集训队作业2018]复读机(生成函数,单位根反演) 题面 UOJ 题解 似乎是\(\mbox{Anson}\)爷的题. \(d=1\)的时候,随便怎么都行,答案就是\(k^n\). ...
- 【UOJ#246】套路(动态规划)
[UOJ#246]套路(动态规划) 题面 UOJ 题解 假如答案的选择的区间长度很小,我们可以做一个暴力\(dp\)计算\(s(l,r)\),即\(s(l,r)=min(s(l+1,r),s(l,r- ...
- 【UOJ#340】【清华集训2017】小 Y 和恐怖的奴隶主(矩阵快速幂,动态规划)
[UOJ#340][清华集训2017]小 Y 和恐怖的奴隶主(矩阵快速幂,动态规划) 题面 UOJ 洛谷 题解 考虑如何暴力\(dp\). 设\(f[i][a][b][c]\)表示当前到了第\(i\) ...
- 【UOJ#422】【集训队作业2018】小Z的礼物(min-max容斥,轮廓线dp)
[UOJ#422][集训队作业2018]小Z的礼物(min-max容斥,轮廓线dp) 题面 UOJ 题解 毒瘤xzy,怎么能搬这种题当做WC模拟题QwQ 一开始开错题了,根本就不会做. 后来发现是每次 ...
- 【UOJ#275】组合数问题(卢卡斯定理,动态规划)
[UOJ#275]组合数问题(卢卡斯定理,动态规划) 题面 UOJ 题解 数据范围很大,并且涉及的是求值,没法用矩阵乘法考虑. 发现\(k\)的限制是,\(k\)是一个质数,那么在大组合数模小质数的情 ...
随机推荐
- hexo搭建博客
在使用hexo搭建个人博客的时候,修改.yml文件后出现错误:FATAL can not read a block mapping entry; a multiline key may not be ...
- url 传参数时出现中文乱码
1.前端通过 url 传递参数,但是参数又有中文,在下一个页面接受参数的时候中文会乱码 解决方案为: 定义和用法 decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解 ...
- 解析MYsql写的表达式
今天遇到个问题,Sql中直接写的是复杂表达式,如何解析呢? round(((0.00579049505+0.00006600324*JING_JIE^2*SHU_GAO-0.00000046921*J ...
- SPLAY or SPALY ?
写在前面: 由我们可爱的Daniel Sleator和Robert Tarjan提出的一种数据结构,平衡树的一种,本质是二叉树. 至于到底是splay还是spaly,我认为可能splay更对一些 毕竟 ...
- 什么是Capability
desired capability的功能是配置Appium会话.他们告诉Appium服务器您想要自动化的平台和应用程序. Desired Capabilities是一组设置的键值对的集合,其中键对应 ...
- nyoj24-素数 距离问题
素数距离问题 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度素数 ...
- [luogu2679] 子串 (多维dp)
传送门 Description 有两个仅包含小写英文字母的字符串 A 和 B . 现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在字符串 A 中出现的顺序依次连接起来 ...
- SQL中的条件判断语句(case when zhen if,ifnull)用法
简介: case具有两种格式.简单case函数和case搜索函数.这两种方式,可以实现相同的功能.简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式.还有 ...
- Solr数据不同步
Solr配置了集群,本地有253和254,2个独立的Solr服务. 同一个页面的图片,刷新2次,图片地址不一样,最后查明,后台数据源Solr1和Solr2的数据不一致. 第1步推测:本地缓存, ...
- VirtualBox没有权限访问共享文件夹
将用户添加至vboxsf组 命令: sudo adduser ly vboxsf 搞定!