QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#345690#4914. Slight HopeANIG0 0ms0kbC++142.1kb2024-03-07 11:56:432024-03-07 11:56:43

Judging History

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

  • [2024-03-07 11:56:43]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-03-07 11:56:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2.5e5+5,mods=998244353,T=1,M=5005;
int n,m,w[N],fa[N],bh[N],st[N],ed[N],dep[N],siz[N],f[M][M],ans,zd[N],zx[N],dy[N],dfn[N],d[N],mk[N],lg2[N],idx;
vector<int>p[N];
vector<int>g[N];
void dfs(int x){
    for(int i=1;i<=19;i++)f[x][i]=f[f[x][i-1]][i-1];
    for(int i=0;i<p[x].size();i++){
        int c=p[x][i];
        f[c][0]=x;dep[c]=dep[x]+1;
        dfs(c);
        zd[x]=max(zd[x],zd[c]+1);
        if(zd[c]>=zd[zx[x]])zx[x]=c;
    }
}
void dfs(int x,int y){
    d[x]=y;mk[x]=1;dfn[x]=++idx;dy[idx]=x;
    if(zx[x])dfs(zx[x],y);
    for(int i=0;i<p[x].size();i++){
        int c=p[x][i];
        if(mk[c])continue;
        dfs(c,c);
    }
    if(x==y){
        g[x].push_back(x);
        for(int i=1,nw=x;i<=zd[x];i++)nw=fa[nw],g[x].push_back(nw);
    }
}
int up(int x,int k){
    if(!k)return x;
    x=f[x][lg2[k]];
    k-=1<<lg2[k];
    if(dep[x]-dep[d[x]]>=k)return dy[dfn[d[x]]+dep[x]-dep[d[x]]-k];
    return g[d[x]][k-(dep[x]-dep[d[x]])];
}
int gets(int x,int l){
    if(fa[x]<l)return x;
    int k=dep[x]-dep[l];
    if(up(x,k)>=l)return up(x,k);
    return up(x,k-1);
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++)lg2[i]=__lg(i);
    for(int i=1;i<=n;i++)cin>>w[i];
    for(int i=2;i<=n;i++){
        cin>>fa[i];
        p[fa[i]].push_back(i);
    }
    for(int i=1;i<=n;i++)bh[i]=(i-1)/T+1,st[i]=ed[i-1]+1,ed[i]=min(i*T,n);
    dfs(1);
    dfs(1,1);
    for(int i=1;i<=bh[n];i++){
        memset(siz,0,sizeof(siz));
        for(int j=i;j<=bh[n];j++){
            f[i][j]=f[i][j-1];
            for(int k=st[j];k<=ed[j];k++){
                int c=gets(k,st[i]);
                f[i][j]-=siz[c]*siz[c];
                siz[c]+=w[k];siz[c]%=mods;
                f[i][j]+=siz[c]*siz[c];
                f[i][j]%=mods;
            }
            //cout<<i<<" "<<j<<" "<<f[i][j]<<endl;
        }
    }
    while(m--){
        int l,r;
        cin>>l>>r;
        l^=ans,r^=ans;
        cout<<(ans=f[l][r])<<"\n";
    }
}

详细

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:


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:


result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%