QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#860744 | #9676. Ancestors | tebiezhichu | 0 | 851ms | 87072kb | C++14 | 3.1kb | 2025-01-18 14:47:52 | 2025-01-18 14:50:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int n,m,fa[maxn],rt,dep[maxn],top,pre[maxn],x,y,z,C[maxn],ans[maxn];
struct edge{
int x;
int y;
int z;
int zmax;
int op;
int ans;
}st[maxn*100];
vector<int>tu[maxn];
set<pair<int,int> >s[maxn];
inline void add(int q,int w){
if(!pre[q]||dep[q]==dep[w]){
return;
}
top++;
st[top].x=pre[q];
st[top].y=q;
st[top].z=dep[q]-dep[w];
st[top].op=0;
st[top].ans=1;
return;
}
inline void del(int q,int w){
if(!pre[q]||dep[q]==dep[w]){
return;
}
top++;
st[top].x=pre[q];
st[top].y=q;
st[top].z=dep[q]-dep[w];
st[top].op=0;
st[top].ans=-1;
return;
}
inline void merge(int q,int w){
if(s[q].size()<s[w].size()){
swap(s[q],s[w]);
}
for(auto v:s[w]){
auto it=s[q].lower_bound(v);
if(it!=s[q].begin()){
it--;
if((*it).first==v.first){
del(v.second,q);
pre[v.second]=(*it).second;
add(v.second,q);
}
it++;
}
it=s[q].upper_bound(v);
if(it!=s[q].end()&&(*it).first==v.first){
del((*it).second,q);
pre[(*it).second]=v.second;
add((*it).second,q);
}
s[q].insert(v);
}
return;
}
inline void dfs(int q,int w){
dep[q]=dep[w]+1;
s[q].insert({dep[q],q});
add(q,q);
for(int i=0;i<tu[q].size();i++){
dfs(tu[q][i],q);
merge(q,tu[q][i]);
}
return;
}
inline int cp(edge q,edge w){
return q.op>w.op;
}
struct node{
inline int lowbit(int q){
return q&(-q);
}
inline void add(int q,int w){
for(int i=q;i<=n;i+=lowbit(i)){
C[i]+=w;
}
return;
}
inline int query(int q){
int he=0;
for(int i=q;i;i-=lowbit(i)){
he+=C[i];
}
return he;
}
}tree;
inline int cmp(edge q,edge w){
if(q.y!=w.y){
return q.y<w.y;
}
return q.op<w.op;
}
inline int cm(edge q,edge w){
if(q.x!=w.x){
return q.x>w.x;
}
return q.op<w.op;
}
inline void cdq(int l,int r){
if(l>=r){
return;
}
int mid=(l+r)/2;
cdq(l,mid);
cdq(mid+1,r);
int zl=l,zr=mid+1;
while(zl!=mid+1||zr!=r+1){
if(zr==r+1||(zl!=mid+1&&st[zl].y<=st[zr].y)){
if(st[zl].op==0){
tree.add(st[zl].z,st[zl].ans);
tree.add(st[zl].zmax,-st[zl].ans);
}
zl++;
}
else{
if(st[zr].op!=0){
st[zr].ans+=tree.query(st[zr].z);
}
zr++;
}
}
for(int i=l;i<=mid;i++){
if(st[i].op==0){
tree.add(st[i].z,-st[i].ans);
}
}
inplace_merge(st+l,st+mid+1,st+r+1,cmp);
return;
}
signed main(){
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{
tu[fa[i]].push_back(i);
}
}
dfs(rt,0);
for(int i=1;i<=top;i++){
st[i].zmax=dep[st[i].y];
}
for(int i=1;i<=n;i++){
top++;
st[top].x=i;
st[top].y=i;
st[top].z=dep[i];
st[top].zmax=n+1;
st[top].op=0;
st[top].ans=1;
}
for(int i=1;i<=m;i++){
cin>>x>>y>>z;
top++;
st[top].x=x;
st[top].y=y;
st[top].z=z;
st[top].op=i;
}
sort(st+1,st+1+top,cm);
cdq(1,top);
sort(st+1,st+1+top,cp);
for(int i=m;i>=1;i--){
cout<<st[i].y-st[i].x+1-st[i].ans<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 23276kb
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 2
result:
wrong answer 5th numbers differ - expected: '1', found: '2'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 17
Accepted
time: 203ms
memory: 38724kb
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: 221ms
memory: 41080kb
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:
776234 1436968 451967 1226382 815133 749837 857436 837828 767495 45137 978189 1025169 410589 548573 684677 843707 584876 226110 383829 477714 788642 1128982 800669 952073 752866 265580 490867 247758 530872 148258 388906 974576 676336 410062 287718 399723 955671 1152304 875595 430923 618902 924907 47...
result:
wrong answer 1st numbers differ - expected: '14606', found: '776234'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #67:
score: 17
Accepted
time: 817ms
memory: 85200kb
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: 0
Wrong Answer
time: 851ms
memory: 87072kb
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:
812208 1266751 2201151 739445 261472 1517614 1518518 2103957 502980 1816548 2127891 2017429 1253517 383246 1407574 1116729 335544 788712 1671901 2067807 1504537 1193556 2393433 741726 2228418 2661703 267209 2245115 1422029 1608109 970326 1732903 588973 1177095 1161159 1579267 817011 1903257 1803753 ...
result:
wrong answer 1st numbers differ - expected: '44469', found: '812208'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%