HDU 5044 Tree 树链剖分

【题目大意】

有一棵树。两种操作,1、令一条路径上的所有的点,点权+k;2、令一条路径上的所有的边,边权+k。

【思路】

这题其实就是hdu 5029的简化版。可以直接看我博客中的hdu 5029的题解,那道题更有意思一些。

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
typedef __int64 LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const int INF = 100011122;
const double INFF = 1e100;
const double eps = 1e-8;
const int mod = 1000000007;
const int NN = 100010;
const int MM = 5000010;
/* ****************** */

struct G
{
    int u, v, next;
}E[NN*2];
int p[NN],T;
//c_dfs1
int deep[NN],si[NN],per[NN],anc[NN];
//c_dfs2
int tsp,pos[NN],top[NN];
LL re[2][NN];
LL a[2][NN];

void add(int u,int v)
{
    E[T].u = u;
    E[T].v = v;
    E[T].next = p[u];
    p[u] = T++;
}
void c_dfs1(int u,int fa,int cen)
{
    anc[u]=fa;
    deep[u]=cen;
    si[u]=1;
    per[u]=-1;
    int i,v;
    for(i=p[u];i+1;i=E[i].next)
    {
        v=E[i].v;
        if(v==fa)continue;
        c_dfs1(v,u,cen+1);
        si[u]+=si[v];
        if(per[u]==-1 || si[v]>si[ per[u] ])
            per[u]=v;
    }
}
void c_dfs2(int u,int fa,int now_top)
{
    pos[u]=++tsp;
    top[u]=now_top;
    if(per[u]!=-1)c_dfs2(per[u],u,now_top);
    int i,v;
    for(i=p[u];i+1;i=E[i].next)
    {
        v=E[i].v;
        if(v==per[u] || v==fa)continue;
        c_dfs2(v,u,v);
    }
}
//dian
void c_update0(int u,int v,LL col)
{
    while(top[u]!=top[v])
    {
        if(deep[ top[u] ] > deep[ top[v] ])
            swap(u,v);
        re[0][ pos[ top[v] ] ] += col;
        re[0][ pos[ v ] + 1 ] -= col;
        v = anc[ top[v] ];
    }
    if(pos[u]>pos[v])
        swap(u,v);
    re[0][ pos[u] ] += col;
    re[0][ pos[v] + 1 ] -= col;
}
//bian
void c_update1(int u,int v,LL col)
{
    while(top[u]!=top[v])
    {
        if(deep[ top[u] ] > deep[ top[v] ])
            swap(u,v);
        re[1][ pos[ top[v] ] ] += col;
        re[1][ pos[ v ] + 1 ] -= col;
        v=anc[ top[v] ];
    }
    if(pos[u]>pos[v])
        swap(u,v);
    if(pos[u]+1 <= pos[v])
    {
        re[1][ pos[u] + 1 ] += col;
        re[1][ pos[v] + 1 ] -= col;
    }
}

int main()
{
    int cas, ee = 0;
    int n,m,i,u,v,k;
    LL t0, t1;
    char op[15];
    scanf("%d", &cas);
    while(cas--)
    {
        memset(p,-1,sizeof(p));
        memset(re,0,sizeof(re));
        T = 0;
        scanf("%d%d",&n, &m);
        for(i = 1; i < n; i ++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        c_dfs1(1,-1,0);
        tsp=0;
        c_dfs2(1,-1,1);
        while(m--)
        {
            scanf("%s%d%d%d",op,&u,&v,&k);
            if(op[3]=='1')
            {
                c_update0(u,v,(LL)k);
            }
            else
            {
                c_update1(u,v,(LL)k);
            }
        }
        t0 = t1 = 0;
        for(i = 1; i <= n; i ++)
        {
            t0 += re[0][i];
            t1 += re[1][i];
            a[0][i] = t0;
            a[1][i] = t1;
        }

        printf("Case #%d:\n", ++ee);
        for(i = 1; i<= n; i ++)
        {
            printf("%I64d%c",a[0][ pos[i] ],i==n?'\n':' ');
        }
        for(i = 0; i < T; i += 2)
        {
            if(anc[ E[i].v ] == E[i].u)
                v = E[i].v;
            else
                v = E[i].u;
            if(i != 0)
                printf(" ");
            printf("%I64d",a[1][ pos[v] ]);
        }
        puts("");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值