The meaning of problems
Given n nodes rooted tree, q interrogation times, each query request $ \ sum _ {l \ leq i \ leq r} dep [LCA (i, z)] $
Thinking
According wys said TitansIf this is not the constraint dep removed, it will not be easy to maintain a data structure, as for the different I, \ (dep [the LCA] \) may be different
1. In order to remove the DEP, we adopt a clever but useless for a top \ (I \) , the \ (the root \) to \ (I \) all points on this path weight plus one, then the \ (dep [LCA] \) can query the \ (the root \) to \ (Z \) points on the right and give this path (WTF???)
So the question becomes chain modified with chain query , you can ask us outCode is well written briefTree chain split classmates
However, we can not have violence modifications to each challenge \ (root \) to \ ([l, r] \ ) that some of the chain, so we also need some small optimization
2. offline processing , ask each split into \ ([1, l-1 ] \) and \ ([1, r] \ ) in duplicate, then the query can be \ (ans [1, r] -ans [1, l-1] \ ) to obtain, since the chain modification operation to the z-independent (due to increased \ (the root \) to \ (I \) so that only the \ (I \) related), can be the right to all inquiries sorting point, and then again for the chain in order to modify
Split Tree chain template titleIt is said to be online but I do not ah too konjac up
As there are many repetitive operations (+ codes over B) removal of unnecessary portions so that it ~~~
Code:
struct Q
{
int r,z,opt;
ll ans;
bool operator < (const Q a)const
{
return r<a.r;
}
}q[N<<1];int qsum=-1;
bool cmp(Q a,Q b) {return a.opt<b.opt;}
int main()
{
read(n);read(m);
for(int i=1;i<n;++i)
{
int father; read(father);
add_edge(father+1,i+1);
}
for(int i=1;i<=m;++i)
{
int l,r,z;
read(l);read(r);read(z);
++l;++r;++z;
q[++qsum].r=l-1; q[qsum].z=z; q[qsum].opt=qsum;
q[++qsum].r=r; q[qsum].z=z; q[qsum].opt=qsum;
}
sort(q,q+qsum+1);
seg[1]=rev[1]=top[1]=hfu=1;
dfs1(1);
dfs2(1);
int now=0;
for(int i=0;i<=qsum;++i)
{
while(now<q[i].r) modify_edge(1,++now,1);
q[i].ans=query_edge(1,q[i].z);
}
sort(q,q+qsum+1,cmp);
for(int i=1;i<=qsum;i+=2) printf("%lld\n",((q[i].ans-q[i-1].ans)%mod+mod)%mod);
return 0;
}