QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#405998#4914. Slight Hope27455185850 18ms11256kbC++203.9kb2024-05-06 18:31:302024-05-06 18:31:32

Judging History

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

  • [2024-05-06 18:31:32]
  • 评测
  • 测评结果:0
  • 用时:18ms
  • 内存:11256kb
  • [2024-05-06 18:31:30]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
inline char gc()
{
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
template<typename T> inline void read(T &x)
{
    T u=1,t=0;char c=gc();
    while(c<'0'||c>'9') {if(c=='-') u=-1; c=gc();}
    while(c>='0'&&c<='9') t*=10,t+=c-'0',c=gc();
    x=u*t;return;
}
template<typename T,typename ...O> inline void read(T &x,O &...o) {read(x),read(o...);}
template<typename T> inline void print(T x)
{
    if(x==0) return putchar('0'),void();
    if(x<0) putchar('-'),x=-x;
    int c[129]={0},k=0;
    while(x) c[++k]=x%10,x/=10;
    for(int i=k;i;--i) putchar(c[i]+'0');
}
template<typename T,typename ...O> inline void print(T x,O ...o) {print(x),putchar(' '),print(o...);}
const int N=250001,M=600;
const ll P=998244353;
int n,m,a[N],b[N],c[N],*f[N/M+1],*g[N/M+1],h[N];
namespace tc
{
    int lg[N];
    vector<int> a[N],fa1[N],fa2[N];
    struct tree
    {
        int f,l,d,t,z,fa[21];
    }T[N];
    void dfs1(int x)
    {
        T[x].l=1;
        T[x].d=T[T[x].f].d+1;
        T[x].fa[0]=T[x].f;
        for(int i=1;i<=20;++i)
        {
            T[x].fa[i]=T[T[x].fa[i-1]].fa[i-1];
        }
        for(auto i:a[x])
        {
            if(i==T[x].f) continue;
            T[i].f=x;
            dfs1(i);
            if(T[i].l+1>T[x].l)
            {
                T[x].l=T[i].l+1;
                T[x].z=i;
            }
        }
    }
    void dfs2(int x,int k)
    {
        fa2[k].push_back(x);
        T[x].t=k;
        for(auto i:a[x])
        {
            if(i==T[x].f) continue;
            if(i==T[x].z) dfs2(i,k);
            else
            {
                fa1[i].push_back(i);
                for(int j=1;j<=T[i].l;++j)
                {
                    fa1[i].push_back(T[fa1[i].back()].f);
                }
                dfs2(i,i);
            }
        }
    }
    int get(int x,int k)
    {
        if(k==0) return x;
        int q=T[x].d-k;
        x=T[T[x].fa[lg[k]]].t;
        if(T[x].d<q) return fa2[x][q-T[x].d];
        else return fa1[x][T[x].d-q];
        return x;
    }
    void init()
    {
        for(int i=2;i<=n;++i)
        {
            a[b[i]].push_back(i);
        }
        dfs1(1);
        fa1[1].push_back(1);
        for(int i=1;i<=T[1].l;++i)
        {
            fa1[1].push_back(T[fa1[1].back()].f);
        }
        dfs2(1,1);
        for(int i=0;i<=20;++i)
        {
            for(int j=(1<<i);j<=min((1<<(i+1))-1,n);++j) lg[j]=i;
        }
    }
    int find(int x,int l,int r)
    {
        int z=get(x,T[x].d-(T[l].d+1));
        if(z<=c[l]) return z;
        else return b[z];
    }
}
int main()
{
    read(n,m);
    for(int i=1;i<=n;++i) read(a[i]);
    for(int i=2;i<=n;++i) read(b[i]);
    tc::init();
    for(int i=1;i<=n;++i)
    {
        c[i]=c[i-1];
        while(c[i]<=n&&b[c[i]+1]<i) ++c[i];
    }
    for(int i=0;i<=n/M;++i)
    {
        f[i]=new int[(i+1)*M+10];
        g[i]=new int[(i+1)*M+10];
        for(int j=1;j<=min((i+1)*M,n);++j) f[i][j]=a[j];
        for(int j=i*M;j>=1;--j) f[i][b[j]]=(f[i][b[j]]+f[i][j])%P;
        for(int j=1;j<=min((i+1)*M,n);++j) g[i][j]=(g[i][j-1]+(ll)f[i][j]*f[i][j])%P;
    }
    int las=0;
    for(int i=1;i<=m;++i)
    {
        int l,r;
        read(l,r);
        l^=las,r^=las;
        int p=r/M;
        ll s=(g[p][min(r,c[l])]-g[p][l-1])%P;
        for(int j=max(p*M,c[l])+1;j<=r;++j)
        {
            int x=tc::find(j,l,r);
            s+=((ll)f[p][x]*2+a[j])*a[j]%P;
            f[p][x]=(f[p][x]+a[j])%P;
            h[j]=x;
        }
        for(int j=max(p*M,c[l])+1;j<=r;++j)
        {
            f[p][h[j]]=(f[p][h[j]]-a[j])%P;
        }
        print(las=((s%P+P)%P));
        putchar('\n');
    }
    return 0;
}

詳細信息

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 20
Accepted
time: 17ms
memory: 9476kb

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:

148388816
822655068
341356275
153648250
610508782
658092334
115032444
61006948
232896592
132532556
281147472
170577711
453242756
564024529
950510296
417644928
35757971
824080049
490493323
96124139
158766038
597702738
69672015
841035419
405236159
258190228
66326403
227299828
359113584
34672879
117324...

result:

ok 5000 numbers

Test #2:

score: 20
Accepted
time: 18ms
memory: 11256kb

input:

5000 5000
208095035 189388209 910573484 151351163 857032742 409791136 427941504 561340305 440094307 848312088 743662365 160219901 584491656 208412392 473499824 79323110 469342548 790678258 903532373 940176368 916303640 12519872 294579622 875495570 887250524 595928577 642259826 222149097 873031301 42...

output:

597291565
466590571
543666430
955934901
739756468
993328702
393451173
956126185
739018480
946329402
990581650
589085210
656552878
941726143
200128337
610853683
191337824
450787673
494378602
960833257
772065473
317098668
597989053
965678661
839020241
790226238
400110943
786188793
733806002
190637300
...

result:

ok 5000 numbers

Test #3:

score: 0
Runtime Error

input:

5000 5000
758829686 846421586 795445842 923423765 650851801 380291052 781540894 445139283 780903169 45635176 303532742 70594608 850028582 748168745 597843692 246704898 350529309 7279358 973729172 269517593 22112036 125333845 309121459 968441730 686776540 975145839 926379079 346059534 292798462 19216...

output:

463197740
26979945
961115125
585910970
224451566
822036760
183497285
813892643
8997869
291414695
131978485
899584925
779621202
716522212
879375421
91045492
330290371
691691224
52633475
340848115
186272999
890054530
911975959
514382765
763838056
167285656
930279525
785049503
150045446
570741767
97509...

result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #15:

score: 0
Time 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:

645687892
804644292
204845058
833159303
438840602
426417420
912375389
325277630
897894204
651802587
232723981
612719545
449284803
97772899
900152796
609960724
641400352
204127911
176781081
752207129
720790544
624982826
923898464
696425075
641906962
568669044
49974863
45637134
16934743
814263646
3524...

result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%