bzoj3626: [LNOI2014]LCA

来源:互联网 发布:php array diff keys 编辑:程序博客网 时间:2024/05/21 19:27

将询问离线,从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);    }}
原创粉丝点击