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. Django之连接多个数据库的相关配置

    01-修改django默认的数据库 # settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NA ...

  2. Nice Garland CodeForces - 1108C (思维+暴力)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  3. python实现一个二分查找

    二分查找: 二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法.但是,折半查找要求线性表必须采用顺序存储结构,而且表中元素按关键字有序排列 查找过程: 首先,假设表中元素是 ...

  4. Python_内置函数之map()

    map 会根据提供的函数对指定序列做映射. 代码如下: def square(x): return x ** 2 ret = map(square, [1, 2, 3, 4, 5]) # 计算列表各元 ...

  5. MySQL 优化集锦

    case 1: 如果筛选or条件有多个的时候,应该将最好判断的放在最前面,将最不好判断的放在最后面 比如,有一个学生表,想要找出其中年龄是20岁,住址中包含666这个数字的记录.可以下面这两个方案: ...

  6. MySQL数据性能优化-修改方法与步骤

    原文:http://bbs.landingbj.com/t-0-240421-1.html 数据库优化应该是每个设计到数据库操作应用必须涉及到的操作. 经常调试修改数据库性能主要有三个方面 1.MyS ...

  7. python3 selenium webdriver 元素定位xpath定位骚操作

    源文http://www.cnblogs.com/qingchunjun/p/4208159.html By.xpath() 这个方法是非常强大的元素查找方式,使用这种方法几乎可以定位到页面上的任意元 ...

  8. Python解释器有哪些?Python解释器种类

    Python是一门解释器语言,代码想运行,必须通过解释器执行,Python存在多种解释器,分别基于不同语言开发,每个解释器有不同的特点,但都能正常运行Python代码,以下是常用的五种Python解释 ...

  9. jvm 虚拟机内存模型

    来源:https://blog.csdn.net/A_zhenzhen/article/details/77917991?locationNum=8&fps=1    https://blog ...

  10. [转帖]linux sed命令

    linux sed命令就是这么简单 https://www.cnblogs.com/wangqiguo/p/6718512.html 用到的最多的就是一个sed -i 's/nn/mm/' 的命令了. ...