这个就比较简单了~

Code:

#include <cstdio>
#include <algorithm>
#define N 100004
#define inf 1000000000
#define setIO(s) freopen(s".in","r",stdin) // , freopen(s".out","w",stdout)
using namespace std;
int n,edges;
int hd[N],to[N<<1],nex[N<<1],val[N<<1];
void add(int u,int v,int c)
{
nex[++edges]=hd[u],hd[u]=edges,to[edges]=v,val[edges]=c;
}
namespace tree
{
int size[N],son[N],dep[N],dis[N],fa[N],top[N];
void dfs1(int u,int ff)
{
size[u]=1,fa[u]=ff;
for(int i=hd[u];i;i=nex[i])
if(to[i]!=ff)
{
dis[to[i]]=dis[u]+val[i],dep[to[i]]=dep[u]+1;
dfs1(to[i],u),size[u]+=size[to[i]];
if(size[to[i]]>size[son[u]]) son[u]=to[i];
}
}
void dfs2(int u,int tp)
{
top[u]=tp;
if(son[u]) dfs2(son[u],tp);
for(int i=hd[u];i;i=nex[i])
if(to[i]!=fa[u]&&to[i]!=son[u])
dfs2(to[i],to[i]);
}
int LCA(int x,int y)
{
while(top[x]!=top[y])
dep[top[x]]>dep[top[y]]?x=fa[top[x]]:y=fa[top[y]];
return dep[x]<dep[y]?x:y;
}
int Dis(int x,int y)
{
return dis[x]+dis[y]-(dis[LCA(x,y)]<<1);
}
};
namespace seg
{
int tot;
#define lson t[x].ls
#define rson t[x].rs
struct Node
{
int ls,rs,min;
}t[N*100];
int newnode()
{
t[++tot].min=inf;
return tot;
}
void pushup(int x)
{
t[x].min=inf;
if(lson) t[x].min=min(t[x].min,t[lson].min);
if(rson) t[x].min=min(t[x].min,t[rson].min);
}
void update(int &x,int l,int r,int p,int v)
{
if(!x) x=newnode();
if(l==r)
{
t[x].min=min(t[x].min,v);
return;
}
int mid=(l+r)>>1;
if(p<=mid) update(lson,l,mid,p,v);
else update(rson,mid+1,r,p,v);
pushup(x);
}
int query(int x,int l,int r,int L,int R)
{
if(!x) return inf;
if(l>=L&&r<=R) return t[x].min;
int mid=(l+r)>>1,re=inf;
if(L<=mid) re=min(re,query(lson,l,mid,L,R));
if(R>mid) re=min(re,query(rson,mid+1,r,L,R));
return re;
}
#undef lson
#undef rson
};
int root,sn;
int size[N],Fa[N],mx[N],vis[N],rt[N];
void getroot(int u,int ff)
{
size[u]=1,mx[u]=0;
for(int i=hd[u];i;i=nex[i])
if(to[i]!=ff&&!vis[to[i]])
getroot(to[i],u),size[u]+=size[to[i]],mx[u]=max(mx[u],size[to[i]]);
mx[u]=max(mx[u],sn-mx[u]);
if(mx[u]<mx[root]) root=u;
}
void dfs(int u,int ff)
{
size[u]=1;
for(int i=hd[u];i;i=nex[i])
if(to[i]!=ff&&!vis[to[i]])
dfs(to[i],u),size[u]+=size[to[i]];
}
void calc(int u,int ff,int dep,int tp)
{
seg::update(rt[tp],1,n,u,dep);
for(int i=hd[u];i;i=nex[i])
if(to[i]!=ff&&!vis[to[i]])
calc(to[i],u,dep+val[i],tp);
}
void prepare(int u)
{
vis[u]=1,calc(u,0,0,u);
for(int i=hd[u];i;i=nex[i])
if(!vis[to[i]])
dfs(to[i],u),root=0,sn=size[to[i]],getroot(to[i],u),Fa[root]=u,prepare(root);
}
int query(int u,int l,int r)
{
int re=seg::query(rt[u],1,n,l,r),U=u;
for(;Fa[u];u=Fa[u])
re=min(re, seg::query(rt[Fa[u]],1,n,l,r)+tree::Dis(U,Fa[u]));
return re;
}
int main()
{
int i,j,m;
// setIO("input");
scanf("%d",&n);
for(i=1;i<n;++i)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c),add(a,b,c),add(b,a,c);
}
tree::dfs1(1,0),tree::dfs2(1,1);
root=0,mx[root]=sn=n,getroot(1,0),prepare(root);
scanf("%d",&m);
for(i=1;i<=m;++i)
{
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
printf("%d\n",query(x,l,r));
}
return 0;
}

  

LOJ #6145. 「2017 山东三轮集训 Day7」Easy 点分树+线段树的更多相关文章

  1. 【loj6145】「2017 山东三轮集训 Day7」Easy 动态点分治+线段树

    题目描述 给你一棵 $n$ 个点的树,边有边权.$m$ 次询问,每次给出 $l$ .$r$ .$x$ ,求 $\text{Min}_{i=l}^r\text{dis}(i,x)$ . $n,m\le ...

  2. #6145. 「2017 山东三轮集训 Day7」Easy 动态点分治

    \(\color{#0066ff}{题目描述}\) JOHNKRAM 最近在参加 C_SUNSHINE 举办的聚会. C 国一共有 n 座城市,这些城市由 n−1 条无向道路连接.任意两座城市之间有且 ...

  3. 「2017 山东三轮集训 Day7」Easy

    一棵带边权的树,多次询问 $x$ 到编号为 $[l,r]$ 的点最短距离是多少 $n \leq 100000$ sol: 动态点分治,每层重心维护到所有点的距离 查询的时候在管辖这个点的 log 层线 ...

  4. Loj #6142. 「2017 山东三轮集训 Day6」A

    link: https://loj.ac/problem/6142 推完一波式子之后发现求的是:ΣC(N,i)^2, 其中i是偶数. 然后就可以卢卡斯乱搞了,分奇偶和之前的答案合并就好了233. #i ...

  5. loj #6138. 「2017 山东三轮集训 Day4」Right

    题目: 题解: 暴力一波 \(SG\) 函数可以发现这么一个规律: \(p\) 为奇数的时候 : \(SG(n) = n \% 2\) \(p\) 为偶数的时候 : \(SG(n) = n \% (p ...

  6. loj #6136. 「2017 山东三轮集训 Day4」Left

    题目: 题解: 我们可以发现所有的交换器都是一个位置连接着下一层左侧的排序网络,另一个位置连着另一侧的排序网络. 而下一层是由两个更低阶的排序网络构成的. 两个网络互不干扰.所以我们可以通过第一行和最 ...

  7. 「2017 山东三轮集训 Day7 解题报告

    「2017 山东三轮集训 Day7」Easy 练习一下动态点分 每个点开一个线段树维护子树到它的距离 然后随便查询一下就可以了 注意线段树开大点... Code: #include <cstdi ...

  8. 「2017 山东三轮集训 Day1」Flair

    模拟赛的题 好神仙啊 题面在这里 之前的Solution很蠢 现在已经update.... 题意 有$ n$个商品价格均为$ 1$,您有$ m$种面值的货币,面值为$ C_1..C_m$ 每种物品你有 ...

  9. 【loj6142】「2017 山东三轮集训 Day6」A 结论题+Lucas定理

    题解: 当奇数 发现答案就是C(n,1)^2+C(n,3)^2+...C(n,n)^2 倒序相加,发现就是C(2n,n) 所以答案就是C(2n,n)/2 当偶数 好像并不会证 打表出来可以得到 2.当 ...

随机推荐

  1. 2019 徐州icpc网络赛 E. XKC's basketball team

    题库链接: https://nanti.jisuanke.com/t/41387 题目大意 给定n个数,与一个数m,求ai右边最后一个至少比ai大m的数与这个数之间有多少个数 思路 对于每一个数,利用 ...

  2. 【DP 好题】Kick Start 2019 Round C Catch Some

    题目链接 题目大意 在一条数轴上住着 $N$ 条狗和一个动物研究者 Bundle.Bundle 的坐标是 0,狗的坐标都是正整数,可能有多条狗住在同一个位置.每条狗都有一个颜色.Bundle 需要观测 ...

  3. 认识并学会springCloud的使用

    SpringCloud将现在一些流行的技术整合到一起,实现如:配置管理,服务发现,智能路由,负载均衡,熔断器,控制总线,集群状态等等功能.主要涉及的组件有 netflix Eureka:注册中心 Zu ...

  4. Ubuntu中配置Python虚拟环境Virtualenv

    Ubuntu版本为18.04 Virtualenv介绍 在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-pac ...

  5. 一些通用的js工具类,添加自定义插件

    common_t.js /** * 通用工具组件 对原有的工具进行封装,自定义某方法统一处理<br> * ^_^ * * Author: em.D * Date: 2016-05-17 * ...

  6. poj2226-Muddy Fields二分匹配 最小顶点覆盖 好题

    题目 给到一个矩阵,有些格子上是草,有些是水.需要用宽度为1,长度任意的若干块木板覆盖所有的水,并不能覆盖草,木板可以交叉,但只能横竖放置,问最少要多少块板. 分析 经典的矩阵二分图构图和最小点覆盖. ...

  7. C++ 对象的构造

    在类里面成员函数的初始值是多少了?(取决于创建对象的位置,是在堆.栈.还是在静态存储区中创建.) 例如: #include <stdio.h> class Test { private: ...

  8. 给Repater增加等号

    //不改变数据结构的情况下,增加行号.对Application服务器压力增大,减少DB服务器压力.    protected void repShow_ItemDataBound(object sen ...

  9. vs编译项目报错:The OutputPath property is not set for this project

    今天使用VS2008编译项目时报错: The OutputPath property is not set for this project.  Please check to make sure t ...

  10. python django网站编程视频教程学习资料下载

    “人生苦短,我用python”,学python的小伙伴应该都了解这句话的含义.但是,学python,你真正了了解强大的Django框架吗!?据说Django还是由吉普赛的一个吉他手的名字命名的呢,有木 ...