bzoj3626: [LNOI2014]LCA

将询问离线,从1到n分别到根进行区间修改,答案为query(r+1)-query(l).

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> q[50005];
inline int read()
{
    int f=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') {f=f*10+(ch^48);ch=getchar();}
    return f;
}
int fa[50005],dep[50005],son[50005],pos[50005],npos[50005],size[50005],tot,cnt,n,m;
int head[50005],top[50005],num=1,ql[50005],qr[50005],qc[50005],mod=201314;
long long ansl[50005],ansr[50005];
struct node
{
    int from;
    int to;
    int next;
}edge[100005];
struct segtree
{
    int ls;
    int rs;
    long long sum;
    long long mark;
}tree[200005];
inline void build(int p,int l,int r)
{
    int mid=l+r>>1;
    if(l==r)
    return;
    tree[p].ls=++num;
    tree[p].rs=++num;
    build(tree[p].ls,l,mid);
    build(tree[p].rs,mid+1,r);
}
inline void add(int u,int v)
{
    edge[tot].from=u;
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
inline void dfs1(int x)
{
    dep[x]=dep[fa[x]]+1;
    size[x]=1;
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        if(edge[i].to!=fa[x])
        {
            dfs1(edge[i].to);
            size[x]+=size[edge[i].to];
            if(size[edge[i].to]>size[son[x]])
            son[x]=edge[i].to;
        }
    }
}
inline void dfs2(int x)
{
    pos[x]=++cnt;
    npos[cnt]=x;
    if(son[fa[x]]==x)
    {
        top[x]=top[fa[x]];
    }
    else top[x]=x;
    if(son[x])
    dfs2(son[x]);
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        if(edge[i].to!=fa[x]&&edge[i].to!=son[x])
        dfs2(edge[i].to);
    }
}
inline void modify(int p,int x,int y,int l,int r)
{
    int mid=l+r>>1;
    if(l==x&&r==y)
    {
        tree[p].sum+=(r-l+1);
        tree[p].mark++;
        return;
    }
    tree[tree[p].ls].sum+=(mid-l+1)*tree[p].mark;
    tree[tree[p].rs].sum+=(r-mid)*tree[p].mark;
    tree[tree[p].ls].mark+=tree[p].mark;
    tree[tree[p].rs].mark+=tree[p].mark;
    tree[p].mark=0;
    if(y<=mid)
    modify(tree[p].ls,x,y,l,mid);
    else if(x>mid)
    modify(tree[p].rs,x,y,mid+1,r);
    else
    {
        modify(tree[p].ls,x,mid,l,mid);
        modify(tree[p].rs,mid+1,y,mid+1,r);
    }
    tree[p].sum=tree[tree[p].ls].sum+tree[tree[p].rs].sum;
}
inline void modify(int x,int y)
{
    int fy=top[y],fx=top[x];
    while(fx!=fy)
    {
        if(dep[fx]<dep[fy])
        swap(x,y),swap(fx,fy);
        modify(1,pos[fx],pos[x],1,n);
        x=fa[fx],fx=top[x];
    }
    if(dep[x]<dep[y])
    swap(x,y);
    modify(1,pos[y],pos[x],1,n);
}
inline int getsum(int p,int x,int y,int l,int r)
{
    int mid=l+r>>1;
    if(l==x&&r==y)
    {
        return tree[p].sum%mod;
    }
    tree[tree[p].ls].sum+=(mid-l+1)*tree[p].mark;
    tree[tree[p].rs].sum+=(r-mid)*tree[p].mark;
    tree[tree[p].ls].mark+=tree[p].mark;
    tree[tree[p].rs].mark+=tree[p].mark;
    tree[p].mark=0;
    if(y<=mid)
    return getsum(tree[p].ls,x,y,l,mid)%mod;
    else if(x>mid)
    return getsum(tree[p].rs,x,y,mid+1,r)%mod;
    return (getsum(tree[p].ls,x,mid,l,mid)+getsum(tree[p].rs,mid+1,y,mid+1,r))%mod;
}
inline int  getsum(int x,int y)
{
    int fx=top[x],fy=top[y];long long sum=0;
    while(fx!=fy)
    {
        if(dep[fx]<dep[fy])
        swap(x,y),swap(fx,fy);
        sum+=getsum(1,pos[fx],pos[x],1,n);
        sum%=mod;
        x=fa[fx];fx=top[x];
    }
    if(dep[x]<dep[y])
    swap(x,y);
    sum+=getsum(1,pos[y],pos[x],1,n);
    return sum%mod;
}
int main()
{
    //freopen("input.txt","r",stdin);
    memset(head,-1,sizeof(head));
    n=read(),m=read();
    for(int i=2;i<=n;i++)
    {
        fa[i]=read()+1;
        add(i,fa[i]);
        add(fa[i],i);
    }
    dfs1(1);
    dfs2(1);
    build(1,1,n);
    for(int i=1;i<=m;i++)
    {
        int l=read(),r=read()+1,c=read()+1;
        ql[i]=l;
        qr[i]=r;
        qc[i]=c;
        q[l].push_back(i);
        q[r].push_back(i);
    }
    int x=q[0].size();
    for(int i=0;i<x;i++)
    {
        ql[q[0][i]]=0;
    }
    for(int i=1;i<=n;i++)
    {
        modify(1,i);
        x=q[i].size();
        for(int j=0;j<x;j++)
        {
            if(ql[q[i][j]]==i)
            {
                ansl[q[i][j]]=getsum(1,qc[q[i][j]]);
            }
            if(qr[q[i][j]]==i)
            {
                ansr[q[i][j]]=getsum(1,qc[q[i][j]]);
            }
        }
    }
    for(int i=1;i<=m;i++)
    {
        printf("%lld\n",(((ansr[i]-ansl[i])%mod)+mod)%mod);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值