bzoj 3681 Arietta
一棵有根树,每个点有一个音高,有 $m$ 中弹奏方法,每种方法可以弹奏 $d$ 子树中音高在 $[l,r]$ 间的音符,每种方法最多弹 $t$ 次
求最多能弹出多少个音符
$n \leq 10000$
sol:
网络流
暴力连边是
1. $S \rightarrow 每个点$
2. $每个方法 \rightarrow T$
3. $每个点 \rightarrow 每个能用到的方法$
第一种边限制是 $1$ ,第二种边限制是 $t$,第三种边没有限制
第一第二种不好优化,考虑优化第三种
第三种本质上是对树上 $dfn$ 是一段区间(子树),权值也是一段区间的点连边
考虑数据结构优化
可以使用可持久化线段树合并
一开始每个点向它所在的线段树上的点连边
每次合并的时候原来的点向合并出来的新点连边
对于方法我们把它能作用的区域向它连边
这样边数是 $O(2n) + O(可持久化线段树)$ 的,大概是 $O(nlogn)$
#include <bits/stdc++.h>
#define LL long long
#define rep(i, s, t) for (register int i = (s), i##end = (t); i <= i##end; ++i)
#define dwn(i, s, t) for (register int i = (s), i##end = (t); i >= i##end; --i)
using namespace std;
inline int read() {
int x = , f = ; char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') f = -f;
for (; isdigit(ch); ch = getchar()) x = * x + ch - '';
return x * f;
}
const int maxn = , maxm = , inf = ;
struct Dinic {
int cur[maxm], head[maxm], nx[maxm];
int n, m, s, t;
struct Edge {
int from, to, caps;
Edge(){}
Edge(int _1, int _2, int _3): from(_1), to(_2), caps(_3) {}
}es[maxm];
void AddEdge(int u, int v, int w) {
es[m] = Edge(u, v, w); nx[m] = head[u]; head[u] = m++;
es[m] = Edge(v, u, ); nx[m] = head[v]; head[v] = m++;
}
void setn(int _) {n = _;}
Dinic() {m = ; memset(head, -, sizeof(head));}
queue<int> q; int dis[maxn];
bool BFS() {
rep(i, , n) dis[i] = ;
q.push(t); dis[t] = ;
while(!q.empty()) {
int now = q.front(); q.pop();
for(int i=head[now];~i;i=nx[i]) {
Edge &e = es[i^];
if(!dis[e.from] && e.caps) {
dis[e.from] = dis[now] + ;
q.push(e.from);
}
}
}
return (dis[s] > );
}
int DFS(int u, int a) {
if(u == t || !a) return a;
int flow = , f;
for(int &i = cur[u]; ~i; i = nx[i]) {
Edge &e = es[i];
if(dis[e.to] == dis[u] - && (f = DFS(e.to, min(e.caps, a)))) {
e.caps -= f; es[i^].caps += f;
a -= f; flow += f;
if(!a) return flow;
}
}
return flow;
}
int MaxFlow(int _s, int _t) {
s = _s, t = _t; int res = ;
while(BFS()) {
memcpy(cur, head, (n + ) * sizeof(int));
res += DFS(s, );
}
return res;
}
} sol;
int s, t, nodes;
struct Ques {
int l, r, d, t;
Ques(){}
Ques(int _1, int _2, int _3, int _4) : l(_1), r(_2), d(_3), t(_4) {}
}qs[maxn];
int n, m;
int fa[maxn], h[maxn];
int first[maxn], nx[maxn], to[maxn], cnt;
inline void add(int u, int v) {
to[++cnt] = v;
nx[cnt] = first[u];
first[u] = cnt;
}
int root[maxn], ls[maxm << ], rs[maxm << ], dfn;
inline void Insert(int &x, int l, int r, int pos, int p) {
x = ++dfn;
if(l == r) {
sol.AddEdge(p, x + nodes, inf);
return;
}
int mid = (l + r) >> ;
if(pos <= mid) Insert(ls[x], l, mid, pos, p) ;
else Insert(rs[x], mid + , r, pos, p) ;
if(ls[x]) sol.AddEdge(ls[x] + nodes, x + nodes, inf);
if(rs[x]) sol.AddEdge(rs[x] + nodes, x + nodes, inf);
}
inline int merge(int x, int y, int l, int r) {
if(!x || !y) return x + y;
int z = ++dfn;
if(l == r) {
sol.AddEdge(x + nodes, z + nodes, inf);
sol.AddEdge(y + nodes, z + nodes, inf);
return z;
}
int mid = (l + r) >> ;
ls[z] = merge(ls[x], ls[y], l, mid);
rs[z] = merge(rs[x], rs[y], mid+, r);
if(ls[z]) sol.AddEdge(ls[z] + nodes, z + nodes, inf);
if(rs[z]) sol.AddEdge(rs[z] + nodes, z + nodes, inf);
return z;
}
inline void link(int x, int l, int r, int L, int R, int p) {
if(!x) return;
if(L <= l && r <= R) {
sol.AddEdge(x + nodes, p, inf);
return;
}
int mid = (l + r) >> ;
if(L <= mid) link(ls[x], l, mid, L, R, p);
if(R > mid) link(rs[x], mid+, r, L, R, p);
}
inline void dfs(int x) {
Insert(root[x], , n, h[x], x);
for(int i=first[x];i;i=nx[i]) {
dfs(to[i]);
root[x] = merge(root[x], root[to[i]], , n);
}
} int main() {
n = read(), m = read();
s = n + m + , t = n + m + , nodes = t + ;
rep(i, , n) {
fa[i] = read();
add(fa[i], i);
}
rep(i, , n) {
h[i] = read();
sol.AddEdge(s, i, );
} dfs();
rep(i, , m) {
int l = read(), r = read(), d = read(), ct = read();
link(root[d], , n, l, r, i+n);
qs[i] = Ques(l, r, d, ct); sol.AddEdge(i + n, t, ct);
}
sol.setn(nodes + dfn + );
cout << sol.MaxFlow(s, t) << endl;
}
bzoj 3681 Arietta的更多相关文章
- 【BZOJ 3681】Arietta
传送门 题目描述 Arietta 的命运与她的妹妹不同,在她的妹妹已经走进学院的时候,她仍然留在山村中. 但是她从未停止过和恋人 Velding 的书信往来.一天,她准备去探访他. 对着窗外的阳光,临 ...
- [BZOJ 3681]Ariettad
终于是打完了 CH 上的数据结构专场了…… 不过看样子还有一套 5555 传送门: http://ch.ezoj.tk/contest/CH%20Round%20%2351%20-%20Shinrei ...
- BZOJ 3681 线段树合并+网络流
思路: 暴力建图有n*m条边 考虑怎么优化 (那就只能加个线段树了呗) 然后我就不会写了..... 抄了一波题解 //By SiriusRen #include <bits/stdc++.h&g ...
- BZOJ3681: Arietta
题解: 数据结构来优化网络流,貌似都是用一段区间来表示一个点,然后各种乱搞... 发现主席树好吊...在树上建主席树貌似有三种方法: 1.建每个点到根节点这条链上的主席树,可以回答和两点间的路径的XX ...
- 【BZOJ-3681】Arietta 网络流 + 线段树合并
3681: Arietta Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 182 Solved: 70[Submit][Status][Discuss ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ 2127: happiness [最小割]
2127: happiness Time Limit: 51 Sec Memory Limit: 259 MBSubmit: 1815 Solved: 878[Submit][Status][Di ...
- BZOJ 3275: Number
3275: Number Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 874 Solved: 371[Submit][Status][Discus ...
- BZOJ 2879: [Noi2012]美食节
2879: [Noi2012]美食节 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1834 Solved: 969[Submit][Status] ...
随机推荐
- 020_自己编写的wordcount程序在hadoop上面运行,不使用插件hadoop-eclipse-plugin-1.2.1.jar
1.Eclipse中无插件运行MP程序 1)在Eclipse中编写MapReduce程序 2)打包成jar包 3)使用FTP工具,上传jar到hadoop 集群环境 4)运行 2.具体步骤 说明:该程 ...
- 微信小程序相关资料整理
微信小程序官方介绍https://mp.weixin.qq.com/debug/wxadoc/introduction/index.html?t=201818 微信小程序开发资源https://jue ...
- Linux Shell基础 环境变量
环境变量 环境变量和用户自定义变量最主要的区别在于,环境变量是全局变量,而用户自定义变量是局部变量.用户自定义变量只在当前的 Shell 中生效,而环境变量会在当前 Shell 和这个 Shell 的 ...
- python之json模块的基本使用
json模块的作用:将字符串和字典相互转换 json和eval的区别: eval函数不能识别null转换成None json可以将null转换成python可以识别的None json序列化和反序列化 ...
- AWK的行循环控制
1.控制函数:next,getline,exit. next: 该行的action运行到next就停止,读取下一行. getline:1.没有"<"或“|”的情况下 ...
- CentOS 6.5 下vim 配置
1. 登录并进入你常用的用户名下,查看其主目录 命令: # su xxx $ cd xxx $ ls -a 2.查看并建立目录和文件 首先看你的主目录~/ 下是否有.vimrc文件,没有就输入指令 $ ...
- php5.6 连接SQL SERVER
PHP Fatal error: Call to undefined function sqlsrv_connect() in php链接sqlserver出现该错误: 原因是:php5.3 及以上版 ...
- JavaWeb Cookie
1. Cookie 1.1. Cookie概述 Cookie译为小型文本文件或小甜饼,Web应用程序利用Cookie在客户端缓存服务器端文件.Cookie是以键值对形式存储在客户端主机硬盘中,由服务器 ...
- ResourceBundle和properties 读取配置文件区别
java.util.ResourceBundle 和java.util.properties 读取配置文件区别 这两个类都是读取properties格式的文件的,而Properties同时还能用来写文 ...
- iptables基础知识详解
iptables防火墙可以用于创建过滤(filter)与NAT规则.所有Linux发行版都能使用iptables,因此理解如何配置 iptables将会帮助你更有效地管理Linux防火墙.如果你是第一 ...