如果出现某些询问一堆东西的和的时候(如LCA的深度和),我们可以考虑不要把这些东西每一个都全部求出来,而应该考虑合并这些询问然后一次性询问他们的和。(例如用树剖转化为区间加和和区间询问)
具体思路见hzw博客
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
inline int r()
{
int s=0,k=1;char c=getchar();
while(!isdigit(c))
{
if(c=='-')k=-1;
c=getchar();
}
while(isdigit(c))
{
s=s*10+c-'0';
c=getchar();
}
return s*k;
}
int deep[1000001],top[1000001],fa[1000001],size[1000001],head[1000001],son[1000001],cnt,w[1000001],tr[1000001],la[1000001],id[1000001],ccnt;
map<pair<int,int>,int>mp;
struct ask
{
int l,r,z;
}t[1000001];
struct asks
{
int bh,x;
}f[1000001];
int n,m,times,root;
const int mod=201314;
struct node
{
int to,next;
}a[1000001];
void add_edge(int from,int to)
{
a[++cnt].to=to;
a[cnt].next=head[from];
head[from]=cnt;
}
void dfs1(int u,int father,int dp)
{
fa[u]=father;
deep[u]=dp;
size[u]=1;
int maxi=0;
for(int i=head[u];i;i=a[i].next)
{
int v=a[i].to;
if(v==father)continue;
dfs1(v,u,dp+1);
if(size[v]>maxi)
{
maxi=size[v];
son[u]=v;
}
size[u]+=size[v];
}
}
void dfs2(int u,int tp)
{
id[u]=++times;
top[u]=tp;
if(son[u])dfs2(son[u],tp);
for(int i=head[u];i;i=a[i].next)
{
int v=a[i].to;
if(!top[v])dfs2(v,v);
}
}
void push_down(int l,int r,int k,int bh)
{
tr[bh]+=k*(r-l+1);
tr[bh]%=mod;
la[bh]+=k;
la[bh]%=mod;
}
void add(int now_l,int now_r,int l,int r,int k,int bh)
{
if(l<=now_l&&now_r<=r)
{
tr[bh]+=k*(now_r-now_l+1);
tr[bh]%=mod;
la[bh]+=k;
la[bh]%=mod;
return;
}
if(now_l>r||now_r<l)return;
int mid=(now_l+now_r)/2;
if(la[bh])
{
push_down(now_l,mid,la[bh],bh*2);
push_down(mid+1,now_r,la[bh],bh*2+1);
la[bh]=0;
}
add(now_l,mid,l,r,k,bh*2);
add(mid+1,now_r,l,r,k,bh*2+1);
tr[bh]=tr[bh*2]+tr[bh*2+1];
}
int sum(int now_l,int now_r,int l,int r,int bh)
{
if(l<=now_l&&now_r<=r)return tr[bh];
if(now_l>r||now_r<l)return 0;
int mid=(now_l+now_r)/2;
if(la[bh])
{
push_down(now_l,mid,la[bh],bh*2);
push_down(mid+1,now_r,la[bh],bh*2+1);
la[bh]=0;
}
return (sum(now_l,mid,l,r,bh*2)+sum(mid+1,now_r,l,r,bh*2+1))%mod;
}
void tr_add(int x,int y,int k)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]])swap(x,y);
add(1,n,id[top[x]],id[x],k,1);
x=fa[top[x]];
}
if(deep[x]<deep[y])swap(x,y);
add(1,n,id[y],id[x],k,1);
}
int tr_sum(int x,int y)
{
int ans=0;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]])swap(x,y);
ans+=sum(1,n,id[top[x]],id[x],1);
ans%=mod;
x=fa[top[x]];
}
if(deep[x]<deep[y])swap(x,y);
ans+=sum(1,n,id[y],id[x],1);
ans%=mod;
return ans;
}
bool cmp(asks x,asks y)
{
return x.bh<y.bh;
}
int main()
{
n=r();m=r();
int x,y,z;
for(int i=2;i<=n;i++)
{
x=r()+1;
add_edge(i,x);
add_edge(x,i);
}
dfs1(1,0,1);
dfs2(1,1);
for(int i=1;i<=m;i++)
{
t[i].l=r()+1;t[i].r=r()+1;t[i].z=r()+1;
f[++ccnt].bh=t[i].l-1;
f[ccnt].x=t[i].z;
f[++ccnt].bh=t[i].r;
f[ccnt].x=t[i].z;
}
sort(f+1,f+ccnt+1,cmp);
int lst=1;
for(int i=1;i<=ccnt;i++)
{
int x=f[i].bh,y=f[i].x;
for(int j=lst;j<=x;j++)
{
tr_add(1,j,1);
// cout<<"add"<<1<<" "<<j<<endl;
}
int sm=tr_sum(1,y);
// cout<<"ask"<<1<<" "<<y<<" "<<sm<<endl;
mp[make_pair(x,y)]=sm;
lst=x+1;
}
for(int i=1;i<=m;i++)
{
int ans=mp[make_pair(t[i].r,t[i].z)]-mp[make_pair(t[i].l-1,t[i].z)];
cout<<(ans%mod+mod)%mod<<endl;
}
}