QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#860891#9676. Ancestorsnalemy0 4ms10184kbC++202.1kb2025-01-18 15:27:322025-01-18 15:28:11

Judging History

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

  • [2025-01-18 15:28:11]
  • 评测
  • 测评结果:0
  • 用时:4ms
  • 内存:10184kb
  • [2025-01-18 15:27:32]
  • 提交

answer

#include<algorithm>
#include<iostream>
#include<vector>
#include<set>

const int N = 1e5;
int n, m, rt, tt, pr[N], ans[N], ft[N+1];
std::vector<int>g[N];
std::vector<std::pair<int, int>>rg[N];
std::set<int>p[N];
struct el { int i, a, b, c, k; } a[N*20];
void ad(int i, int k) {
	for (i+=2; i<=n; i+=i&-i) ft[i] += k;
}
int qr(int i) { int k = 0;
	for (i+=2; i; i-=i&-i) k += ft[i];
	return k;
}
std::vector<int>dfs(int u) {
	std::vector<int>s, t;
	for (int v : g[u]) {
		if (t = dfs(v), s.size() < t.size()) s.swap(t);
		for (int i=0; i<t.size(); i++)
			rg[t.size()-i].push_back({s[i], t[i]});
	}
	return s.push_back(u), std::move(s);
}
void inline mg(int i, std::set<int>&s, std::set<int>&t) {
	auto md = [&](int x, int y) {
		a[tt++] = {-1, i, x, pr[x], -1};
		a[tt++] = {-1, i, x, pr[x]=y, 1};
	};
	for (int x : t) {
		auto it = s.lower_bound(x);
		if (it != s.end()) md(*it, x);
		if (it != s.begin()) md(x, *std::prev(it));
		s.insert(it, x);
	}
}
void sv(int l, int r) {
	if (r - l == 1) return;
	int m = l + r >> 1, i, j;
	for (sv(l, m), sv(m, r), i=m, j=l; i<r; i++) {
		for (; j<m && a[j].b<=a[i].b; j++)
			if (!~a[j].i) ad(a[j].c, a[j].k);
		if (~a[i].i) ans[a[i].i] += a[i].k*qr(a[i].c);
	}
	while (j-- > l) if (!~a[j].i) ad(a[j].c, -a[j].k);
	std::inplace_merge(a+l, a+m, a+r, [](el x, el y){return x.b^y.b?x.b<y.b:x.i<y.i;});
}
int main() {
	std::cin >> n >> m;
	for (int i=0, x; i<n; a[tt++]={-1, 0, i, pr[i]=-1, 1}, p[i]={i}, i++)
		std::cin >> x, x ? g[x-1].push_back(i) : void(rt = i);
	auto s = dfs(rt);
	for (int i=1; i<s.size(); i++) {
		for (auto u : p[i-1]) a[tt++] = {-1, i, u, pr[u], -1};
		for (auto[u, v] : rg[i]) p[u].size() < p[v].size()
			? mg(i, p[v], p[u]), p[u].swap(p[v]) : mg(i, p[u], p[v]);
	}
	for (int i=0, l, r, x; i<m; i++) {
		std::cin >> l >> r >> x;
		a[tt++] = {i, x, l-2, l-2, -1}, a[tt++] = {i, x, r-1, l-2, 1};
		for (int j=0; j<tt; j++) if (!~a[j].i && a[j].a<=x && a[j].b>=l-1 && a[j].b<=r-1 && a[j].c<=l-2)
			ans[i] += a[j].k;
	}
	// std::sort(a, a+tt, [](el x, el y){return x.a^y.a?x.a<y.a:x.i<y.i;}), sv(0, tt);
	for (int i=0; i<m; i++) std::cout << ans[i] << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 11
Accepted
time: 0ms
memory: 9800kb

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: 10184kb

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
713
681
291
435
585
208
521
489
348
425
45
361
329
297
265
233
201
169
137
105
73
41
9
0
0
0
0
0
0
0
0
59
64
0
0
241
0
0
86
290
93
183
476
614
547
122
602
650
695
680
0
430
297
730
3
128
110
203
213
3
92
0
4
303
0
74
0
51
544
172
514
0
0
526
645
40
264
0
444
190
0
0
219...

result:

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

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #30:

score: 0
Time Limit Exceeded

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:


result:


Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Time Limit Exceeded

Test #67:

score: 0
Time Limit Exceeded

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:


result:


Subtask #6:

score: 0
Skipped

Dependency #1:

0%