问题其实就是,当维尼熊走过一段路径时,将这些点的权值加1,统计完所有路径后输出每个点的权值
维护路径权值和,树剖
#include
using namespace std;
#define loop(i,start,end) for(register int i=start;i<=end;++i)
#define anti_loop(i,start,end) for(register int i=start;i>=end;--i)
#define clean(arry,num); memset(arry,num,sizeof(arry));
#define max(a,b) ((a>b)?a:b)
#define min(a,b) ((a
#define asc(a) (a-'A')
#define ll long long
int n,cnt=0;
const int maxn=300000+10;
struct node{
int e;int nxt;
}edge[maxn<<1];
int head[maxn];
int dep[maxn],son[maxn],fa[maxn],siz[maxn];
int rev[maxn],seg[maxn],top[maxn];
int path[maxn];
ll sum[maxn<<2],lazy[maxn<<2];
inline void addl(int u,int v){
edge[cnt].e=v;
edge[cnt].nxt=head[u];
head[u]=cnt++;
}
template<typename T>void read(T &x)
{
x=0;char r=getchar();T neg=1;
while(r>'9'||r<'0'){if(r=='-')neg=-1;r=getchar();}
while(r>='0'&&r<='9'){x=(x<<3)+(x<<1)+r-'0';r=getchar();}
x*=neg;
}
void dfs1(int u,int f){
fa[u]=f;dep[u]=dep[f]+1;
siz[u]=1;
for(int i=head[u];i!=-1;i=edge[i].nxt){
int v=edge[i].e;
if(v==f)continue;
dfs1(v,u);
siz[u]+=siz[v];
son[u]=((siz[son[u]]<siz[v])?v:son[u]);
}
}
void dfs2(int u,int fa){
if(son[u]){
seg[son[u]]=++seg[0];
rev[seg[0]]=son[u];
top[son[u]]=top[u];
dfs2(son[u],u);
}
for(int i=head[u];i!=-1;i=edge[i].nxt){
int v=edge[i].e;
if(!top[v]){
seg[v]=++seg[0];
rev[seg[0]]=v;
top[v]=v;
dfs2(v,u);
}
}
}
inline void pushup(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
inline void pushdown(int rt,int l,int r){
if(!lazy[rt])return;
lazy[rt<<1]+=lazy[rt],lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=lazy[rt]*(((l+r)>>1)-l+1);
sum[rt<<1|1]+=lazy[rt]*(r-(((l+r)>>1)+1)+1);
lazy[rt]=0;
}
void update(int l,int r,int nl,int nr,int rt,int w){
if(l<=nl&&nr<=r){
sum[rt]+=(nr-nl+1)*w;
lazy[rt]+=w;
return;
}
pushdown(rt,nl,nr);
int mid=nl+((nr-nl)>>1);
if(mid>=l)update(l,r,nl,mid,rt<<1,w);
if(mid<r)update(l,r,mid+1,nr,rt<<1|1,w);
pushup(rt);
}
ll query(int l,int r,int nl,int nr,int rt){
if(l<=nl&&nr<=r){
return sum[rt];
}
pushdown(rt,nl,nr);
int mid=nl+((nr-nl)>>1);
ll _sum=0;
if(mid>=l)_sum+=query(l,r,nl,mid,rt<<1);
if(mid<r)_sum+=query(l,r,mid+1,nr,rt<<1|1);
return _sum;
}
inline void reply(){
loop(i,1,n-1){
int x=path[i],y=path[i+1];
int fx=top[x],fy=top[y];
while(fx!=fy){
if(dep[fx]<dep[fy])swap(fx,fy),swap(x,y);
update(seg[fx],seg[x],1,seg[0],1,1);
x=fa[fx],fx=top[x];
}
if(dep[y]>dep[x])swap(x,y);
update(seg[y],seg[x],1,seg[0],1,1);
if(i!=1)
update(seg[path[i]],seg[path[i]],1,seg[0],1,-1);
if(i==n-1)
update(seg[path[i+1]],seg[path[i+1]],1,seg[0],1,-1);
/*loop(i,1,n){
printf("%lld ",query(seg[i],seg[i],1,seg[0],1));
}
printf("\n");*/
}
loop(i,1,n){
printf("%lld\n",query(seg[i],seg[i],1,seg[0],1));
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("datain.txt","r",stdin);
#endif
clean(head,-1);clean(dep,0);clean(son,0);clean(fa,0);clean(siz,0);clean(rev,0);clean(seg,0);clean(top,0);
read(n);
loop(i,1,n)read(path[i]);
loop(i,1,n-1){
int xi,yi;
read(xi),read(yi);
addl(xi,yi);
addl(yi,xi);
}
dfs1(1,0);
seg[0]=1;seg[1]=1;rev[1]=1;top[1]=1;
dfs2(1,0);
//loop(i,1,n)printf("point:%d dep:%d siz:%d son:%d fa:%d seg:%d rev:%d top:%d\n",i,dep[i],siz[i],son[i],fa[i],seg[i],rev[i],top[i]);
reply();
return 0;
}