bzoj 3626 树链剖分

本文深入探讨了树链剖分算法的应用,特别是在处理区间查询问题上的高效解决方案。通过将树形结构分解成一系列的“重链”,可以大幅度降低查询复杂度。文章详细介绍了树链剖分的数据结构构建过程,并提供了完整的实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于sigma_{l<=i<=r}dep[LCA(i,z)],其(num[i]表示:l到r,有num[i]个点在i子树中)

= deep[z] * num[z] + deep[ fa(z) ] * ( num[ fa(z) ] - num[z] ) + .... + deep[root] * ( num[root] - num[ ?? ] )

=  deep[ z ]*num[z] -0

  +deep[ fa(z) ]*num[ fa(z) ] - deep[ fa(z) ]*num[z]

  +...

  +deep[root]*num[root] - deep[ fa(fa(fa(...))) ]*num[  fa(fa(fa(...))) ]

= num[z] + num[ fa(z) ] + num[ fa(fa(z)) ] + ... + num[root]

而对于询问区间[l,r],我们可以将其插为[1,l-1]和[1,r]两个区间,sigma_{l<=i<=r}dep[LCA(i,z)] = sigma_{1<=i<=r}dep[LCA(i,z)] - sigma_{1<=i<=l-1}dep[LCA(i,z)],

这样的话,我们就可以离线统计所有分拆后的区间。

而剩下的,则采用树链剖分求解

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
typedef long long LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const int INF=100011122;
const double INFF=1e100;
const double eps=1e-8;
const int mod=1000000009;
const int NN=50010;
const int MM=1000010;
/* ****************** */

struct Q
{
    int r,x,id,ad;
    Q(){}
    Q(int a,int b,int c,int d)
    {
        r=a;
        x=b;
        id=c;
        ad=d;
    }
    bool operator<(const Q &tt)const
    {
        return r<tt.r;
    }
}q[NN*2];
LL ans[NN];
struct TR
{
    int l,r,fg;
    LL sum;
    int mid()
    {
        return (l+r)>>1;
    }
}tr[NN*4];

//c_dfs1
int anc[NN];//anc[i] i的父亲
int per[NN];//pre[i] -1:i下面没有重边;其他x表示通过重边连到的儿子
int si[NN]; //si[i] i子树有多少个儿子
//c_dfs2
int pos[NN];//pos[i] i在oula序中的位置
int top[NN];//top[i] i向上通过重边连到的最高点;top[]相同,说明在一条链上面
int tsp;

struct G
{
    int v,next;
}E[NN];
int p[NN],T;

void add(int u,int v)
{
    E[T].v=v;
    E[T].next=p[u];
    p[u]=T++;
}

void push_up(int R)
{
    tr[R].sum=tr[R<<1].sum+tr[R<<1|1].sum;
}
void down(int R,int col)
{
    tr[R].sum+=(LL)(tr[R].r-tr[R].l+1)*col;
    tr[R].fg+=col;
}
void push_down(int R)
{
    if(tr[R].fg!=0)
    {
        down(R<<1,tr[R].fg);
        down(R<<1|1,tr[R].fg);
        tr[R].fg=0;
    }
}
void build(int l,int r,int R)
{
    tr[R].l=l;
    tr[R].r=r;
    tr[R].fg=0;
    tr[R].sum=0;
    if(l==r)return;
    int mid=tr[R].mid();
    build(l,mid,R<<1);
    build(mid+1,r,R<<1|1);
}
LL query(int l,int r,int R)
{
    if(l<=tr[R].l && tr[R].r<=r)
        return tr[R].sum;
    push_down(R);
    LL t=0;
    int mid=tr[R].mid();
    if(l<=mid)
        t=t+query(l,r,R<<1);
    if(r>=mid+1)
        t=t+query(l,r,R<<1|1);
    return t;
}
void update(int l,int r,int R,int col)
{
    if(l<=tr[R].l && tr[R].r<=r)
    {
        down(R,col);
        return;
    }
    push_down(R);
    int mid=tr[R].mid();
    if(l<=mid)
        update(l,r,R<<1,col);
    if(r>=mid+1)
        update(l,r,R<<1|1,col);
    push_up(R);
}
//root->v的和
LL c_query(int v)
{
    LL s=0;
    while(v!=-1)
    {
        s=s+query(pos[top[v]],pos[v],1);
        v=top[v];
        v=anc[v];
    }
    return s;
}
//root->v +1
void c_update(int v)
{
    while(v!=-1)
    {
        update(pos[top[v]],pos[v],1,1);
        v=top[v];
        v=anc[v];
    }
}
void c_dfs1(int u,int fa)
{
    int i,v;
    si[u]=1;
    per[u]=-1;
    anc[u]=fa;
    for(i=p[u];i+1;i=E[i].next)
    {
        v=E[i].v;
        if(v==fa)continue;
        c_dfs1(v,u);
        si[u]+=si[v];
        if(per[u]==-1 || si[v]>si[ per[u] ])
            per[u]=v;
    }
}
void c_dfs2(int u,int fa,int now_head)
{
    pos[u]=++tsp;
    top[u]=now_head;
    if(per[u]!=-1)c_dfs2(per[u],u,now_head);
    int i,v;
    for(i=p[u];i+1;i=E[i].next)
    {
        v=E[i].v;
        if(v==fa || v==per[u])continue;
        c_dfs2(v,u,v);
    }
}

int main()
{
    int n,m,i,j,v;
    int l,r,x,tol;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(p,-1,sizeof(p));
        T=0;
        build(1,n,1);
        tol=0;

        for(i=2;i<=n;i++)
        {
            scanf("%d",&v);
            v++;
            add(v,i);
        }
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&l,&r,&x);
            q[tol++]=Q(l,x+1,i,-1);
            q[tol++]=Q(r+1,x+1,i,1);
        }

        c_dfs1(1,-1);
        tsp=0;
        c_dfs2(1,-1,1);

        sort(q,q+tol);
        j=0;
        memset(ans,0,sizeof(ans));
        for(i=0;i<=n;i++)
        {
            if(i)
            {
                c_update(i);
            }
            while(j<tol && q[j].r==i)
            {
                LL t=c_query(q[j].x);
                t=t*q[j].ad;
                ans[ q[j].id ]+=t;
                j++;
            }
        }

        for(i=0;i<m;i++)
        {
            printf("%lld\n",ans[i]%201314);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值