QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#431883#5830. 树Crying20 1600ms562184kbC++141.9kb2024-06-06 11:33:302024-06-06 11:33:32

Judging History

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

  • [2024-06-06 11:33:32]
  • 评测
  • 测评结果:20
  • 用时:1600ms
  • 内存:562184kb
  • [2024-06-06 11:33:30]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6+10,MA = 20;
int n,q,v[MAXN]; vector<int> e[MAXN];

int pre[MAXN],buc[MAXN],dep[MAXN],to[MAXN][MA],cnt[MAXN][MA][2],dfn[MAXN],dtot;
ll f[MAXN][MA];

void dfs(int u){
    pre[u] = buc[dep[u]],buc[dep[u]] = u; dfn[++dtot] = u;
    to[u][0] = to[pre[u]][0];
    f[u][0] = f[pre[u]][0] + v[u];

    for(int i=0;i<MA;i++)cnt[u][i][v[u]>>i&1]++;
    for(auto v : e[u]){
        dep[v] = dep[u]+1;
        dfs(v);
        to[u][0] = v;
        for(int i=0;i<MA;i++)for(int j=0;j<2;j++)cnt[u][i][j] += cnt[v][i][j];
    }
}

ll Q(int x,int k){
    if(!x)return 0;
    int p = x; for(int j=MA-1;j>=0;j--)if(k>>j&1)p = to[p][j];
    ll ans = 0;
    int q = x;
    for(int j=MA-1;j>=0;j--)if(k>>j&1){
        ans += f[q][j]; q = to[q][j];
        int c[2] = {cnt[q][j][0]-cnt[p][j][0],cnt[q][j][1]-cnt[p][j][1]};
        ans += 1ll * (c[0]-c[1]) * (1<<j);
    }
    return ans;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0);

    cin>>n;
    for(int i=1;i<=n;i++)cin>>v[i];
    for(int i=2,p;i<=n;i++)cin>>p,e[p].push_back(i);
    dfs(1);

    for(int i=n;i>=1;i--){
        int u = dfn[i];
        for(int j=1;j<MA;j++)to[u][j] = to[to[u][j-1]][j-1];
    }
    for(int i=1;i<=n;i++){
        int u = dfn[i];
        for(int j=0;j<MA;j++)for(int k=0;k<2;k++)cnt[u][j][k] += cnt[pre[u]][j][k];
    }
    for(int i=n;i>=1;i--){
        for(int j=1;j<MA;j++){
            f[i][j] = f[i][j-1] + f[to[i][j-1]][j-1];
            int p = to[i][j-1],q = to[i][j];
            int c[2] = {cnt[p][j-1][0]-cnt[q][j-1][0],cnt[p][j-1][1]-cnt[q][j-1][1]};
            f[i][j] += 1ll * (c[0]-c[1]) * (1<<(j-1));
        }
    }

    cin>>q;
    while(q--){
        int x,k; cin>>x>>k; k++;
        ll ans = Q(x,k) - Q(pre[x],k);
        cout<<ans<<"\n";
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 6ms
memory: 32744kb

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:

93562797525
558747809
12352523
14314640081
8505590826
585197158
1628586765
922104954
8179034002
12369344526
1832814618
1719999173
28977703452
2418952903
8644836955
20805786003
60352946639
1458062157
31398460836
708763390
974312741
22160481221
541384937
16723053352
755479297
465075002
51201629
950537...

result:

wrong answer 1st numbers differ - expected: '31727996563', found: '93562797525'

Test #2:

score: 0
Wrong Answer
time: 74ms
memory: 83044kb

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:

2509771019
6436315211
528899856
125427731
1953894159
26392676484
102137108383
586671581
18278764698
798898621
468901916
1620113250
25990778
1594379152
3535933560
12941170290
1721547881
1491500623
14636587253
12941170290
263989598
1350176621
8689948014
727716410
15686024432
1849681495
1925816579
9713...

result:

wrong answer 2nd numbers differ - expected: '102808896745', found: '6436315211'

Test #3:

score: 10
Accepted
time: 1560ms
memory: 562184kb

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:

322288180595345
64144900444023
131621992874362
185914449206495
4835864536999
52498320047300
91107191167946
133549265294890
287851078359140
123171067016186
128593134440538
8701280311335
14021811618693
65043919486113
310510908841731
15605852792829
75072265994522
73630260235120
66821009550818
217424650...

result:

ok 1000000 numbers

Test #4:

score: 10
Accepted
time: 1600ms
memory: 562176kb

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:

1437301063221
97809309869545
338628270305337
233680181746714
7398781587300
271306073103235
20877473367084
90775179215463
2535173725689
66604505519276
211534325021331
84757341592614
17886216279466
132051595671397
1860377236584
46253790496717
83950882453932
257274371637175
240943229927301
118446604805...

result:

ok 1000000 numbers

Test #5:

score: 0
Wrong Answer
time: 751ms
memory: 469784kb

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:

2258826661
938786622
1803221827
43208511706
30287611849
2783437365
41181595551
2720450858
1859550937
476794111
986063591
1579646962
14494868585
38176990
781099105
527479272
2392930136
2519423502
1620138021
5102485044
7534577049
6903424087
1206547828
12018279859
49589751
499629803
7188393771
13793119...

result:

wrong answer 4th numbers differ - expected: '20412967786', found: '43208511706'

Test #6:

score: 0
Wrong Answer
time: 813ms
memory: 471288kb

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:

-6898182465
904057442
5205910500
32056922189
32973679199
976251948
-2571469038
43760580
1510389651
3077329408
20312172933
716198149
190685751
19536879911
21440224770
557364187
-13813513744
3104092
-10264268635
17294021944
-15836865722
-12431616770
-3537488427
8944089373
9310522798
426018723
-1916787...

result:

wrong answer 1st numbers differ - expected: '994051214', found: '-6898182465'

Test #7:

score: 0
Wrong Answer
time: 792ms
memory: 475988kb

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
11832493104
27674421664
28054843270
75832446140
1146711843
27900783136
2910590132
4981801495
1079679207
2140524944
-12135277034
26287832592
56162167402
-11650500854
38870201815
-22764650417
11306856959
28145343211
32551079552
990736484
15443575689
13481987423
13656652674
34769680...

result:

wrong answer 3rd numbers differ - expected: '206278425790162', found: '11832493104'

Test #8:

score: 0
Wrong Answer
time: 737ms
memory: 473704kb

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:

3145420
4905217
3790110
28
2212686
4951201
0
2075746
30292
1
3
1
1
1
1
0
5710734
1
0
2402811
8
0
0
3
0
651865
1
0
0
0
0
7027885
1285123
0
867088
4223068
10
2707113
215224
1
1
199426
581738
3
2946294
0
4728139
2
1
4
2182781
6
2624908
0
8746475
6414696
3
13
1
3
-2636942
1
8035393
3
0
1
0
2
-940260
1
5...

result:

wrong answer 1st numbers differ - expected: '3826404725', found: '3145420'

Test #9:

score: 0
Wrong Answer
time: 888ms
memory: 476712kb

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:

54227963514
9333823226
39157249575
9451644711
275400654
1384234654
64716568
41635431053
30269201769
6406740696
18712326917
528398853
1456723425
1214744688
10159367008
23475037
52232548322
245789356
109738951
26161973053
21631883648
102619945
13596613317
1556076353
14666313046
31507163349
978264700
7...

result:

wrong answer 1st numbers differ - expected: '96094116015', found: '54227963514'

Test #10:

score: 0
Wrong Answer
time: 752ms
memory: 480448kb

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
31159218916
14599824618
33382346071
11597531249
762015332
17617433297
1184478776
594507511
22179141906
25261679207
306250439
62500137
17387770249
786078000
20357453512
4162775188
22017440434
877965179
15693320481
1013123764
13228415101
11005788921
542306751
1978036157
937968345
1...

result:

wrong answer 3rd numbers differ - expected: '77562434895287', found: '31159218916'