F - No Link, Cut Tree! Gym - 101484F
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
6 2 3
4 1 1
5 1 4
2 4 7
3 4 6
6 5 5
4
5
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的更多相关文章
- link cut tree 入门
		鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ... 
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
		为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ... 
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
		link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ... 
- Link Cut Tree学习笔记
		从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ... 
- Link Cut Tree 总结
		Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ... 
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
		题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ... 
- AC日记——【模板】Link Cut Tree 洛谷 P3690
		[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ... 
- 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree
		一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ... 
- 学习笔记:Link Cut Tree
		模板题 原理 类似树链剖分对重儿子/长儿子剖分,Link Cut Tree 也做的是类似的链剖分. 每个节点选出 \(0 / 1\) 个儿子作为实儿子,剩下是虚儿子.对应的边是实边/虚边,虚实时可以进 ... 
随机推荐
- JS ~  Promise 对象
			Promise 对象用于表示一个异步操作的最终状态(完成或失败),以及该异步操作的结果值. Promise.all(iterable) 这个方法返回一个新的promise对象,该promise对象在i ... 
- 【LeetCode】验证二叉搜索树
			[问题]给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征:节点的左子树只包含小于当前节点的数.节点的右子树只包含大于当前节点的数.所有左子树和右子树自身必须也是二叉搜 ... 
- java课程课后作业190530之用户体验评价
			每个人评价一下大家手头正在使用输入法或者搜索类的软件产品. 从用户界面.记住用户选择.短期刺激.长期使用的好处坏处.不要让用户犯简单的错误四个方面发表一篇博客. 输入法:苹果自带的输入法 用户界面:简 ... 
- 【数据结构】二叉树的遍历(前、中、后序及层次遍历)及leetcode107题python实现
			文章目录 二叉树及遍历 二叉树概念 二叉树的遍历及python实现 二叉树的遍历 python实现 leetcode107题python实现 题目描述 python实现 二叉树及遍历 二叉树概念 二叉 ... 
- python----linux下简单的排序
			1.选择排序:把一个数与余下所有的数排序,最小的排到最前面 [root@besttest liyn_test]# cat test.py #! /usr/bin/python a=[,,,] ,len ... 
- JavaScript—飞机大战
			今天来写个游戏,飞机大战 1,布局 2,思路 1,动态创建自己的飞机 让它在规定的区域,跟着鼠标运动. 2,在自己飞机的上方,间隔1s生成子弹.子弹往上移动 当top:0 子弹消失 3,每隔1s 产生 ... 
- autorclone使用心得
			一边使用一边更新. 0x00 SAs最坑的那地方在于,当我新建了一个group,却只能每天添加100个SAs.但是autorclone在本地调用的SAs却有500个,这样每次copy的时候,auto ... 
- Momentum
			11.6 Momentum 在 Section 11.4 中,我们提到,目标函数有关自变量的梯度代表了目标函数在自变量当前位置下降最快的方向.因此,梯度下降也叫作最陡下降(steepest desce ... 
- 基础练习--huffman
			问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, pn-},用这列数构造Huffman树的过程如下: . 找 ... 
- JAVA中常用的异常处理情况
			1.java.lang.nullpointerexception程序遇上空指针 这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者 ... 
