QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#858288#9676. Ancestorsc#0 227ms82472kbC++235.9kb2025-01-16 15:40:182025-01-16 15:40:27

Judging History

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

  • [2025-01-16 15:40:27]
  • 评测
  • 测评结果:0
  • 用时:227ms
  • 内存:82472kb
  • [2025-01-16 15:40:18]
  • 提交

answer

#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "\33[32m[" << __LINE__ << "]\33[m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T& x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T& x, T y) {return x = min(x, y);}
template <typename T> T& read(T &x) {
	x = 0; int f = 1; char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = - f;
	for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	return x *= f;
}
const int N = 1e5 + 10, M = 1e6 + 10, K = N * 100;
int n, m, fa[N], rt, ans[N], s[N], sz[N], son[N];
vector <int> v[N];
struct Q {
	int l, r, x, id;
} q[M], tmpq[M];
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(pair <int, int> x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(((uint64_t) x.first << 32 | x.second) + FIXED_RANDOM);
    }
};
void dfs(int x) {
	sz[x] = 1;
	for (int i: v[x]) {
		dfs(i);
		sz[x] += sz[i];
		if (sz[i] > sz[son[x]]) son[x] = i;
	}
}
vector <set <int>> vw[N];
vector <pair <pair <int, int>, int>> tt[N];
int k;
struct O {
	int l, r, x, v;
} o[K], tmpo[K];
void ins(int x, int l, int r, int v) {
	if (l <= r) tt[x].emplace_back(make_pair(l, r), v);
}
void dfs2(int x) {
	if (son[x]) {
		dfs2(son[x]);
		swap(vw[x], vw[son[x]]);
		for (int i: v[x]) if (i != son[x]) {
			dfs2(i);
			while (vw[x].size() < vw[i].size()) vw[x].insert(vw[x].begin(), set <int> {});
			F(j, 0, SZ(vw[i])) {
				int jj = SZ(vw[x]) - (SZ(vw[i]) - j), pos = vw[i].size() - j;
					// if (x == 5) {
					// 	debug << i << ' ' << vw[i][j].size() << endl;
					// }
				int lst = 0;
				for (int k: vw[i][j]) {
					auto it = vw[x][jj].insert(k).first, iit = next(it);
					int l = it == vw[x][jj].begin() ? 1 : * prev(it) + 1, r = iit == vw[x][jj].end() ? n : * iit - 1;
					// if () l = * prev(it) + 1;
					// else l = 1;
					// if (iit == vw[x][jj].end()) r = n;
					// else r = * iit - 1;
					ins(pos, l, r, - 1);
					ins(pos, l, k - 1, 1);
					ins(pos, k + 1, r, 1);
					ins(pos, lst + 1, k - 1, - 1);
					lst = k;
				}
				ins(pos, lst + 1, n, - 1);
			}
			vector <set <int>> ().swap(vw[i]);
		}
	}
	s[vw[x].size()]++;
	vw[x].push_back(set <int> {x});
	ins(1, 1, x - 1, 1);
	ins(1, x + 1, n, 1);
}
int info[N], tim[N], cur;
void mdf(int x, int y) {
	for (; x <= n; x += x & - x)
		if (tim[x] != cur) tim[x] = y, info[x] = y;
		else info[x] += y;
}
int qry(int x) {
	int s = 0;
	for (; x; x ^= x & - x)
		(tim[x] == cur) && (s += info[x]);
	return s;
}
void cdq(int l1, int r1, int l2, int r2, int l3, int r3) {
	if (l1 > r1 || l2 > r2 || l3 == r3) {
		if (l1 <= r1) {
			sort(q + l1, q + r1 + 1, [&] (Q x, Q y) {
				return x.r < y.r;
			});
		}
		if (l2 <= r2) {
			sort(o + l2, o + r2 + 1, [&] (O x, O y) {
				return x.r < y.r;
			});
		}
		return;
	}
	int mid = (l3 + r3) >> 1;
	F(i, l1, r1) tmpq[i] = q[i];
	// F(i, l2, r2) tmpo[i] = o[i];
	int s1 = l1 - 1;
	F(i, l1, r1)
		if (tmpq[i].x <= mid) q[++s1] = tmpq[i];
	int mid1 = s1;
	F(i, l1, r1)
		if (tmpq[i].x > mid) q[++s1] = tmpq[i];
	// int s2 = l2 - 1;
	int mid2 = l2 - 1;
	F(i, l2, r2)
		if (o[i].x <= mid) mid2++;
	// int mid2 = s2;
	// F(i, l2, r2)
	// 	if (tmpo[i].x > mid) o[++s2] = tmpo[i];
	cdq(l1, mid1, l2, mid2, l3, mid);
	cdq(mid1 + 1, r1, mid2 + 1, r2, mid + 1, r3);
	cur++;
	int pos = mid2;
	DF(i, r1, mid1 + 1) {
		while (pos >= l2 && q[i].r <= o[pos].r) mdf(o[pos].l, o[pos].v), pos--;
		// F(j, pos + 1, mid2) {
		// 	debug << o[j].l << " " << o[j].v << endl;
		// }
		// debug << qry(q[i].l) << " " << q[i].id << " " << pos << " " << mid2 << " " << l2 << endl;
		ans[q[i].id] -= qry(q[i].l);
	}
	{
		F(i, l1, r1) tmpq[i] = q[i];
		int p1 = l1, p2 = mid1 + 1, pp = l1;
		while (p1 <= mid1 || p2 <= r1) {
			if (p1 <= mid1 && (p2 > r1 || tmpq[p1].r < tmpq[p2].r)) q[pp++] = tmpq[p1++];
			else q[pp++] = tmpq[p2++];
		}
	}
	{
		F(i, l2, r2) tmpo[i] = o[i];
		int p1 = l2, p2 = mid2 + 1, pp = l2;
		while (p1 <= mid2 || p2 <= r2) {
			if (p1 <= mid2 && (p2 > r2 || tmpo[p1].r < tmpo[p2].r)) o[pp++] = tmpo[p1++];
			else o[pp++] = tmpo[p2++];
		}
	}
}
signed main() {
	read(n), read(m);
	F(i, 1, n)
		if (read(fa[i])) v[fa[i]].push_back(i);
		else rt = i;
	dfs(rt), dfs2(rt);
	F(j, 0, SZ(vw[rt])) {
		int pos = vw[rt].size() - j;
			// if (x == 5) {
			// 	debug << i << ' ' << vw[i][j].size() << endl;
			// }
		int lst = 0;
		for (int k: vw[rt][j]) {
			ins(pos, lst + 1, k - 1, - 1);
			lst = k;
		}
		ins(pos, lst + 1, n, - 1);
	}
	F(i, 1, n) {
		unordered_map <pair <int, int>, int, custom_hash> id;
		for (auto [a, b]: tt[i]) id[a] += b;
		// tt[i].clear();
		vector <pair <pair <int, int>, int>> ().swap(tt[i]);
		for (auto [a, b]: id)
			if (b) o[++k] = {a.first, a.second, i - 1, b};//, debug << i << " " << a.first << ' ' << a.second << ' ' << b << endl;
	}
	DF(i, n, 1) s[i] += s[i + 1];
	F(i, 1, m) read(q[i].l), read(q[i].r), ans[i] = s[read(q[i].x)], q[i].id = i;
	// F(i, 1, m) debug << ans[i] << '\n';
	cdq(1, m, 1, k, 0, n);
	F(i, 1, m) cout << ans[i] << '\n';
	return 0;
}
/* why?
*/

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

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: 2ms
memory: 14364kb

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
455
927
111
66
101
439
721
64
771
744
61
458
825
228
283
574
746
517
575
836
942
843
386
834
850
863
816
57
502
337
738
444
573
992
490
809
328
278
529
17
354
528
434
77
400
694
4...

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: 186ms
memory: 55656kb

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: 0
Wrong Answer
time: 227ms
memory: 82472kb

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:

68153
31418
65636
18111
17459
3059
79396
98102
68289
54110
91945
20973
9003
45795
3294
10349
61955
58909
96063
81623
27068
68731
6053
12174
67230
11619
59135
49690
65068
91205
43818
20166
28367
87909
82295
81749
42414
77311
83383
8784
16490
26208
13294
62196
27518
5782
8571
65010
49330
97615
20286
7...

result:

wrong answer 1st numbers differ - expected: '52956', found: '68153'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%