QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#875293 | #9676. Ancestors | dingdingtang11514 | 0 | 1423ms | 129892kb | C++14 | 4.8kb | 2025-01-29 15:06:37 | 2025-01-29 15:06:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Rof(i,a,b) for(int i=(a);i>=(b);i--)
#define ll long long
#define ull unsigned ll
#define vi vector<int>
#define pb push_back
#define SZ(x) ((int)x.size())
#define BG(x) (x.begin())
#define ED(x) (x.end())
#define pii pair<int,int>
#define x first
#define y second
#define MD(x) ((x)%=mod)
#define gmax(x,y) (x=max(x,y))
#define gmin(x,y) (x=min(x,y))
#define ppct __builtin_popcount
#define mkp make_pair
namespace Mine {
#define gc() getchar()
ll read() {
ll x=0;bool f=0;char ch=gc();
while(!isdigit(ch)) {if(ch=='-') f=1;ch=gc();}
while(isdigit(ch)) x=x*10+(ch^48),ch=gc();
return f?-x:x;
}
const int N=1e5+100,M=1e6+100+2e6,Q=1e6+10,len=18;
int n,m,ans[Q],fa[N],f[N][19],root;
vi G[N];
int top[N],dep[N],son[N],siz[N],dfn[N],rk[N],R[N];
namespace T {
void dfs1(int u) {
f[u][0]=fa[u];
For(i,1,len) f[u][i]=f[f[u][i-1]][i-1];
siz[u]=1,dep[u]=dep[fa[u]]+1;
for(int to:G[u]) {
dfs1(to),siz[u]+=siz[to];
if(siz[to]>siz[son[u]]) son[u]=to;
}
}
int tot=0;
void dfs2(int u,int tt) {
top[u]=tt,dfn[u]=++tot,rk[dfn[u]]=u;
if(son[u]) dfs2(son[u],tt);
for(int to:G[u]) if(to!=son[u]) dfs2(to,to);
}
void build() {dfs1(root),dfs2(root,root);}
}
namespace sgt {
#define lc (u<<1)
#define rc (u<<1|1)
#define mid ((l+r)>>1)
set<pii> tr[N<<2];
void mdf(int ql,int qr,int val,int u=1,int l=1,int r=n) {
// For(i,ql,qr) tr[i].insert((pii){dep[val],val});
// return ;
if(ql<=l && r<=qr) {
tr[u].insert((pii){dep[val],val});
return ;
}
if(ql<=mid) mdf(ql,qr,val,lc,l,mid);
if(mid<qr) mdf(ql,qr,val,rc,mid+1,r);
}
int ask(int pos,int val,int u=1,int l=1,int r=n) {
// auto it=prev(tr[pos].upper_bound((pii){dep[val],1e9}));
// return (it->x)==dep[val];
int ret=0;
auto it=prev(tr[u].upper_bound((pii){dep[val],1e9}));
if((it->x)==dep[val]) ret=it->y;
if(l==r) return ret;
if(pos<=mid) gmax(ret,ask(pos,val,lc,l,mid));
else gmax(ret,ask(pos,val,rc,mid+1,r));
return ret;
}
void build(int u=1,int l=1,int r=n) {
tr[u].insert((pii){-1e9,-1e9}),tr[u].insert((pii){1e9,1e9});
if(l==r) return ;
build(lc,l,mid),build(rc,mid+1,r);
}
#undef mid
#undef lc
#undef rc
}
namespace bit {
int tr[N];
void mdf(int pos,int val) {for(pos++;pos;pos&=pos-1) tr[pos]+=val;}
int ask(int pos) {int ret=0; for(pos++;pos<N;pos+=pos&-pos) ret+=tr[pos];return ret;}
};
struct node {
int l,r,d,t,id;
} q[M];
int tot;
void ins(int l,int r,int d,int t) {q[++tot]={l,r,d,t,0};}
void add(int l,int r,int dl,int dr,int a) {
ins(l,-r,dr,a);
if(dl) ins(l,-r,dl-1,-a);
}
namespace cdq {
void solve(int ql,int qr) {
if(ql>=qr) return ;
int mid=(ql+qr)>>1;
solve(ql,mid),solve(mid+1,qr);
{
int i=ql,j=mid+1;
while(i<=mid && j<=qr) {
if(q[i].l>=q[j].l) {
if(!q[i].id) bit::mdf(q[i].d,q[i].t);
i++;
} else {
if(q[j].id) ans[q[j].id]+=bit::ask(q[j].d);
j++;
}
}
while(j<=qr) {if(q[j].id) ans[q[j].id]+=bit::ask(q[j].d); j++;}
}
{
int i=ql,j=mid+1;
while(i<=mid && j<=qr) {
if(q[i].l>=q[j].l) {
if(!q[i].id) bit::mdf(q[i].d,-q[i].t);
i++;
} else j++;
}
}
sort(q+ql,q+qr+1,[](node x,node y){return x.l>y.l;});
}
}
int ask(int x,int col) {
int t=x; Rof(i,len,0) if(f[x][i] && sgt::ask(dfn[f[x][i]],t)==col) x=f[x][i];
return x;
}
void mdf(int x) {
int gg=x,col=sgt::ask(dfn[x],x);
while(x) {
int tt=ask(x,col);
// cout<<gg<<" "<<x<<" "<<sgt::ask(dfn[x],gg)<<" "<<tt<<endl;
if(col) add(col,gg,dep[gg]-dep[x],dep[gg]-dep[tt],-1);
add(gg,gg,dep[gg]-dep[x],dep[gg]-dep[tt],1);
x=fa[tt];
col=sgt::ask(dfn[x],gg);
}
x=gg;
while(x) {
sgt::mdf(dfn[top[x]],dfn[x],gg);
x=fa[top[x]];
}
}
void main() {
n=read(),m=read();
For(i,1,n) {
fa[i]=read();
// cout<<fa[i]<<" "<<i<<endl;
if(fa[i]==0) root=i;
else G[fa[i]].pb(i);
}
T::build();
// For(i,1,n) cout<<dep[i]<<" ";
// cout<<endl;
sgt::build();
For(i,1,m) {
int l=read(),r=-read(),d=read();
// vec[r].pb((pii){d,i});
q[++tot]={l,r,d,0,i};
}
For(i,1,n) mdf(i);
sort(q+1,q+1+tot,[](node x,node y) {return x.r>y.r;});
// cout<<tot<<endl;
// For(i,1,tot) {
// cout<<q[i].l<<" "<<q[i].r<<" "<<q[i].d<<" "<<q[i].t<<" "<<" "<<q[i].id<<endl;
// }
cdq::solve(1,tot);
For(i,1,m) {
printf("%d\n",ans[i]);
}
}
}
signed main() {
// freopen("prefix.in","r",stdin);
// freopen("prefix.out","w",stdout);
Mine::main();
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 11
Accepted
time: 2ms
memory: 30548kb
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: 0
Wrong Answer
time: 4ms
memory: 33032kb
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:
451 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 56 10 9 16 87 161 19 217 232 24 177 333 103 139 293 400 299 351 529 632 592 296 640 678 715 708 52 465 322 731 2 69 110 286 171 0 40 31 16 105 75 45 8 94 540 115 357 7 11 431 580 37 ...
result:
wrong answer 1st numbers differ - expected: '452', found: '451'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 0
Wrong Answer
time: 525ms
memory: 76208kb
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:
wrong answer 448th numbers differ - expected: '97', found: '96'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #67:
score: 0
Wrong Answer
time: 1423ms
memory: 129892kb
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:
52955 18766 1319 13405 11021 455 50594 81481 6813 3640 58937 10991 70 4713 36 9517 39730 1165 67346 74637 2667 45181 4914 6774 1625 4198 52270 30435 60137 48653 29767 2815 6846 73090 21944 49692 9923 46794 29786 6866 2435 20773 2959 34666 4377 2428 4582 7527 38292 7253 3586 63817 28075 43827 20215 1...
result:
wrong answer 1st numbers differ - expected: '52956', found: '52955'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%