POJ - 2763 Housewife Wind(树链剖分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cherish0222/article/details/77524654

点我看题

题意:一棵树,每条边都有对应权值,两个操作,更改边的权值和求任意两点的权值之和。

分析:树链剖分模板题。

参考代码:

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

using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define lson rt<<1
#define rson rt<<1|1
const int maxn = 1e5+10;
const int N = 1e5+10;
int a[maxn];
int n,q,s;
int e[maxn][3];
int head[maxn<<1],tot;
struct Edge{
    int to,next;
}edge[maxn<<1];
int siz[maxn],fa[maxn],son[maxn],dep[maxn];
int p[maxn],fp[maxn],top[maxn],pos;
//线段树
int sum[maxn<<2];

void Init()
{
    tot = pos = 0;
    mem(head,-1);
    mem(son,-1);
}

void AddEdge( int u, int v)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}

void dfs1( int u, int pre, int d)
{
    siz[u] = 1;
    fa[u] = pre;
    dep[u] = d;
    for( int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if( v != fa[u])
        {
            dfs1(v,u,d+1);
            siz[u] += siz[v];
            if( son[u] == -1 || siz[v] > siz[son[u]])
                son[u] = v;
        }
    }
}

void dfs2( int u, int tp)
{
    top[u] = tp;
    p[u] = ++pos;
    fp[p[u]] = u;
    if( son[u] == -1)
        return;
    dfs2(son[u],tp);
    for( int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if( v != fa[u] && v != son[u])
            dfs2(v,v);
    }
}

void PushUp( int rt)
{
    sum[rt] = sum[lson]+sum[rson];
}

void Build( int l, int r, int rt)
{
    if( l == r)
    {
        sum[rt] = a[l];
        return;
    }
    int mid = (l+r)>>1;
    Build(l,mid,lson);
    Build(mid+1,r,rson);
    PushUp(rt);
}

void Update( int l, int r, int rt, int k, int val)
{
    if( l == r)
    {
        sum[rt] = val;
        return;
    }
    int mid = (l+r)>>1;
    if( k <= mid)
        Update(l,mid,lson,k,val);
    else
        Update(mid+1,r,rson,k,val);
    PushUp(rt);
}

int Query( int l, int r, int rt, int L, int R)
{
    if( L <= l && R >= r)
        return sum[rt];
    int mid = (l+r)>>1;
    if( R <= mid)
        return Query(l,mid,lson,L,R);
    else if( L > mid)
        return Query(mid+1,r,rson,L,R);
    else
        return Query(l,mid,lson,L,mid)+Query(mid+1,r,rson,mid+1,R);
}

int Find( int u, int v)
{
    int ans = 0;
    int t1 = top[u];
    int t2 = top[v];
    while( t1 != t2)
    {
        if( dep[t1] < dep[t2])
        {
            swap(u,v);
            swap(t1,t2);
        }
        ans += Query(1,pos,1,p[t1],p[u]);
        u = fa[t1];
        t1 = top[u];
    }
    if( u == v)
        return ans;
    if( dep[u] > dep[v])
        swap(u,v);
    ans += Query(1,pos,1,p[son[u]],p[v]);
    return ans;
}

int main()
{
    while( ~scanf("%d%d%d",&n,&q,&s))
    {
        Init();
        for( int i = 1; i < n; i++)
        {
            scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]);
            AddEdge(e[i][0],e[i][1]);
            AddEdge(e[i][1],e[i][0]);
        }

        dfs1(1,0,0);
        dfs2(1,1);

        for( int i = 1; i < n; i++)
        {
            if( dep[e[i][0]] > dep[e[i][1]])
                swap(e[i][0],e[i][1]);
            a[p[e[i][1]]] = e[i][2];
        }
        Build(1,pos,1);

        while( q--)
        {
            int op,u,i,w;
            scanf("%d",&op);
            if( op == 0)
            {
                scanf("%d",&u);
                printf("%d\n",Find(s,u));
                s = u;
            }
            else if( op == 1)
            {
                scanf("%d%d",&i,&w);
//                Update(1,pos,1,p[e[i][1]],w);
                Update(1,pos,1,p[e[i][1]],w);
            }
        }
    }

    return 0;
}


展开阅读全文

Housewife Wind

10-19

DescriptionnnAfter 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.nnSince 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!’nnAt 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.nnWind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?nnInputnnThe 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.nnThe 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.nnThe following q lines each is one of the following two types:nnMessage A: 0 u nA kid in hut u calls Wind. She should go to hut u from her current position. nMessage B: 1 i w nThe 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.nnOutputnnFor each message A, print an integer X, the time required to take the next child.nnSample Inputnn3 3 1 n1 2 1 n2 3 2 n0 2 n1 2 3 n0 3nnSample Outputnn1 n3 问答

没有更多推荐了,返回首页