题目传送门

题解:

先用题目给定的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. CSDN Markdown 超链接

    CSDN Markdown 的超链接总是在当前页面打开新的链接,后来发现了一种可以在新窗口打开超链接的语法,如下: <a href="https://zh.wikipedia.org/ ...

  2. Ping、Traceroute工作原理

    在工作开发过程中,我们经常会使用到ping和traceroute.在这里,我们将细述其工作原理,让你在会用的基础之上理解其内部工作过程. ICMP应用实例--Ping Ping 是 ICMP 的一个重 ...

  3. MySql性能优化读书比较<一> 数据类型

    一,选择优化的数据类型 1.更小的通常更好. 更小的数据类型通常占用更少的磁盘,内存和cpu缓存,通常更快. 2.简单就好 简单的数据类型操作,通常需要更少的CPU周期. 3.尽量避免NULL值 列可 ...

  4. 【POJ - 3273】Monthly Expense (二分)

    Monthly Expense 直接上中文 Descriptions 给你一个长度为N的序列,现在要让你把他们切割成M份(所以每一份都是连续的),然后每一份都有一个和sum[i],其中最大的一个是ma ...

  5. AppBoxFuture: 123挨个站-数据按序存储

      最近几天在优化存储的编码规则,顺带把之前设计了但未实现的倒排序一并实现了.由于所有数据(元数据.实体.索引等)都映射至RocksDB的Key-Value存储,所以必须扩展RocksDB的自定义比较 ...

  6. Storm初识(1)

    在Storm集群中,有两类节点:主节点 master node 和工作节点 worker nodes. 主节点运行着一个叫做Nimbus的守护进程.这个守护进程负责在集群中分发代码,为工作节点分配任务 ...

  7. Linux软件的安装

    yum -y groups install "GNOME Desktop"  安装桌面系统startx  安装完成后输入指令进入到桌面化指令 安装tomcat sudo yum i ...

  8. 做梦也没有想到:Windows 上的 .NET Core 表现更糟糕

    昨天晚上 18:15 左右我们发布了跑在 Windows 上 .NET Core 博客系统,本想与 .NET Framework 版进行同“窗”的较量,结果刚发布上线就发现 CPU 占用异常高,发布不 ...

  9. React Native 混合开发与实现

    关于 微信公众号:前端呼啦圈(Love-FED) 我的博客:劳卜的博客 知乎专栏:前端呼啦圈 前言 随着 React 的盛行,其移动开发框架 React Native 也收到了广大开发者的青睐,以下简 ...

  10. Java学习|强引用,软引用,弱引用,幻想引用有什么区别?

    在Java语言中,除了基本数据类型外,其他的都是指向各类对象的对象引用:Java中根据其生命周期的长短,将引用分为4类. 1 强引用 特点:我们平常典型编码Object obj = new Objec ...