P3178 树上操作

Posted ukcxrtjr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3178 树上操作相关的知识,希望对你有一定的参考价值。

题面:https://www.luogu.org/problemnew/show/P3178

Code:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#include<iostream>
using namespace std;
const long long N=100005,INF=1000000000;
long long n,m,a[N],x,y,now[N*4],id[N*4],Cnt,topf[N*4],top[N*4];
long long head[N*4],Next[N*4],to[N*4],cnt,son[N*4],size[N*4],deep[N*4],father[N*4];
long long read()
    long long X=0,W=1;
    char ch=0;
    while(ch<'0'||ch>'9')
        if(ch=='-')
            W=-1;
        
        ch=getchar();
    
    while(ch>='0'&&ch<='9')
        X=(X<<3)+(X<<1)+ch-'0';
        ch=getchar();
    
    return X*W;

struct node
    long long w;
    long long left;
    long long right;
    long long v;
    long long mx;
tree[N*4];
void Spread_Tree(long long o)
    if(tree[o].v)
        tree[o*2].w+=tree[o].v*(tree[o*2].right-tree[o*2].left+1);
        tree[o*2+1].w+=tree[o].v*(tree[o*2+1].right-tree[o*2+1].left+1);
        tree[o*2].v+=tree[o].v;
        tree[o*2+1].v+=tree[o].v;
        tree[o*2].mx+=tree[o].v;
        tree[o*2+1].mx+=tree[o].v;
        tree[o].v=0;
    

void Build_Tree(long long o,long long l,long long r)
    tree[o].left=l;
    tree[o].right=r;
    if(l==r)
        tree[o].w=tree[o].mx=now[tree[o].left];
        return;
    
    long long mid=(l+r)/2;
    Build_Tree(o*2,l,mid);
    Build_Tree(o*2+1,mid+1,r);
    tree[o].w=tree[o*2].w+tree[o*2+1].w;
    tree[o].mx=max(tree[o*2].mx,tree[o*2+1].mx);

long long Insert_Tree(long long o)
    if(tree[o].left==tree[o].right)

        return tree[o].w;
    
    tree[o].w=Insert_Tree(o*2)+Insert_Tree(o*2+1);
    tree[o].mx=max(Insert_Tree(o*2),Insert_Tree(o*2+1));
    return tree[o].w;

long long Query_Tree_Sum(long long o,long long l,long long r)
    if(tree[o].left>=l&&tree[o].right<=r)
        return tree[o].w;
    
    long long mid=(tree[o].left+tree[o].right)/2;
    Spread_Tree(o);
    long long ans=0;
    if(l<=mid)
        ans+=Query_Tree_Sum(o*2,l,r);
    
    if(r>mid)
        ans+=Query_Tree_Sum(o*2+1,l,r);
    
    return ans;

long long Query_Tree_Max(long long o,long long l,long long r)
    if(tree[o].left>=l&&tree[o].right<=r)
        return tree[o].mx;
    
    long long mid=(tree[o].left+tree[o].right)/2;
    Spread_Tree(o);
    long long ans=-INF;
    if(l<=mid)
        ans=max(ans,Query_Tree_Max(o*2,l,r));
    
    if(r>mid)
        ans=max(ans,Query_Tree_Max(o*2+1,l,r));
    
    return ans;

void Change_Tree(long long o,long long l,long long r,long long k)
    if(tree[o].right<=r&&tree[o].left>=l)
        tree[o].w+=k*(tree[o].right-tree[o].left+1);
        tree[o].mx+=k;
        tree[o].v+=k;
        return;
    
    Spread_Tree(o);
    long long mid=(tree[o].left+tree[o].right)/2;
    if(l<=mid)
        Change_Tree(o*2,l,r,k);
    
    if(r>mid)
        Change_Tree(o*2+1,l,r,k);
    
    tree[o].w=tree[o*2].w+tree[o*2+1].w;
    tree[o].mx=max(tree[o*2].mx,tree[o*2+1].mx);

void Insert(long long u,long long v)
    ++cnt;
    Next[cnt]=head[u];
    head[u]=cnt;
    to[cnt]=v;

void dfs_1(long long u,long long f)
    deep[u]=deep[f]+1;
    size[u]=1;
    father[u]=f;
    for(long long i=head[u];i;i=Next[i])
        long long v=to[i];
        if(v!=f)
            dfs_1(v,u);
            size[u]+=size[v];
            if(size[v]>size[son[u]])
                son[u]=v;
            
        
    

void dfs_2(long long u,long long topf)
    id[u]=++Cnt;
    now[Cnt]=a[u];
    top[u]=topf;
    if(!son[u])
        return;
    
    dfs_2(son[u],topf);
    for(long long i=head[u];i;i=Next[i])
        long long v=to[i];
        if(v==father[u]||v==son[u])
            continue;
        
        dfs_2(v,v); 
    

long long Query_Interval_Sum(long long x,long long y)
    long long ans=0;
    while(top[x]!=top[y])
        if(deep[top[x]]<deep[top[y]])
            swap(x,y);
        
        ans+=Query_Tree_Sum(1,id[top[x]],id[x]);
        x=father[top[x]];
    
    if(deep[x]>deep[y])
        swap(x,y);
    
    ans+=Query_Tree_Sum(1,id[x],id[y]);
    return ans;

long long Query_Interval_Max(long long x,long long y)
    long long ans=-INF;
    while(top[x]!=top[y])
        if(deep[top[x]]<deep[top[y]])
            swap(x,y);
        
        ans=max(ans,Query_Tree_Max(1,id[top[x]],id[x]));
        x=father[top[x]];
    
    if(deep[x]>deep[y])
        swap(x,y);
    
    ans=max(ans,Query_Tree_Max(1,id[x],id[y]));
    return ans;

void Change_Interval(long long x,long long y,long long k)
    while(top[x]!=top[y])
        if(deep[top[x]]<deep[top[y]])
            swap(x,y);
        
        Change_Tree(1,id[top[x]],id[x],k);
        x=father[top[x]];   
    
    if(deep[x]>deep[y])
        swap(x,y);
    
    Change_Tree(1,id[x],id[y],k);

long long Query_Point_Sum(long long o)
    return Query_Tree_Sum(1,id[o],id[o]); 

long long Query_Point_Max(long long o)
    return Query_Tree_Max(1,id[o],id[o]); 

void Change_Point1(long long o,long long k)
    Change_Tree(1,id[o],id[o],k);

void Change_Point2(long long o,long long k)
    Change_Tree(1,id[o],id[o]+size[o]-1,k);

int main()
    scanf("%lld%lld",&n,&m);
    for(long long i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    
    for(long long i=1;i<n; i++) 
        scanf("%lld%lld",&x,&y);
        Insert(x,y);
        Insert(y,x);
    
    dfs_1(1,0);
    dfs_2(1,1);
    Build_Tree(1,1,n);
    for(long long i=1;i<=m;i++)
        long long opt,p,k;
        scanf("%lld",&opt);
        if(opt==1)
            scanf("%lld%lld",&p,&k);
            Change_Point1(p,k);
        
        else if(opt==2)
            scanf("%lld%lld",&p,&k);
            Change_Point2(p,k);
        
        else if(opt==3)
            scanf("%lld",&p);
            printf("%lld\n",Query_Interval_Sum(1,p));
        
    
    return 0;

以上是关于P3178 树上操作的主要内容,如果未能解决你的问题,请参考以下文章

P3178 树上操作

「Luogu P3178」[HAOI2015]树上操作

P3178 [HAOI2015]树上操作

洛谷——P3178 [HAOI2015]树上操作

luogu P3178 [HAOI2015]树上操作 题解

洛谷P3178 [HAOI2015]树上操作