bzoj 4034: [HAOI2015]树上操作 树链剖分+线段树

4034: [HAOI2015]树上操作

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 4352  Solved: 1387
[Submit][Status][Discuss]

Description

有一棵点数为 N 的树,以点 1 为根,且树点有边权。然后有 M 个
操作,分为三种:
操作 1 :把某个节点 x 的点权增加 a 。
操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a 。
操作 3 :询问某个节点 x 到根的路径中所有点的点权和。

Input

第一行包含两个整数 N, M 。表示点数和操作数。接下来一行 N 个整数,表示树中节点的初始权值。接下来 N-1 
行每行三个正整数 fr, to , 表示该树中存在一条边 (fr, to) 。再接下来 M 行,每行分别表示一次操作。其中
第一个数表示该操作的种类( 1-3 ) ,之后接这个操作的参数( x 或者 x a ) 。
 

Output

对于每个询问操作,输出该询问的答案。答案之间用换行隔开。

 

Sample Input

5 5
1 2 3 4 5
1 2
1 4
2 3
2 5
3 3
1 2 1
3 5
2 1 2
3 3

Sample Output

6
9
13

HINT

 对于 100% 的数据, N,M<=100000 ,且所有输入数据的绝对值都不会超过 10^6 。

思路:由于有3的操作,很容易想到树链剖分,2怎么办?

   对于树链剖分来说,其子树中的节点,也是连续的,所以找子树中编号最大的;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=1e6+10,inf=1e9+10;
const ll INF=1e18+10,mod=2147493647;

///数组大小
struct edge
{
    int v,next;
} edge[N<<1];
int head[N<<1],edg,id,n;
/// 树链剖分

int fa[N],dep[N],son[N],siz[N]; // fa父亲,dep深度,son重儿子,siz以该点为子树的节点个数
int a[N],ran[N],top[N],tid[N],mx[N];  // tid表示边的标号,top通过重边可以到达最上面的点,ran表示标记tid
void init()
{
    memset(son,-1,sizeof(son));
    memset(head,-1,sizeof(head));
    edg=0;
    id=0;
}

void add(int u,int v)
{
    edg++;
    edge[edg].v=v;
    edge[edg].next=head[u];
    head[u]=edg;
}

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

void dfs2(int u,int tp)
{
    tid[u]=mx[u]=++id;
    top[u]=tp;
    ran[tid[u]]=u;
    if(son[u]==-1)return;
    dfs2(son[u],tp),mx[u]=max(mx[u],mx[son[u]]);
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==fa[u])continue;
        if(v!=son[u])
            dfs2(v,v),mx[u]=max(mx[u],mx[v]);
    }
}

struct SGT
{
    ll sum[N<<2],lazy[N<<2];
    void pushup(int pos)
    {
        sum[pos]=sum[pos<<1]+sum[pos<<1|1];
    }
    void pushdown(int pos,int l,int r)
    {
        if(lazy[pos])
        {
            int mid=(l+r)>>1;
            lazy[pos<<1]+=lazy[pos];
            lazy[pos<<1|1]+=lazy[pos];
            sum[pos<<1]+=lazy[pos]*(mid-l+1);
            sum[pos<<1|1]+=lazy[pos]*(r-mid);
            lazy[pos]=0;
        }
    }
    void build(int l,int r,int pos)
    {
        lazy[pos]=0;
        if(l==r)
        {
            sum[pos]=a[ran[l]];
            return;
        }
        int mid=(l+r)>>1;
        build(l,mid,pos<<1);
        build(mid+1,r,pos<<1|1);
        pushup(pos);
    }
    void update(int L,int R,ll c,int l,int r,int pos)
    {
        if(L<=l&&r<=R)
        {
            sum[pos]+=c*(r-l+1);
            lazy[pos]+=c;
            return;
        }
        pushdown(pos,l,r);
        int mid=(l+r)>>1;
        if(L<=mid)update(L,R,c,l,mid,pos<<1);
        if(R>mid) update(L,R,c,mid+1,r,pos<<1|1);
        pushup(pos);
    }
    ll query(int L,int R,int l,int r,int pos)
    {
        if(L<=l&&r<=R)return sum[pos];
        pushdown(pos,l,r);
        int mid=(l+r)>>1;
        ll ans=0;
        if(L<=mid)ans+=query(L,R,l,mid,pos<<1);
        if(R>mid)ans+=query(L,R,mid+1,r,pos<<1|1);
        return ans;
    }
}tree;

ll up(int l,int r)
{
    ll ans=0;
    while(top[l]!=top[r])
    {
        if(dep[top[l]]<dep[top[r]])swap(l,r);
        ans+=tree.query(tid[top[l]],tid[l],1,n,1);
        l=fa[top[l]];
    }
    if(dep[l]<dep[r])swap(l,r);
    ans+=tree.query(tid[r],tid[l],1,n,1);
    return ans;
}

int main()
{
    init();
    int q;
    scanf("%d%d",&n,&q);
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    for(int i=1; i<n; i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }
    dfs1(1,-1,1);
    dfs2(1,1);
    tree.build(1,n,1);
    while(q--)
    {
        int t,x;
        scanf("%d%d",&t,&x);
        if(t==1)
        {
            ll z;
            scanf("%lld",&z);
            tree.update(tid[x],tid[x],z,1,n,1);
        }
        else if(t==2)
        {
            ll z;
            scanf("%lld",&z);
            tree.update(tid[x],mx[x],z,1,n,1);
        }
        else
            printf("%lld
",up(1,x));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jhz033/p/6785496.html