CF1110F Nearest Leaf
传送门
这是我第二次看见这个题目了,第一次看见是另一场比赛的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的更多相关文章
- Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线
Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...
- Codeforces.1110F.Nearest Leaf(线段树)
题目链接 \(dls\)讲过这道题,所以这不是线段树裸题吗,这场没打气气气气气=-= 现在是写着玩=v= \(Description\) 给定一棵\(n\)个点的树.\(q\)次询问,每次询问给定\( ...
- CodeForces 1110F Nearest Leaf | 线段树/换根
我--又诈尸了-- 代码几乎都不会写了,打场CF居然上分啦,开心!(虽然还是比不过列表里的各路神仙) 题目链接 题目描述 一棵\(n\)个点的有根树,规定一种dfs序(规则:编号小的点优先dfs),\ ...
- CF - 1110F Nearest Leaf
题目传送门 题解: 先用题目给定的dfs方式得到dfs序,记录下出入的dfs序. 很明显可以得知的是,以u为根的子树的dfs序在 in[u] - out[u] 的范围之内. 将每个询问先全部存到对应的 ...
- [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 ...
- Solution -「树上杂题?」专练
主要是记录思路,不要被刚开始错误方向带偏了 www 「CF1110F」Nearest Leaf 特殊性质:先序遍历即为 \(1 \to n\),可得出:叶子节点编号递增或可在不改变树形态的基础上调整为 ...
随机推荐
- mongodb学习之:mongo安装以及远程访问
在linux下通过apt-get install mongo的方式一键式安装mongo 安装后mongo的配置文件位于/etc/mongodb.conf. 里面有mongo的各项配置,文件内容如下:重 ...
- 关于indexOf的使用
今天项目中出现一个bug,在筛选数据的时候出现了冗余数据,查找发现在indexOf方法判断的时候找到了问题的所在. package demo; public class test { public s ...
- [IR课程笔记]统计语言模型
Basic idea 1.一个文档(document)只有一个主题(topic) 2.主题指的是这个主题下文档中词语是如何出现的 3.在某一主题下文档中经常出现的词语,这个词语在这个主题中也是经常出现 ...
- ubuntu12.04出现ERROR: Removing 'hello': Device or resource busy和insmod: error inserting 'hello.ko': -1 Device or resource busy解决方案
一:insmod时候错误: 1:错误信息insmod: error inserting 'hello.ko': -1 Device or resource busy 2:原因:你的代码里面的设备号和系 ...
- Appium——连接真机,adb devices获取不到设备号
连接真机后,使用 adb devices获取不到设备号. 1.检查usb接口是否正常,是否正常链接到电脑 2.手机开发者模式是否开启,usb调试是否开启 3.检查驱动是否正常 4.如果驱动显示黄叹号, ...
- hdu-5742 It's All In The Mind(数学)
题目链接: It's All In The Mind Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- codeforces 569D D. Symmetric and Transitive(bell数+dp)
题目链接: D. Symmetric and Transitive time limit per test 1.5 seconds memory limit per test 256 megabyte ...
- Opencv— — Circle Filter
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- mpvue微信小程序分包
## 微信小程序分包(mpvue) 使用mpvue分包示例:1.下载vue脚手架(先有node环境,v8.12.0) npm install -g vue-cli 2.先用vue初始化一个mpvue小 ...
- springMVC之HttpServletRequest的getParameterMap()
request.getParameterMap()的返回类型是Map类型的对象,也就是符合key-value的对应关系,但这里要注意的是,value的类型是String[],而不是String. 得到 ...