QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#858013 | #9676. Ancestors | Zi_Gao | 0 | 1787ms | 59952kb | C++20 | 7.2kb | 2025-01-16 12:22:36 | 2025-01-16 12:22:36 |
Judging History
answer
#include<bits/stdc++.h>
using u32=unsigned int;
using i64=long long;
using u64=unsigned long long;
// #define ONLINE_JUDGE
#define INPUT_DATA_TYPE int
#define OUTPUT_DATA_TYPE int
inline __attribute((always_inline)) INPUT_DATA_TYPE read(){register INPUT_DATA_TYPE x=0;register char f=0,c=getchar();while(c<'0'||'9'<c)f=(c=='-'),c=getchar();while('0'<=c&&c<='9')x=(x<<3)+(x<<1)+(c&15),c=getchar();return f?-x:x;}void print(OUTPUT_DATA_TYPE x){if(x<0)x=-x,putchar('-');if(x>9)print(x/10);putchar(x%10^48);return;}
int par[100010],id[100010],dep[100010],siz[100010],hson[100010],lkt[100010],cntid;
std::vector<int> e[100010],curs[100010];
void dfs(int u){
curs[dep[u]=dep[par[u]]+1].push_back(u),siz[u]=1;
for(auto v:e[u]){
dfs(v),siz[u]+=siz[v];
if(siz[v]>siz[hson[u]]) hson[u]=v;
}
}
void dfsid(int u,int top){
id[u]=++cntid,lkt[u]=top;
if(hson[u]) dfsid(hson[u],top);
for(auto v:e[u]) if(v!=hson[u]) dfsid(v,v);
return;
}
int getlca(int u,int v){
while(lkt[u]!=lkt[v]){
if(dep[lkt[u]]<dep[lkt[v]]) std::swap(u,v);
u=par[lkt[u]];
}
if(dep[u]<dep[v]) std::swap(u,v);
return v;
}
const int INF=0x3f3f3f3f;
struct NODE{
int val;
int x,ly,ry,d;
};
std::vector<NODE> nods;
int n;
void ins(int Ll,int Lr,int Rl,int Rr,int val,int d){
nods.push_back({val,Ll,Rl,Rr,d});
nods.push_back({-val,Lr+1,Rl,Rr,d});
return;
}
namespace subtree{
int dd,rt;
std::vector<int> e[100010];
int siz[100010],id[100010],sid[100010],rid[100010],in[100010],cntid;
int hson[100010];//??!
std::set<int> st;
void dfs(int u){
siz[u]=1;
for(auto v:e[u]){
dfs(v),siz[u]+=siz[v];
if(siz[v]>siz[hson[u]]) hson[u]=v;
}
}
void clear(){
st.clear();
st.insert(0),st.insert(n+1);
}
void solve(int u,char keep){
id[u]=cntid+1;
if(in[u]) sid[++cntid]=u;
for(auto v:e[u]) if(v!=hson[u]) solve(v,0);
if(hson[u]){
int i,d=dd-dep[u],lat;
solve(hson[u],1);
for(auto v:e[u]) if(v!=hson[u]){
std::vector<std::pair<int,int>> now;
now.push_back({0,0});
auto it=--st.lower_bound(sid[id[v]]);
if(*it!=0) now.push_back({*(++st.begin()),*it});
std::sort(sid+id[v],sid+rid[v]+1);
lat=sid[id[v]];
for(i=id[v];i<rid[v];++i){
int u=sid[i],v=sid[i+1];
auto it=st.lower_bound(u);
if(it!=st.end()&&*it<v){
auto it2=st.lower_bound(v);
--it2;
now.push_back({lat,u});
now.push_back({*it,*it2});
lat=v;
}
}
now.push_back({lat,sid[rid[v]]});
it=st.lower_bound(sid[rid[v]]);
if(*it!=n+1) now.push_back({*it,*(++st.rbegin())});
now.push_back({n+1,n+1});
ins(1,n,1,n,-1,d);
// printf("sub d%d dd:%d :\n",d,dd);
for(i=1;i+1<now.size();++i){
int lx=now[i-1].second+1;
int rx=now[i].second;
int ly=now[i].first;
int ry=now[i+1].first-1;
if(i==1) lx=ly=1;
if(i+2==now.size()) rx=ry=n;
ins(lx,rx,ly,ry,1,d);
// printf("d:%d dd:%d (%d,%d) (%d,%d)\n",d,dd,lx,rx,ly,ry);
}
for(i=id[v];i<=rid[v];++i) st.insert(sid[i]);
}
}else st.insert(u);
if(!keep) clear();
rid[u]=cntid;
}
void solve(std::vector<int> cur){
int i;
for(auto u:cur) in[u]=1,dd=dep[u];
std::vector<int> tree(cur);
std::sort(tree.begin(),tree.end(),[](int a,int b){return ::id[a]<::id[b];});
int d=tree.size();
for(i=1;i<d;++i) tree.push_back(getlca(tree[i-1],tree[i]));
std::sort(tree.begin(),tree.end(),[](int a,int b){return ::id[a]<::id[b];});
tree.erase(std::unique(tree.begin(),tree.end()),tree.end());
for(i=1;i<tree.size();++i) e[getlca(tree[i-1],tree[i])].push_back(tree[i]);
rt=tree[0];
// printf("\n\n%d:",rt);
// printf("\ncurs:");
// for(auto u:cur) printf("%d ",u);
// printf("\ntree:");
// for(auto u:tree) printf("%d ",u);
// printf("\nedges:\n");
// for(auto u:tree) for(auto v:e[u]) printf("%d %d\n",u,v);
clear();
dfs(rt);
solve(rt,1);
int l=*++st.begin();
int r=*++st.rbegin();
ins(1,r,l,n,-1,dd);
for(auto u:tree){
e[u].clear();
hson[u]=in[u]=0;
}
cntid=0;
}
}
struct BIT{
int tree[100010];
void add(int pos,int x){
while(pos<=n) tree[pos]+=x,pos+=lowbit(pos);
}
int pre(int pos){
int res=0;
while(pos) res+=tree[pos],pos-=lowbit(pos);
return res;
}
void clear(int pos){
while(pos<=n) tree[pos]=0,pos+=lowbit(pos);
}
int lowbit(int x){return x&(-x);}
}bit;
int res[1000010];//!!?
void solve(int l,int r){
if(l==r) return;
int mid=(l+r)>>1,i,j;
std::sort(nods.begin()+l,nods.begin()+r+1,[](NODE a,NODE b){
if(a.x==b.x) return a.val<b.val;
return a.x<b.x;
});
solve(l,mid),solve(mid+1,r);
std::sort(nods.begin()+l,nods.begin()+mid+1,[](NODE a,NODE b){
return a.d<b.d;//!!?
});
std::sort(nods.begin()+mid+1,nods.begin()+r+1,[](NODE a,NODE b){
return a.d<b.d;//!!?
});
// printf("\nsolve %d %d %d:\n",l,mid,r);
for(i=mid+1,j=l;i<=r;++i){
while(j<=mid&&nods[j].d<=nods[i].d){
if(nods[j].val!=INF){
bit.add(nods[j].ly,nods[j].val);
bit.add(nods[j].ry+1,-nods[j].val);
// printf("add id:%d d:%d x:%d ly:%d ry:%d val:%d\n",j,nods[j].d,nods[j].x,nods[j].ly,nods[j].ry,nods[j].val);
}
++j;
}
if(nods[i].val==INF){
res[nods[i].ry]+=bit.pre(nods[i].ly);
// printf("qry id:%d d:%d x:%d y:%d val:%d\n",nods[i].ry,nods[i].d,nods[i].x,nods[i].ly,bit.pre(nods[i].ly));
}
}
for(i=l;i<j;++i) if(nods[i].val!=INF) bit.clear(nods[i].ly),bit.clear(nods[i].ry+1);
}
int main(){
int i,u,v,rt,d;
n=read();
int m=read();
for(i=1;i<=n;++i){
if(par[i]=read()) e[par[i]].push_back(i);//,printf("%d %d\n",par[i],i);
else rt=i;
ins(1,i,i,n,1,0);
}
for(i=0;i<m;++i){
int l=read();
int r=read();
int x=read();
nods.push_back({INF,l,r,i,x});
}
dfs(rt),dfsid(rt,rt);
// for(u=1;u<=n;++u) for(v=u;v<=n;++v) printf("%d %d %d\n",u,v,getlca(u,v));
for(d=1;d<=n;++d) if(curs[d].empty()) break; else subtree::solve(curs[d]);
solve(0,nods.size()-1);
for(i=0;i<m;++i) print(res[i]),putchar('\n');
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: 1ms
memory: 7756kb
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: 3ms
memory: 8144kb
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: 8144kb
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 -31 453 196 314 89 -321 111 171 353 51 -64 289 75 -55 15 402 -24 132 232 206 218 2...
result:
wrong answer 63rd numbers differ - expected: '61', found: '-31'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 17
Accepted
time: 393ms
memory: 27668kb
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
Wrong Answer
time: 450ms
memory: 28540kb
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:
14606 766 20003 9866 13393 15046 12175 16064 14351 32598 8598 4328 20759 18242 14279 16634 19676 27649 22510 18209 10566 6103 7184 10324 11352 25597 17392 26556 19067 28802 18806 8556 16176 19224 25066 21928 3542 3540 2793 21786 15621 831 18062 6304 5590 24262 14451 14273 10795 9579 10524 6779 16154...
result:
wrong answer 895th numbers differ - expected: '1896', found: '-5157'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #67:
score: 17
Accepted
time: 1529ms
memory: 59952kb
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:
ok 1000000 numbers
Test #68:
score: 17
Accepted
time: 1648ms
memory: 59596kb
input:
100000 1000000 82160 95864 48267 17482 19568 35077 14202 20440 4649 64145 148 2227 6969 39096 36508 20991 67700 90300 69215 57284 18492 9246 9629 7538 7845 30368 55600 48445 18542 41242 45052 25380 20894 91677 77040 73134 15572 21966 25343 14501 16227 23870 39207 50024 30787 11148 16884 63700 33205 ...
output:
44469 16565 2546 424 43423 23219 20745 3045 35596 11249 3763 322 27634 52470 20391 28546 14538 26343 9739 15234 20199 4332 1072 43633 3512 1432 47918 11347 4284 9054 6824 9773 39448 19949 18592 16534 43419 3303 11308 51071 1613 30619 8351 15408 6155 28590 13050 10085 2851 15353 33854 11870 5231 3742...
result:
ok 1000000 numbers
Test #69:
score: 0
Wrong Answer
time: 1787ms
memory: 58264kb
input:
100000 1000000 25194 62545 57529 6618 90347 4663 56805 81144 16504 79307 43199 91078 67805 33396 46754 26525 75274 39333 20904 40286 76299 83579 79770 37838 8666 33691 40228 71445 16473 53091 68958 32037 72079 38640 89820 67042 68888 34997 65595 87669 425 95432 78811 30382 34738 51750 96653 87265 96...
output:
8298 8670 11182 18170 35931 18149 12691 15302 20403 3285 1446 13437 3743 8790 18549 6217 6605 15624 15078 27050 8624 2834 -123 9655 18308 3309 17955 3482 11440 6424 6725 26079 14198 4974 9324 11366 12314 2246 2004 15608 33944 14841 30678 4905 21318 4848 2372 14158 13120 5353 8440 20694 32446 21952 1...
result:
wrong answer 1st numbers differ - expected: '8417', found: '8298'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%