题目传送门

题解:

先用题目给定的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. git指令-未完待更新

    git指令 1. $ git config --global user.name "Your Name" $ git config --global user.email &quo ...

  2. UE4 游戏模块初始化顺序

    最近看教学,有个讲解UE4初始化顺序的,记录一下. 首先创建一个Actor,Character,GameInstance,GameMode,LevelScriptActor(关卡),PlayerCon ...

  3. QQ第三方登录逻辑(微信,微博等同)

    实现过程:生成qq扫码登录连接(需要注册,链接里有几个参数需要按照开发文档的格式进行拼接,要后端完成),点击QQ登录按钮,前端Vue发送axios请求,后端收到请求把生成的QQ登录链接发送给vue,v ...

  4. iDevice取证的一大突破

    近日手机取证领域传出令人震撼的消息,知名取证大厂Cellebrite宣称可破解任何版本,任何机型的iDevice,连最新的iPhone X也逃不过. 若真属实,代表着iOS的取证又重现光明.只是不确定 ...

  5. Flink 从0到1学习—— 分享四本 Flink 国外的书和二十多篇 Paper 论文

    前言 之前也分享了不少自己的文章,但是对于 Flink 来说,还是有不少新入门的朋友,这里给大家分享点 Flink 相关的资料(国外数据 pdf 和流处理相关的 Paper),期望可以帮你更好的理解 ...

  6. redis分布式锁&队列应用

    分布式锁 setnx(set if not exists) 如果设值成功则证明上锁成功,然后再调用del指令释放. // 这里的冒号:就是一个普通的字符,没特别含义,它可以是任意其它字符,不要误解 & ...

  7. .net持续集成测试篇之Nunit参数化测试

    系列目录 在进行单元测试的时候,很多时候,很多时候我们都是在单元测试方法内部提供特定的值,但是这样测试往往造成样本数不足从而导致覆盖的结果不够全面,很多时候我们更想提供来自外部的,满足条件的一组值来进 ...

  8. CSS3 Flex 布局教程

    网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂 ...

  9. [趣学程序]java的常用类之String

    java基础之常用类 String类 String表示字符串,所谓字符串,就是一连串的字符,是java中最常用的类之一. String是不可变类,一旦String对象被创建,包含在对象中的字符序列(内 ...

  10. Go类型别名与类型定义区别

    类型别名和自定义类型区别 自定义类型 //自定义类型是定义了一个全新的类型 //将MyInt定义为int类型 type MyInt int 类型别名 //类型别名规定:TypeAlias只是Type的 ...