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. oracle批量修改字段长度

    alter table 表名 modify (字段名1 字段类型1(长度1),字段名2 字段类型2(长度2)) alter table 表名 modify column_name varchar2(3 ...

  2. java String字符串判断

    判断空字符串:StringUtils.isBlank StringUtils.isBlank(null) = true StringUtils.isBlank("") = true ...

  3. tyvj 1860 后缀数组

    真·模板题(然而还是TLE了,tyvj真是个毒瘤,输出double什么的就是 -0.00000000,这些就TLE2333) 简单的说一下本蒟蒻看了一天后缀数组的收获(这东西太神了,,,wcwc,,收 ...

  4. HttpServlet中文乱码问题

    客户端提交数据给服务器端(Requset) 如果数据中带有中文的话,有可能会出现乱码情况,那么可以参照以下方法解决. 如果是GET方式 1.代码转码 String username = request ...

  5. django-替代为自定义的User model

    https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model Subs ...

  6. PAT 2019 春

    算是第二次做这套题吧,感觉从上次考试到现在自己有了挺大提高,提前30min做完了. 7-1 Sexy Primes 读懂题意就行. #include <cstdio> #include & ...

  7. OO第三单元“技术”博客

    主要针对第三单元的三次作业 JML语言的理论基础.应用工具链情况 JML指的是Java建模语言,全称是Java modeling language,是一种行为接口规范语言,可用于指定Java模块的行为 ...

  8. Sequence Models Week 1 Building a recurrent neural network - step by step

    Building your Recurrent Neural Network - Step by Step Welcome to Course 5's first assignment! In thi ...

  9. .NET技术-5.0. NETCORE设置返回数据字段的大小写

    .NET技术-5.0. NETCORE设置返回数据字段的大小写 问题来源于我写了一个接口,接口的返回类型是JsonResult,但是对接之后反应返回结果的首字母全小写了, 后来查了写资料返现.net ...

  10. 当切换用户时出现-bash-4.1$

    问题重现 [root@localhost ~]# su - yh -bash-4.1$ -bash-4.1$ -bash-4.1$ -bash-4.1$ -bash-4.1$ cd /home -ba ...