题目传送门

题解:

先用题目给定的dfs方式得到dfs序,记录下出入的dfs序。

很明显可以得知的是,以u为根的子树的dfs序在 in[u] - out[u] 的范围之内。

将每个询问先全部存到对应的节点上。

然后我们以1为root,先求出每个叶子节点到1的距离。

对1的询问查询更新完答案之后。

(假设1和2之间有一条权值为w的边)

那么以2为根的话,就相当于是 in[2] --- out[2] 区间里面的所有值的距离 -= w, 其他点的距离 += w。

然后对于每个点都这样处理。

注意修改和还原更新。

代码:

/*
code by: zstu wxk
time: 2019/02/09
*/
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 5e5 + ;
vector<pll> vc[N];
int in[N], out[N], tot;
LL a[N];
int n, q;
void dfs(int u, LL ct){
in[u] = ++tot;
if(vc[u].size()) a[tot] = INF;
else a[tot] = ct;
for(pll p : vc[u]){
dfs(p.fi, ct+p.se);
}
out[u] = tot;
}
LL Mn[N<<], lz[N<<];
void PushUp(int rt){
Mn[rt] = min(Mn[rt<<], Mn[rt<<|]);
}
void PushDown(int rt){
if(lz[rt]){
lz[rt<<] += lz[rt];
lz[rt<<|] += lz[rt];
Mn[rt<<] += lz[rt];
Mn[rt<<|] += lz[rt];
lz[rt] = ;
}
return ;
}
void Build(int l, int r, int rt){
if(l == r){
Mn[rt] = a[l];
return ;
}
int m = l+r >> ;
Build(lson); Build(rson);
PushUp(rt);
return ;
}
void Updata(int L, int R, LL C, int l, int r, int rt){
if(L <= l && r <= R){
lz[rt] += C;
Mn[rt] += C;
return ;
}
PushDown(rt);
int m = l+r >> ;
if(L <= m) Updata(L, R, C, lson);
if(m < R) Updata(L, R, C, rson);
PushUp(rt);
return ;
}
LL Query(int L, int R, int l, int r, int rt){
if(L <= l && r <= R){
return Mn[rt];
}
PushDown(rt);
int m = l+r >> ;
LL ret = INF;
if(L <= m) ret = Query(L, R, lson);
if(m < R) ret = min(ret, Query(L, R, rson));
return ret;
}
#define mp make_pair
vector<pair<pll,int>> Q[N];
LL ans[N];
void Dfs(int u, int ct){
Updata(, n, ct, , n, );
Updata(in[u], out[u], -*ct, , n, );
for(auto x : Q[u]){
ans[x.se] = Query(x.fi.fi, x.fi.se, , n, );
}
for(pll x : vc[u]){
Dfs(x.fi, x.se);
}
Updata(, n, -ct, , n, );
Updata(in[u], out[u], *ct, , n, );
return ;
}
void Ac(){
for(int i = , v, w; i <= n; ++i){
scanf("%d%d", &v, &w);
vc[v].pb({i,w});
}
dfs(, );
Build(, n, );
for(int i = ,u,l,r; i <= q; ++i){
scanf("%d%d%d", &u, &l, &r);
Q[u].pb(mp(mp(l,r),i));
}
Dfs(, );
for(int i = ; i <= q; ++i)
printf("%I64d\n", ans[i]);
}
int main(){
while(~scanf("%d%d", &n, &q)){
Ac();
}
return ;
}

CF - 1110F Nearest Leaf的更多相关文章

  1. CodeForces 1110F Nearest Leaf | 线段树/换根

    我--又诈尸了-- 代码几乎都不会写了,打场CF居然上分啦,开心!(虽然还是比不过列表里的各路神仙) 题目链接 题目描述 一棵\(n\)个点的有根树,规定一种dfs序(规则:编号小的点优先dfs),\ ...

  2. Codeforces.1110F.Nearest Leaf(线段树)

    题目链接 \(dls\)讲过这道题,所以这不是线段树裸题吗,这场没打气气气气气=-= 现在是写着玩=v= \(Description\) 给定一棵\(n\)个点的树.\(q\)次询问,每次询问给定\( ...

  3. Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线

    Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...

  4. CF 1042 F. Leaf Sets

    F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...

  5. CF1110F Nearest Leaf

    传送门 这是我第二次看见这个题目了,第一次看见是另一场比赛的A题,想了一个小时不会写就弃了 从来没想过这个题能这么玩 线段树上记录根到叶子节点的距离 初始线段树上先记下根节点1到各叶子节点的距离 先离 ...

  6. [LeetCode] Closest Leaf in a Binary Tree 二叉树中最近的叶结点

    Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...

  7. 742. Closest Leaf in a Binary Tree查找最近的叶子节点

    [抄题]: Given a binary tree where every node has a unique value, and a target key k, find the value of ...

  8. Leetcode: Closest Leaf in a Binary Tree

    Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...

  9. LeetCode 742. Closest Leaf in a Binary Tree

    原题链接在这里:https://leetcode.com/problems/closest-leaf-in-a-binary-tree/ 题目: Given a binary tree where e ...

随机推荐

  1. 【iOS】containsString iOS7 报错

    前几天发现了这个问题,原来是因为 containsString 只支持 iOS8.0 以后的系统,不支持 7... 有些地方可以用其他方法替代,如下: NSString *urlString = [[ ...

  2. maven-build-downloading

    1. 场景描述 maven库用的是公司私服和阿里云结合的方式(maven多仓库配置),本项目maven依赖的有其他项目组的jar包(单点登录),但是天有不测风云,依赖单点登录的好几个jar包,在编译( ...

  3. vue 移动端/PC常见问题及解决方法

    一.判断手机/PC浏览器语言 navigator.language // 返回语言代码 语言代码文档: http://www.lingoes.cn/zh/translator/langcode.htm ...

  4. netty使用EmbeddedChannel对channel的出入站进行单元测试

    一种特殊的Channel实现----EmbeddedChannel,它是Netty专门为改进针对ChannelHandler的单元测试而提供的. 名称 职责 writeInbound 将入站消息写到E ...

  5. win系统上Anaconda国内镜像配置

    清华镜像2019.6.15已恢复 中科大镜像2019.7.1停机维护后恢复 1.打开anaconda prompt 2.添加清华镜像1:https://mirrors.tuna.tsinghua.ed ...

  6. 如何阅读JDK源码

    JDK源码阅读笔记: https://github.com/kangjianwei/LearningJDK 如何阅读源码,是每个程序员需要面临的一项挑战. 为什么需要阅读源码?从实用性的角度来看,主要 ...

  7. Spark 系列(三)—— 弹性式数据集RDDs

    一.RDD简介 RDD 全称为 Resilient Distributed Datasets,是 Spark 最基本的数据抽象,它是只读的.分区记录的集合,支持并行操作,可以由外部数据集或其他 RDD ...

  8. GooglePlay新版排行榜接入

    新版本的GMS的api和老版本的有很大的差异,刚接了一下,在这里留一个记号,以便查阅:判定是否已经登录 private static boolean isSignedIn(Cocos2dxActivi ...

  9. tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)

    tensorflow学习笔记——使用TensorFlow操作MNIST数据(1) 一:神经网络知识点整理 1.1,多层:使用多层权重,例如多层全连接方式 以下定义了三个隐藏层的全连接方式的神经网络样例 ...

  10. JWT+Interceptor实现无状态登录和鉴权

    无状态登录原理 先提一下啥是有状态登录 单台tomcat的情况下:编码的流程如下 前端提交表单里用户的输入的账号密码 后台接受,查数据库, 在数据库中找到用户的信息后,把用户的信息存放到session ...