bzoj4034 [HAOI2015]树上操作

题目链接

分析:
树链剖分练手题
注意线段树是建立在dfs序上的,所以子树一定在线段树上是一个连续的区间
子树加的时候直接线段树区间加即可
询问树链信息的时候,因为top,deep,pre数组都是建立在原树上的
所以传入的是原树上的结点编号(输入编号)

这道题也算是一个史前巨坑了,以前一直A不了
今天终于一A了

tip

开ll

//这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>
#define ll long long

using namespace std;

const int N=100010;
int pre[N],top[N],son[N],size[N],out[N],num[N],shu[N],clo=0,deep[N];
int n,m;
ll a[N];
struct Tree{
    int x,y;
    ll sum,lazy;
};
Tree t[N<<2];
struct node{
    int x,y,nxt;
};
node way[N<<1];
int st[N],tot=0;

void add(int u,int w)
{
    tot++;
    way[tot].x=u;way[tot].y=w;way[tot].nxt=st[u];st[u]=tot;
    tot++;
    way[tot].x=w;way[tot].y=u;way[tot].nxt=st[w];st[w]=tot;
}

void dfs_1(int now,int fa,int dep)
{
    deep[now]=dep;
    pre[now]=fa;
    size[now]=1;
    int maxx=0;
    for (int i=st[now];i;i=way[i].nxt)
        if (way[i].y!=fa)
        {
            dfs_1(way[i].y,now,dep+1);
            size[now]+=size[way[i].y];
            if (size[way[i].y]>maxx)
            {
                maxx=size[way[i].y];
                son[now]=way[i].y;
            }
        }
}

void dfs_2(int now,int fa)
{
    if (son[fa]!=now) top[now]=now;
    else top[now]=top[fa];
    num[now]=++clo; shu[num[now]]=now;
    if (son[now])
    {
        dfs_2(son[now],now);
        for (int i=st[now];i;i=way[i].nxt)
            if (way[i].y!=fa&&way[i].y!=son[now])
                dfs_2(way[i].y,now);
    }
    out[now]=clo;
}

void update(int bh)
{
    t[bh].sum=t[bh<<1].sum+t[(bh<<1)+1].sum;
}

void push(int bh)
{
    int lc=bh<<1;
    int rc=(bh<<1)+1;
    if (t[bh].lazy&&t[bh].x!=t[bh].y)
    {
        t[lc].sum+=(t[lc].y-t[lc].x+1)*t[bh].lazy;
        t[lc].lazy+=t[bh].lazy;
        t[rc].sum+=(t[rc].y-t[rc].x+1)*t[bh].lazy;
        t[rc].lazy+=t[bh].lazy;
        t[bh].lazy=0;
    }
}

void build(int bh,int l,int r)
{
    t[bh].x=l; t[bh].y=r; t[bh].lazy=0;
    if (l==r)
    {
        t[bh].sum=a[shu[l]];
        return;
    }
    int mid=(l+r)>>1;
    build(bh<<1,l,mid);
    build((bh<<1)+1,mid+1,r);
    update(bh);
}

void add(int bh,int l,int r,ll z)
{
    push(bh);
    if (t[bh].x>=l&&t[bh].y<=r)
    {
        t[bh].sum+=(t[bh].y-t[bh].x+1)*z;
        t[bh].lazy+=z;
        return;
    }
    int mid=(t[bh].x+t[bh].y)>>1;
    if (l<=mid) add(bh<<1,l,r,z);
    if (r>mid) add((bh<<1)+1,l,r,z);
    update(bh);
}

ll ask(int bh,int l,int r)
{
    push(bh);
    if (t[bh].x>=l&&t[bh].y<=r)
    {
        return t[bh].sum;
    }
    int mid=(t[bh].x+t[bh].y)>>1;
    ll ans=0;
    if (l<=mid) ans+=ask(bh<<1,l,r);
    if (r>mid) ans+=ask((bh<<1)+1,l,r);
    return ans;
}

ll asksum(int u,int w)
{
    ll ans=0;
    int f1=top[u];
    int f2=top[w];
    while (f1!=f2)
    {
        if (deep[f1]<deep[f2]) swap(u,w),swap(f1,f2);  //
        ans+=ask(1,num[f1],num[u]);
        u=pre[f1];
        f1=top[u];
    }
    if (num[u]>num[w]) swap(u,w);
    ans+=ask(1,num[u],num[w]);
    return ans;
}

int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
    for (int i=1;i<n;i++)
    {
        int u,w;
        scanf("%d%d",&u,&w);
        add(u,w);
    }

    dfs_1(1,0,1);
    dfs_2(1,0);
    build(1,1,n);

    for (int i=1;i<=m;i++)
    {
        int opt,x,y;
        scanf("%d",&opt);
        if (opt==1)
        {
            scanf("%d%d",&x,&y);
            add(1,num[x],num[x],(ll)y);
        }
        else if (opt==2)
        {
            scanf("%d%d",&x,&y);
            add(1,num[x],out[x],(ll)y);
        }
        else
        {
            scanf("%d",&x);
            printf("%lld\n",asksum(1,x));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值