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的更多相关文章

  1. Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

  2. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  3. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

  4. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  5. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  6. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  7. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  8. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

  9. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

随机推荐

  1. maven搭建springmvc+mybatis项目

    上一篇中已经成功使用maven搭建了一个web项目,本篇描述在此基础上怎么搭建一个基于springmvc+mybatis环境的项目. 说了这么久,为什么那么多人都喜欢用maven搭建项目?我们都知道m ...

  2. 沉迷AC自动机无法自拔之:[UVALive 4126] Password Suspects

    图片加载可能有点慢,请跳过题面先看题解,谢谢 一看到这么多模式串就非常兴奋,又是\(AC\)自动机 题目就是要求:经过 \(n\) 个节点,把所有单词都遍历一遍的方案数,和那道题差不多嘛 所以这样设: ...

  3. LOJ #2141. 「SHOI2017」期末考试

    题目链接 LOJ #2141 题解 据说这道题可以三分(甚至二分)? 反正我是枚举的 = = 先将t和b数组排序后计算出前缀和, 然后枚举最晚的出成绩时间,每次可以O(1)直接计算调整到该时间所需的代 ...

  4. 【UOJ #351】新年的叶子(树的直径,期望)

    题目链接 这的确是一道好题,我们不妨依循思路一步步推导,看问题是如何被解决的. 做一些约定,设$m$为树的叶子节点个数,设$len$为该树的直径(经过的点数). 毫无疑问,直径可能有多条,我们需要把所 ...

  5. 解题:九省联考2018 秘密袭击CoaT

    题面 按照*Miracle*的话来说,网上又多了一篇n^3暴力的题解 可能是因为很多猫题虽然很好,但是写正解性价比比较低? 直接做不可做,转化为统计贡献:$O(n)$枚举每个权值,直接统计第k大大于等 ...

  6. Python中的requests模块注意事项

    主要是说requests.post()方法, 参数: url :  这就不解释了 data:  如果传入的是字典类型,则字典在发出请求时会自动编码为表单形式,表单形式会将字典中的键和值进行一些操作: ...

  7. mysql concat_ws 与 concat 多字段模糊匹配应用

    有时我们希望在表中多个字段检索是否有我们给出的关键字,我们可以使用 concat_ws 和 concat 连接需要检索的字段,如: select * from userInfo where conca ...

  8. Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators

    Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值运算符 表 ...

  9. TradingView学习记录

    官网:https://cn.tradingview.com   申请图表库 用本地服务器打开 二:文件目录 三:基础概念 3.1 UDF:通用数据饲料(Universal Data Feed)     ...

  10. SQL记录-PLSQL异常

    PL/SQL异常   程序执行过程中出现错误情况被称为在PL/SQL异常. PL/SQL支持程序员在程序中使用异常块捕获这样的条件并采取适当的动作应对错误情况.有两种类型的异常: 系统定义的异常 用户 ...