QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#421355 | #5830. 树 | atgc | 10 | 1142ms | 787532kb | C++14 | 2.7kb | 2024-05-25 17:01:10 | 2024-05-25 17:01:11 |
Judging History
answer
#include<bits/stdc++.h>
//ign V fir bit
#pragma GCC diagnostic ignored "-Wc++17-extensions"
#define int long long
using namespace std;
const int maxn = 2e6+10, lm = __lg(maxn)+1;
struct{int nxt,v;}edge[maxn<<1];
int head[maxn],ec;
void add(int u,int v){edge[++ec]={head[u],v};head[u]=ec;}
int n,Q,V[maxn];
int son[maxn],dfn[maxn],dfc;
int dfs1(int u){
int len=0;
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].v,vl=dfs1(v);
if(vl>len)len=vl,son[u]=v;
} return len+1;
}
int cnt1[maxn][lm],cnt0[maxn][lm], f[maxn][lm], othbit[maxn];
struct QRY{int id,k;};vector<QRY>q[maxn];
int ans[maxn];
int dfs2(int u){
// infun(u);
int buf=dfn[u]=++dfc,pr=0,res=dfc;
if(son[u])pr=dfc+1,res=dfs2(son[u]);
// else ++dfc;
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].v;if(v!=son[u]){
int cur=dfc,las=dfs2(v);
for(int j=cur+1;j<=las;++j) {
for(int k=0;k<lm;++k)
cnt1[buf+j-cur][k]+=cnt1[j][k],
cnt0[buf+j-cur][k]+=cnt0[j][k],
f[buf+j-cur][k]+=f[j][k];
othbit[buf+j-cur]+=othbit[j];
}
}
}
for(int i=0;i<lm;++i)cnt1[buf][i]=cnt1[pr][i]+(V[u]>>i&1),
cnt0[buf][i]=cnt0[pr][i]+(~V[u]>>i&1);
othbit[buf]=othbit[pr]+(V[u]>>lm);
// deb(buf,arr(cnt1[buf]-1,lm));
for(int i=0;i<lm;++i) {
int ed = buf+(1<<i) > res ? 0 : buf+(1<<i);
int pred = buf+(1<<(i-1)) > res ? 0 : buf+(1<<(i-1));
f[buf][i] = ((cnt1[buf][i] - cnt1[ed][i])<<i)
+ (i? f[buf][i-1]+f[pred][i-1]
+((cnt0[pred][i-1]-cnt0[ed][i-1])<<(i-1))
-((cnt1[pred][i-1]-cnt1[ed][i-1])<<(i-1)) :0);
// deb(f[buf][i]);
// deb(((cnt1[buf][i] - cnt1[ed][i])<<i));
if(!i)continue;
// deb(f[buf+(1<<(i-1))][i-1]);
// deb(((cnt0[buf+(1<<(i-1))][i-1]-cnt0[ed][i-1])<<(i-1)));
// deb(((cnt1[buf+(1<<(i-1))][i-1]-cnt1[ed][i-1])<<(i-1)));
}
for(auto[id,k]:q[u]){
// infun(id,k);
int va=0,cur=buf;k=min(k,res-buf)+1;
int ed=(buf+k-1==res?0:buf+k);
for(int i=lm-1;~i;--i){
// deb(i,cur);
if(k>>i&1){
// deb("BinGO!"_,f[cur][i]);
va += f[cur][i];
cur+=1<<i;
// deb("and then:"_,((cnt0[cur==res+1?0:cur][i]-cnt0[ed][i])<< i));
va += ((cnt0[cur==res+1?0:cur][i]-cnt0[ed][i])<< i);
}else{
// deb("only then:"_,((cnt1[cur==res+1?0:cur][i]-cnt1[ed][i])<< i));
va += ((cnt1[cur==res+1?0:cur][i]-cnt1[ed][i]) << i);
}
// deb(va);
} ans[id]=va+othbit[buf]-othbit[ed];
}
// deb(res);
return res;
}
signed main() {
// freopen("pai.in","r",stdin);
// freopen("pai.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0);
cin>>n;for(int i=1;i<=n;++i)cin>>V[i];
for(int i=2,f;i<=n;++i)cin>>f,add(f,i);
cin>>Q;for(int i=1,x,k;i<=Q;++i)
cin>>x>>k,q[x].push_back({i,k});
dfs1(1),dfs2(1);
for(int i=1;i<=Q;++i)cout<<ans[i]<<'\n';
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 53532kb
input:
2000 946347867 341162491 202762650 295215762 254064439 66267204 693162580 357612557 492940637 939526638 59775103 374919339 144042807 861369313 651043728 999024805 439554916 167782038 597475252 56704409 69846137 22185655 79439847 769194737 145071391 226046618 915359433 392527325 84982946 54584098 827...
output:
71502218 905643 1866768 1905536864 189202732 92029 1197589 1455665 16922310 114769634 4098946 4529655 129245020 3034951 263145718 195964416 882669123 2639363 302652531 2023503 1234677 48121139 319979 253856335 504937 1604631 870005 528454 111035957 647896 6308388 365225 3536913 6431658 231274 323185...
result:
wrong answer 1st numbers differ - expected: '31727996563', found: '71502218'
Test #2:
score: 0
Wrong Answer
time: 69ms
memory: 111944kb
input:
99999 792064428 473106195 799314986 65440734 948726345 442414256 280245405 873012700 466192412 899092866 283555341 657824017 963883713 793944180 767444438 105576842 542107696 580140098 65321660 381184238 584604194 397414881 861590124 309323011 217641157 120832524 303744205 961590116 110259426 380351...
output:
5772725 230949882 2514955 1695822 3543729 5545435968 11274158363 1566452 8909942252 1981241 1237243 5306980 824966 2641543 7461072933 68247981059 1884061 426262 5764307314 33840945329 1845723 3805679 3330815309 5013 4910656247 2091464 3569008 347662 2779952 991487 499102008 436308 1814527 1777734535...
result:
wrong answer 1st numbers differ - expected: '2509771019', found: '5772725'
Test #3:
score: 0
Wrong Answer
time: 1142ms
memory: 777200kb
input:
1000000 947727010 179844140 923847675 171881267 5552129 974443359 989307850 869400987 126992154 527448411 141137718 136124474 917813105 392020809 79012342 473860575 969007624 833563354 90169336 878445705 84352622 403307122 733751738 670851448 942399068 731541999 101293644 545785337 964751520 9168003...
output:
717792790112 142991359070 293427006492 414250028565 10808722182 117167152208 202821437044 297928629821 641995886912 274436482278 286642855406 19303760794 31444668063 144811454642 692210751893 34905076519 167300317834 163919172922 148905052266 48215966698 298733931989 520143121314 273423149486 344682...
result:
wrong answer 1st numbers differ - expected: '322288180595345', found: '717792790112'
Test #4:
score: 0
Wrong Answer
time: 1036ms
memory: 787532kb
input:
1000000 264862971 751386013 921867736 711577153 262726588 565608444 975324815 440219681 107888226 928241413 729126923 283912914 86248857 896446999 12839598 651796991 139813366 105131395 341646170 839485925 939265720 844548518 102280410 457829889 8602879 737140565 17206920 974175632 535833885 8373832...
output:
3151768965 217150765455 753206660575 519042618566 16262710889 603567585910 46648644768 201704260794 5636326811 148187372869 469924359384 187842178865 39746482002 293808005508 4109194746 102639883023 186422686006 572404252830 535685411246 263759448276 227252274465 85801338850 11388114454 308454713280...
result:
wrong answer 1st numbers differ - expected: '1437301063221', found: '3151768965'
Test #5:
score: 0
Wrong Answer
time: 737ms
memory: 661396kb
input:
1000000 978606419 773027389 111265179 979476504 280718851 476857501 751854950 579263969 848569548 781051974 31782627 533831186 812822170 111553645 297770650 331144396 676977983 2236128 258203325 75591120 676466973 60056446 494411414 286185093 92474576 173276071 535648669 87210101 355790411 880267291...
output:
4389336 1360125 1769118 45437274 29766618 4712290 31352172 6738060 3572302 740834 402621 4686561 35012440 428272 958933 1094371 6372298 9133755 3234600 51134295 13085299 16380203 2783154 40137795 1355278 507865 9723125 1616376 3419740 19937367 1758384 36543423 430818 1936851 1538161 24485561 2737336...
result:
wrong answer 1st numbers differ - expected: '2258826661', found: '4389336'
Test #6:
score: 0
Wrong Answer
time: 739ms
memory: 663776kb
input:
1000000 952470566 585754087 120174600 401525004 458588768 5487567 31210348 446333263 231409083 521960132 457721893 866842852 925207283 16805978 4706826 99640835 619272676 136536623 459247161 308807462 633687300 717271369 23906473 865522890 173799280 424309108 719410673 118906385 110627845 730629403 ...
output:
1640 185361 2644318 9041970171 78469326070 1076733 603314 1817560 2538082 2906042 147792233625 1069658 1942377 234007363996 30896370617 1619172 2709659 1006941 1964148 218892179595 188000 1592262 1930147 51029179336 217262806416 297070 2494322 865751 167145676531 2388759 14091210 2487177 2394674 433...
result:
wrong answer 1st numbers differ - expected: '994051214', found: '1640'
Test #7:
score: 0
Wrong Answer
time: 780ms
memory: 670308kb
input:
1000000 732367509 105027907 958920212 886798715 102486738 813075884 301085392 242303497 979657287 944859684 307768 438158233 561755409 740706505 791145209 283862713 828081846 771569552 59044985 600549571 191330226 438693570 36976319 810654215 220068818 771875421 740642902 839964155 206129566 2065543...
output:
3762028 1214638 460302109986 432906590771 127078267731 239981183344 1667397 224554140007 10230299 9456474 1743593 3528075 17320 20950580887 165138765365 1530663 129616596739 3613150 115479688636 929248809069 220501768593 881212 9049177 83179498375 65613547818 224342555062 1401873 470192713706 554358...
result:
wrong answer 1st numbers differ - expected: '999908753', found: '3762028'
Test #8:
score: 10
Accepted
time: 788ms
memory: 669916kb
input:
1000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
output:
3826404725 4857376598 25168931 28 7223089410 64321117 0 334067473 60992 1 3 1 1 1 1 0 672988753 1 0 3075365115 8 0 0 3 0 320487913 1 0 0 0 0 423617904 687492110 0 301348 220115086 10 35419106 37297971 1 1 28561996 517977935 3 2643348428 0 1132906086 2 1 4 20029285275 6 6191566989 0 20341562923 93968...
result:
ok 1000000 numbers
Test #9:
score: 0
Wrong Answer
time: 810ms
memory: 672936kb
input:
1000000 465660691 982007525 816592310 377030959 572981469 679249520 86377999 709561525 940473306 35102782 886143915 792819787 903287397 264564177 857982095 91486434 217197704 123118964 383387342 820268798 497623987 255010796 607884194 848568529 38169627 197987657 421323589 664004905 485409127 696844...
output:
199789389 18431187626 184805015490 76147366624 673873 2212145 1802038 26448914721 22484205638 600952679 34938491516 2013952 3397782 2591410 28957256658 406376 63358761318 422689 687099 108179814585 180707851404 1956697 39525132706 2087462 5196492523 62838735424 992334 763090298 341287 28979802217 78...
result:
wrong answer 1st numbers differ - expected: '96094116015', found: '199789389'
Test #10:
score: 0
Wrong Answer
time: 812ms
memory: 676032kb
input:
1000000 665830082 788228483 245541444 289601309 641764988 150723484 925214020 557415731 310210969 379707835 517820381 883917428 134445288 775557009 444476671 89856268 655841087 888410254 37788122 694551869 563331754 488108584 839551943 415095075 445425438 35452604 562044723 640544531 146258096 66852...
output:
1940088 280168 172959748842 15654549962 140711833301 30711100565 2846670 11162748821 1685612 3110929 5981407207 57411380695 2163544 1682758 167659051182 1743526 70049338429 6221906 11116566107 1356061 41492254919 2296982 20045625977 12664196665 1241793 4617066 541848 58727023038 1823254 4576457 3962...
result:
wrong answer 1st numbers differ - expected: '431856043', found: '1940088'