题目传送门

题解:

先用题目给定的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. Thinkphp 5.1.7 parseData缺陷导致insert/update注入 分析

    目录 环境搭建 分析 参考 环境搭建 $ composer create-project topthink/think thinkphp-5.1.7 修改composer.json 5.1.* =&g ...

  2. AI and Robot

    Have you ever seen a movie called "Ex Machina"?  I like this movie very much. Artificial i ...

  3. 基于Spring注解的上下文初始化过程源码解析(二)

    上一篇看完了register方法的代码,继续跟后面代码 后面执行refresh方法,代码清单如下: public void refresh() throws BeansException, Illeg ...

  4. Java模拟并解决缓存穿透

    什么叫做缓存穿透 缓存穿透只会发生在高并发的时候,就是当有10000个并发进行查询数据的时候,我们一般都会先去redis里面查询进行数据,但是如果redis里面没有这个数据的时候,那么这10000个并 ...

  5. DevOps实施历程-v1.0

    ​    有AF项目的成功案例(DevOps实施历程-半自动化),公司新项目全部依此为模板,实现了从代码到安装的自动化流水线,为此我输出了Jenkins自动化指南.AF项目指南等文档,方便大家查阅和参 ...

  6. mysql limit分页查询效率比拼

    1.直接使用数据库提供的SQL语句 limit M ,N SELECT * from message limit 0 , 10 ; -- 0.044 SELECT * from message lim ...

  7. spark学习(10)-RDD的介绍和常用算子

    RDD(弹性分布式数据集,里面并不存储真正要计算的数据,你对RDD的操作,他会在Driver端转换成Task,下发到Executor计算分散在多台集群上的数据) RDD是一个代理,你对代理进行操作,他 ...

  8. 消息中间件-activemq安全机制

    activemq作为消息中间件这样一个独立的个体存在,连通用户和服务器.如果没有一套完备的安全机制去设置用户权限设置消息分发机制可想后果是非常严重.ActiveMQ如果不加入安全机制的话,任何人只要知 ...

  9. Python模块之requests,urllib和re

    目录 一.爬虫的步骤 二.使用Jupyter 三.爬虫请求模块之urllib 四.爬虫请求模块之requests 五.爬虫分析之re模块 一.爬虫的步骤 1.发起请求,模拟浏览器发送一个http请求 ...

  10. 2018年蓝桥杯b组国赛真题

    1.标题:换零钞x星球的钞票的面额只有:100元,5元,2元,1元,共4种.小明去x星旅游,他手里只有2张100元的x星币,太不方便,恰好路过x星银行就去换零钱.小明有点强迫症,他坚持要求200元换出 ...