QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#858404 | #9676. Ancestors | Hanghang# | 11 | 1559ms | 29800kb | C++20 | 2.8kb | 2025-01-16 16:58:36 | 2025-01-16 16:58:37 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const int N=1e5+3,M=1e6+3;
int n,m,rt,tot,fa[N],sz[N],hson[N],dep[N],top[N],dwn[N];
int ans[M];
vector<int>ve[N],D[N];
vector<array<int,3>>res[N],now[N];
array<int,4>c[M*3];
void Dfs1(int x)
{
dep[x]=dep[fa[x]]+1;
for(int y:ve[x])
{
Dfs1(y);sz[x]+=sz[y];
if(sz[hson[x]]<sz[y])hson[x]=y;
}
}
void Dfs2(int x,int tp)
{
top[x]=tp;
if(hson[x])Dfs2(hson[x],tp);
for(int y:ve[x])if(y!=hson[x])Dfs2(y,y);
dwn[x]=hson[x]?dwn[hson[x]]:x;
}
void Color(int id)
{
int x=id;
while(x)
{
int p=top[x];
while(res[p].size()&&res[p].back()[0]<=dep[x])
{
auto [l,r,o]=res[p].back();res[p].pop_back();
now[id].pb({dep[id]-r,dep[id]-l,o});
if(r>dep[x])res[p].pb({dep[x]+1,r,o});
}
res[p].pb({dep[p],dep[x],id});x=fa[p];
}
}
void Clear(int p){while(p)res[top[p]]={{dep[top[p]],dep[dwn[p]],0}},p=fa[top[p]];}
void Work(int d)
{
for(int id:D[d])Color(id);
for(int id:D[d])Clear(id);
for(int id:D[d])for(auto [l,r,o]:now[id])
{
c[++tot]={id,o,r,1};
if(l)c[++tot]={id,o,l-1,-1};
}
// cout<<"Work: "<<d<<endl;
// for(int id:D[d])
// {
// cout<<"Now: "<<id<<endl;
// for(auto [l,r,o]:now[id])cout<<l<<" "<<r<<" "<<o<<endl;
// }
}
bool Cmp(array<int,4> A,array<int,4> B){return A[2]!=B[2]?A[2]<B[2]:A[3]>B[3];}
void Sol(int L,int R)
{
if(L==R)return;
int mi=(L+R)/2;Sol(L,mi);Sol(mi+1,R);
for(int i=L;i<=mi;i++)if(c[i][3]>1)
{
auto [l,r,d,id]=c[i];
for(int j=mi+1;j<=R;j++)if(c[j][3]<=1)
{
auto [t,lim,v,op]=c[j];
if(lim<l&&l<=t&&t<=r)ans[id]+=op;
}
}
}
int main()
{
//freopen("1.in","r",stdin);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>fa[i];
if(!fa[i])rt=i;
else ve[fa[i]].pb(i);
}
Dfs1(rt);Dfs2(rt,rt);
for(int i=1;i<=n;i++)D[dep[i]].pb(i);
for(int i=1;i<=n;i++)if(top[i]==i)res[i].pb({dep[i],dep[dwn[i]],0});
for(int d=1;d<=n;d++)if(D[d].size())Work(d);
for(int i=1,l,r,d;i<=m;i++)cin>>l>>r>>d,c[++tot]={l,r,d,i+1};
sort(c+1,c+tot+1,Cmp);
//for(int i=1;i<=tot;i++)cout<<c[i][0]<<" "<<c[i][1]<<" "<<c[i][2]<<" "<<c[i][3]<<endl;
for(int i=1;i<=tot;i++)if(c[i][3]>1)
{
auto [l,r,d,id]=c[i];
for(int j=i+1;j<=tot;j++)if(c[j][3]<=1)
{
auto [t,lim,v,op]=c[j];
if(lim<l&&l<=t&&t<=r)ans[id]+=op;
}
}
//Sol(1,tot);
for(int i=1;i<=m;i++)cout<<ans[i+1]<<"\n";
// for(int t=1,l,r,d;t<=m;t++)
// {
// cin>>l>>r>>d;int ans=0;
// // if(t!=2)continue;
// for(int i=1;i<=r;i++)
// {
// //cout<<"Sol: "<<i<<endl;
// for(auto [u,v,lim]:now[i])
// {
// //cout<<u<<" "<<v<<" "<<lim<<endl;
// if(lim<l&&l<=i&&u<=d&&d<=v)ans++;
// }
// //cout<<ans<<endl;
// }
// cout<<ans<<"\n";
// }
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 11
Accepted
Test #1:
score: 11
Accepted
time: 0ms
memory: 5900kb
input:
7 5 3 1 0 5 3 5 1 1 3 1 5 7 2 1 5 1 4 7 1 4 7 2
output:
2 1 3 3 1
result:
ok 5 number(s): "2 1 3 3 1"
Test #2:
score: 11
Accepted
time: 1559ms
memory: 29800kb
input:
1000 1000 686 337 192 336 405 0 108 485 350 762 258 780 179 939 25 657 571 662 119 786 604 224 935 494 685 575 369 178 249 740 954 204 598 592 68 771 498 86 55 38 298 704 239 292 993 286 16 813 719 187 14 476 792 49 944 52 227 720 310 470 900 243 663 950 627 300 728 189 45 610 673 548 873 95 48 841 ...
output:
452 67 611 126 329 486 354 25 559 585 184 265 576 116 489 289 147 287 13 282 151 192 146 141 148 131 43 72 69 29 5 15 57 10 9 16 87 162 19 217 232 24 178 334 103 139 293 400 299 351 529 632 592 296 640 678 715 708 52 465 322 731 2 69 110 286 172 0 40 31 16 105 75 45 8 94 540 115 357 7 11 431 581 37 ...
result:
ok 1000 numbers
Test #3:
score: 11
Accepted
time: 1261ms
memory: 23584kb
input:
1000 1000 594 766 788 546 408 364 152 525 963 359 339 746 747 58 628 60 144 646 222 863 418 1000 494 84 225 21 202 146 81 879 239 526 662 816 483 140 7 834 978 26 370 619 12 112 824 319 855 852 877 32 708 236 296 791 40 102 238 930 734 609 740 309 982 837 272 451 825 977 717 597 761 90 305 224 216 8...
output:
441 362 300 535 197 383 360 29 428 31 473 9 310 242 232 37 96 227 50 146 180 93 108 83 58 106 61 26 61 32 7 25 54 25 59 17 48 42 68 237 83 95 206 339 161 47 377 250 335 136 436 98 6 515 216 132 263 270 137 463 117 292 61 468 237 326 149 0 126 228 353 168 76 332 169 98 75 418 49 133 284 281 282 134 1...
result:
ok 1000 numbers
Test #4:
score: 11
Accepted
time: 654ms
memory: 17036kb
input:
1000 1000 330 124 926 768 265 445 803 501 173 371 897 686 88 668 787 5 440 818 604 119 918 821 30 400 391 262 299 334 279 58 45 101 814 582 467 182 538 549 455 761 121 769 480 639 605 906 883 341 138 862 684 42 855 535 778 154 407 439 967 780 128 565 622 739 910 69 647 612 345 407 929 352 296 869 31...
output:
347 340 86 105 386 17 172 27 104 146 341 160 298 321 97 112 232 110 222 37 40 42 84 173 114 25 82 81 56 14 1 2 41 62 12 126 122 50 125 152 100 123 26 0 106 34 173 52 184 89 268 270 152 77 39 322 154 50 0 284 146 199 54 149 27 18 101 84 140 232 73 199 334 195 35 1 107 138 1 25 158 78 4 10 178 202 315...
result:
ok 1000 numbers
Test #5:
score: 11
Accepted
time: 210ms
memory: 11576kb
input:
1000 1000 461 631 908 871 9 818 589 859 917 846 415 498 899 405 954 857 192 327 912 204 453 253 923 705 211 775 882 41 956 634 761 588 676 694 428 729 575 747 216 979 10 472 471 422 580 944 295 121 62 603 376 520 720 864 792 22 891 773 77 520 801 680 590 70 617 807 784 136 566 699 167 726 75 701 780...
output:
95 177 33 70 54 125 39 35 0 263 17 65 55 46 144 73 55 152 58 145 194 103 68 101 34 32 83 60 43 24 0 1 23 46 51 114 57 27 116 38 86 70 53 52 147 129 136 46 41 14 137 154 161 159 205 6 149 157 21 24 7 50 81 9 15 53 13 134 225 36 32 3 104 44 73 139 35 24 191 94 61 128 399 29 103 2 31 34 23 116 9 91 158...
result:
ok 1000 numbers
Test #6:
score: 11
Accepted
time: 85ms
memory: 8772kb
input:
1000 1000 287 431 325 595 467 406 982 985 603 656 520 452 564 548 370 451 331 407 538 749 304 421 813 467 941 545 807 796 484 617 276 908 461 162 323 903 455 796 466 897 972 859 359 684 644 348 245 579 149 18 702 537 415 64 906 382 210 478 145 369 128 336 574 86 847 475 671 849 135 238 920 421 512 9...
output:
117 162 29 84 31 73 55 52 148 119 26 88 88 186 44 96 41 39 276 8 27 81 60 38 58 13 5 17 17 32 8 14 26 24 23 10 58 52 140 6 63 14 66 99 185 207 51 163 61 13 1 183 252 70 87 42 45 127 108 23 92 70 4 35 136 143 7 34 7 53 31 57 39 11 24 132 13 0 23 39 37 13 16 60 25 5 10 130 0 47 5 1 103 54 45 24 31 176...
result:
ok 1000 numbers
Test #7:
score: 11
Accepted
time: 47ms
memory: 7628kb
input:
1000 1000 744 630 563 578 427 227 792 774 559 165 201 450 227 905 966 321 29 908 183 636 603 275 148 739 196 551 884 685 487 276 139 646 352 52 96 246 212 295 502 968 1000 938 121 100 326 309 567 582 744 290 170 312 464 262 768 179 921 741 4 431 107 465 240 973 526 369 172 744 580 420 134 566 875 85...
output:
65 71 39 51 42 31 29 29 3 25 170 6 3 38 58 97 9 11 65 3 27 29 136 24 108 67 83 54 38 13 0 11 58 22 33 17 6 5 50 96 75 13 60 32 239 165 20 49 16 69 73 280 98 29 341 67 25 21 46 69 101 65 12 134 81 3 61 10 5 4 42 46 23 88 39 153 96 55 14 31 1 0 48 57 42 4 24 95 41 68 13 24 20 0 9 28 190 53 35 79 68 10...
result:
ok 1000 numbers
Test #8:
score: 11
Accepted
time: 22ms
memory: 10844kb
input:
1000 1000 566 649 769 706 200 912 838 31 319 791 685 53 463 151 803 239 656 355 5 739 729 295 655 349 403 776 187 277 256 792 488 796 953 925 702 255 740 794 33 801 137 591 875 101 842 815 969 713 701 295 202 360 44 959 494 684 80 425 270 272 155 700 287 994 921 12 214 831 963 307 167 946 980 782 81...
output:
23 429 186 23 122 29 32 75 247 7 60 82 114 17 113 19 17 47 151 25 45 16 162 28 74 17 114 2 17 12 8 0 24 50 102 19 83 10 18 145 1 23 228 277 54 0 124 9 59 114 11 61 5 93 105 29 160 47 97 3 17 23 26 15 27 51 9 6 40 46 97 14 29 20 91 62 19 26 6 46 26 0 10 91 33 37 104 218 61 53 2 172 8 167 50 32 17 2 2...
result:
ok 1000 numbers
Test #9:
score: 11
Accepted
time: 17ms
memory: 8676kb
input:
1000 1000 764 506 989 348 872 809 829 365 739 924 383 727 511 53 628 31 786 95 836 751 621 947 398 814 190 398 382 895 663 98 296 28 960 509 354 106 523 62 630 423 270 16 764 118 607 33 336 459 185 812 315 1 998 174 943 523 277 734 72 202 905 542 173 138 493 585 793 61 427 94 283 295 278 142 748 604...
output:
61 87 61 0 1 290 103 370 361 83 345 38 215 6 54 12 0 45 218 0 49 224 151 10 12 160 96 13 42 1 8 3 44 70 12 35 3 9 51 64 55 31 249 21 352 289 12 16 422 35 21 4 79 504 376 69 69 58 403 240 105 24 74 11 18 157 2 15 10 17 55 39 30 36 20 74 127 64 0 7 41 0 2 41 0 27 47 25 54 4 27 1 65 0 87 1 144 4 36 178...
result:
ok 1000 numbers
Test #10:
score: 11
Accepted
time: 9ms
memory: 10388kb
input:
1000 1000 522 493 442 21 785 730 493 508 750 600 670 661 422 716 820 417 819 974 83 859 382 748 63 262 960 384 999 579 653 955 870 496 136 473 764 117 523 224 160 254 868 45 200 609 442 512 449 7 826 534 765 36 65 834 740 543 138 245 230 722 287 791 649 497 960 953 194 362 124 759 719 347 443 126 31...
output:
8 40 22 207 39 15 508 147 21 19 454 25 318 68 171 21 157 0 80 18 8 209 29 13 73 12 73 91 0 3 9 6 6 0 0 0 27 0 2 4 130 79 182 59 9 205 26 4 4 307 73 106 147 110 4 39 4 7 113 1 0 57 331 4 69 67 4 181 83 23 32 20 246 14 37 28 6 356 4 213 10 7 50 21 56 2 4 1 4 6 7 7 13 59 176 11 2 20 13 6 24 66 67 117 1...
result:
ok 1000 numbers
Test #11:
score: 11
Accepted
time: 7ms
memory: 6412kb
input:
1000 1000 395 337 905 274 702 42 864 411 818 384 745 901 328 380 321 822 953 182 278 962 907 207 8 131 424 928 456 567 119 305 423 443 531 408 532 882 722 488 365 554 811 619 280 184 183 964 802 719 293 637 234 177 6 684 454 375 105 561 879 880 805 196 609 255 8 768 772 757 359 184 294 623 125 44 21...
output:
127 17 79 12 1 190 477 12 282 7 11 11 7 5 239 104 1 148 5 44 43 14 26 0 36 23 21 36 11 17 8 12 3 2 17 10 1 0 32 13 0 6 0 0 41 0 42 69 6 2 416 32 447 457 10 186 48 123 7 81 126 17 2 52 27 0 1 3 32 1 307 2 14 3 13 0 4 69 0 0 1 0 13 267 6 119 0 2 27 1 5 0 46 17 0 34 17 310 34 17 34 24 0 71 3 56 1 1 5 2...
result:
ok 1000 numbers
Test #12:
score: 11
Accepted
time: 6ms
memory: 6400kb
input:
1000 1000 797 184 557 796 605 213 135 636 713 129 518 178 641 192 850 415 492 26 472 129 586 505 121 348 631 948 725 403 988 937 277 20 738 69 441 207 523 337 809 550 433 66 389 9 380 86 774 525 238 687 277 28 914 538 695 188 588 736 536 135 491 143 568 448 960 209 152 569 707 586 129 25 389 425 93 ...
output:
68 11 0 171 458 11 161 62 62 6 0 0 6 92 0 0 16 81 0 0 12 5 3 61 91 15 120 61 68 1 1 1 23 1 1 14 121 45 147 0 19 259 273 127 198 2 3 24 60 0 5 40 413 158 435 161 66 11 68 68 28 109 76 1 6 19 2 9 8 266 89 0 1 10 5 0 1 6 59 31 268 13 61 205 0 0 157 97 0 0 72 10 9 0 17 52 1 46 44 0 172 24 56 55 383 2 44...
result:
ok 1000 numbers
Test #13:
score: 11
Accepted
time: 9ms
memory: 6500kb
input:
1000 1000 518 308 645 387 69 663 532 44 924 655 952 521 239 811 208 909 531 329 376 778 885 471 42 815 506 155 344 704 90 99 133 29 881 491 528 869 791 352 703 261 13 695 902 19 276 449 950 727 52 55 253 50 708 487 497 967 310 234 570 953 530 120 158 94 687 269 993 945 331 79 897 471 488 655 252 632...
output:
7 62 62 31 124 0 3 3 62 124 249 91 3 0 7 3 7 0 302 7 90 102 31 62 190 7 500 0 15 265 15 100 245 15 31 0 1 62 15 31 7 15 240 1 31 62 83 15 1 62 147 15 62 50 133 438 8 64 124 1 1 15 62 0 1 62 0 0 0 331 31 3 3 3 15 3 7 240 3 3 0 0 1 11 124 7 124 339 225 84 31 249 186 7 1 125 0 248 0 62 1 1 15 0 3 3 1 5...
result:
ok 1000 numbers
Subtask #2:
score: 0
Runtime Error
Dependency #1:
100%
Accepted
Test #14:
score: 0
Runtime Error
input:
10000 10000 9604 441 474 6593 7295 7622 5538 3949 131 5596 7590 2194 8222 6170 5424 6009 9447 9516 3302 3375 2030 4876 99 2757 3207 7323 7979 255 9612 1485 5970 2268 2899 6083 7319 7491 2335 1878 5123 5338 3470 80 895 6483 9950 4840 3548 8394 5702 8400 8010 1 3760 4207 8587 1193 1562 8196 9645 824 1...
output:
result:
Subtask #3:
score: 0
Runtime Error
Test #30:
score: 0
Runtime Error
input:
50000 200000 42574 43129 47328 17982 40521 6668 12729 32377 201 11940 8599 11734 18349 41045 26854 22540 9897 33419 7463 1243 47272 27135 49050 49111 22435 42539 39924 20272 5843 9308 45963 3283 31185 13692 38952 20583 15885 24802 4773 953 49907 28689 36942 23550 19449 8970 33340 31665 5407 46023 18...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Runtime Error
Test #67:
score: 0
Runtime Error
input:
100000 1000000 6457 23693 90928 23592 90440 75018 16865 3342 83718 16731 95103 31510 38719 27886 29093 41955 6596 46409 51839 10527 91993 61074 14405 34833 53674 42363 11490 43757 46191 6058 59164 96938 57858 40178 97523 84164 21582 72243 11267 47368 97058 6637 95208 60092 53943 16441 28363 64965 52...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%