1. 程式人生 > >【bzoj3631】【JLOI2014】松鼠的新家

【bzoj3631】【JLOI2014】松鼠的新家

get jloi2014 har main type ret d+ () nbsp

題目描述

松鼠的新家是一棵樹,前幾天剛剛裝修了新家,新家有n個房間,並且有n-1根樹枝連接,每個房間都可以相互到達,且倆個房間之間的路線都是唯一的。天哪,他居然真的住在“樹”上。松鼠想邀請小熊維尼前來參觀,並且還指定一份參觀指南,他希望維尼能夠按照他的指南順序,先去a1,再去a2,……,最後到an,去參觀新家。 可是這樣會導致維尼重復走很多房間,懶惰的維尼不聽地推辭。可是松鼠告訴他,每走到一個房間,他就可以從房間拿一塊糖果吃。維尼是個饞家夥,立馬就答應了。 現在松鼠希望知道為了保證維尼有糖果吃,他需要在每一個房間各放至少多少個糖果。因為松鼠參觀指南上的最後一個房間an是餐廳,餐廳裏他準備了豐盛的大餐,所以當維尼在參觀的最後到達餐廳時就不需要再拿糖果吃了。


輸入

第一行一個整數n,表示房間個數第二行n個整數,依次描述a1-an

接下來n-1行,每行兩個整數x,y,表示標號x和y的兩個房間之間有樹枝相連。


輸出

一共n行,第i行輸出標號為i的房間至少需要放多少個糖果,才能讓維尼有糖果吃。


樣例輸入

5
1 4 5 3 2
1 2
2 4
2 3
4 5


樣例輸出

1
2
1
2
1



題解

a[ i ] 到 a[ i+1 ]之間的點+1,a[ i+1 ]再-1,然後查詢所有點就ok。

#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include
<iostream> #include<algorithm> using namespace std; #define ll long long const int maxn=300000+50; const int maxm=600000+50; int n,fa[maxn],m,cnt,a[maxn]; int fir[maxn],nex[maxm],to[maxm],ecnt; int top[maxn],son[maxn],dep[maxn],sz[maxn],id[maxn]; struct SegmentTree{int v,l,r,add;}st[maxn<<2
]; void add(int u,int v){ nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v; } void dfs1(int x,int f,int deep){ fa[x]=f;dep[x]=deep; sz[x]=1; for(int e=fir[x];e;e=nex[e]){ int v=to[e]; if(v==f) continue; dfs1(v,x,deep+1); sz[x]+=sz[v]; if(sz[v]>sz[son[x]]) son[x]=v; } } void dfs2(int x,int topf){ top[x]=topf; id[x]=++cnt; if(!son[x]) return ; dfs2(son[x],topf); for(int e=fir[x];e;e=nex[e]){ int v=to[e]; if(v==fa[x]||v==son[x]) continue; dfs2(v,v); } } void pushup(int root){ st[root].v=st[root<<1].v+st[root<<1|1].v; } void build(int root,int l,int r){ st[root].l=l;st[root].r=r; if(l==r) return ; int m=l+r>>1; build(root<<1,l,m);build(root<<1|1,m+1,r); pushup(root); } void pushdown(int root){ st[root<<1].v=st[root<<1].v+st[root].add*(st[root<<1].r-st[root<<1].l+1); st[root<<1|1].v=st[root<<1|1].v+st[root].add*(st[root<<1|1].r-st[root<<1|1].l+1); st[root<<1].add=st[root<<1].add+st[root].add; st[root<<1|1].add=st[root<<1|1].add+st[root].add; st[root].add=0; } void add(int root,int l,int r,int val){ if(st[root].l>r||st[root].r<l) return ; if(st[root].l>=l&&st[root].r<=r){ st[root].v=st[root].v+val*(st[root].r-st[root].l+1); st[root].add=st[root].add+val; } else{ pushdown(root); add(root<<1,l,r,val);add(root<<1|1,l,r,val); pushup(root); } } int query(int root,int l,int r){ if(st[root].l>r||st[root].r<l) return 0; if(st[root].l>=l&&st[root].r<=r) return st[root].v; pushdown(root); return query(root<<1,l,r)+query(root<<1|1,l,r); } void change(int x,int y,int val){ int f1=top[x],f2=top[y]; while(f1!=f2){ if(dep[f1]<dep[f2]) swap(x,y),swap(f1,f2); add(1,id[f1],id[x],val); x=fa[f1],f1=top[x]; } if(dep[x]>dep[y]) swap(x,y); add(1,id[x],id[y],val); } template<typename T>void read(T& aa){ char cc; ll ff;aa=0;cc=getchar();ff=1; while((cc<0||cc>9)&&cc!=-) cc=getchar(); if(cc==-) ff=-1,cc=getchar(); while(cc>=0&&cc<=9) aa=aa*10+cc-0,cc=getchar(); aa*=ff; } int main(){ read(n); for(int i=1;i<=n;i++) read(a[i]); for(int i=1;i<n;i++){ int x,y; read(x),read(y); add(x,y);add(y,x); } dfs1(1,0,1);dfs2(1,1);build(1,1,n); for(int i=1;i<n;i++){ change(a[i],a[i+1],1); change(a[i+1],a[i+1],-1); } for(int i=1;i<=n;i++) printf("%d\n",query(1,id[i],id[i])); return 0; }

【bzoj3631】【JLOI2014】松鼠的新家