用类似
L
c
a
Lca
Lca那道题的做法做
但这里是
d
e
p
k
dep^k
depk
把一个点系数设为
d
e
p
k
−
(
d
e
p
−
1
)
k
dep^k-(dep-1)^k
depk−(dep−1)k即可
#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
#define poly vector<int>
cs int mod=998244353;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline void Add(int &a,int b){(a+=b)>=mod?a-=mod:0;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline void Dec(int &a,int b){(a-=b)<0?a+=mod:0;}
inline int mul(int a,int b){return 1ll*a*b%mod;}
inline void Mul(int &a,int b){a=1ll*a*b%mod;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=50005;
vector<int> e[N];
int dfn,in[N],out[N],siz[N],son[N],fa[N],dep[N],top[N],idx[N];
int n,q,t,ans[N];
struct ask{
int y,id;
ask(int _y=0,int _d=0):y(_y),id(_d){}
};
vector<ask> p[N];
namespace Seg{
int tag[N<<2],s[N<<2],coef[N<<2];
#define lc (u<<1)
#define rc ((u<<1)|1)
#define mid ((l+r)>>1)
inline void build(int u,int l,int r){
if(l==r){coef[u]=dec(ksm(dep[idx[l]],t),ksm(dep[idx[l]]-1,t));return;}
build(lc,l,mid),build(rc,mid+1,r);
coef[u]=add(coef[lc],coef[rc]);
}
inline void pushup(int u){
s[u]=add(s[lc],s[rc]);
}
inline void pushnow(int u,int k){
Add(tag[u],k),Add(s[u],mul(coef[u],k));
}
inline void pushdown(int u){
if(!tag[u])return ;
pushnow(lc,tag[u]);
pushnow(rc,tag[u]);
tag[u]=0;
}
void update(int u,int l,int r,int st,int des){
if(st<=l&&r<=des)return pushnow(u,1);
pushdown(u);
if(st<=mid)update(lc,l,mid,st,des);
if(mid<des)update(rc,mid+1,r,st,des);
pushup(u);
}
int query(int u,int l,int r,int st,int des){
if(st<=l&&r<=des)return s[u];
int res=0;pushdown(u);
if(st<=mid)Add(res,query(lc,l,mid,st,des));
if(mid<des)Add(res,query(rc,mid+1,r,st,des));
pushup(u);return res;
}
}
namespace SLPF{
void update(int u){
while(top[u]){
Seg::update(1,1,n,in[top[u]],in[u]);
u=fa[top[u]];
}
}
int query(int u){
int res=0;
while(top[u]){
Add(res,Seg::query(1,1,n,in[top[u]],in[u]));
u=fa[top[u]];
}
return res;
}
}
void dfs1(int u){
siz[u]=1;
for(int &v:e[u]){
dep[v]=dep[u]+1;
dfs1(v),siz[u]+=siz[v];
if(siz[v]>siz[son[u]])son[u]=v;
}
}
void dfs2(int u,int tp){
top[u]=tp,in[u]=++dfn,idx[dfn]=u;
if(son[u])dfs2(son[u],tp);
for(int &v:e[u]){
if(v==son[u])continue;
dfs2(v,v);
}
out[u]=dfn;
}
int main(){
n=read(),q=read(),t=read();
for(int i=2;i<=n;i++)fa[i]=read(),e[fa[i]].pb(i);
dep[1]=1,dfs1(1),dfs2(1,1);
Seg::build(1,1,n);
for(int i=1;i<=q;i++){
int x=read(),y=read();
p[x].pb(ask(y,i));
}
for(int i=1;i<=n;i++){
SLPF::update(i);
for(ask &x:p[i])
ans[x.id]=SLPF::query(x.y);
}
for(int i=1;i<=q;i++)cout<<ans[i]<<'\n';
}