#include<bits/stdc++.h>
#define int long long
#define mid ((l+r)>>1)
#define ls (p<<1)
#define rs (ls|1)
using namespace std;
const int N=1e6+10,M=1e6+10,V=1e6,INF=1e9,mod=998244353;
int n,q,cnt,a[N],d[N],w[N],t[N<<2],lz[N<<2];vector<int>e[N];
namespace Fast_IO
{
static char buf[1000000],*paa=buf,*pd=buf,out[10000000];int length=0;
#define getchar() paa==pd&&(pd=(paa=buf)+fread(buf,1,1000000,stdin),paa==pd)?EOF:*paa++
inline int read()
{
int x(0),t(1);char fc(getchar());
while(!isdigit(fc)){if(fc=='-') t=-1;fc=getchar();}
while(isdigit(fc)) x=(x<<1)+(x<<3)+(fc^48),fc=getchar();
return x*t;
}
inline void flush(){fwrite(out,1,length,stdout);length=0;}
inline void put(char c){if(length==9999999) flush();out[length++]=c;}
inline void put(string s){for(char c:s) put(c);}
inline void print(int x)
{
if(x<0) put('-'),x=-x;
if(x>9) print(x/10);
put(x%10+'0');
}
inline bool chk(char c) { return !(c=='?'||c=='+'||c>='a'&&c<='z'||c>='A'&&c<='Z'||c>='0'&&c<='9'); }
inline bool ck(char c) { return c!='\n'&&c!='\r'&&c!=-1&&c!=' '; }
inline void rd(char s[],int&n)
{
s[++n]=getchar();
while(chk(s[n])) s[n]=getchar();
while(ck(s[n])) s[++n]=getchar();
n--;
}
}
using namespace Fast_IO;
namespace tree_decomposition
{
int fa[N],son[N],si[N],dep[N];
int top[N],id[N],dfn[N];
inline void clear(int n)
{
memset(son,0,sizeof son);
cnt=0;for(int i=1;i<=n;i++) e[i].clear();
}
inline void dfs1(int u,int ff)
{
fa[u]=ff,si[u]=1,dep[u]=dep[ff]+1;d[u]=(d[u]+a[u])%mod;
for(auto v:e[u])
{
if(v==ff) continue;
d[v]=d[u];dfs1(v,u);si[u]+=si[v];
if(si[son[u]]<si[v]) son[u]=v;
}
}
inline void dfs2(int u,int topf)
{
top[u]=topf;
dfn[u]=++cnt,id[cnt]=u;
if(son[u]) dfs2(son[u],topf);
for(auto v:e[u])
{
if(v==fa[u]||v==son[u]) continue;
dfs2(v,v);
}
}
inline int LCA(int x,int y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
}
using namespace tree_decomposition;
inline int query(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
return (d[x]-d[fa[y]]+mod)%mod;
}
inline void build(int p,int l,int r)
{
if(l==r) return t[p]=dep[id[l]]-1+(l==1),void();
build(ls,l,mid);build(rs,mid+1,r);t[p]=min(t[ls],t[rs]);
}
inline void pushson(int p,int w){t[p]+=w;lz[p]+=w;}
inline void pushdown(int p){pushson(ls,lz[p]);pushson(rs,lz[p]);lz[p]=0;}
inline int query(int p,int l,int r,int s,int e,int w)
{
// cout<<p<<" "<<l<<" "<<r<<" "<<s<<" "<<e<<" "<<w<<" "<<t[p]<<endl;
if(l>e||r<s||t[p]>=w) return 0;if(l==r) return id[l];pushdown(p);
int res=query(ls,l,mid,s,e,w);if(res) return res;
return query(rs,mid+1,r,s,e,w);
}
inline void modify(int p,int l,int r,int s,int e)
{
if(l>=s&&r<=e) return pushson(p,1),void();pushdown(p);
if(mid>=s) modify(ls,l,mid,s,e);
if(mid<e) modify(rs,mid+1,r,s,e);t[p]=min(t[ls],t[rs]);
}
inline void modify(int x,int y)
{
while(top[x]^top[y]) modify(1,1,n,dfn[top[y]],dfn[y]),y=fa[top[y]];
modify(1,1,n,dfn[x],dfn[y]);
}
inline void solve()
{
n=read()+1;a[1]=read();
for(int i=2;i<=n;i++) e[read()].emplace_back(i),a[i]=read(),w[i]=-1;
dfs1(1,0),dfs2(1,1);build(1,1,n);
for(int i=2,ans=0;i<=n;i++)
{
int x=i,lst=x,y=0;ans=(ans-a[i]+mod)%mod;
while(w[top[x]]+dep[top[x]]<dep[i]&&x) w[top[x]]=dep[i]-dep[top[x]],lst=top[x],x=fa[top[x]];
if(!x) y=1;else y=query(1,1,n,dfn[top[x]],dfn[x],dep[i]);if(!y) y=lst;
ans=(ans+query(y,i))%mod;print(ans);put('\n');modify(y,i);
}
}
signed main()
{
int T=1;
// freopen("rand.in","r",stdin);
// freopen("1.out","w",stdout);
// T=read();
while(T--) solve();
genshin:;flush();return 0;
}剪贴板
剪贴板 1gj819tb
剪贴板信息
剪贴板信息
- 收录
- 更新