QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#875260#9676. Ancestorsdingdingtang115140 1160ms127760kbC++144.1kb2025-01-29 14:18:592025-01-29 14:19:00

Judging History

This is the latest submission verdict.

  • [2025-01-29 14:19:00]
  • Judged
  • Verdict: 0
  • Time: 1160ms
  • Memory: 127760kb
  • [2025-01-29 14:18:59]
  • Submitted

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);
		}
	}
	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,-1};}
	// void add(int l,int r,int dl,int dr,int add) {
		// ins(l,-r,dr,add);
		// ins(l,-r,dl-1,-add);
	// }
	// namespace cdq {
// 		
	// }
	namespace bit {
		int tr[N];
		void add(int pos,int val) {for(pos++;pos<N;pos+=pos&-pos) tr[pos]+=val;}
		int ask(int pos) {
			int ret=0; for(pos++;pos;pos&=pos-1) ret+=tr[pos];
			return ret;
		}
		void add(int ql,int qr,int val) {add(ql,val),add(qr+1,-val);}
	};
	int ask(int x) {
		int t=x; Rof(i,len,0) if(f[x][i] && !sgt::ask(dfn[f[x][i]],t)) x=f[x][i];
		// cout<<x<<"  "<<t<<" "<<sgt::ask(dfn[f[x][0]],t)<<endl;
		return dep[t]-dep[x];
	}
	void mdf(int x) {
		int t=ask(x);
		// cout<<x<<" "<<t<<endl;//" "<<sgt::ask(dfn[f[x][1]],x)<<endl;
		bit::add(0,t,1);
		while(x) {
			sgt::mdf(dfn[top[x]],dfn[x],x);
			x=fa[top[x]];
		}
	}
	// vector<pii> vec[N];
	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();
		int tot=0;
		For(i,1,m) {
			int l=read(),r=read(),d=read();
			// vec[r].pb((pii){d,i});
			q[++tot]={l,r,d,0,i};
		}
		sort(q+1,q+1+m,[](node x,node y) {return x.r<y.r;});
		int j=1;
		For(i,1,n) {
			mdf(i);
			while(j<=m && q[j].r==i) {
				ans[q[j].id]=bit::ask(q[j].d);
				j++;
			}
			// for(auto j:vec[i]) {
				// ans[j.y]=bit::ask(j.x);
			// }
		}
		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: 1ms
memory: 32392kb

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: 5ms
memory: 33092kb

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:

467
72
672
143
391
602
463
35
781
855
291
435
985
208
936
593
348
678
45
758
474
679
584
624
737
747
270
642
968
731
408
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
378
460
604
356
405
279
223
63
17
335
76
289
50
274
584
325
633
7...

result:

wrong answer 1st numbers differ - expected: '452', found: '467'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #30:

score: 0
Wrong Answer
time: 364ms
memory: 71612kb

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:

12072
1327
12370
2475
32809
37028
39185
31040
16761
16995
48336
42266
24077
1825
19174
7069
27509
30915
9298
17017
40463
31502
23214
8070
4021
36889
18667
6870
13467
25014
12480
17705
26332
12391
4251
2389
41200
14194
31780
26412
43116
38273
41276
23016
13035
45955
49523
19874
26233
30012
48616
4673...

result:

wrong answer 1st numbers differ - expected: '12045', found: '12072'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 17
Accepted
time: 1015ms
memory: 127760kb

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: 1160ms
memory: 112472kb

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:

45407
16335
2509
422
43011
23674
20595
3024
35168
11086
3708
321
27647
52745
20234
28376
14430
25992
9582
15175
20133
4315
1055
43518
3453
1406
47559
11166
4222
8916
6777
9837
39134
19737
18354
16391
43787
3242
11201
51122
1570
30831
8362
15231
6059
29210
12990
10013
2817
15248
33385
11870
5204
3712...

result:

wrong answer 1st numbers differ - expected: '44469', found: '45407'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%