QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#350919 | #4914. Slight Hope | zzafanti | 0 | 0ms | 0kb | C++20 | 2.5kb | 2024-03-11 09:52:59 | 2024-03-11 09:53:01 |
answer
#include<bits/stdc++.h>
using namespace std;
using E=long long;
constexpr E mod=998244353;
int n,Q,bksz,Len,timestamp;
vector<vector<int>> ver;
vector<int> a,fa,belong,L,R;
vector<int> hson,dfn,sz,len,dep,top;
void dfs0(int u){
sz[u]=1;
for(auto p:ver[u]){
dep[p]=dep[u]+1;
dfs0(p);
sz[u]+=sz[p];
if(len[p]>len[hson[u]]) hson[u]=p;
}
len[u]=len[hson[u]]+1;
}
vector<vector<int>> up,down,par;
void dfs1(int u){
dfn[u]=++timestamp;
if(sz[u]==1){
return ;
}
down[top[u]].emplace_back(hson[u]),up[top[u]].emplace_back(fa[up[top[u]].back()]);
top[hson[u]]=top[u];
dfs1(hson[u]);
for(auto p:ver[u]){
if(p==hson[u]) continue;
top[p]=p;
down[p].emplace_back(p);
up[p].emplace_back(p);
dfs1(p);
}
}
int KA(int u,int k){
if(!k) return u;
int p=__lg(k);
u=par[u][p];
k^=(1<<p);
int dlt=dep[u]-dep[top[u]];
if(k<=dlt) return down[top[u]][dlt-k];
return up[top[u]][k-dlt];
}
int find(int u,int L){
assert(dep[u]>=dep[L]);
if(dep[u]==dep[L]) return u;
int t=KA(u,dep[u]-dep[L]-1);
if(fa[t]<L) return t;
return fa[t];
}
void prework(){
bksz=sqrt(n);
for(int i=1; i<=n; i++){
belong[i]=(i-1)/bksz+1;
}
Len=belong[n];
L=R=vector<int>(Len+1);
for(int i=1; i<=Len; i++){
L[i]=R[i-1]+1;
R[i]=min(n,bksz*i);
}
int K=__lg(n)+1; par=vector<vector<int>>(n+1,vector<int>(K));
up.resize(n+1),down.resize(n+1);
hson=dfn=sz=len=top=dep=vector<int>(n+1);
top[1]=dep[1]=1;
up[1]=down[1]=vector<int>(1,1);
dfs0(1);
dfs1(1);
for(int i=1; i<=n; i++) par[i][0]=fa[i];
for(int j=1; j<K; j++){
for(int i=1; i<=n; i++){
par[i][j]=par[par[i][j-1]][j-1];
}
}
}
int main(){
#ifdef zzafanti
freopen("in.in","r",stdin);
#endif // zzafanti
cin.tie(nullptr),cout.tie(nullptr)->sync_with_stdio(false);
cin>>n>>Q;
ver.resize(n+1);
belong=a=fa=vector<int>(n+1);
for(int i=1; i<=n; i++) cin>>a[i];
for(int i=2; i<=n; i++){
cin>>fa[i];
ver[fa[i]].emplace_back(i);
}
prework();
vector<E> sum(n+1);
E ans=0;
while(Q--){
E l,r;
cin>>l>>r;
l^=ans,r^=ans;
ans=0;
vector<int> St;
for(int i=l; i<=r; i++){
int t=find(i,l);
St.emplace_back(t);
ans=(ans+a[i]*a[i])%mod;
ans=(ans+2*a[i]*sum[t])%mod;
sum[t]=(sum[t]+a[i])%mod;
}
for(auto p:St){
sum[p]=0;
}
cout<<ans<<endl;
}
return 0;
}
詳細信息
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:
546448565 0 0
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Runtime Error
Test #15:
score: 0
Runtime Error
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:
45556046 0
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
0%