QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#351022#4914. Slight Hopezzafanti0 0ms0kbC++234.3kb2024-03-11 12:17:382024-03-11 12:17:40

Judging History

你现在查看的是最新测评结果

  • [2024-03-11 12:17:40]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-03-11 12:17:38]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
using E=long long;
constexpr E mod=998244353;
constexpr int N=250010;

int n,Q,bksz,Len,timestamp;
vector<vector<int>> ver;
vector<int> 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;
}

const int K=19;
array<array<int,K>,N> par;
vector<vector<int>> up,down;
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 LL){
  assert(dep[u]>=dep[LL]);
  if(dep[u]==dep[LL]) return u;
  int t=KA(u,dep[u]-dep[LL]-1);
  if(fa[t]<LL) return t;
  return fa[t];
}

vector<E> a;
vector<vector<int>> f;
E mem[250010*750];
vector<E*> S,S2;
E* pt;

void prework(){
  bksz=800;
  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);
  }
  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];
    }
  }

  S=S2=vector<E*>(n+1);
  pt=mem;
  for(int i=1; i<=n; i++){
    int LEN=Len-belong[i]+1;
    S[i]=pt;
    pt+=LEN;
    S2[i]=pt;
    pt+=LEN;
  }

  for(int i=1; i<=Len; i++){
    for(int u=R[i]; u; u--){
      S[u][Len-i]+=a[u];
      S[u][Len-i]%=mod; S2[u][Len-i]%=mod;
      if(fa[u]==0) continue;
      if(belong[fa[u]]>i) continue;
      S[fa[u]][Len-i]+=S[u][Len-i];
      S2[fa[u]][Len-i]+=S[u][Len-i]*S[u][Len-i]%mod;
    }
  }

  f=vector<vector<int>>(Len+1,vector<int>(Len+1));

  vector<E> sum(n+1);
  for(int l=1; l<=Len; l++){
    fill(sum.begin(),sum.end(),0);
    for(int r=l; r<=Len; r++){
      long long T=f[l][r-1];
      for(int i=L[r]; i<=R[r]; i++){
        int t=find(i,l);
        T=(T+a[i]*1ll*a[i])%mod;
        T=(T+2*a[i]*sum[t])%mod;
        sum[t]=(sum[t]+a[i])%mod;
      }
      f[l][r]=T;
    }
  }
}

vector<E> sum,vis;
E query0(int l,int r){
  E ret=0;
  vector<int> stk;
  for(int i=l; i<=r; i++){
    int t=find(i,l);
    ret=(ret+a[i]*1ll*a[i])%mod;
    ret=(ret+2*a[i]*sum[t])%mod;
    sum[t]=(sum[t]+a[i])%mod;
    stk.emplace_back(t);
  }
  for(auto p:stk) vis[p]=sum[p]=0;
  return ret;
}

E query(int l,int r){
  if(belong[l]+1>=belong[r]) return query0(l,r);
  int p=belong[l],q=belong[r];
  E ret=f[p+1][q-1];
  for(int i=l; i<=R[p]; i++){
    //cerr<<i<<' '<<S[q-1][i]<<' '<<S2[q-1][i]<<endl;
    ret=(ret+S[i][Len-q+1]*1ll*S[i][Len-q+1]%mod-S2[i][Len-q+1]+mod)%mod;
  }
  //cerr<<ret<<endl;
  vector<int> stk;
  for(int i=L[q]; i<=r; i++){
    int t=find(i,l);
    //cerr<<i<<' '<<t<<endl;
    if(vis[t]==0&&t<L[q]){
      sum[t]=S[q-1][t];
    }
    vis[t]=1;
    stk.emplace_back(t);
    ret=(ret+a[i]*1ll*a[i])%mod;
    ret=(ret+a[i]*2ll*sum[t])%mod;
    sum[t]=(sum[t]+a[i])%mod;
  }
  while(stk.size()) sum[stk.back()]=vis[stk.back()]=0,stk.pop_back();
  return (ret%mod+mod)%mod;
}

signed main(){

#ifdef zzafanti
  freopen("in.in","r",stdin);
  //freopen("out.out","w",stdout);
#endif // zzafanti

  cin.tie(nullptr),cout.tie(nullptr)->sync_with_stdio(false);

  cin>>n>>Q;
  ver.resize(n+1);
  belong=fa=vector<int>(n+1);
  a=vector<E>(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();
  sum.resize(n+1); vis.resize(n+1);
  E ans=0;
  while(Q--){
    //if(Q%10000==0) cerr<<Q<<endl;
    E l,r;
    cin>>l>>r;
    l^=ans,r^=ans;
    ans=query(l,r);
    cout<<ans<<'\n'<<flush;
  }

  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:

685557687

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:

870790689

result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%