像树的直径一样,两个集合的最长路也是由两个集合内部的最长路的两个端点组成的,于是我们知道了两个集合的最长路,枚举一下两两端点算出答案就可以合并了,所以就可以用线段树维护一个区间里的最长路了。

#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 树上的最远点对(线段树)的更多相关文章

  1. 51nod 1766 树上的最远点对——线段树

    n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即你需要求出max{dis(i,j) |a<=i<=b,c<=j& ...

  2. 51 nod 1766 树上的最远点对(线段树+lca)

    1766 树上的最远点对 基准时间限制:3 秒 空间限制:524288 KB 分值: 80 难度:5级算法题   n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个 ...

  3. 51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径

    51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径 题面 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即 ...

  4. [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树)

    [51nod 1766]树上的最远点对 (树的直径+ST表求lca+线段树) 题面 给出一棵N个点的树,Q次询问一点编号在区间[l1,r1]内,另一点编号在区间[l2,r2]内的所有点对距离最大值.\ ...

  5. 51Nod 1766 树上的最远点对

    Description 一棵树,询问两个端点编号分别在在 \([a,b]\) 和 \([c,d]\) 两个区间中的最长链. Sol 线段树+ST表. 树上最长链可以合并,只需要合并两个区间最长链的两个 ...

  6. 【树形结构】51nod 1766 树上的最远点对

    题目内容 \(n\)个点被\(n−1\)条边连接成了一颗树,边有权值\(w_i\).有\(q\)个询问,给出\([a,b]\)和\([c,d]\)两个区间,表示点的标号请你求出两个区间内各选一点之间的 ...

  7. 【51nod】1766 树上的最远点对

    [题意]给定n个点的树,m次求[a,b]和[c,d]中各选出一个点的最大距离.abcd是标号区间,n,m<=10^5 [算法]LCA+树的直径理论+线段树 [题解] 树的直径性质:距离树上任意点 ...

  8. 51Nod.1766.树上最远点对(树的直径 RMQ 线段树/ST表)

    题目链接 \(Description\) 给定一棵树.每次询问给定\(a\sim b,c\sim d\)两个下标区间,从这两个区间中各取一个点,使得这两个点距离最远.输出最远距离. \(n,q\leq ...

  9. csu 1798(树上最远点对,线段树+lca)

    1798: 小Z的城市 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 60  Solved: 16[Submit][Status][Web Board] ...

随机推荐

  1. iOS 播放音频文件

    //        播放音乐 NSString *path = [[NSBundle mainBundle] pathForResource:@"1670" ofType:@&qu ...

  2. leetcode27_C++Remove Element

    给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...

  3. UI优秀框架(库)

    1.vux 官网:https://doc.vux.li/zh-CN/ Github:https://github.com/airyland/vux 13818  Stars  3064 Forks   ...

  4. react-native debug js remotely跨域问题

    react-native debug js remotely跨域问题 我们在安卓真机上调试react-native时,启用debug js remotely的时候,会出现跨域问题.这个时候我们只需要一 ...

  5. 20172330 2017-2018-1 《Java程序设计》第十周学习总结

    20172330 2017-2018-1 <程序设计与数据结构>第十周学习总结 教材学习内容总结 本周的学习内容为集合 集合 对象具有定义良好的接口,从而成为一种实现集合的完善体制. 动态 ...

  6. static块的本质

    在网上看到了下面的一段代码: public class Test { static { _i = 20; } public static int _i = 10; public static void ...

  7. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  8. POJ 2392 Space Elevator 贪心+dp

    题目链接: http://poj.org/problem?id=2392 题意: 给你k类方块,每类方块ci个,每类方块的高度为hi,现在要报所有的方块叠在一起,每类方块的任何一个部分都不能出现在ai ...

  9. ACM-ICPC 2018 沈阳赛区网络预赛

    Supreme Number 1000ms 131072K   A prime number (or a prime) is a natural number greater than 111 tha ...

  10. prototype.js中Function.prototype.bind方法浅解

    prototype.js中的Function.prototype.bind方法: Function.prototype.bind = function() { var __method = this; ...