传送门

这是我第二次看见这个题目了,第一次看见是另一场比赛的A题,想了一个小时不会写就弃了

从来没想过这个题能这么玩

线段树上记录根到叶子节点的距离

初始线段树上先记下根节点1到各叶子节点的距离

先离线,然后dfs整颗树,在dfs过程中考虑根的移动对线段树的影响

如果当前点是查询点,在当前线段树上查询

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=5e5+10;
const long long inf=1e15;
int n,m,cnt,mx[maxn],v[maxn*2],pre[maxn*2],nxt[maxn*2],h[maxn],x[maxn],y[maxn],now=1;
struct oo{int l,r;long long mx,la;}s[maxn*4];
struct o{int v,l,r,id;}a[maxn];
long long ans[maxn];
void add(int x,int y,int z)
{
pre[++cnt]=y,nxt[cnt]=h[x],h[x]=cnt,v[cnt]=z;
pre[++cnt]=x,nxt[cnt]=h[y],h[y]=cnt,v[cnt]=z;
}
void update(int x){s[x].mx=min(s[x<<1].mx,s[x<<1|1].mx);}
void build(int x,int l,int r)
{
s[x].l=l,s[x].r=r;
if(l==r){s[x].mx=inf;return ;}
int mid=(l+r)>>1;
build(x<<1,l,mid),build(x<<1|1,mid+1,r);
update(x);
}
void pushdown(int x)
{
int ls=x<<1,rs=x<<1|1;
s[ls].mx+=s[x].la,s[rs].mx+=s[x].la;
s[ls].la+=s[x].la,s[rs].la+=s[x].la;
s[x].la=0;
}
void change(int x,int l,int r,long long v,int id)
{
if(l<=s[x].l&&r>=s[x].r){id?s[x].mx=v:s[x].mx+=v;s[x].la+=v;return ;}
if(s[x].la)pushdown(x);
int mid=(s[x].l+s[x].r)>>1;
if(l<=mid)change(x<<1,l,r,v,id);
if(r>mid)change(x<<1|1,l,r,v,id);
update(x);
}
void dfs(int x,int fa,long long dep)
{
mx[x]=x;
for(rg int i=h[x];i;i=nxt[i])
if(pre[i]!=fa)dfs(pre[i],x,dep+v[i]),mx[x]=max(mx[x],mx[pre[i]]);
if(mx[x]==x)change(1,x,x,dep,1);
}
bool cmp(o a,o b){return a.v<b.v;}
long long get(int x,int l,int r)
{
if(l<=s[x].l&&r>=s[x].r)return s[x].mx;
if(s[x].la)pushdown(x);
int mid=(s[x].l+s[x].r)>>1;long long ans=inf;
if(l<=mid)ans=min(ans,get(x<<1,l,r));
if(r>mid)ans=min(ans,get(x<<1|1,l,r));
update(x);return ans;
}
void dfs1(int x,int fa)
{
while(a[now].v==x)ans[a[now].id]=get(1,a[now].l,a[now].r),now++;
for(rg int i=h[x];i;i=nxt[i])
if(pre[i]!=fa)
{
change(1,1,n,v[i],0),change(1,pre[i],mx[pre[i]],-2*v[i],0);
dfs1(pre[i],x);
change(1,1,n,-v[i],0),change(1,pre[i],mx[pre[i]],2*v[i],0);
}
}
signed main()
{
read(n),read(m),build(1,1,n);
for(rg int i=2;i<=n;i++)read(x[i]),read(y[i]);
for(rg int i=n;i>=2;i--)add(i,x[i],y[i]);
dfs(1,0,0);
for(rg int i=1;i<=m;i++)read(a[i].v),read(a[i].l),read(a[i].r),a[i].id=i;
sort(a+1,a+m+1,cmp);
dfs1(1,0);
for(rg int i=1;i<=m;i++)printf("%lld\n",ans[i]);
}

CF1110F Nearest Leaf的更多相关文章

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

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

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

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

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

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

  4. CF - 1110F Nearest Leaf

    题目传送门 题解: 先用题目给定的dfs方式得到dfs序,记录下出入的dfs序. 很明显可以得知的是,以u为根的子树的dfs序在 in[u] - out[u] 的范围之内. 将每个询问先全部存到对应的 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. LeetCode 742. Closest Leaf in a Binary Tree

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

  9. Solution -「树上杂题?」专练

    主要是记录思路,不要被刚开始错误方向带偏了 www 「CF1110F」Nearest Leaf 特殊性质:先序遍历即为 \(1 \to n\),可得出:叶子节点编号递增或可在不改变树形态的基础上调整为 ...

随机推荐

  1. Nothing but the key 属性全部依赖于主键 third norm form

    全依赖 Designs that Violate 1NF CustomerCustomer ID First Name Surname Telephone Number123 Pooja Singh ...

  2. react面试宝典

    调用 setState 之后发生了什么? 在代码中调用setState函数之后,React 会将传入的参数对象与组件当前的状态合并,然后触发所谓的调和过程(Reconciliation).经过调和过程 ...

  3. SE18 BADI定义 / SE19 BADI 实现

    明天花30分 再研究下这个: 如果你知道一个BADI名称,可以: 1)使用SE18,输入该BADI名称后,选择Interface,然后查看对应的接口实施样例代码(Example implementat ...

  4. 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】

    02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...

  5. 生成chm格式帮助文档的步骤

    开场前,道具先得被齐全了. 道具:struts2的开源代码(以生成struts2的帮助文档为例).chm格式生成工具jd2chm.exe(网上有) 好了,准备演出 1.在eclipse中新建一个jav ...

  6. hibernate入门(-)

    1.struts2的支持 在web.xml中配置struts2的支持 <?xml version="1.0" encoding="UTF-8"?> ...

  7. Python:深浅拷贝

    导入模块: >>> import copy 深浅拷贝: >>> X = copy.copy(Y) #浅拷贝:只拷贝顶级的对象,或者说:父级对象 >>&g ...

  8. H264解码器源码(Android 1.6 版)

    H264解码器源码,移植ffmpeg中的H264解码部分到Android,深度删减优化,在模拟器(320x480)中验证通过. 程序的采用jni架构.界面部分,文件读取,视频显示都是用java做的,底 ...

  9. 基于WinDbg的内存泄漏分析

    在前面C++中基于Crt的内存泄漏检测一文中提到的方法已经可以解决我们的大部分内存泄露问题了,但是该方法是有前提的,那就是一定要有源代码,而且还只能是Debug版本调试模式下.实际上很多时候我们的程序 ...

  10. 在项目中添加全局的 pch 文件

    说明,本片博文仅仅是方便自己以后在添加 pch 文件的配置时候参照使用,担心一些配置的路径由于时间而遗忘. (1)建一个 pch 文件 注意下面要 在 Targets 后打上 对号 (2)对该文件进行 ...