树上的最远点对

n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即你需要求出max{dis(i,j) |a<=i<=b,c<=j<=d}
(PS 建议使用读入优化)
Input
第一行一个数字 n n<=100000。
第二行到第n行每行三个数字描述路的情况, x,y,z (1<=x,y<=n,1<=z<=10000)表示x和y之间有一条长度为z的路。
第n+1行一个数字m,表示询问次数 m<=100000。
接下来m行,每行四个数a,b,c,d。
Output
共m行,表示每次询问的最远距离
Input示例
5
1 2 1
2 3 2
1 4 3
4 5 4
1
2 3 4 5
Output示例

10

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;

const int N = 100010;

int h[N], tot;
int f[N*2], dis[N], dep[N], rmq[N*2][20], fir[N], u;
struct edge
{
    int x, len, next;
}e[N*2];

struct point
{
    int l, r, a[2], len;
}tree[N*2], lt, rt, sam;
int k, n, m, root;
double tim;

int get()
{
    char ch;
    int s = 0;
    while (ch = getchar(), ch < '0' || ch > '9');
    s = ch - '0';
    while (ch = getchar(), ch >= '0' && ch <= '9') 
    {
        s = s * 10 + ch - '0';
    }
    
    return s;
}

void addEdge(int x, int y, int z)
{
    e[++tot].x = y;
    e[tot].len = z;
    e[tot].next = h[x];
    h[x] = tot;
}

void dfs(int x)
{
    f[fir[x] = ++u] = x;
    for (int p = h[x]; p; p = e[p].next)
    {
        if (!dep[e[p].x])
        {
            dep[e[p].x] = dep[x] + 1;
            dis[e[p].x] = dis[x] + e[p].len;
            dfs(e[p].x);
            f[++u] = x;
        }
    }
}

void getRmq()
{
    for (int i = 1; i <= u; i++)
    {
        rmq[i][0] = f[i];
    }

    for (int j =1; j <= log(u) / log(2); j++)
    {
        for (int i = 1; i <= u - (1 << j) + 1; i++)
        {
            if (dep[rmq[i][j-1]] < dep[rmq[i+(1<<(j-1))][j-1]])
            {
                rmq[i][j] = rmq[i][j-1];
            }
            else
            {
                rmq[i][j] = rmq[i + (1 << (j - 1))][j - 1];
            }
        }
    }
}

int getDis(int x, int y)
{
    if (!x || !y)
    {
        return 0;
    }
    int tx = x, ty = y;
    x = fir[x], y = fir[y];
    if (x > y)
    {
        swap(x, y);
    }
    int t = log(y - x + 1) / log(2);
    if (dep[rmq[x][t]] < dep[rmq[y - (1 << t) + 1][t]])
    {
        x = rmq[x][t];
    }
    else
    {
        x = rmq[y - (1 << t) + 1][t];
    }
    
    return dis[tx] + dis[ty] - 2 * dis[x];
}

void merge(point a, point b, point &c)
{
    c.len = b.len;
    c.a[0] = b.a[0];
    c.a[1] = b.a[1];
    if (c.len < a.len)
    {
        c.len = a.len;
        c.a[0] = a.a[0];
        c.a[1] = a.a[1];
    }
    
    for (int x = 0; x <= 1; x++)
    {
        for (int y = 0; y <= 1; y++)
        {
            int v = getDis(a.a[x],b.a[y]);
            if (v > c.len)
            {
                c.len = v;
                c.a[0] = a.a[x];
                c.a[1] = b.a[y];
            }
        }
    }
}

void build(int &now, int l, int r)
{
    now = ++k;
    if (l == r)
    {
        tree[now].a[0] = tree[now].a[1] = l;
        tree[now].len = 0;
        return;
    }
    
    int mid = (l + r) / 2;
    build(tree[now].l, l, mid);
    build(tree[now].r, mid + 1, r);
    merge(tree[tree[now].l], tree[tree[now].r], tree[now]);
}

void getPath(int now, int l, int r, int x, int y)
{
    if (x <= l && r <= y)
    {
        merge(sam, tree[now], sam);
        return;
    }
    int mid = l + (r - l) / 2;
    if (x <= mid)
    {
        getPath(tree[now].l, l, mid, x, y);
    }
    if (y > mid)
    {
        getPath(tree[now].r, mid + 1, r, x, y);
    }
}

int main()
{
    scanf("%d", &n);
    tim = log(n) / log(2);
    
    for (int i = 1; i <n; i++)
    {
        int x = get(), y = get(), z = get();
        addEdge(x, y, z);
        addEdge(y, x, z);
    }
    dfs(dep[1] = 1);
    getRmq();
    build(root, 1, n);
    scanf("%d", &m);
    
    for (int i = 1; i <= m; i++)
    {
        int a = get(), b = get(), c = get(), d = get();
        sam.len = sam.a[0] = sam.a[1] = 0;
        getPath(1, 1, n, a, b);
        lt = sam;
        sam.len = sam.a[0] = sam.a[1] = 0;
        getPath(1, 1, n, c, d);
        rt = sam;
        int result = 0;
        
        for (int x = 0; x <= 1; x++)
        {
            for (int y = 0; y <= 1; y++)
            {
                result = max(result, getDis(lt.a[x], rt.a[y]));
            }
        }
        
        printf("%d\n", result);
    }
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值