F. Cookies

链接:http://codeforces.com/contest/1099/problem/F

题意:

给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干需要花费bi时间,有两个玩家,玩家一可以移动到当前点的子节点也可以申请游戏结束返回根节点并吃沿途的饼干,玩家二可以删除当前点到儿子节点的一条边,走路和吃饼干都消耗时间,会给出一个总时间,在总时间内尽可能的多吃饼干,问最多能吃多少个?

思路:

由于是玩家一先手,那么最开始的最大边则不会被删除,但之后路途的最大边都会被玩家二删除,所以我们对于当前点我们需要求:

f1.如果现在回头那么最多可以吃到多少饼干

f2.向下走最多可以吃到多少饼干

f3.向下走次多可以吃到多少饼干

当在根节点是取12最大值,其余情况取13最大值,用线段树维护剩余时间最多能取多少,

对单个饼干的时间建线段树,权值为饼干个数,查询时优先向左取,因为左边是花费时间少的,这样就可以取得剩余时间内能得到的最多的饼干。

实现代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid ll m = (l + r) >> 1 const ll M = 1e6+;
const ll Max = 1e6;
struct node{
ll to,w,next;
}e[M<<];
ll sum[M<<],num[M<<],a[M],tim[M],head[M],cnt;
void add(ll u,ll v,ll w){
e[++cnt].to = v;e[cnt].next = head[u];e[cnt].w = w;head[u] = cnt;
} void pushup(ll rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
num[rt] = num[rt<<] + num[rt<<|];
} void update(ll p,ll c,ll l,ll r,ll rt){
if(l == r){
sum[rt] += p*c;
num[rt] += c;
return ;
}
mid;
if(p <= m) update(p,c,lson);
else update(p,c,rson);
pushup(rt);
} ll query(ll t,ll l,ll r,ll rt){
if(sum[rt] <= t) return num[rt];
if(l == r){
return t/l;
}
mid;
if(sum[rt<<] >= t) return query(t,lson);
else return num[rt<<] + query(t-sum[rt<<],rson);
} ll dfs(ll u,ll fa,ll t){
update(tim[u],a[u],,Max,);
ll f1 = query(t,,Max,);
ll f2 = ,f3 = ; for(ll i = head[u];i;i=e[i].next){
ll v = e[i].to;
if(v == fa) continue;
if(t <= e[i].w*) continue;
ll k = dfs(v,u,t-e[i].w*);
if(k > f2) f3 = f2,f2 = k;
else if(k > f3) f3 = k; }
update(tim[u],-a[u],,Max,);
if(u == ) return max(f1,f2);
else return max(f1,f3);
} int main()
{
ll n,T,x,y;
scanf("%lld%lld",&n,&T);
for(ll i = ;i <= n;i ++) scanf("%lld",&a[i]);
for(ll i = ;i <= n;i ++) scanf("%lld",&tim[i]);
for(ll i = ;i <= n;i ++){
scanf("%lld%lld",&x,&y);
add(i,x,y); add(x,i,y);
}
printf("%lld\n",dfs(,,T));
return ;
}

Codeforces Round #530 (Div. 2) F (树形dp+线段树)的更多相关文章

  1. Codeforces Round #587 (Div. 3) F Wi-Fi(线段树+dp)

    题意:给定一个字符串s 现在让你用最小的花费 覆盖所有区间 思路:dp[i]表示前i个全覆盖以后的花费 如果是0 我们只能直接加上当前位置的权值 否则 我们可以区间询问一下最小值 然后更新 #incl ...

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

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

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

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

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

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

  5. Codeforces Round #530 (Div. 2) F - Cookies

    F - Cookies 思路:我们先考虑如何算出在每个节点结束最多能吃多少饼干, 这个dfs的时候用线段树维护一下就好了, 然后有个这个信息之后树上小dp一下就好啦. #include<bits ...

  6. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  7. Codeforces 671D. Roads in Yusland(树形DP+线段树)

    调了半天居然还能是线段树写错了,药丸 这题大概是类似一个树形DP的东西.设$dp[i]$为修完i这棵子树的最小代价,假设当前点为$x$,但是转移的时候我们不知道子节点到底有没有一条越过$x$的路.如果 ...

  8. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  9. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

随机推荐

  1. java 抽象

    MotoVehicle抽象类 package text1; /* * 抽象 */ public abstract class MotoVehicle { // 共同的属性 private String ...

  2. 埋锅。。。BZOJ1004-置换群+burnside定理+

    看这道题时当时觉得懵逼...这玩意完全看不懂啊...什么burnside...难受... 于是去看了点视频和资料,大概懂了置换群和burnside定理,亦步亦趋的懂了别人的代码,然后慢慢的打了出来.. ...

  3. Python入门-Hello Word

    1.python语言介绍 Python创始人:Guido Van Rossum 2.python是一种解释型.动态类型计算机程序设计语言. 解释型:程序无需编译成二进制代码,而是在执行时对语句一条一条 ...

  4. ocrosoft 程序设计提高期末复习问题M 递归求猴子吃桃

    http://acm.ocrosoft.com/problem.php?cid=1172&pid=12 题目描述 猴子吃桃问题.猴子第1天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个. ...

  5. 封装day.js

    封装day.js import dayjs from 'dayjs' import 'dayjs/locale/zh-cn' import relativeTime from 'dayjs/plugi ...

  6. SQLServer数据库分页

    以  项目表 PM_Project  为例. PM_Project 全部内容如下(共6条数据): 一.Top – Not In - Top 方式分页 直接的,原始的,不采用函数,纯手动挡. 分步探索过 ...

  7. 4 HttpServletResponse 与 HttpServletRequest

    Web 服务器收到一个http请求,会针对每个请求创建一个HttpServletRequest 和 HttpServletReponse 对象,response用于向客户端发送数据,request用于 ...

  8. 11 The superlative

    1 最高级用来表明三个或更多事物之间的关系.最高级是通过在形容词之前加 "the" 并在之后加 "-est",或在形容词之前加 "the most&q ...

  9. K3CLOUD数据权限授权

    1.定义角色,把用户放入角色内 2.设置数据规则 3.设置业务对象功能授权

  10. 剑指offer(5)

    题目: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解法: 一个栈专门用来存数,当需要输出数时,把所有数倒到第二个栈,当然,若此时第二个栈中已经有数了(之前倒 ...