Distance Queries 【POJ--1986】【动态树LCT】

本文探讨了在解决特定类型的问题时,动态树(Dynamic Tree)相较于LCA算法的优势。通过一个具体的题目实例,详细介绍了如何使用轻重链剖分(LCT)算法来简化动态树的操作,包括旋转、路径查询等核心操作。文章提供了完整的C++代码实现,展示了如何通过LCT处理动态树上的节点链接和查询问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接


  虽然LCA也是可以写的,但是动态树明显写起来简单的多(雾)只是个人观点。

  我们这道题,暗藏了一个信息就是它一定是一棵树,题目中有这样说到,每两个点一定是相互链接,或者是“一串”这样的可以到达的。

  然后就是直接上LCT的模板咯,并且将边看成是一个新的点即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
#define MP3(a, b, c) MP(MP(a, b), c)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 8e4 + 7;
int N, M, Q, tot;
int c[maxN][2], fa[maxN], r[maxN], st[maxN];
ll val[maxN], s[maxN];
bool isroot(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; }
void pushup(int x) { s[x] = s[c[x][0]] + s[c[x][1]] + val[x]; }
void pushr(int x) { swap(c[x][0], c[x][1]); r[x] ^= 1; }
void pushdown(int x)
{
    if(r[x])
    {
        if(c[x][0]) pushr(c[x][0]);
        if(c[x][1]) pushr(c[x][1]);
        r[x] = 0;
    }
}
void Rotate(int x)
{
    int y = fa[x], z = fa[y], k = c[y][1] == x;
    if(!isroot(y)) c[z][c[z][1] == y] = x;
    fa[x] = z;
    c[y][k] = c[x][k^1];
    fa[c[x][k^1]] = y;
    c[x][k^1] = y;
    fa[y] = x;
    pushup(y); pushup(x);
}
void Splay(int x)
{
    int y = x, z = 0;
    st[++z] = y;
    while(!isroot(y)) st[++z] = y = fa[y];
    while(z) pushdown(st[z--]);
    while(!isroot(x))
    {
        y = fa[x]; z = fa[y];
        if(!isroot(y)) (c[z][0] == y) ^ (c[y][0] == x) ? Rotate(x) : Rotate(y);
        Rotate(x);
    }
}
void access(int x)
{
    int y = 0;
    while(x)
    {
        Splay(x); c[x][1] = y;
        pushup(x);
        y = x; x = fa[x];
    }
}
void makeroot(int x)
{
    access(x); Splay(x);
    pushr(x);
}
int findroot(int x)
{
    access(x); Splay(x);
    while(c[x][0]) { pushdown(x); x = c[x][0]; }
    Splay(x);
    return x;
}
void split(int x, int y)
{
    makeroot(x);
    access(y); Splay(y);
}
void link(int x, int y)
{
    makeroot(x);
    if(findroot(y) != x) fa[x] = y;
}
inline void init()
{
    tot = 0;
    memset(s, 0, sizeof(s));
    memset(c, 0, sizeof(c));
    memset(r, 0, sizeof(r));
    memset(fa, 0, sizeof(fa));
}
char omg[10];
int main()
{
    while(scanf("%d%d", &N, &M) != EOF)
    {
        init();
        for(int i=1, u, v, w; i<=M; i++)
        {
            scanf("%d%d%d%s", &u, &v, &w, omg);
            val[N + i] = w;
            link(u, N + i);
            link(N + i, v);
        }
        scanf("%d", &Q);
        int x, y;
        while(Q--)
        {
            scanf("%d%d", &x, &y);
            if(x == y) { printf("0\n"); continue; }
            split(x, y);
            printf("%lld\n", s[y]);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值