QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#860741#9676. Ancestorstebiezhichu0 878ms85576kbC++143.1kb2025-01-18 14:46:002025-01-18 14:50:21

Judging History

你现在查看的是最新测评结果

  • [2025-01-18 14:50:21]
  • 评测
  • 测评结果:0
  • 用时:878ms
  • 内存:85576kb
  • [2025-01-18 14:46:00]
  • 提交

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++;
		}
		if(it!=s[q].end()){
			it++;
			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: 4ms
memory: 20464kb

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
2
3
3
3

result:

wrong answer 2nd 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: 215ms
memory: 38744kb

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: 222ms
memory: 37692kb

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:

405683
724260
271054
579602
463457
392674
484116
439391
475591
46654
452738
631407
317560
264322
358878
379189
286162
142301
249003
313018
405575
425543
457162
487841
388821
163749
319423
122046
284424
94391
287968
668116
327427
315842
162062
236137
635514
633838
546130
251994
269636
575715
282044
2...

result:

wrong answer 1st numbers differ - expected: '14606', found: '405683'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 17
Accepted
time: 863ms
memory: 84920kb

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: 878ms
memory: 85576kb

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:

606999
573770
984934
417078
160454
852795
696239
1158717
275630
996390
870102
1242343
709741
268752
788897
628557
214700
486922
755433
945533
841902
596967
1303567
369578
1215218
1318569
176495
939045
1101869
1096220
722496
1180385
410240
881447
837990
795214
441604
1176123
1000822
269136
1219665
73...

result:

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

Subtask #6:

score: 0
Skipped

Dependency #1:

0%