[bzoj3631][树链剖分]松鼠的新家

109 篇文章 4 订阅
15 篇文章 0 订阅

Description

松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的。天哪,他居然真的住在“树”上。松鼠想邀请小熊维尼前来参观,并且还指定一份参观指南,他希望维尼能够按照他的指南顺序,先去a1,再去a2,……,最后到an,去参观新家。
可是这样会导致维尼重复走很多房间,懒惰的维尼不听地推辞。可是松鼠告诉他,每走到一个房间,他就可以从房间拿一块糖果吃。维尼是个馋家伙,立马就答应了。
现在松鼠希望知道为了保证维尼有糖果吃,他需要在每一个房间各放至少多少个糖果。因为松鼠参观指南上的最后一个房间an是餐厅,餐厅里他准备了丰盛的大餐,所以当维尼在参观的最后到达餐厅时就不需要再拿糖果吃了。

Input

第一行一个整数n,表示房间个数 第二行n个整数,依次描述a1-an
接下来n-1行,每行两个整数x,y,表示标号x和y的两个房间之间有树枝相连。

Output

一共n行,第i行输出标号为i的房间至少需要放多少个糖果,才能让维尼有糖果吃。

Sample Input

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

Sample Output

1
2
1
2
1

HINT

2<= n <=300000

题解

这个不就是一棵树上两点之间路径全部增大一个值吗
然而原来稚嫩的我想用树上差分,写了一个中午写挂了宣告GG回归数据结构
那就直接上裸的树剖呗。。
不过有一个细节,1~4 4~5的这两条路径中,4会被加两次,但实际上只用加一次
起点则一定只加了一次
于是乎我们起点特判一下,其他点找到的糖果数全部要-1再输出。。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
    int x,y,next;
}a[611000];int len,last[311000];
void ins(int x,int y)
{
    len++;
    a[len].x=x;a[len].y=y;
    a[len].next=last[x];last[x]=len;
}
int fa[311000],dep[311000],tot[311000],son[311000];
void pre_tree_node(int x)
{
    son[x]=0;tot[x]=1;
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(y!=fa[x])
        {
            fa[y]=x;
            dep[y]=dep[x]+1;
            pre_tree_node(y);
            if(tot[son[x]]<tot[y])son[x]=y;
            tot[x]+=tot[y];
        }
    }
}
int ys[311000],z,top[311000];
void pre_tree_edge(int x,int tp)
{
    ys[x]=++z;top[x]=tp;
    if(son[x]!=0)pre_tree_edge(son[x],tp);
    for(int k=last[x];k;k=a[k].next)if(a[k].y!=fa[x] && a[k].y!=son[x])pre_tree_edge(a[k].y,a[k].y);
}
struct trnode
{
    int lc,rc,l,r,c,lazy;
}tr[1111000];int trlen;
void bt(int l,int r)
{
    int now=++trlen;
    tr[now].l=l;tr[now].r=r;
    tr[now].lc=tr[now].rc=-1;tr[now].c=tr[now].lazy=0;
    if(l<r)
    {
        int mid=(l+r)/2;
        tr[now].lc=trlen+1;bt(l,mid);
        tr[now].rc=trlen+1;bt(mid+1,r);
    }
}
void upd(int x)
{
    int lc=tr[x].lc,rc=tr[x].rc;
    tr[x].c+=tr[x].lazy;
    if(lc!=-1){tr[lc].lazy+=tr[x].lazy;}
    if(rc!=-1){tr[rc].lazy+=tr[x].lazy;}
    tr[x].lazy=0;
}
void change(int now,int l,int r,int c)
{
    if(tr[now].l==l && tr[now].r==r){tr[now].lazy+=c;return ;}
    int lc=tr[now].lc,rc=tr[now].rc;
    int mid=(tr[now].l+tr[now].r)/2;
    if(r<=mid)change(lc,l,r,c);
    else if(mid+1<=l)change(rc,l,r,c);
    else {change(lc,l,mid,c);change(rc,mid+1,r,c);}
}
int fd(int now,int l,int r)
{
    if(tr[now].l==l && tr[now].r==r){upd(now);return tr[now].c;}
    int lc=tr[now].lc,rc=tr[now].rc;
    int mid=(tr[now].l+tr[now].r)/2;
    if(tr[now].lazy!=0)upd(now);
    if(r<=mid)return fd(lc,l,r);
    else return fd(rc,l,r);
}
void sol(int x,int y)
{
    int tx=top[x],ty=top[y];
    while(tx!=ty)
    {
        if(dep[tx]>dep[ty])swap(x,y),swap(tx,ty);
        change(1,ys[ty],ys[y],1);
        y=fa[ty];ty=top[y];
    }
    if(x==y){change(1,ys[x],ys[x],1);return ;}
    else
    {
        if(dep[x]>dep[y])swap(x,y);
        change(1,ys[x],ys[y],1);
        return ;
    }
}
int ans[311000];
int c[311000];
int n,tmp[311000];
int main()
{
    len=0;memset(last,0,sizeof(last));
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&tmp[i]);
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        ins(x,y);ins(y,x);
    }
    fa[1]=0;dep[1]=1;
    pre_tree_node(1);
    z=0;pre_tree_edge(1,1);
    trlen=0;bt(1,z);
    for(int i=2;i<=n;i++)
        sol(tmp[i],tmp[i-1]);
    ans[tmp[1]]=fd(1,ys[tmp[1]],ys[tmp[1]]);
    for(int i=2;i<=n;i++)ans[tmp[i]]=fd(1,ys[tmp[i]],ys[tmp[i]])-1;
    for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值