QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#857973 | #9676. Ancestors | Tx_Lcy | 0 | 4ms | 11928kb | C++14 | 1.8kb | 2025-01-16 11:30:42 | 2025-01-16 11:30:43 |
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 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}),nxt[x]=*it;
if (it!=d[dep[x]].begin()) add.push_back((node){*prev(it),x,g}),nxt[*prev(it)]=x;
if (it!=d[dep[x]].begin() && it!=d[dep[x]].end()) del.push_back((node){*prev(it),*it,g});
vec.push_back(x);
for (auto v:a[x]) Add(v);
}
inline void dfs2(int x){
for (auto v:a[x]) if (v^heavy[x]){
dfs2(v);
for (int D=dep[v];d[D].size();++D) d[D].clear();
}
if (heavy[x]) dfs2(heavy[x]);
out=dep[x];
for (auto v:a[x]) if (v^heavy[x]){
vec.clear(),Add(v);
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);
rep(i,1,n) if (nxt[i]) del.push_back((node){i,nxt[i],dep[i]+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: 11928kb
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: 11524kb
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: 4ms
memory: 11200kb
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: 0
Wrong Answer
time: 4ms
memory: 11772kb
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:
329 323 70 91 375 6 162 19 96 138 338 157 295 318 94 110 230 108 221 36 40 42 84 173 114 25 82 81 56 14 1 2 41 61 11 125 120 48 123 150 98 121 21 -7 99 25 166 41 174 80 260 262 140 61 22 309 140 31 -21 266 127 178 51 146 27 18 100 83 140 221 68 198 331 195 19 -1 105 138 1 25 155 76 4 10 177 199 307 ...
result:
wrong answer 1st numbers differ - expected: '347', found: '329'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #30:
score: 0
Time Limit Exceeded
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:
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%