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. 浅入 dancing links x(舞蹈链算法)

    abastract:利用dancing links 解决精确覆盖问题,例如数独,n皇后问题:以及重复覆盖问题. 要学习dacning links 算法,首先要先了解该算法适用的问题,精确覆盖问题和重复 ...

  2. Linux上vi编辑文件非正常退出后文件恢复

    Vim另存文件的命令为 编辑完文件后Esc,输入以下指令 :w filename 编辑文件时非正常退出,会生成.hello.txt.swp的文件,还有一些其他信息 恢复文件要使用以下命令: [keys ...

  3. Excel:公式应用技巧汇总

    1.合并单元格添加序号:=MAX(A$1:A1)+1 不重复的个数: 公式1:{=SUM(1/COUNTIF(A2:A8,A2:A8))} 公式2:{=SUM(--(MATCH(A2:A8,A2:A8 ...

  4. 多线程(模拟买票)-----java基础知识总结

    这次的的问题引入的比较深入,如果看了这篇博客,不看下一篇,你会很懵逼. 代码: package com.day13.math; /** * 类说明 :模拟三个窗口同时售票 * @author 作者 : ...

  5. python 喜马拉雅 音乐下载 演示代码

    1.主程序文件 import os import json import requests from contextlib import closing from progressbar import ...

  6. Sublime Text 之运行 js 方法[2015-5-6更新mac下执行js]

    昨天说完<Sublime Text 2 绿化与汉化 [Windows篇]>,今天我们来说说怎么用st直接运行 js 吧.群里的小伙伴一直对我的 ST 能直接运行js感到非常好奇,今天我就公 ...

  7. win8开wifi共享无法使用的问题解决办法

    相信现在不少人都安装了windows8操作系统,因为windows8这个全新的操作系统用起来 确实挺强大,包括漂亮的开始屏,但是不得不说这个系统的兼容性还是有待提高,所以win8我的 装了又卸,卸了又 ...

  8. 【ORACLE】创建表空间

    CREATE TABLESPACE dna36 DATAFILE 'D:\oracle\oradata\orcl\dna36.dbf' SIZE 100M AUTOEXTEND ON NEXT 10M ...

  9. RTS与CTS的含义【转】

    转自:http://www.cnblogs.com/sunyubo/archive/2010/04/21/2282176.html 一.RS232标准中的RTS与CTS RTS,CTS------请求 ...

  10. Tomcat优化步骤【转】

    一.Tomcat的缺省是多少,怎么修改 Tomcat的缺省端口号是8080.修改Tomcat端口号:1.找到Tomcat目录下的conf文件夹2.进入conf文件夹里面找到server.xml文件3. ...