QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#351075 | #4914. Slight Hope | zzafanti | 40 | 446ms | 114828kb | C++23 | 4.2kb | 2024-03-11 14:24:43 | 2024-03-11 14:24:43 |
Judging History
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=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);
}
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++){
for(int r=l; r<=Len; r++){
E T=f[l][r-1];
for(int i=L[r]; i<=R[r]; i++){
int t=find(i,L[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;
}
fill(sum.begin(),sum.end(),0);
}
}
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++){
ret=(ret+S[i][Len-q+1]*1ll*S[i][Len-q+1]%mod-S2[i][Len-q+1]+mod)%mod;
}
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[t][Len-q+1];
}
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;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 4ms
memory: 12268kb
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: 0
Accepted
time: 8ms
memory: 9680kb
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
Accepted
time: 8ms
memory: 9832kb
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:
ok 5000 numbers
Test #4:
score: 0
Accepted
time: 12ms
memory: 9996kb
input:
5000 5000 831640431 885460668 883290311 161278701 616481017 280845019 388780100 404687561 721779426 228266599 363600658 948454460 388371957 246028605 655768029 731008092 605246260 564445876 760408308 26166250 71491179 320543409 377278440 522729682 703367018 322884625 692206745 411811213 557175990 77...
output:
973564643 126270676 513879186 160315552 474692304 502474019 656022067 882003880 105940288 652267128 369294132 107152190 559575798 752075334 355587403 735085822 578199820 483981812 74890692 358519217 379879058 32070627 446003765 10882971 382837033 282137994 909326140 46106521 358500230 735281951 3598...
result:
ok 5000 numbers
Test #5:
score: 0
Accepted
time: 14ms
memory: 10184kb
input:
5000 5000 386586821 473104785 923115322 80627280 723972117 76500891 901470530 40141226 605413269 508843738 568157568 511437103 387870706 707269439 370782055 605387529 647770332 427178306 213279296 635360385 647527011 43127485 265465400 93955214 147422318 973617973 166812244 700732812 611016742 80446...
output:
789940086 697637206 408002193 990320561 762711381 901590837 762961896 271872873 291306692 551854139 437673271 270540330 691269003 978607745 525751254 343060584 72443046 300879617 278943004 760067231 235472075 976841481 936605508 479647311 686662820 58080719 886417986 681348294 512210247 340704136 86...
result:
ok 5000 numbers
Test #6:
score: 0
Accepted
time: 13ms
memory: 9896kb
input:
5000 5000 792216357 552236642 821494867 527714453 169575822 820595714 51053252 285231608 834824001 795954150 856055581 789265115 391543052 623878414 831103431 973063635 851485478 722429924 377946447 74393554 229570805 77829523 104941568 904281532 519848089 214665346 829017846 170077097 530089743 975...
output:
231592521 710420358 76758915 241413386 430224292 922200556 894399093 539685138 594923071 860157611 94869286 900661909 4590133 587348986 920780522 790820661 536438280 976402941 439797034 327200752 920824344 894723053 962589436 412895743 843160266 488926976 317592562 384947824 445153417 711558885 8449...
result:
ok 5000 numbers
Subtask #2:
score: 20
Accepted
Dependency #1:
100%
Accepted
Test #7:
score: 20
Accepted
time: 232ms
memory: 114036kb
input:
50000 50000 768966868 59696813 88144593 167570175 440425517 667021648 50234623 250465092 611066949 942141813 37909983 544761802 310089692 393644730 822735225 212716527 45802184 416983999 978914560 514007472 271601592 803603476 153018866 463206234 430926936 205142491 621149405 347663094 909171725 990...
output:
535964317 498669705 110235462 577178694 458409211 180052579 833120408 528030659 854656432 578186212 647551642 106109933 785867614 715665434 119860891 227206432 978129416 355043244 824024937 889456087 653872988 218204727 768257490 147883304 92932220 842681002 700378724 973977972 293932154 820122893 4...
result:
ok 50000 numbers
Test #8:
score: 0
Accepted
time: 309ms
memory: 111472kb
input:
50000 50000 926434242 833464466 818417327 197100897 15934287 644632732 232006706 557905827 95530335 953397962 584509490 134039675 456180816 88153497 341272750 905812813 623878180 753926638 345776279 455413981 491623547 971836681 670535283 525534014 876800156 818154122 115494771 967365768 513073832 9...
output:
50189578 614003924 406679462 891916825 854664555 383564578 656197644 2618026 701413889 802641599 87893214 112489935 882216004 256603507 510017739 429242037 619481996 800403686 180804571 620515587 694607788 124659042 803354134 599430985 133489716 153343561 718376448 482871834 148308285 176675015 3185...
result:
ok 50000 numbers
Test #9:
score: 0
Accepted
time: 394ms
memory: 114688kb
input:
50000 50000 771988922 212436623 804443396 817827846 830699527 784422632 729884276 912003446 995885315 824721340 837642183 283068922 372817603 100299803 60891115 676351412 641016821 983300431 887927410 950464053 960650766 801946150 981593160 201935570 609178140 408519531 929272301 588279454 605903956...
output:
331430307 842168506 514428667 355689311 865723558 272088222 5106326 724323283 63969592 24689102 375126359 172043952 425829760 749877111 327857093 646470881 65951216 305793882 95712629 723748855 433154753 809570577 771906309 477420435 245967760 460647924 923047416 272709139 700228441 906998284 174826...
result:
ok 50000 numbers
Test #10:
score: 0
Accepted
time: 385ms
memory: 114828kb
input:
50000 50000 900366749 674207154 742580520 423717735 53773991 646025272 25689686 304532836 101502882 140887825 84382607 434523825 626129678 318135577 937888950 303284365 836439804 651694084 478701931 947065798 684677978 332843230 554104599 912552910 417149550 163252218 881219604 924426344 842413390 6...
output:
866369968 233793457 165937004 573866572 499141399 50069437 126848823 357332948 487646929 199006759 335058982 690767657 892697514 805303044 886885871 694863196 5801194 954769260 109473624 573415210 828217282 59732343 505053373 352386067 840363722 636894175 425645259 208103655 473793746 127002236 7193...
result:
ok 50000 numbers
Test #11:
score: 0
Accepted
time: 364ms
memory: 114608kb
input:
50000 50000 128009702 835394168 195582915 237058448 308987847 332547707 3181530 522885776 607169352 843503488 560544297 234676319 754038773 971946242 251601569 713013731 449922156 964200243 270751518 485953250 983022206 416540821 96233750 773462469 218771460 869573801 1826928 217439825 101327578 643...
output:
809413913 900087043 897489309 490488662 860111942 389091241 845061538 685662609 477979480 990731474 975276464 524647592 959334221 279016791 579113462 806057027 834352370 414957639 135587999 772681462 736164077 81034876 768035946 828362323 685292149 417723235 77932963 281011135 28218190 682861276 258...
result:
ok 50000 numbers
Test #12:
score: 0
Accepted
time: 391ms
memory: 114716kb
input:
50000 50000 599691755 74459068 334593294 689318718 842905094 443210545 96280108 643838657 591570435 747119928 963798260 884441507 753584023 622768897 446596420 332458502 185519101 709830529 141249071 885916331 314791389 276299436 336855194 552443961 696874117 738278010 593700707 229502424 335910014 ...
output:
162968440 829114 814500713 268429015 476122947 818974632 467331763 12940232 70162519 26148936 753219232 517231641 428317039 509625675 870990487 600796230 385649006 922400831 368412364 536483891 715148335 810869176 206671100 908712544 377398265 119727728 226288513 266653103 263036296 547334935 552450...
result:
ok 50000 numbers
Test #13:
score: 0
Accepted
time: 427ms
memory: 112676kb
input:
50000 50000 509049147 846306934 497301582 156085268 418734386 738839401 463112303 670194644 148295378 897052920 337867193 22462071 304900070 737711091 665480978 823159855 481862593 827100577 316492474 693956517 618429668 870188133 65980921 940212747 403208852 268141196 171329777 255058173 740325554 ...
output:
395519880 988998325 123071413 493713767 662307084 960909185 899731232 660410246 707341210 28490302 887326532 11593659 417615875 848911573 573179668 137809591 475174820 133873443 756015822 372043435 654859285 863530053 521918428 957117123 712163801 591167143 206607972 85953124 12946789 66960555 99205...
result:
ok 50000 numbers
Test #14:
score: 0
Accepted
time: 446ms
memory: 113736kb
input:
50000 50000 271460757 98184763 207476207 318550358 36132989 163998764 834240576 816456648 104049969 565152081 639115703 63526614 540131202 584035121 125994524 103597578 198061629 69949079 246096816 249710935 309174260 770090066 306030177 506695475 716974345 478421640 989138506 146754109 896531147 87...
output:
227639846 402726965 215887242 144596012 768374615 60584104 132321406 817024981 963910026 42360689 737913188 348299774 276641697 65869541 636135686 129087361 794550831 458540944 2270242 99210275 74519859 983209150 670092005 849382923 682067660 418183252 61685226 144713076 943183123 543020736 32933718...
result:
ok 50000 numbers
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:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%