POJ - 2763. Housewife Wind LCA+单点更新

After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique.

Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: ‘Mummy, take me home!’

At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road.

Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?
Input
The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001.

The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000.

The following q lines each is one of the following two types:

Message A: 0 u
A kid in hut u calls Wind. She should go to hut u from her current position.
Message B: 1 i w
The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid.
Output
For each message A, print an integer X, the time required to take the next child.
Sample Input
3 3 1
1 2 1
2 3 2
0 2
1 2 3
0 3
Sample Output
1
3

题意:给你一棵树,一开始在q点, m个操作, 0代表询问走到x点的距离, 1代表更新第x条路径的权值。

思路:暂时不会树链剖分, 想到lca, 求出两点之间的lca, 答案就是dist[u] + dist[v] - 2 * dist[lca], dist代表到根节点的距离。单点更新可以使用线段树或者树桩数组, 但是数据比较弱, 竟然暴力更新也可以过。
说一下树状数组的写法, 记录每个点第一访问的时间戳l[u], 然后第二次访问(回溯到)的时间戳r[u],可以得到一个以节点时间戳为标号的序列。 更新单点的话只用tree[l[u]]+=w和tree[r[u] + 1] -= w就可以使得l[u] 到r[u]之间的所有点都被更新了, 也就是该节点的子树全部被更新, 而其他的点不会影响,tree[l[u]]也就表示该点到根节点的距离。

第一个是树状数组优化的, 第二个是暴力。

树桩数组优化:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

struct node
{
    int u, v, w, next;
}e[200001];

int n, m, s, cnt;
int g[100001], val[100001];
int l[100001], r[100001], f[100001][20], depth[100001];
int dist[100001], tree[200001];

void addedge(int u, int v, int w)
{
    e[cnt].u = u;
    e[cnt].v = v;
    e[cnt].w = w;
    e[cnt].next = g[u];
    g[u] = cnt++;
}

void dfs(int u, int fa, int deep)
{
    depth[u] = deep;
    f[u][0] = fa;
    l[u] = ++cnt;
    for (int i = g[u]; i != -1; i = e[i].next)
    {
        int v = e[i].v;
        if (v != fa)
        {
            dist[v] = dist[u] + e[i].w;
            dfs(v, u, deep + 1);
        }
    }
    r[u] = cnt;
}

int lca(int u, int v)
{
    if (depth[u] < depth[v])
        swap(u, v);
    int d = depth[u] - depth[v];
    for (int i = 0; d; d >>= 1, i++)
        if (d & 1)
            u = f[u][i];
    if (u == v)
        return u;
    for (int i = 19; i >= 0; i--)
        if (f[u][i] != f[v][i])
            u = f[u][i], v = f[v][i];
    return f[u][0];
}

void update(int pos, int x)
{
    while (pos <= n)
    {
        tree[pos] += x;
        pos += pos & (-pos);
    }
}

int sum(int pos)
{
    int ans = 0;
    while (pos)
    {
        ans += tree[pos];
        pos -= pos & (-pos);
    }
    return ans;
}

int main()
{
    while (~scanf("%d %d %d", &n, &m, &s))
    {
        memset(g, -1, sizeof(g));
        memset(dist, 0, sizeof(dist));
        memset(f, 0, sizeof(f));
        memset(tree, 0, sizeof(tree));
        cnt = 0;
        for (int i = 1; i < n; i++)
        {
            int u, v, w;
            scanf("%d %d %d", &u, &v, &w);
            addedge(u, v, w);
            addedge(v, u, w);
            val[i] = w;
        }
        cnt = 0;
        dfs(1, 0, 1);
        for (int j = 1; (1 << j) <= n; j++)
            for (int i = 1; i <= n; i++)
                f[i][j] = f[f[i][j - 1]][j - 1];

        for (int i = 1; i < n; i++)
        {
            int u = e[(i - 1) * 2].u, v = e[(i - 1) * 2].v, w = e[(i - 1) * 2].w;
            if (depth[u] < depth[v])
                swap(u, v);
            update(l[u], w);
            update(r[u] + 1, -w);
        }

        while (m--)
        {
            int a, b, c;
            scanf("%d", &c);
            if (c)
            {
                scanf("%d %d", &a, &b);
                int w = val[a];
                int u = e[(a - 1) * 2].u, v = e[(a - 1) * 2].v;
                if (depth[u] < depth[v])
                    swap(u, v);
                update(l[u], -w);
                update(r[u] + 1, w);
                update(l[u], b);
                update(r[u] + 1, -b);
                val[a] = b;
            }
            else
            {
                scanf("%d", &a);
                int x = lca(s, a);
                printf("%d\n", sum(l[s]) + sum(l[a]) - 2 * sum(l[x]));
                s = a;
            }
        }
    }

    return 0;
}

暴力更新单点

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

struct node
{
    int u, v, w, next;
}e[200001];

int n, m, s, cnt;
int g[100001];
int l[100001], r[100001], f[100001][20], depth[100001];
int dist[100001];

void addedge(int u, int v, int w)
{
    e[cnt].u = u;
    e[cnt].v = v;
    e[cnt].w = w;
    e[cnt].next = g[u];
    g[u] = cnt++;
}

void dfs(int u, int fa, int deep)
{
    depth[u] = deep;
    f[u][0] = fa;
    l[u] = ++cnt;
    for (int i = g[u]; i != -1; i = e[i].next)
    {
        int v = e[i].v;
        if (v != fa)
        {
            dist[v] = dist[u] + e[i].w;
            dfs(v, u, deep + 1);
        }
    }
    r[u] = ++cnt;
}

int lca(int u, int v)
{
    if (depth[u] < depth[v])
        swap(u, v);
    int d = depth[u] - depth[v];
    for (int i = 0; d; d >>= 1, i++)
        if (d & 1)
            u = f[u][i];
    if (u == v)
        return u;
    for (int i = 19; i >= 0; i--)
        if (f[u][i] != f[v][i])
            u = f[u][i], v = f[v][i];
    return f[u][0];
}

void cal(int u, int fa, int w)
{
    dist[u] += w;
    for (int i = g[u]; i != -1; i = e[i].next)
    {
        int v = e[i].v;
        if (v != fa)
            cal(v, u, w);
    }
}


int main()
{
    while (~scanf("%d %d %d", &n, &m, &s))
    {
        memset(g, -1, sizeof(g));
        memset(dist, 0, sizeof(dist));
        memset(f, 0, sizeof(f));
        cnt = 0;
        for (int i = 1; i < n; i++)
        {
            int u, v, w;
            scanf("%d %d %d", &u, &v, &w);
            addedge(u, v, w);
            addedge(v, u, w);
        }
        dfs(1, 0, 1);
        for (int j = 1; (1 << j) <= n; j++)
            for (int i = 1; i <= n; i++)
                f[i][j] = f[f[i][j - 1]][j - 1];

        while (m--)
        {
            int a, b, c;
            scanf("%d", &c);
            if (c)
            {
                scanf("%d %d", &a, &b);
                a--; a <<= 1;
                int delta = b - e[a].w;
                e[a].w = e[a ^ 1].w = b;
                int u = e[a].u, v = e[a].v;
                if (depth[u] > depth[v])
                    cal(u, v, delta);
                else
                    cal(v, u, delta);
            }
            else
            {
                scanf("%d", &a);
                int x = lca(s, a);
                printf("%d\n", dist[s] + dist[a] - 2 * dist[x]);
                s = a;
            }
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值