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. Python 自省指南

    原作者:Patrick K. O'Brien 什么是自省? 在日常生活中,自省(introspection)是一种自我检查行为.自省是指对某人自身思想.情绪.动机和行为的检查.伟大的哲学家苏格拉底将生 ...

  2. oracle常用密令大全

    1.create user username identified by password;//建用户名和密码oracle ,oracle 2.grant connect,resource,dba t ...

  3. idea将web项目打成war包放在tomcat/webapps上运行

    1.进入Project Structure 或者 file -> Project Structure 或者 快捷键ctrl+alt+shift+s 2.选中Artifacts 3.点加号,然后如 ...

  4. 使用DOM4J生成XML文档

    package xml; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; imp ...

  5. JS照片轮换

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. x264报错No working C compiler found.

    现象: 缺少C++部署包 解决 [root@localhost x264]# yum -y install gcc gcc-c++ kernel-devel [root@localhost x264] ...

  7. c# 异步和同步 多线程

    在执行较为耗时的处理时,很容易出现用户界面“卡顿”现象,用异步编程模型,将耗时处理的代码放到另一个线程上执行,不会阻止用户界面线程的继续执行,应用程序 就不再出现“卡顿”现象. 本例子提供同步加载和异 ...

  8. 19 01 13 JQery 加载 选择器 样式操作

    在Javascript   中应该用下方法经行编辑 <script type="text/javascript" src="js/jquery-1.12.4.min ...

  9. == 与 equals区别(HashCode方法)

    1:==分析 1.2:基本类型比较 判断基本类型的数值是不是相等 1.3:对象类型比较 判断两个引用是不是指向同一个对象,即内存地址是不是相等. 2:equals分析 来判断对象内容是不是相等,一般有 ...

  10. Cf水题B - Combination

    地址: https://vjudge.net/problem/27861/origin Ilya plays a card game by the following rules. A player ...