QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#857960 | #9676. Ancestors | Tx_Lcy | 0 | 1844ms | 22440kb | C++14 | 1.7kb | 2025-01-16 11:10:49 | 2025-01-16 11:10:51 |
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],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())
add.push_back((node){x,(*it),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 clear(int x){
d[dep[x]].clear();
for (auto v:a[x]) clear(v);
}
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),clear(v);
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;
}
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;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 11
Accepted
time: 0ms
memory: 11748kb
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: 1ms
memory: 11072kb
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: 1ms
memory: 10724kb
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:
659 540 442 763 277 529 489 39 560 38 603 11 387 298 279 44 114 259 58 162 199 103 118 91 63 114 67 27 61 32 7 25 54 27 62 18 53 45 73 261 93 110 242 395 197 52 454 312 413 168 553 122 6 682 292 174 370 386 193 674 168 448 68 579 282 393 166 0 168 267 449 190 80 395 197 105 92 533 50 142 322 325 339...
result:
wrong answer 1st numbers differ - expected: '441', found: '659'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #30:
score: 17
Accepted
time: 1844ms
memory: 22440kb
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:
21789 1162 29871 14693 19869 22306 18025 23758 21202 48393 12749 6470 30660 26868 20976 24413 28926 40652 33036 26660 15457 8995 10556 15045 16498 37264 25249 38622 27667 41789 27223 12392 23312 27732 36131 31565 5160 5150 4047 31217 22304 1193 25795 9072 8058 34529 20516 20238 15315 13591 14905 965...
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%