Marge is already preparing for Christmas and bought a beautiful tree, decorated with shiny ornaments. Her Christmas tree can be represented as a complete binary tree composed of N nodes, numbered from 1to N and rooted on node 1. Each node has an integer value associated to it, representing its shininess.

The shininess of the h - th level of the tree is the sum of the shininess of all the nodes with depth h and the shininess of the tree is the largest value of shininess of its levels.

Nicoleta has a crush on a girl and wants to give her a part of Marge's beautiful tree. To do so, he will choose a node u and give his crush the subtree rooted at node u, including u. However, he doesn't want to get in (too much) trouble with Marge, so he will consider some candidates before making the cut.

Nicoleta has M candidate nodes to be the root of the cut subtree. For each candidate, Nicoleta wants to know what is the value of shininess of the remaining tree.

Input

The first line of the input contains a three integers N (2 ≤ N ≤ 105) and M (1 ≤ M ≤ 105) and w (0 ≤ w ≤ 104), indicating, respectively, the number of nodes of the tree, the number of candidate nodes and the shininess of node 1.

Each of the next N - 1 lines contains three integers u (2 ≤ u ≤ N) , v (1 ≤ v ≤ N) and w (0 ≤ w ≤ 104), indicating that node u is a child of node v and has shininess w.

M lines follow, each with a single integer u (2 ≤ u ≤ N), indicating the number of a candidate node.

Output

For each candidate node, in the order that they appear in the input, output a single line containing a single integer: the shininess of the remaining tree.

Example

Input
6 2 3
4 1 1
5 1 4
2 4 7
3 4 6
6 5 5
4
5
Output
5
13

Note

More about complete binary trees: https://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees

题解:题目给出一个完全二叉树,并定义一个此树中每层的权值为该层的每个点的权值之和,总的权为每层中最大的那个.

让我们求去掉以u为根的子树后,所得的剩下的树的权(shinness). 由于是完全二叉树,则每个点的儿子的下标为id*2,id*2+1,并且每一层的下标范围为[2^i,2^(i+1)-1](第i层,从0开始计数).那么去掉一颗子树后,损失的信息就可以区间求和来快速算出来, 总共的该层的权重之和也可以快速算出来.

用线段树,树状数组,ST表什么的都可以哇~~.

首先建立完全二叉树根据预处理得到的每个点的siz[i]数组大小(以i为根的子树元素有多少个).建立正确的完全二叉树,然后为其建立新的下标,对应下标赋予正确的权值.然后对于每一个询问u,我们首先找根节点1,看去掉u这一层能否影响到1这一层,然后向下递归就好啦.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
int val[maxn],dfn[maxn];
int siz[maxn],data[maxn];
int sum[maxn<<];
vector<int>G[maxn];
void dfs(int u)
{
siz[u]=;
for(int i=;i<G[u].size();i++){
dfs(G[u][i]);
siz[u]+=siz[G[u][i]];
}
} void DFS(int u,int index)
{
dfn[u]=index;
if(G[u].size()==)return ;
else if(G[u].size()==){
DFS(G[u][],index*);
}
else{
if(siz[G[u][]]>=siz[G[u][]]){
DFS(G[u][],index*);
DFS(G[u][],index*+);
}
else{
DFS(G[u][],index*+);
DFS(G[u][],index*);
}
}
}
void build(int l,int r,int rt)
{
if(l==r){
sum[rt]=data[l];
return ;
}
int mid=(l+r)/;
build(l,mid,rt*);
build(mid+,r,rt*+);
sum[rt]=sum[rt*]+sum[rt*+];
}
int querysum(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)return sum[rt];
int ans=;
int mid=(l+r)/;
if(L<=mid)ans+=querysum(L,R,l,mid,rt*);
if(R>=mid+)ans+=querysum(L,R,mid+,r,rt*+);
return ans;
}
int main()
{
int n,m,w;
cin >> n >> m >> w;
val[]=w;
for(int i = ; i <= n-; i++){
int u, v, w;
cin >> u >> v >> w;
G[v].push_back(u);
val[u]=w;
} dfs();
DFS(,);
for(int i = ; i <= n; i++){
data[dfn[i]]=val[i];
}
build(,n,);
while(m--){
int u,ans=;
cin >> u;
u=dfn[u];
int now1=,siz1=;
int now2=u,siz2=;
while(now1<=n){
if(now2>=now1&&now2<=min(n,now1+siz1-)){
ans=max(ans,querysum(now1,min(n,now1+siz1-),,n,)-querysum(now2,min(n,now2+siz2-),,n,));
now2*=;
siz2*=;
}
else{
ans=max(ans,querysum(now1,min(n,now1+siz1-),,n,));
}
now1*=;
siz1*=;
}
cout<<ans<<endl;
}
return ;
}

F - No Link, Cut Tree! Gym - 101484F的更多相关文章

  1. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  2. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  3. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

  4. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  5. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  6. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  7. AC日记——【模板】Link Cut Tree 洛谷 P3690

    [模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...

  8. 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

    一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...

  9. 学习笔记:Link Cut Tree

    模板题 原理 类似树链剖分对重儿子/长儿子剖分,Link Cut Tree 也做的是类似的链剖分. 每个节点选出 \(0 / 1\) 个儿子作为实儿子,剩下是虚儿子.对应的边是实边/虚边,虚实时可以进 ...

随机推荐

  1. filter滤镜兼容ie的rgba属性

    要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如下的属性即可: background: rgba(255,255,255,0.1); 但是要兼容到ie8.这个就有点 ...

  2. MyBatis整体架构

    Mybatis整体架构 基础支持层 反射模块 Java中的反射很强大,但是还是需要封装的.MyBatis专门提供了反射模块,对元素的反射进行了封装,提供了简洁的API,对反射进行了优化,例如缓存了类的 ...

  3. No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst

    今日遇到一个报错如下: No enclosing instance of type test is accessible. Must qualify the allocation with an en ...

  4. Social GAN代码要点记录

    近日在阅读Social GAN文献的实验代码,加深对模型的理解,发现源代码的工程化很强,也比较适合构建实验模型的学习,故细致阅读.下文是笔者阅读中一些要点总结,有关于pytorch,也有关于模型自身的 ...

  5. xxe

    XXE xml external entity injection xml外部实体注入 概述 xml是可扩展的标记语言,涉及被用来传输.存储数据 html被用来显示数据 其中xml的标签没有预定义的, ...

  6. 2020牛客寒假算法基础集训营4 H坐火车

    题目描述 牛牛是一名喜欢旅游的同学,在来到渡渡鸟王国时,坐上了颜色多样的火车. 牛牛同学在车上,车上有 n 个车厢,每一个车厢有一种颜色. 他想知道对于每一个正整数 $ x \in [1,\ n] $ ...

  7. linux 替换jdk指定jar包

    我的bug是:jdk1.8的安全策略和腾讯邮箱服务有冲突.我知道的解决方法: 1更换低版本安全策略相关的jar包.(windows:http://www.cnblogs.com/dennyzhangd ...

  8. PPT |《Kubernetes的兴起》

    京东云开发者社区技术沙龙--<Cloud Native时代的应用之路与开源创新> Part1-<Kubernetes的兴起> 欢迎点击"链接"了解更多精彩内 ...

  9. 吴裕雄--天生自然 PHP开发学习:While 循环

    <html> <body> <?php $i=1; while($i<=5) { echo "The number is " . $i . &q ...

  10. TCP_Wrappers简介

    转载自:http://www.cnblogs.com/duzhaoqi/ TCP_Wrappers     简介 TCP_Wrappers是一个工作在第四层(传输层)的的安全工具,对有状态连接的特定服 ...