Description
给出一个n个节点的有根树(编号为0到n-1,根节点为0)。一个点的深度定义为这个节点到根的距离+1。
设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先。 有q次询问,每次询问给出l r
z,求sigma_{l<=i<=r}dep[LCA(i,z)]。 (即,求在[l,r]区间内的每个节点i与z的最近公共祖先的深度之和)
Input
第一行2个整数n q。 接下来n-1行,分别表示点1到点n-1的父节点编号。 接下来q行,每行3个整数l r z。
Output
输出q行,每行表示一个询问的答案。每个答案对201314取模输出
Sample Input
5 2
0
0
1
1
1 4 3
1 4 2
Sample Output
8
5
HINT
共5组数据,n与q的规模分别为10000,20000,30000,40000,50000。
题解
好题,把思路反一下。
对于一个询问z,如果我们把root~z这一段全部改成1
那么实际上就是要找sigma l<=i<=r,root~i的sum值
为什么,因为z的lca一定在z到root这条链上,然后只需要找i到root与z到root的最长公共链即可
这个操作是可逆的,也就是说如果我们把i到root这一段+1,找z~root的sum值也可以诶
考虑离线
对于答案,可以发现拆成两段是可以的,即1~r这一段减去1~l-1这段
我们把1~n依次加入修改,当枚举到有询问左端点+1或者询问右端点在这个地方时,更新答案即可
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int mod=201314;
struct node
{
int x,y,next;
}a[110000];int len,last[51000];
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[51000],son[51000],tot[51000],dep[51000];
int n,m;
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[y]>tot[son[x]])son[x]=y;
tot[x]+=tot[y];
}
}
}
int ys[51000],z,top[51000];
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;
int lazy;
}tr[210000];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 now)
{
int lc=tr[now].lc,rc=tr[now].rc;
int lsum=tr[lc].r-tr[lc].l+1,rsum=tr[rc].r-tr[rc].l+1;
tr[lc].lazy+=tr[now].lazy;tr[rc].lazy+=tr[now].lazy;
tr[lc].c+=lsum*tr[now].lazy;tr[rc].c+=rsum*tr[now].lazy;
tr[now].lazy=0;
}
void change(int now,int l,int r)
{
if(tr[now].l==l && tr[now].r==r)
{
tr[now].c+=(r-l+1);tr[now].lazy++;
return ;
}
int lc=tr[now].lc,rc=tr[now].rc;
int mid=(tr[now].l+tr[now].r)/2;
if(tr[now].lazy)upd(now);
if(r<=mid)change(lc,l,r);
else if(mid+1<=l)change(rc,l,r);
else change(lc,l,mid),change(rc,mid+1,r);
tr[now].c=tr[lc].c+tr[rc].c;
}
int findsum(int now,int l,int r)
{
if(tr[now].l==l && tr[now].r==r)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)upd(now);
if(r<=mid)return findsum(lc,l,r);
else if(mid+1<=l)return findsum(rc,l,r);
else return findsum(lc,l,mid)+findsum(rc,mid+1,r);
}
void chmul(int x,int y)
{
int tx=top[x],ty=top[y];
while(tx!=ty)
{
if(dep[tx]>dep[ty])swap(tx,ty),swap(x,y);
change(1,ys[ty],ys[y]);
y=fa[ty];ty=top[y];
}
if(x==y){change(1,ys[x],ys[x]);return ;}
else
{
if(dep[x]>dep[y])swap(x,y);
change(1,ys[x],ys[y]);return ;
}
}
int sol(int x,int y)
{
int tx=top[x],ty=top[y],ans=0;
while(tx!=ty)
{
if(dep[tx]>dep[ty])swap(tx,ty),swap(x,y);
ans+=findsum(1,ys[ty],ys[y]);
y=fa[ty];ty=top[y];
}
if(x==y){return ans+findsum(1,ys[x],ys[x]);}
else
{
if(dep[x]>dep[y])swap(x,y);
return ans+findsum(1,ys[x],ys[y]);
}
}
struct ask
{
int l,r,z,op;
int prel,prer;
}A[51000];
bool cmp(ask n1,ask n2)
{
if(n1.z!=n2.z)return n1.z<n2.z;
if(n1.r!=n2.r)return n1.r>n2.r;
return n1.l<n2.l;
}
int s[51000],answer[51000];
int prel[51000],prer[51000];
int main()
{
scanf("%d%d",&n,&m);
for(int i=2;i<=n;i++)
{
int x;
scanf("%d",&x);x++;
ins(x,i);
}
fa[1]=0;dep[1]=1;pre_tree_node(1);
z=0;pre_tree_edge(1,1);
trlen=0;bt(1,z);
memset(prel,-1,sizeof(prel));memset(prer,-1,sizeof(prer));
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&A[i].l,&A[i].r,&A[i].z);A[i].l++,A[i].r++,A[i].z++,A[i].op=i;
A[i].prel=prel[A[i].l];prel[A[i].l]=i;
A[i].prer=prer[A[i].r];prer[A[i].r]=i;
}
for(int i=1;i<=n;i++)
{
chmul(i,1);
if(prel[i+1]!=-1)
{
int p=prel[i+1];
while(p!=-1)
{
answer[A[p].op]-=sol(A[p].z,1);
p=A[p].prel;
}
}
if(prer[i]!=-1)
{
int p=prer[i];
while(p!=-1)
{
answer[A[p].op]+=sol(A[p].z,1);
p=A[p].prer;
}
}
}
for(int i=1;i<=m;i++)printf("%d\n",answer[i]%mod);
return 0;
}

本文介绍了一种针对有根树结构中特定节点深度及最近公共祖先(LCA)的高效查询算法。通过巧妙地利用离线处理技巧和数据结构优化,实现对一系列查询的有效响应。该方法将查询转化为树上路径的累加操作,通过预处理和区间更新来减少计算复杂度。

281

被折叠的 条评论
为什么被折叠?



