bzoj4034: [HAOI2015]树上操作

链剖模板题,然而本辣鸡选手没写1LL调了好长时间。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int from;
    int to;
    int next;
}edge[200005];
struct segtree
{
    int ls;
    int rs;
    long long sum;
    long long mark;
}tree[400005];
int n,tot,pos[100005],npos[100005],dep[100005],fa[100005],son[100005],top[100005],num=1,tar,head[100005],size[100005];
long long a[100005];
void add(int u,int v)
{
    edge[tot].from=u;
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void dfs1(int x)
{
    dep[x]=dep[fa[x]]+1;
    size[x]=1;
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        if(edge[i].to!=fa[x])
        {
            fa[edge[i].to]=x;
            dfs1(edge[i].to);
            size[x]+=size[edge[i].to];
            if(size[edge[i].to]>size[son[x]])
            son[x]=edge[i].to;
        }
    }
}
void build(int p,int l,int r)
{
    int mid=l+r>>1;
    if(l==r)
    {
        tree[p].sum=a[npos[mid]];
        return;
    }
    tree[p].ls=++num;
    tree[p].rs=++num;
    build(tree[p].ls,l,mid);
    build(tree[p].rs,mid+1,r);
    tree[p].sum=tree[tree[p].ls].sum+tree[tree[p].rs].sum;
}
void dfs2(int x)
{
    pos[x]=++tar;
    npos[tar]=x;
    if(son[fa[x]]==x)
    top[x]=top[fa[x]];
    else top[x]=x;
    if(son[x]) dfs2(son[x]);
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        if(edge[i].to!=fa[x]&&edge[i].to!=son[x])
        {
            dfs2(edge[i].to);
        }
    }
}
void modify(int p,int x,int y,int k,int l,int r)
{
    int mid=l+r>>1;
    if(l==x&&r==y)
    {
        tree[p].sum+=1LL*(r-l+1)*k;
        tree[p].mark+=k;
        return;
    }
    tree[tree[p].ls].sum+=(mid-l+1)*tree[p].mark;
    tree[tree[p].rs].sum+=(r-mid)*tree[p].mark;
    tree[tree[p].ls].mark+=tree[p].mark;
    tree[tree[p].rs].mark+=tree[p].mark;
    tree[p].mark=0;
    if(y<=mid)
    modify(tree[p].ls,x,y,k,l,mid);
    else if(x>mid)
    modify(tree[p].rs,x,y,k,mid+1,r);
    else
    {
        modify(tree[p].ls,x,mid,k,l,mid);
        modify(tree[p].rs,mid+1,y,k,mid+1,r);
    }
    tree[p].sum=tree[tree[p].ls].sum+tree[tree[p].rs].sum;
}
long long query(int p,int x,int y,int l,int r)
{
    int mid=l+r>>1;
    if(l==x&&r==y)
    {
        return tree[p].sum;
    }
    tree[tree[p].ls].sum+=(mid-l+1)*tree[p].mark;
    tree[tree[p].rs].sum+=(r-mid)*tree[p].mark;
    tree[tree[p].ls].mark+=tree[p].mark;
    tree[tree[p].rs].mark+=tree[p].mark;
    tree[p].mark=0;
    if(y<=mid)
    return query(tree[p].ls,x,y,l,mid);
    else if(x>mid) return query(tree[p].rs,x,y,mid+1,r);
    else return query(tree[p].ls,x,mid,l,mid)+query(tree[p].rs,mid+1,y,mid+1,r);
}
long long getsum(int x,int y)
{
    int fx=top[x],fy=top[y];long long sum=0;
    while(fx!=fy)
    {
        if(dep[fx]<dep[fy])
        swap(x,y),swap(fx,fy);
        sum+=query(1,pos[fx],pos[x],1,n);
        x=fa[fx];fx=top[x];
    }
    if(dep[x]<dep[y])
    swap(x,y);
    sum+=query(1,pos[y],pos[x],1,n);
    return sum;
}
int main()
{
    memset(head,-1,sizeof(head));
    int m,x,y,z;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    for(int j=1;j<n;j++)
    {
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    dfs1(1);
    dfs2(1);
    build(1,1,n);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&x);
        if(x==1)
        {
            scanf("%d%d",&x,&y);
            modify(1,pos[x],pos[x],y,1,n);
        }
        else if(x==2)
        {
            scanf("%d%d",&x,&y);
            modify(1,pos[x],pos[x]+size[x]-1,y,1,n);
        }
        else
        {
            scanf("%d",&x);
            printf("%lld\n",getsum(1,x));
        }
    }
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值