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\),可得出:叶子节点编号递增或可在不改变树形态的基础上调整为 ...
随机推荐
- ElasticSearch(三)mac安装
1.首先要安装jdk 2.到官网或是用brew下载ElasticSearch 安装包,这边我们选择在官网下载对应的安装包 https://www.elastic.co/cn/downloads/ela ...
- Android笔记之manifestPlaceholders
有时根据项目需要,AndroidManifest.xml中的meta-data的值分测试和正式 为了能自动地更换meta-data值,就需要用到manifestPlaceholders 语法:mani ...
- tornado安全应用之cookie
目前大多数服务器判断用户是否登录一般通过session机制,Tornado 通过 set_secure_cookie 和 get_secure_cookie 方法直接支持了这种功能.原理类似于sess ...
- SpringBoot-(2)-Web的json接口,静态网页,动态页面
一, 了解注解@Controller和@RestController @Controller:处理Http请求 @RestController:Spring4以后新增注解,相当于@Controller ...
- TabHost的坑
问题1. 运行Activity的时候出现Your content must have a TabHost whose id attribute is ‘android.R.id.tabhost’ 问 ...
- SDUT OJ 之 1571 《人品,有没有?》 字符串的处理问题
人品,有木有? Time Limit: 1000ms Memory limit: 32768K 有疑问?点这里^_^ 题目描述 新一届的山东理工大学ACM网络擂台赛就要开始啦!听说晋级的选手有机 ...
- POJ2115 C Looooops ——模线性方程(扩展gcd)
题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- suishou
sageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn you ...
- SQL server 备份/恢复/压缩 进度查询
第一步,用 sp_who2 查出备份的sid(或在窗口中的连接属性中看) exec sp_who2 第二步,用以下查询获得运行情况(看 percent_complete列) SELECT sessio ...
- 使用Node.js实现简单的网络爬取
由于最近要实现一个爬取H5游戏的代理服务器,隧看到这么一篇不错的文章(http://blog.miguelgrinberg.com/post/easy-web-scraping-with-nodejs ...