QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#857968 | #9676. Ancestors | Tx_Lcy | 0 | 1851ms | 22168kb | C++14 | 1.8kb | 2025-01-16 11:18:07 | 2025-01-16 11:18:14 |
Judging History
answer
//A tree without skin will surely die.
//A man without face will be alive.
#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i=j;i<=k;++i)
#define per(i,j,k) for(int i=j;i>=k;--i)
int const N=1e5+10;
int n,m,root,out,siz[N],nxt[N],dep[N],heavy[N];
struct node{int l,r,dep;};
vector<node>add,del;vector<int>a[N];set<int>d[N];
inline void dfs1(int x){
siz[x]=1;
for (auto v:a[x]){
dep[v]=dep[x]+1,dfs1(v),siz[x]+=siz[v];
if (siz[v]>siz[heavy[x]]) heavy[x]=v;
}
}
vector<int>vec;
inline void Add(int x,int las){
int g=dep[x]-out;
auto it=d[dep[x]].upper_bound(x);
if (it!=d[dep[x]].end() && nxt[x]>(*it)) add.push_back((node){x,(*it),g}),del.push_back((node){x,nxt[x],g});
if (it!=d[dep[x]].begin()) add.push_back((node){(*prev(it)),x,g});
vec.push_back(x);
for (auto v:a[x]) Add(v,las);
}
inline void dfs2(int x,int las){
if (a[x].size()>1) las=1;
else ++las;
for (auto v:a[x]) if (v^heavy[x]){
dfs2(v,las);
for (int D=dep[v];d[D].size();++D){
int las=0;
for (auto it:d[D]) nxt[it]=1e9;
for (auto it:d[D]) nxt[las]=it,las=it;
d[D].clear();
}
}
if (heavy[x]) dfs2(heavy[x],las);
out=dep[x];
for (auto v:a[x]) if (v^heavy[x]){
vec.clear(),Add(v,las);
for (auto x:vec) d[dep[x]].insert(x);
}
d[dep[x]].insert(x);
}
inline void solve(){
cin>>n>>m;
rep(i,1,n){
int p;cin>>p;
if (p) a[p].push_back(i);
else root=i;
}
memset(nxt,0x3f,sizeof(nxt));
dfs1(root),dfs2(root,1);
while (m--){
int l,r,x;cin>>l>>r>>x;
int res=0;
rep(i,l,r) res+=dep[i]>=x;
for (auto it:add) if (l<=it.l && it.r<=r && it.dep<=x) --res;
for (auto it:del) if (l<=it.l && it.r<=r && it.dep<=x) ++res;
cout<<res<<'\n';
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t=1;
// cin>>t;
while (t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 11
Accepted
time: 0ms
memory: 11916kb
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: 0ms
memory: 12208kb
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: 0
Wrong Answer
time: 2ms
memory: 11196kb
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:
344 245 163 504 38 307 289 -148 392 -119 461 -119 269 197 194 -33 46 203 12 121 162 69 91 67 44 101 57 23 61 32 7 25 54 25 57 10 41 30 54 236 59 67 192 337 131 -24 368 213 305 50 419 -30 -163 501 93 -39 141 139 -70 392 -136 117 39 467 204 298 144 0 -15 215 309 159 76 322 147 98 -23 398 46 130 280 27...
result:
wrong answer 1st numbers differ - expected: '441', found: '344'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #30:
score: 17
Accepted
time: 1851ms
memory: 22168kb
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:
12045 1321 12292 2444 32443 36534 38575 30505 16464 16648 47153 41144 23401 1762 18567 6831 26486 29716 8905 16295 38569 29990 22061 7638 3820 34754 17582 6466 12632 23384 11657 16493 24434 11503 3945 2205 37806 13036 29052 24114 39158 34702 37326 20815 11766 41253 44355 17807 23452 26717 43107 4133...
result:
ok 200000 numbers
Test #31:
score: 0
Time Limit Exceeded
input:
50000 200000 13611 7267 47234 19765 39458 30493 19935 34465 46369 11995 35207 5752 13718 47418 12913 13725 27694 47726 25253 30406 27587 25790 21091 12008 13534 8804 26484 34270 33810 49102 25288 37607 11863 23340 22747 40502 4461 24803 31317 25617 46077 6504 46299 34533 6829 17841 2812 35413 16394 ...
output:
5170 -15383 13398 -1701 3554 6061 1857 7661 5180 32442 -3127 -9334 14916 11194 5382 8886 13473 25278 17730 11427 300 -6085 -4456 100 1628 22466 10514 23958 13072 27259 12765 -1988 8997 13493 21960 17461 -8877 -8805 -9843 17389 8546 -12497 12173 -4484 -5431 21114 7164 6964 2105 446 1820 -3367 9850 27...
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Time Limit Exceeded
Test #67:
score: 0
Time Limit Exceeded
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:
52956 18767 1319 13405 11021 455 50595 81481 6813 3640 58937 10991 70 4713 36 9517 39731 1166 67346 74637 2667 45182 4914 6774 1625 4198 52270 30435 60137 48654 29768 2815 6846 73091 21944 49693 9923 46795 29787 6866 2435 20773 2959 34666 4377 2428 4582 7527 38292 7253 3586 63817 28075 43828 20215 1...
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%