QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#875353 | #9676. Ancestors | dingdingtang11514 | 0 | 1193ms | 149864kb | C++14 | 5.7kb | 2025-01-29 16:33:44 | 2025-01-29 16:33:44 |
Judging History
answer
#pragma GCC optimize(2)
#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 fastread {
const unsigned int S=1<<18;
char B[S+3],*H,*T;
inline unsigned int gc() {
if(H==T) T=(H=B)+fread(B,1,S,stdin);
return (H==T)?EOF:*H++;
}
inline unsigned int read() {
unsigned int x,ch;
while((ch=gc())<48);
x=ch^'0';
while((ch=gc())>47) x=x*10+(ch^'0');
return x;
}
} using fastread::read;
// 究极快写
namespace fastwrite {
const unsigned int S=1<<18;
unsigned int cnt;
char B[S+3];
inline void write(unsigned int x) {
if(x>9) write(x/10);
B[cnt++]=(x%10)|48;
if(cnt == S){
fwrite(B,1,S,stdout);
cnt=0;
}
}
inline void space() {
B[cnt++]=32;
if(cnt == S){
fwrite(B,1,S,stdout);
cnt=0;
}
}
inline void endl() {
B[cnt++]=10;
if(cnt == S){
fwrite(B,1,S,stdout);
cnt=0;
}
}
inline void show() {
fwrite(B,1,cnt,stdout);
cnt=0;
}
}using fastwrite::write, fastwrite::space, fastwrite::show;
namespace Mine {
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],tmp[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);
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);
// For(i,0,n) cout<<bit::ask(i)<<" ";
// cout<<endl;
// For(i,mid+1,qr) if(q[i].id){
// int res=0;
// For(j,ql,mid) {
// if(q[j].id) {
// if(q[j].d>q[i].d && q[j].l>q[i].l)
// }
// }
// }
int i=ql,j=mid+1,tt=0;
while(i<=mid && j<=qr) {
if((i<=mid)&&((q[i].l>=q[j].l)||(j>qr))) {
if(!q[i].id) bit::mdf(q[i].d,q[i].t);
tmp[++tt]=q[i];
i++;
} else {
if(q[j].id) ans[q[j].id]+=bit::ask(q[j].d);
tmp[++tt]=q[j];
j++;
}
}
For(i,ql,mid) bit::mdf(q[i].d,-q[i].t);
For(i,1,tt) q[ql+i-1]=tmp[i];
// sort(q+ql,q+qr+1,[](node x,node y){return x.l>y.l;});
}
}
int ask(int t,int x,int col) {
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(gg,x,col);
// cout<<gg<<" "<<x<<" "<<col<<" "<<sgt::ask(dfn[fa[tt]],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();
if(fa[i]==0) root=i;
else G[fa[i]].pb(i);
}
T::build();
sgt::build();
For(i,1,n) mdf(i);
int tt=0;
sort(q+1,q+1+tot,[](node a,node b) {return a.l!=b.l?a.l>b.l:a.r!=b.r?a.r>b.r:a.d>b.d;});
int cnt=0;
For(i,1,tot) {
cnt+=q[i].t;
if(q[i].l!=q[i+1].l || q[i].r!=q[i+1].r || q[i].d!=q[i+1].d) {
q[++tt]=q[i]; q[tt].t=cnt; cnt=0;
}
}
tot=tt;
For(i,1,m) {
int l=read(),r=-read(),d=read();
q[++tot]={l,r,d,0,i};
}
sort(q+1,q+1+tot,[](node a,node b) {return a.r!=b.r?a.r>b.r:a.l!=b.l?a.l>b.l:a.d!=b.d?a.d>b.d:a.id<b.id;});
cdq::solve(1,tot);
For(i,1,m) {
// cout<<ans[i]<<"\n";
write(ans[i]);
fastwrite::endl();
}
show();
}
}
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: 0
Wrong Answer
time: 2ms
memory: 32336kb
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:
0 4294967292 0 1 4294967295
result:
wrong answer 1st numbers differ - expected: '2', found: '0'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #30:
score: 0
Wrong Answer
time: 425ms
memory: 82404kb
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:
0 0 0 0 0 0 0 0 0 0 4294816419 4294834716 4294892322 4294962047 4294907850 4294944634 4294880812 4294870814 4294937959 4294914712 4294839724 4294868927 4294854881 4294928108 4294949552 4294789817 4294816035 4294910735 4294858858 4294768051 4294867242 4294824148 4294757343 4294867114 4294934695 42949...
result:
wrong answer 1st numbers differ - expected: '12045', found: '0'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #67:
score: 0
Wrong Answer
time: 1193ms
memory: 149864kb
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:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 1st numbers differ - expected: '52956', found: '0'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%