洛谷 P3258 [JLOI2014]松鼠的新家(树链剖分)

题目描述
松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的。天哪,他居然真的住在”树“上。

松鼠想邀请小熊维尼前来参观,并且还指定一份参观指南,他希望维尼能够按照他的指南顺序,先去a1,再去a2,......,最后到an,去参观新家。可是这样会导致维尼重复走很多房间,懒惰的维尼不停地推辞。可是松鼠告诉他,每走到一个房间,他就可以从房间拿一块糖果吃。

维尼是个馋家伙,立马就答应了。现在松鼠希望知道为了保证维尼有糖果吃,他需要在每一个房间各放至少多少个糖果。

因为松鼠参观指南上的最后一个房间an是餐厅,餐厅里他准备了丰盛的大餐,所以当维尼在参观的最后到达餐厅时就不需要再拿糖果吃了。

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

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

输入输出样例
输入样例#1:
5
1 4 5 3 2
1 2
2 4
2 3
4 5
输出样例#1:
1
2
1
2
1
说明
2<= n <=300000

题解:看来一般难度比树链剖分模板简单的题一般都不难……

很简单的一道树链剖分,每次就是路径修改,注意一个坑点,不要把一趟的终点加完再在下一趟又把它当起点加上去,这样会有重复……

可以预处理出i-i+1的路径上第一个点的位置,但是这不够粗暴,只需要每次加完顺便把这个点减一减就行了,我是最暴力的在线段树上减,其实可以完全点查询答案的时候再减

事实证明区别不大,都会t成狗

但复杂度是对的,所以这道题最恶心在卡常上

我不会卡常,于是我开了o2,就这么a掉了……

代码如下:

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson root<<1
#define rson root<<1|1
#define hi puts("hi");
using namespace std;

struct node
{
    int lazy,sum,l,r;
} tr[1200120];
int a[300030],fa[300030],son[300030],size[300030],deep[300030],top[300030],id[300030],cnt,ans[300030];
vector<int> g[300030];

void push_up(int root)
{
    tr[root].sum=tr[lson].sum+tr[rson].sum;
}

void push_down(int root)
{
    int mid=(tr[root].l+tr[root].r)>>1;
    tr[lson].sum+=tr[root].lazy*(mid-tr[root].l+1);
    tr[lson].lazy+=tr[root].lazy;
    tr[rson].sum+=tr[root].lazy*(tr[root].r-mid);
    tr[rson].lazy+=tr[root].lazy;
    tr[root].lazy=0;
}

void build(int root,int l,int r)
{
    if(l==r)
    {
        tr[root].l=l;
        tr[root].r=r;
        tr[root].sum=0;
        return ;
    }
    tr[root].l=l;
    tr[root].r=r;
    int mid=(l+r)>>1;
    build(lson,l,mid);
    build(rson,mid+1,r);
    push_up(root);
}

void update(int root,int l,int r,int val)
{
    if(tr[root].l==l&&tr[root].r==r)
    {
        tr[root].sum+=val*(tr[root].r-tr[root].l+1);
        tr[root].lazy+=val;
        return ;
    }
    if(tr[root].lazy)
    {
        push_down(root);
    }
    int mid=(tr[root].l+tr[root].r)>>1;
    if(l>mid)
    {
        update(rson,l,r,val);
    }
    else
    {
        if(r<=mid)
        {
            update(lson,l,r,val);
        }
        else
        {
            update(lson,l,mid,val);
            update(rson,mid+1,r,val);
        }
    }
    push_up(root);
}

int query(int root,int l,int r)
{
    if(l==tr[root].l&&r==tr[root].r)
    {
        return tr[root].sum;
    }
    if(tr[root].lazy)
    {
        push_down(root);
    }
    int mid=(tr[root].l+tr[root].r)>>1;
    if(l>mid)
    {
        return query(rson,l,r);
    }
    else
    {
        if(mid>=r)
        {
            return query(lson,l,r);
        }
        else
        {
            return query(lson,l,mid)+query(rson,mid+1,r);
        }
    }
}

void dfs1(int now,int f,int dep)
{
    fa[now]=f;
    deep[now]=dep;
    size[now]=1;
    int maxson=-1;
    for(int i=0; i<g[now].size(); i++)
    {
        if(g[now][i]==f)
        {
            continue;
        }
        dfs1(g[now][i],now,dep+1);
        size[now]+=size[g[now][i]];
        if(size[g[now][i]]>maxson)
        {
            maxson=size[g[now][i]];
            son[now]=g[now][i];
        }
    }
}

void dfs2(int now,int topf)
{
    id[now]=++cnt;
    top[now]=topf;
    if(!son[now])
    {
        return ;
    }
    dfs2(son[now],topf);
    for(int i=0; i<g[now].size(); i++)
    {
        if(g[now][i]==fa[now]||g[now][i]==son[now])
        {
            continue;
        }
        dfs2(g[now][i],g[now][i]);
    }
}

void path_update(int x,int y)
{
    while(top[x]!=top[y])
    {
        if(deep[top[x]]<deep[top[y]])
        {
            swap(x,y);
        }
        update(1,id[top[x]],id[x],1);
        x=fa[top[x]];
    }
    if(deep[x]>deep[y])
    {
        swap(x,y);
    }
    update(1,id[x],id[y],1);
}

void dfs3(int now,int f)
{
    ans[now]=query(1,id[now],id[now]);
    for(int i=0; i<g[now].size(); i++)
    {
        if(g[now][i]==fa[now])
        {
            continue;
        }
        dfs3(g[now][i],now);
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1; i<=n-1; i++)
    {
        int from,to;
        scanf("%d%d",&from,&to);
        g[from].push_back(to);
        g[to].push_back(from);
    }
    dfs1(1,0,1);
    dfs2(1,1);
    build(1,1,n);
    for(int i=1; i<=n-1; i++)
    {
        path_update(a[i],a[i+1]);
        update(1,id[a[i]],id[a[i]],-1);
    }
    dfs3(1,0);
    ans[a[1]]++;
    ans[a[n]]--;
    for(int i=1; i<=n; i++)
    {
        printf("%d
",ans[i]);
    }
}
原文地址:https://www.cnblogs.com/stxy-ferryman/p/8671906.html