神题!
虽然想到离线,但是还是只会随机数据的做法。。。。
有一个比较有意思的结论:把点i到根的所有点权值设为1,其他点为0,此时j到根的所有点权和即为dep[LCA(i,j)]
不难发现,这个方法满足加法性质。
把询问查分,从1到n处理每个点,将其到根的权值+1,询问只要看这个点到根的权值和就是LCA的深度和了!
LCT维护即可
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int Maxn = 50005;
const int Mod = 201314;
int sum[Maxn], size[Maxn];
int fa[Maxn], rev[Maxn];
int w[Maxn], tip[Maxn];
int son[Maxn][2], ans[Maxn];
int n,m,x,l,r,i,j;
struct arr
{
int num,x,ans;
bool operator <(const arr &a)const
{
return num<a.num;
}
} q[Maxn*2];
#define isroot(x) (son[fa[(x)]][0]!=(x)&&son[fa[(x)]][1]!=(x))
void update(int x){
(sum[x] = sum[son[x][0]] + sum[son[x][1]] + w[x]) %= Mod;
size[x] = size[son[x][0]] + size[son[x][1]] + 1;
}
void push1(int x){
swap(son[x][0], son[x][1]);
rev[son[x][0]] ^= 1;
rev[son[x][1]] ^= 1;
rev[x] = 0;
}
void push2(int x){
if (son[x][0]){
(w[son[x][0]] += tip[x]) %= Mod;
(sum[son[x][0]] += (LL)tip[x]*size[son[x][0]]%Mod) %= Mod;
(tip[son[x][0]] += tip[x]) %= Mod;
}
if (son[x][1]){
(w[son[x][1]] += tip[x]) %= Mod;
(sum[son[x][1]] += (LL)tip[x]*size[son[x][1]]%Mod) %= Mod;
(tip[son[x][1]] += tip[x]) %= Mod;
}
tip[x] = 0;
}
void rotate(int x,int ft,int K){
if (!isroot(ft)){
if (son[fa[ft]][0]==ft)
son[fa[ft]][0]=x;
else son[fa[ft]][1]=x;
} fa[x] = fa[ft];
if (son[x][K^1]) fa[son[x][K^1]]=ft;
son[ft][K] = son[x][K^1];
fa[ft]=x; son[x][K^1]=ft;
update(ft);
}
void Splay(int x){
int ft, gf;
while (!isroot(x)){
ft = fa[x]; gf = fa[ft];
if (rev[gf]) push1(gf); if (tip[gf]) push2(gf);
if (rev[ft]) push1(ft); if (tip[ft]) push2(ft);
if (rev[x]) push1(x); if (tip[x]) push2(x);
if (isroot(ft)){
if (son[ft][0]==x) rotate(x,ft,0);
if (son[ft][1]==x) rotate(x,ft,1);
} else
{
if (son[ft][0]==x && son[gf][0]==ft) rotate(ft,gf,0), rotate(x,ft,0);
if (son[ft][1]==x && son[gf][1]==ft) rotate(ft,gf,1), rotate(x,ft,1);
if (son[ft][0]==x && son[gf][1]==ft) rotate(x,ft,0), rotate(x,gf,1);
if (son[ft][1]==x && son[gf][0]==ft) rotate(x,ft,1), rotate(x,gf,0);
}
}
if (rev[x]) push1(x);
if (tip[x]) push2(x);
update(x);
}
void access(int x){
int t = 0;
while (x){
Splay(x);
son[x][1] = t;
update(x);
t = x; x = fa[x];
}
}
void makeroot(int x)
{
access(x); Splay(x); rev[x]^=1;
}
void Link(int x,int y)
{
makeroot(x); access(y); fa[x]=y;
}
void Cut(int x,int y){
makeroot(x); access(y); Splay(y);
son[y][0] = 0; fa[x] = 0;
Splay(x); Splay(y);
}
int main(){
//freopen("3626.in","r",stdin);
//freopen("3626.out","w",stdout);
scanf("%d%d",&n,&m);
for (i=2;i<=n;i++){
scanf("%d",&x); x++;
Link(x,i);
}
for (i=1;i<=m;i++){
scanf("%d%d%d",&l,&r,&x); l++; r++; x++;
q[i].num = l-1; q[i].x = -x; q[i].ans = i;
q[i+m].num = r; q[i+m].x = x; q[i+m].ans = i;
}
sort(q+1,q+m+m+1);
for (j=1;j<=m+m&&q[j].num==0;j++);
for (i=1;i<=n;i++){
makeroot(1); access(i);
Splay(i); tip[i]++;
sum[i] += size[i]; w[i]++;
while (j<=m+m && q[j].num==i){
makeroot(1);
access( abs(q[j].x) );
Splay( abs(q[j].x) );
if (q[j].x>0) ans[q[j].ans] += sum[q[j].x];
else ans[q[j].ans] -= sum[-q[j].x];
j++;
}
}
for (i=1;i<=m;i++)
printf("%d\n",(ans[i]+Mod)%Mod);
return 0;
}