[luogu3258][JLOI2014]松鼠的新家

时间:2024-07-02 12:34:02

题解

我们就在\([a_i,a_{i+1}]\)的路径上都\(+1\),然后单点查询就可以了。

ac代码(吸了氧才过的QwQ)

# include <bits/stdc++.h>
# define LL long long
# define ms(a,b) memset(a,b,sizeof(a))
# define ri (register int)
# define inf (0x7f7f7f7f)
# define pb push_back
# define fi first
# define se second
# define pii pair<int,int>
# define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
inline int gi(){
    int w=0,x=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return w?-x:x;
}
# define N 1300005
struct segment_tree{
    # define mid ((l+r)>>1)
    # define ls (nod<<1)
    # define rs (nod<<1|1)
    struct node{
        int l,r,s,tag;
    }tr[N<<2];
    void pushup(int nod){
        tr[nod].s=tr[ls].s+tr[rs].s;
    }
    void pushdown(int nod){
        register int tmp=tr[nod].tag,l=tr[nod].l,r=tr[nod].r; tr[nod].tag=0;
        if (tmp==0) return;
        tr[ls].tag+=tmp; tr[rs].tag+=tmp;
        tr[ls].s+=tmp*(mid-l+1); tr[rs].s+=tmp*(r-mid);
    }
    void build(int l,int r,int nod){
        tr[nod].l=l,tr[nod].r=r,tr[nod].tag=0,tr[nod].s=0;
        if (l==r) return;
        build(l,mid,ls); build(mid+1,r,rs);
        pushup(nod);
    }
    void update_sec(int nod,int ql,int qr,int v){
        register int l=tr[nod].l,r=tr[nod].r; pushdown(nod);
        if (ql<=l&&r<=qr){
            tr[nod].tag+=v;
            tr[nod].s+=(r-l+1)*v;
            return;
        }
        if (ql<=mid) update_sec(ls,ql,qr,v);
        if (qr>mid) update_sec(rs,ql,qr,v);
        pushup(nod);
    }
    int query_point(int nod,int k){
        register int l=tr[nod].l,r=tr[nod].r;
        pushdown(nod);
        if (l>=r) return tr[nod].s;
        if (k<=mid) return query_point(ls,k);
        else return query_point(rs,k);
    }
}tr;
struct edge{
    int to,nt;
}E[N<<1];
int son[N],top[N],sz[N],fa[N],H[N],dep[N],idx[N],a[N];
int cnt,tot,n,m;
inline void addedge(int u,int v){
    E[++cnt]=(edge){v,H[u]}; H[u]=cnt;
}
inline void dfs1(int u,int ft,int dp){
    dep[u]=dp; fa[u]=ft; sz[u]=1;
    register int maxson=-1;
    for (register int e=H[u];e;e=E[e].nt){
        int v=E[e].to; if (v==fa[u]) continue;
        dfs1(v,u,dp+1); sz[u]+=sz[v];
        if (sz[v]>maxson) maxson=sz[v],son[u]=v;
    }
}
inline void dfs2(int u,int tp){
    top[u]=tp; idx[u]=++tot;
    if (!son[u]) return;
    dfs2(son[u],tp);
    for (register int e=H[u];e;e=E[e].nt){
        int v=E[e].to; if (v==fa[u]||v==son[u]) continue;
        dfs2(v,v);
    }
}
void update(int u,int v,int w){
    while (top[u]!=top[v]){
        if (dep[top[u]]<dep[top[v]]) swap(u,v);
        tr.update_sec(1,idx[top[u]],idx[u],w);
        u=fa[top[u]];
    }
    if (dep[u]>dep[v]) swap(u,v);
    tr.update_sec(1,idx[u],idx[v],w);
}
int main(){
    n=gi();
    for (int i=1;i<=n;++i) a[i]=gi();
    for (int i=1;i<n;++i){
        int u=gi(),v=gi();
        addedge(u,v); addedge(v,u);
    }
    dfs1(1,0,1); dfs2(1,1); tr.build(1,n,1);
    for (int i=1;i<n;++i){
        update(a[i],a[i+1],1);
        update(a[i+1],a[i+1],-1);
    }
    for (int i=1;i<=n;++i) printf("%d\n",tr.query_point(1,idx[i]));
    return 0;
}