Codeforces Round #530 (Div. 2) F - Cookies
思路:我们先考虑如何算出在每个节点结束最多能吃多少饼干, 这个dfs的时候用线段树维护一下就好了,
然后有个这个信息之后树上小dp一下就好啦。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); LL n, T, t[N], cnt[N], dp[N], ans[N];
vector<PLI> G[N]; #define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
LL a[N<<], sum[N<<];
void update(int p, LL val, int l=, int r=, int rt=) {
if(l == r) {
a[rt] += val;
sum[rt] += val*p;
return;
}
int mid = l + r >> ;
if(p <= mid) update(p, val, lson);
else update(p, val, rson);
a[rt] = a[rt<<] + a[rt<<|];
sum[rt] = sum[rt<<] + sum[rt<<|];
}
LL query(LL res, int l=, int r=, int rt=) {
if(sum[rt] <= res) return a[rt];
if(l == r) return res / l;
int mid = l + r >> ;
if(res <= sum[rt<<]) return query(res, lson);
else return query(sum[rt<<], lson) + query(res-sum[rt<<], rson);
} void getDp(int u, LL res) {
update(t[u], cnt[u]);
dp[u] = query(res);
for(auto e : G[u]) getDp(e.se, res-e.fi);
update(t[u], -cnt[u]);
} void dfs(int u) {
ans[u] = dp[u];
if(u == ) {
LL mx = ;
for(auto e : G[u]) dfs(e.se), mx = max(mx, ans[e.se]);
ans[u] = max(ans[u], mx);
} else {
LL mx = , mx2 = ;
for(auto e : G[u]) {
dfs(e.se);
if(ans[e.se] >= mx) mx2 = mx, mx = ans[e.se];
else if(ans[e.se] > mx2) mx2 = ans[e.se];
}
ans[u] = max(ans[u], mx2);
}
} int main() {
cin >> n >> T;
for(int i = ; i <= n; i++) cin >> cnt[i];
for(int i = ; i <= n; i++) cin >> t[i];
for(int i = ; i <= n; i++) {
LL p, l; cin >> p >> l;
G[p].push_back(mk(l<<, i));
}
getDp(, T);
dfs();
printf("%lld\n", ans[]);
return ;
} /*
*/
Codeforces Round #530 (Div. 2) F - Cookies的更多相关文章
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
随机推荐
- MT【86】两个绝对值之和最大
分析:这里只需要注意到$(|x|+|y|)_{max}=max\{|x+y|,|x-y|\}$,所以只需求$max\{|20a|,|14b|\}$ 进而变成熟悉的反解系数问题.容易知道最大值为$a=2 ...
- 洛谷 P1073 最优贸易 解题报告
P1073 最优贸易 题目描述 \(C\)国有\(n\)个大城市和\(m\)条道路,每条道路连接这\(n\)个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这\(m\)条道路中有一部分 ...
- c# 16进制byte转成int
]; Ratedata=System.BitConverter.GetBytes(FreResultTBoxValue); 上面这个 FreResultTBoxValue 变量是int,系统中自带in ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- 界面编程之QT的基本介绍与使用20180722
/*******************************************************************************************/ 一.qt介绍 ...
- shell中exec命令
1.find中的-exec参数 在当前目录下(包含子目录),查找所有txt文件并找出含有字符串"bin"的行 find ./ -name "*.txt" -ex ...
- ansible报错Aborting, target uses selinux but python bindings (libselinux-python) aren't installed【转】
报错内容: TASK [activemq : jvm configuration] ********************************************************** ...
- Win10新增功能快捷键大全
原文地址:http://wenwen.sogou.com/z/q703976788.htm贴靠窗口:Win + 左/右 > Win + 上/下 > 窗口可以变为 1/4 大小放置在屏幕 4 ...
- javascript命名空间
命名空间:命名空间有助于减少程序中所需要的全局变量的数量,并且同时还有助于避免命名冲突或过长的名字前缀. 板栗: var MYAPP = MYAPP || {}; MYAPP.namespace = ...
- 20155206 2016-2017-2 《Java程序设计》第7周学习总结
20155206 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 认识时间与日期 1.格林威治时间(GMT):通过观察太阳而得,因为地球公转轨道为椭圆形且速度 ...