其他分享
首页 > 其他分享> > P3258 松鼠的新家

P3258 松鼠的新家

作者:互联网

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

本题中每次所修改路径的终点等于下一次所修改路径的起点,所以每次修改完后要将终点的果子数-1。

Code:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
#include <iostream>
using namespace std;
const int N=300005;
int n,m,a[N],x,y,now[N*2],id[N*2],rnt,topf[N*2],top[N*2];
int head[N*2],Next[N*2],to[N*2],cnt,son[N*2],size[N*2],deep[N*2],father[N*2];
struct node{
    int w;
    int left;
    int right;
    int v;  
}tree[N*4];
void Spread_Tree(int 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].v=0;
    }
}
void Build_Tree(int o,int l,int r){
    tree[o].left=l;
    tree[o].right=r;
    if(l==r){
        return;
    }
    int 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;
}
int Query_Tree(int o,int l,int r){
    if(tree[o].left>=l&&tree[o].right<=r){
        return tree[o].w;
    }
    int mid=(tree[o].left+tree[o].right)/2;
    Spread_Tree(o);
    long long ans=0;
    if(l<=mid){
        ans+=Query_Tree(o*2,l,r);
    }
    if(r>mid){
        ans+=Query_Tree(o*2+1,l,r);
    }
    return ans;
}
void Change_Tree(int o,int l,int r,int k){
    if(tree[o].right<=r&&tree[o].left>=l){
        tree[o].w+=k*(tree[o].right-tree[o].left+1);
        tree[o].v+=k;
        return;
    }
    Spread_Tree(o);
    int 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;
}
void Insert(int u,int v){
    ++cnt;
    Next[cnt]=head[u];
    head[u]=cnt;
    to[cnt]=v;  
}
void dfs_1(int u,int f){
    deep[u]=deep[f]+1;
    size[u]=1;
    father[u]=f;
    for(int i=head[u];i;i=Next[i]){
        int 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(int u,int topf){
    id[u]=++rnt;
    now[rnt]=a[u];
    top[u]=topf;
    if(!son[u]){
        return;
    }
    dfs_2(son[u],topf);
    for(int i=head[u];i;i=Next[i]){
        int v=to[i];
        if(v==father[u]||v==son[u]){
            continue;
        }
        dfs_2(v,v); 
    }
}   
int Query_Interval(int x,int y){
    int ans=0;
    while(top[x]!=top[y]){
        if(deep[top[x]]<deep[top[y]]) swap(x,y);
        ans+=(Query_Tree(1,id[top[x]],id[x]));
        x=father[top[x]];
    }
    if(deep[x]>deep[y]){
        swap(x,y);
    }
    ans+=Query_Tree(1,id[x],id[y]);
    return ans;
}
void Change_Interval(int x,int y,int 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);
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<n;i++) {
        scanf("%d%d",&x,&y);
        Insert(x,y);
        Insert(y,x);
    }
    dfs_1(1,0);
    dfs_2(1,1);
    Build_Tree(1,1,n);
    for(int i=1;i<n;i++){
        Change_Interval(a[i],a[i+1],1);
        Change_Interval(a[i+1],a[i+1],-1);
    }
    for(int i=1;i<=n;i++){
        printf("%d\n",Query_Interval(i,i));
    }
    return 0;
}

标签:right,新家,int,tree,top,Tree,P3258,松鼠,void
来源: https://www.cnblogs.com/ukcxrtjr/p/11237565.html