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

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 ;
} /*
*/
上一篇:sql执行顺序整理


下一篇:Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路