传送门

这是我第二次看见这个题目了,第一次看见是另一场比赛的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. Android Handle,Looper,Message消息机制

    尊重原创,转载请标明出处    http://blog.csdn.net/abcdef314159 我们知道在Android中更新UI都是在主线程中,而操作一些耗时的任务则须要在子线程中.假设存在多个 ...

  2. python数据分析之ipython

    在用python进行数据分析的时候,需要提前安装如下几个库: Numpy:是python进行科学计算的科学包 pandas:提供了能够快速便捷地处理结构化数据的大量数据结构和函数 matplotlib ...

  3. 开发指南专题十一:JEECG微云高速开发平台--基础用户权限

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/26580037     开发指南专题 ...

  4. socket 学习笔记

    #include <sys/socket.h> ---------------------------------------------------------------------- ...

  5. Gradients渐变属性

    一个很不错的网站http://www.w3schools.com/css/css3_gradients.asp http://www.w3cplus.com/css3/new-css3-linear- ...

  6. Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图

    题目链接:http://codeforces.com/problemset/problem/788/B B. Weird journey time limit per test 2 seconds m ...

  7. Python作业之用户管理

    作业 流程图没有画,懒,不想画 readme没有写,懒,不想写.看注释吧233333 #! /usr/bin/env python # -*- coding: utf-8 -*- # __author ...

  8. java编程思想-基础

    interface: 方法默认为public:成员变量默认 static and final 对象数组的定义:理解? 多接口继承:可以多个接口,但只有一个具体类,具体类在前面 自:多接口继承时,来自不 ...

  9. 让tomcat启动时,自动加载你的项目

    在tomcat-->conf-->serve.xml文件最后加上 <Context path="/atest" docBase="E:\Workspac ...

  10. linux应用之gcc编译器的安装及使用

    gcc是linux系统下功能十分强大的编译器. 本人使用的是CentOS 6.6 64位系统,由于在安装系统的时候并没有勾选安装gcc编译器,因此需要自行安装gcc编译器. 使用yum安装gcc 对于 ...