CF - 1110F Nearest Leaf
题解:
先用题目给定的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的更多相关文章
- CodeForces 1110F Nearest Leaf | 线段树/换根
我--又诈尸了-- 代码几乎都不会写了,打场CF居然上分啦,开心!(虽然还是比不过列表里的各路神仙) 题目链接 题目描述 一棵\(n\)个点的有根树,规定一种dfs序(规则:编号小的点优先dfs),\ ...
- Codeforces.1110F.Nearest Leaf(线段树)
题目链接 \(dls\)讲过这道题,所以这不是线段树裸题吗,这场没打气气气气气=-= 现在是写着玩=v= \(Description\) 给定一棵\(n\)个点的树.\(q\)次询问,每次询问给定\( ...
- Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线
Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...
- CF 1042 F. Leaf Sets
F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...
- CF1110F Nearest Leaf
传送门 这是我第二次看见这个题目了,第一次看见是另一场比赛的A题,想了一个小时不会写就弃了 从来没想过这个题能这么玩 线段树上记录根到叶子节点的距离 初始线段树上先记下根节点1到各叶子节点的距离 先离 ...
- [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 ...
- 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 ...
- 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 ...
- LeetCode 742. Closest Leaf in a Binary Tree
原题链接在这里:https://leetcode.com/problems/closest-leaf-in-a-binary-tree/ 题目: Given a binary tree where e ...
随机推荐
- Mac环境下升级gcc版本--rocksdb
前言 在mac环境下编译rocksdb,需要配置依赖的编译环境,其中有一项比较麻烦:c++编译要支持C++11,但是在mac环境安装xcode-select --install之后,已经安装有了gcc ...
- LinkedList源码分析(jdk1.8)
LinkedList概述 LinkedList 是 Java 集合框架中一个重要的实现,我们先简述一下LinkedList的一些特点: LinkedList底层采用的双向链表结构: LinkedL ...
- Something wrong with EnCase v8 index search results
My friend told me that she installed EnCase v8.05 on her workstation which OS version is Win 10. She ...
- Hexo结合github制作博客
https://blog.csdn.net/Hoshea_chx/article/details/78826689 hexo(themes) vuePress jekylly
- sed流编辑器
一.前言 (一).sed 工作流程 sed 是一种在线的.非交互式的流编辑器,它一次处理一行内容.处理时,把当做前处理的行存储在临时缓存区中,成为“模式空间”(pattern space),接着用se ...
- 01-Spring Security框架学习
目录 01-Spring Security框架学习 简介 Spring Security 是什么 Spring Security 解决那些问题 Spring Security 的优点 历史背景 Spr ...
- C#连接SQL Anywhere 12 数据库
using System;using System.Data.Common; namespace ConsoleApplication27{ class Program { ...
- mysql 查询结果显示行号
mysql 查询时,不像oracle那样,可以直接用 rownum 显示结果行号. 可以用定义用户变量来实现 set @myrnum = 0; select (@myrnum := @myrnum + ...
- javascript中的浅拷贝和深拷贝(拷贝引用和拷贝实例)
作者:千锋教育链接:https://www.zhihu.com/question/23031215/answer/326129003来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- 【Kubernetes 系列四】Kubernetes 实战:管理 Hello World 集群
目录 1. 创建集群 1.1. 安装 kubectl 1.1.1. 安装 kubectl 到 Linux 1.1.2. 安装 kubectl 到 macOS 1.1.3. 安装 kubectl 到 W ...