题5 - [JLOI2014]松鼠的新家

给定一棵树的路径关系,给定一个访问结点的顺序序列a,先去a1,再去a2,……,最后到an。同时每经过一次结点,结点权值+1,对于最后一个访问的结点不用+1。最后询问每一个结点的权值。

思路:对于从x结点去y结点,只需将x到y的链上权值+1,同时对y结点权值-1即可,模板题。

代码:

#include<bits/stdc++.h>
#define ll long long
#define L(i,j,k) for(ll i=(j);i<=(k);i++)
#define R(i,j,k) for(ll i=(j);i>=(k);i--)
#define inf 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define MS(i,j) memset(i,j,sizeof (i))
const ll N=1e6+10,M=5,mod=998244353,mmod=mod-1;
const double pi=acos(-1);
using namespace std;
ll gcd(ll x,ll y){if(y==0) return x;return gcd(y,x%y);}
ll fksm(ll a,ll b){ll r=1;if(b<0)b+=mod-1;for(a%=mod;b;b>>=1){if(b&1)r=r*a%mod;a=a*a%mod;}return r;}//a 分母; b MOD-2
ll lowbit(ll x){return x&(-x);}

ll m,n,t,x,y,z,l,r,k,p,pp,nx,ny,ansx,ansy,lim,num,sum,pos,tot,root,block,key,cnt,minn,maxx,ans;
ll a[N],b[N],head[N],dx[5]={0,0,-1,1},dy[5]={-1,1,0,0};
double dans;
bool vis,flag;
char mapp,zz;
struct qq{ll x,y,z;}q;
struct tree{ll l,r,tag,sum;}trs[N*4];
struct Tree{ll fa,dep,dfn,siz,son,top,w;}tr[N];
struct Trp{ll l,r,fat,dep,n,w;}trp;
struct E{ll to,nxt,w;}eg[N*2];
struct matrix{ll n,m[M][M];};
struct complx{
	double r,i;
	complx(){}
	complx(double r,double i):r(r),i(i){}
	complx operator+(const complx& rhs)const{return complx (r+rhs.r,i+rhs.i);}
	complx operator-(const complx& rhs)const{return complx (r-rhs.r,i-rhs.i);}
	complx operator*(const complx& rhs)const{return complx (r*rhs.r-i*rhs.i,i*rhs.r+r*rhs.i);}
	void operator+=(const complx& rhs){r+=rhs.r,i+=rhs.i;}
	void operator*=(const complx& rhs){r=r*rhs.r-i*rhs.i,i=r*rhs.i+i*rhs.r;}
	void operator/=(const double& x){r/=x,i/=x;}
	complx conj(){return complx(r,-i);}
}; 

bool cmp(qq u,qq v){
    return u.x*v.y>v.x*u.y;
}
bool cmp1(qq u,qq v){
    return u.x<v.x;
}
bool cmpl(ll u,ll v){return u>v;}
struct cmps{bool operator()(ll u,ll v){
    return u>v;
}};//shun序

pair<ll,ll>pre;
vector<ll>v;//v.assign(m,vector<ll>(n));
//priority_queue<ll,vector<ll>,cmps>sp;
deque<qq>sq;
map<ll,ll>mp;
bitset<M>bi;

void add(ll u,ll v,ll w){
	eg[++cnt].to=v;
	eg[cnt].nxt=head[u];
	eg[cnt].w=w;
	head[u]=cnt;
}

void push_up(ll k){
	trs[k].sum=trs[k*2].sum+trs[k*2+1].sum;
}

void push_down(ll k){
	if(trs[k].tag){
		ll l=k*2,r=k*2+1;
		trs[l].tag+=trs[k].tag;
		trs[r].tag+=trs[k].tag;
		trs[l].sum+=(trs[l].r-trs[l].l+1)*trs[k].tag;
		trs[r].sum+=(trs[r].r-trs[r].l+1)*trs[k].tag;
		trs[k].tag=0; 
	}
}

void bd_tree(ll k,ll l,ll r){
	trs[k].tag=0;
	trs[k].l=l,trs[k].r=r;
	if(l==r){
		trs[k].sum=a[l];
		return;
	}
	ll mid=(l+r)/2;
	bd_tree(k*2,l,mid);
	bd_tree(k*2+1,mid+1,r);
	push_up(k);
}

ll query(ll k,ll pl,ll pr){
	ll ml=0,mr=0;
	if(trs[k].l>=pl&&trs[k].r<=pr){
		return trs[k].sum;
	}
	push_down(k);
	ll mid=(trs[k].l+trs[k].r)/2;
	if(mid>=pl)ml=query(k*2,pl,pr);
	if(mid+1<=pr)mr=query(k*2+1,pl,pr);
	return ml+mr;
}

void modify(ll k,ll pl,ll pr,ll val){//[pl,pr]改为val 
	if(trs[k].l>=pl&&trs[k].r<=pr){
		trs[k].sum+=(trs[k].r-trs[k].l+1)*val;
		trs[k].tag+=val;
		return;
	}
	push_down(k);
	ll mid=(trs[k].l+trs[k].r)/2;
	if(mid>=pl)modify(k*2,pl,pr,val);
	if(mid+1<=pr)modify(k*2+1,pl,pr,val);
	push_up(k);
}

void dfs1(ll x,ll ac){
	tr[x].fa=ac;
	tr[x].dep=tr[tr[x].fa].dep+1;
	tr[x].siz=1;
	ll k=head[x];
	while(k){
		ll y=eg[k].to;
		if(y!=ac){
			dfs1(y,x);
			tr[x].siz+=tr[y].siz;
			if(!tr[x].son||tr[y].siz>tr[tr[x].son].siz)tr[x].son=y;
		}
		k=eg[k].nxt;
	}
}

void dfs2(ll x,ll pos){
	tr[x].dfn=++tot;
	tr[x].top=pos;
	a[tot]=tr[x].w;
	if(!tr[x].son)return;
	dfs2(tr[x].son,pos);
	ll k=head[x];
	while(k){
		ll y=eg[k].to;
		if(y!=tr[x].fa&&y!=tr[x].son)dfs2(y,y);
		k=eg[k].nxt;
	} 
}

void mchain(ll x,ll y,ll val){
	while(tr[x].top!=tr[y].top){
		if(tr[tr[x].top].dep<tr[tr[y].top].dep)modify(1,tr[tr[y].top].dfn,tr[y].dfn,val),y=tr[tr[y].top].fa;
		else modify(1,tr[tr[x].top].dfn,tr[x].dfn,val),x=tr[tr[x].top].fa;
	}
	if(tr[x].dep>tr[y].dep)swap(x,y);
	modify(1,tr[x].dfn,tr[y].dfn,val);
}

ll qchain(ll x,ll y){
	ll res=0;
	while(tr[x].top!=tr[y].top){
		if(tr[tr[x].top].dep<tr[tr[y].top].dep)res+=query(1,tr[tr[y].top].dfn,tr[y].dfn),y=tr[tr[y].top].fa;
		else res+=query(1,tr[tr[x].top].dfn,tr[x].dfn),x=tr[tr[x].top].fa;
	}
	if(tr[x].dep>tr[y].dep)swap(x,y);
	res+=query(1,tr[x].dfn,tr[y].dfn);
	return res;
}

int main(){
	scanf("%lld",&n);
	L(i,1,n)scanf("%lld",&b[i]);
	cnt=0;
	L(i,1,n-1){
		scanf("%lld%lld",&x,&y);
		add(x,y,0);
		add(y,x,0);
	}
	tot=0;
	dfs1(1,0);
	dfs2(1,1);
	bd_tree(1,1,n);
	//L(i,1,n)printf("%lld ",tr[i].dfn);printf("\n");
	
	L(i,2,n){
		mchain(b[i-1],b[i],1);
		modify(1,tr[b[i]].dfn,tr[b[i]].dfn,-1);
	}
	L(i,1,n){
		printf("%lld\n",query(1,tr[i].dfn,tr[i].dfn));
	}
}