51nod 1766 树上的最远点对(线段树)
像树的直径一样,两个集合的最长路也是由两个集合内部的最长路的两个端点组成的,于是我们知道了两个集合的最长路,枚举一下两两端点算出答案就可以合并了,所以就可以用线段树维护一个区间里的最长路了。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=,inf=1e9;
struct tjm{int too,dis,pre;}e[maxn];
struct poi{int p[];ll dis;}tree[maxn<<];
int n,m,a,b,x,y,z,tot;
int d[maxn],son[maxn],size[maxn],fa[maxn],top[maxn],last[maxn];
ll dep[maxn];
inline void read(int &k)
{
int f=;k=;char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(c<=''&&c>='')k=k*+c-'',c=getchar();
k*=f;
}
void add(int x,int y,int z){e[++tot].too=y;e[tot].dis=z;e[tot].pre=last[x];last[x]=tot;}
void dfs1(int x)
{
size[x]=;d[x]=d[fa[x]]+;
for(int i=last[x],too=e[i].too;i;i=e[i].pre,too=e[i].too)
if(too!=fa[x])
{
dep[too]=dep[x]+e[i].dis;
fa[too]=x;dfs1(too);
size[x]+=size[too];
if(size[too]>size[son[x]])son[x]=too;
}
}
void dfs2(int x,int tp)
{
top[x]=tp;
if(son[x])dfs2(son[x],tp);
for(int i=last[x],too=e[i].too;i;i=e[i].pre,too=e[i].too)
if(too!=fa[x]&&too!=son[x])dfs2(too,too);
}
int lca(int x,int y)
{
int f1=top[x],f2=top[y];
while(f1!=f2)
{
if(d[f1]<d[f2])swap(x,y),swap(f1,f2);
x=fa[f1];f1=top[x];
}
if(d[x]<d[y])swap(x,y);
return y;
}
void pushup(poi x,poi y,ll &dist,int &p1,int &p2)
{
if(x.dis>y.dis)dist=x.dis,p1=x.p[],p2=x.p[];
else dist=y.dis,p1=y.p[],p2=y.p[];
for(int i=;i<=;i++)
for(int j=;j<=;j++)
if(x.p[i]&&y.p[j])
{
ll dis=dep[x.p[i]]+dep[y.p[j]]-(dep[lca(x.p[i],y.p[j])]<<);
if(dis>dist)dist=dis,p1=x.p[i],p2=y.p[j];
}
}
void build(int x,int l,int r)
{
if(l==r){tree[x].p[]=tree[x].p[]=l;return;}
int mid=(l+r)>>;
build(x<<,l,mid);build(x<<|,mid+,r);
pushup(tree[x<<],tree[x<<|],tree[x].dis,tree[x].p[],tree[x].p[]);
}
void query(int x,int l,int r,int cl,int cr,ll &dis,int &p1,int &p2)
{
if(cl<=l&&r<=cr){dis=tree[x].dis;p1=tree[x].p[];p2=tree[x].p[];return;}
int mid=(l+r)>>;
poi t1,t2;t1.dis=t2.dis=-;t1.p[]=t1.p[]=t2.p[]=t2.p[]=;
if(cl<=mid)query(x<<,l,mid,cl,cr,t1.dis,t1.p[],t1.p[]);
if(cr>mid)query(x<<|,mid+,r,cl,cr,t2.dis,t2.p[],t2.p[]);
pushup(t1,t2,dis,p1,p2);
}
int main()
{
read(n);
for(int i=;i<n;i++)read(x),read(y),read(z),add(x,y,z),add(y,x,z);
dfs1();dfs2(,);
build(,,n);
read(m);
for(int i=;i<=m;i++)
{
read(a),read(b),read(x),read(y);
poi t1,t2;t1.dis=t2.dis=t1.p[]=t1.p[]=t2.p[]=t2.p[]=;ll dis;int p1,p2;
query(,,n,a,b,dis,t1.p[],t1.p[]);query(,,n,x,y,dis,t2.p[],t2.p[]);
pushup(t1,t2,dis,p1,p2);
printf("%lld\n",dis);
}
}
51nod 1766 树上的最远点对(线段树)的更多相关文章
- 51nod 1766 树上的最远点对——线段树
n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即你需要求出max{dis(i,j) |a<=i<=b,c<=j& ...
- 51 nod 1766 树上的最远点对(线段树+lca)
1766 树上的最远点对 基准时间限制:3 秒 空间限制:524288 KB 分值: 80 难度:5级算法题 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个 ...
- 51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径
51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径 题面 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即 ...
- [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树)
[51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树) 题面 给出一棵N个点的树,Q次询问一点编号在区间[l1,r1]内,另一点编号在区间[l2,r2]内的所有点对距离最大值.\ ...
- 51Nod 1766 树上的最远点对
Description 一棵树,询问两个端点编号分别在在 \([a,b]\) 和 \([c,d]\) 两个区间中的最长链. Sol 线段树+ST表. 树上最长链可以合并,只需要合并两个区间最长链的两个 ...
- 【树形结构】51nod 1766 树上的最远点对
题目内容 \(n\)个点被\(n−1\)条边连接成了一颗树,边有权值\(w_i\).有\(q\)个询问,给出\([a,b]\)和\([c,d]\)两个区间,表示点的标号请你求出两个区间内各选一点之间的 ...
- 【51nod】1766 树上的最远点对
[题意]给定n个点的树,m次求[a,b]和[c,d]中各选出一个点的最大距离.abcd是标号区间,n,m<=10^5 [算法]LCA+树的直径理论+线段树 [题解] 树的直径性质:距离树上任意点 ...
- 51Nod.1766.树上最远点对(树的直径 RMQ 线段树/ST表)
题目链接 \(Description\) 给定一棵树.每次询问给定\(a\sim b,c\sim d\)两个下标区间,从这两个区间中各取一个点,使得这两个点距离最远.输出最远距离. \(n,q\leq ...
- csu 1798(树上最远点对,线段树+lca)
1798: 小Z的城市 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 60 Solved: 16[Submit][Status][Web Board] ...
随机推荐
- Jenkins+git+Nginx
1.Jenkins 一.tomcat安装 1.下载JDK和Tomcat //通过wget下载 wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomca ...
- WebGL树形结构的模型渲染流程
今天和大家分享的是webgl渲染树形结构的流程.用过threejs,babylonjs的同学都知道,一个大模型都是由n个子模型拼装而成的,那么如何依次渲染子模型,以及渲染每个子模型在原生webgl中的 ...
- Phaser Matter Collision Plugin 碰撞插件 -- iFiero技术分享
collision-simple-demo Phaser 自带的Arcade虽然易用,但复杂的物理碰撞明显就不够用了,于是Matter等物理引擎还是不得不学的,以下是Matter物理体碰撞的一个插件, ...
- flask_sqlalchemy介绍
快速入门 Flask-SQLAlchemy 使用起来非常有趣,对于基本应用十分容易使用,并且对于大型项目易于扩展.有关完整的指南,请参阅 SQLAlchemy 的 API 文档. 一个最小应用 常见情 ...
- Linux环境下Java应用性能分析定位-CPU使用篇
1 CPU热点分析定位背景 CPU资源还是很昂贵的,为了深刻感受到这种昂贵,间下图当前CPU的资源售价: 所以对于程序猿们来说,需要让程序合理高效的使用CPU资源.利用有限的CPU资源来解决完 ...
- Python常用模块之Pygame(手册篇:首页)
Pygame手册官方网址:http://www.pygame.org/docs/ Pygame首页 说明文档: 自述 关于Pygame的基本信息,它是什么,谁参与了以及在哪里找到它. 安装 在几个平台 ...
- linux-sftp-指定端口号登录远程主机
sftp -oPort=60001 root@192.168.0.254 -o选项来指定端口号 -oPort=远程端口号
- 20181113-3 Beta阶段贡献分配规则
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2382 在新成员加入后,我们经过商讨,决定沿用alpha阶段贡献分分配规则 ...
- 个人作业四:注册github
注册Github账户 账户名称:liurunhan Github地址:https://github.com/liurunhan
- tensorboard入门
Tensorboard tensorboard用以图形化展示我们的代码结构和图形化训练误差等,辅助优化程序 tensorboard实际上是tensorflow机器学习框架下的一个工具,需要先安装ten ...