QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#88696 | #5830. 树 | ruizhangj | 10 | 3512ms | 523116kb | C++14 | 3.5kb | 2023-03-16 21:32:23 | 2023-03-16 21:32:26 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace IO{
const int siz=1<<25;
char a[siz],b[siz],*ia=a+siz,*ea=a+siz,*ib=b,*eb=b+siz;
int p[105],l=0;
inline char gc(){if (ia==ea) fread(a,1,siz,stdin),ia=a;return *ia++;}
inline void flush(){fwrite(b,1,ib-b,stdout);ib=b;}
inline void pc(char c){if (ib==eb) flush();*ib++=c;}
inline int read(){
int x=0;char t=gc();
for (;t<'0' || t>'9';t=gc());
for (;'0'<=t && t<='9';t=gc()) x=x*10+t-'0';
return x;
}
inline void write(ll x){
if (!x) pc('0');
for (;x;x/=10) p[++l]=x%10;
for (;l;--l) pc(p[l]+'0');pc('\n');
}
struct f{~f(){flush();}}F;
}
using IO::read;
using IO::write;
const int N=1e6+5;
const int B=20;
int head[N],go[N],nxt[N];
int last[N],ask_k[N],ask_id[N],lk[N];
int F[N],son[N],st[N],dep[N];
int a[N];
ll ans[N];
int n,m,tot=0,cnt=0;
void dfs(int x){
F[x]=1;
for (int i=head[x];i;i=nxt[i]){
dep[go[i]]=dep[x]+1;
dfs(go[i]);
if (F[go[i]]+1>F[x]) F[x]=F[go[i]]+1,son[x]=go[i];
}
}
void dfs1(int x){
if (!st[x]) st[x]=cnt+1,cnt+=F[x];
if (son[x]){
st[son[x]]=st[x]+1;
dfs1(son[x]);
}
for (int i=head[x];i;i=nxt[i])
if (go[i]!=son[x])
dfs1(go[i]);
}
int c[B][2][N];
int f[B][N][2];
int g[B][N];
ll s[N];
void add(int w,int v,int x,int y){
for (;x<=n;x+=x&-x) c[w][v][x]+=y;
}
int get(int w,int v,int l,int r){
int res=0;
for (;r;r-=r&-r) res+=c[w][v][r];
if (l)
for (--l;l;l-=l&-l) res-=c[w][v][l];
return res;
}
void add(int x,ll y){
for (;x<=n;x+=x&-x) s[x]+=y;
}
ll get(int l,int r){
ll res=0;
for (;r;r-=r&-r) res+=s[r];
for (--l;l;l-=l&-l) res-=s[l];
return res;
}
ll calc(int x,int k){
k=min(k,F[x]-1);
ll res=0;
for (int w=0;w<B;++w){
int t=k/(1<<w+1);
int c=g[w][st[x]]-g[w][st[x]+t*(1<<w+1)];
int l=t*(1<<w+1),r=t*(1<<w+1)+(1<<w);//[l,r)
if (l<k+1){
r=min(r,k+1);
c+=get(w,1,st[x]+l,st[x]+r-1);
}
l=t*(1<<w+1)+(1<<w),r=(t+1)*(1<<w+1);
if (l<k+1){
r=min(r,k+1);
c+=get(w,0,st[x]+l,st[x]+r-1);
}
res+=1ll*(1<<w)*c;
}
res+=get(st[x],st[x]+k)*(1ll<<B);
// if (k==F[x]-1) res+=(get()s[st[x]]<<B);
// else res+=((s[st[x]]-s[st[x]+k+1])<<B);
return res;
}
void dfs2(int x){
// s[st[x]]+=(a[x]>>B);
add(st[x],a[x]>>B);
for (int w=0;w<B;++w){
++f[w][st[x]][(a[x]>>w)&1];
add(w,(a[x]>>w)&1,st[x],1);
}
if (son[x]) dfs2(son[x]);
for (int i=head[x];i;i=nxt[i])
if (go[i]!=son[x]){
dfs2(go[i]);
for (int w=0;w<B;++w)
for (int j=0;j<F[go[i]];++j){
f[w][st[x]+j+1][0]+=f[w][st[go[i]]+j][0];
f[w][st[x]+j+1][1]+=f[w][st[go[i]]+j][1];
add(w,0,st[x]+j+1,f[w][st[go[i]]+j][0]);
add(w,1,st[x]+j+1,f[w][st[go[i]]+j][1]);
g[w][st[x]+j+1]+=g[w][st[go[i]]+j];
}
}
for (int w=0;w<B;++w){
int l=0,r=(1<<w);
if (l<F[x]){
r=min(r,F[x]);
g[w][st[x]]+=get(w,1,st[x]+l,st[x]+r-1);
}
l=(1<<w),r=(1<<w+1);
if (l<F[x]){
r=min(r,F[x]);
g[w][st[x]]+=get(w,0,st[x]+l,st[x]+r-1);
}
if ((1<<w+1)<F[x])
g[w][st[x]]+=g[w][st[x]+(1<<w+1)];
}
for (int i=last[x];i;i=lk[i])
ans[ask_id[i]]=calc(x,ask_k[i]);
}
int main(){
n=read();
for (int i=1;i<=n;++i) a[i]=read();
for (int x,i=2;i<=n;++i){
x=read();
++tot;go[tot]=i;nxt[tot]=head[x];head[x]=tot;
}
tot=0,m=read();
for (int x,k,i=1;i<=m;++i){
x=read(),k=read();
++tot;ask_k[tot]=k;ask_id[tot]=i;lk[tot]=last[x];last[x]=tot;
}
dep[1]=1;dfs(1);
dfs1(1);
dfs2(1);
for (int i=1;i<=m;++i) write(ans[i]);
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 6ms
memory: 4732kb
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:
15027326611 558747809 12352523 130334714014 39331452586 585197158 1628586765 922104954 3638699922 19710531735 835618842 1719999173 21584131141 2418952903 41625218361 33013206024 52480804945 678970189 60372359402 708763390 974312741 8186108931 541384937 39531362280 755479297 465075002 51201629 950537...
result:
wrong answer 1st numbers differ - expected: '31727996563', found: '15027326611'
Test #2:
score: 0
Wrong Answer
time: 246ms
memory: 54644kb
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:
2445807883 42249438441 455499536 125427731 1210453775 990008229650 2022059896238 586671581 1581930369409 798898621 468901916 1026619234 25990778 732449680 1322905428907 4455662022579 1721547881 1491500623 579803904126 2601433478798 263989598 1350176621 589453690457 727716410 905594434247 1849681495 ...
result:
wrong answer 1st numbers differ - expected: '2509771019', found: '2445807883'
Test #3:
score: 0
Time Limit Exceeded
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:
result:
Test #4:
score: 0
Time Limit Exceeded
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:
result:
Test #5:
score: 0
Wrong Answer
time: 2745ms
memory: 507816kb
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:
1066595749 938786622 1803221827 10405358442 4424342953 1172824629 5806900848 1532414846 1399226073 476794111 986063591 1579646962 6872769641 38176990 781099105 527479272 1177630552 1689999886 1187076133 10891301776 2624037110 3307856983 1206547828 8101581123 49589751 499629803 1545884931 137931191 9...
result:
wrong answer 1st numbers differ - expected: '2258826661', found: '1066595749'
Test #6:
score: 0
Wrong Answer
time: 2707ms
memory: 517128kb
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:
994051214 904057442 2210943947 1586149760660 14152009644963 976251948 369701890 43760580 1510389651 2173456896 26574701776261 716198149 190685967 42148147833292 5600497684924 557364187 1084839575 3104092 276690929 39411127602223 467852673 33049527 425554649 9168052160390 39157403077001 426018723 694...
result:
wrong answer 3rd numbers differ - expected: '2995278795', found: '2210943947'
Test #7:
score: 0
Wrong Answer
time: 2727ms
memory: 522948kb
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:
999908753 401770479 60505770903250 55595781452390 22844285553589 43126851837317 1146711843 40454243253401 1850479796 4675617303 1079679207 2140524944 75514756 3785049779701 29704005243516 167205592 23372168206748 1085743066 20793898022249 76102493597787 39639233471821 990736484 4225370989 1491333569...
result:
wrong answer 3rd numbers differ - expected: '206278425790162', found: '60505770903250'
Test #8:
score: 10
Accepted
time: 3254ms
memory: 502180kb
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: 3512ms
memory: 518224kb
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:
39454234799 3315075760475 33048478545823 13816459182847 275400654 1384234654 64716568 4753682919512 4074531476052 107755857170 6257177892488 528398853 1456723425 1214744688 5204684471631 23475037 11309387851967 245789356 109738951 19518066036389 24962020274033 102619945 7052667750105 1431295809 9251...
result:
wrong answer 1st numbers differ - expected: '96094116015', found: '39454234799'
Test #10:
score: 0
Wrong Answer
time: 3358ms
memory: 523116kb
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:
431856043 520373616 27436560498103 2859951331213 19791440608810 5537480704532 762015332 2015297394381 1184478776 594507511 1093542189929 10253464882230 306250439 62500137 30031984294676 786078000 12522094609432 2905532564 1988198999822 877965179 7484947989132 1013123764 3578493077913 2308315220819 5...
result:
wrong answer 3rd numbers differ - expected: '77562434895287', found: '27436560498103'