QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#402477 | #4914. Slight Hope | marher | 0 | 0ms | 0kb | C++14 | 2.5kb | 2024-04-30 17:08:51 | 2024-04-30 17:08:52 |
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2.5e5+500,mod=998244353;
void ad(int&x,int y)
{
x+=y;
if(x>=mod)x-=mod;
}
int n,q,S=505,a[N],fa[N],b[N],ro[N],f[N][20],ex[N],dep[N],son[N],mx[N],hb[N],las,la[N],T,val[N],vis[N];
vector<int>up[N],down[N];
struct block
{
int*w,*fir;
}B[N/500+10];
int find(int x,int k)
{
if(k<=0)return x;
int p=f[x][hb[k]],t=ro[p],d=dep[p]-dep[t];
k-=1<<hb[k];
if(d>=k)return down[t][d-k];
return up[t][k-d];
}
void insert(int x,int&ans,int d,int pre)
{
if(vis[x]!=T)vis[x]=T,val[x]=pre;
ans=(ans-1ll*val[x]*val[x]%mod)%mod;
ad(val[x],d);
ans=(ans+1ll*val[x]*val[x]%mod)%mod;
}
main()
{
// freopen("in.txt","r",stdin);
cin>>n>>q;
for(int i=1;i<=n;i++)cin>>a[i];dep[1]=1;
for(int i=2;i<=n;i++)cin>>fa[i],dep[i]=dep[fa[i]]+1,f[i][0]=fa[i];
for(int i=1,pos=1;i<=n;i++)
{
while(pos<=n&&fa[pos]<i)++pos;
la[i]=pos-1;
}
for(int i=2;i<=n;i++)for(int j=1;(1<<j)<dep[i];j++)f[i][j]=f[f[i][j-1]][j-1];
for(int i=0;(1<<i)<dep[n];i++)for(int j=(1<<i);j<(1<<i+1)&&j<dep[n];j++)hb[j]=i;
for(int i=n;i>=1;i--)
{
mx[i]=max(mx[i],dep[i]);
if(mx[son[fa[i]]]<mx[i])son[fa[i]]=i;
}
son[0]=0;
for(int i=1;i<=n;i++)ro[i]=son[fa[i]]==i?ro[fa[i]]:i;
for(int i=1;i<=n;i++)down[ro[i]].push_back(i);
for(int i=1;i<=n;i++)if(i==ro[i])
{
int x=i,re=mx[i]-dep[i]+1;
while(re--)up[i].push_back(x),x=fa[x];
}
for(int i=1;i<=n;i++)b[i]=(i-1)/S+1;
for(int i=1;i<=b[n];i++)
{
int l=(i-1)*S+1,r=min(n,i*S);
B[i].w=new int[r+10];B[i].fir=new int[r+10];
for(int j=1;j<l;j++)B[i].w[j]=B[i-1].w[j],ex[j]=0;
for(int j=l;j<=r;j++)ex[j]=a[j];
for(int j=r;j>=1;j--)ad(ex[fa[j]],ex[j]),ad(B[i].w[j],ex[j]);
for(int j=1;j<=r;j++)B[i].fir[j]=(B[i].fir[j-1]+1ll*B[i].w[j]*B[i].w[j]%mod)%mod;
}
while(q--)
{
int l,r;cin>>l>>r;l^=las;r^=las;T++;
int rr=min(r,la[l]),k=b[r]-1,w=0;
if(l<=(k-1)*S+1)w=(B[k].fir[min(rr,k*S)]-B[k].fir[l-1]+mod)%mod;
// cout<<l<<' '<<rr<<' '<<la[1]<<'\n';
for(int i=max(l,k*S+1);i<=r;i++)
{
int d=find(i,dep[i]-dep[rr]);
// cout<<"ece "<<i<<' '<<d<<' '<<dep[i]-dep[rr]<<'\n';
if(d>rr)d=fa[d];
insert(d,w,a[i],d<=k*S?B[k].w[d]:0);
}
cout<<(las=w)<<'\n';
// return 0;
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
input:
5000 5000 63141398 126604376 697512251 741353996 356604726 507953968 494219271 973110260 460861914 988416009 970876787 448429588 725493166 883501210 51076237 784293761 8003146 766721589 406465089 330785670 782582116 501008987 936941546 564663613 40330818 320342607 566652836 806983548 79234347 581976...
output:
-159962359
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Memory Limit Exceeded
Test #15:
score: 0
Memory Limit Exceeded
input:
250000 250000 768540930 17767906 372927484 987601476 466807233 279110163 484031897 581293130 869165405 440806324 190995959 228277657 510008046 885148108 825022142 732048181 608976903 327270073 923084855 752263987 475969665 911033413 561860569 377841111 401028041 117941740 350378391 295513473 2304741...
output:
-352556461
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%