QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#876713#9676. Ancestorsrlc2022040 967ms338536kbC++145.8kb2025-01-31 11:38:182025-01-31 11:38:19

Judging History

This is the latest submission verdict.

  • [2025-01-31 11:38:19]
  • Judged
  • Verdict: 0
  • Time: 967ms
  • Memory: 338536kb
  • [2025-01-31 11:38:18]
  • Submitted

answer

#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define mp(x, y) make_pair(x, y) 
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int N = 1e5 + 5;
const int M = 1e6 + 5;

int n, m, rt;
vector<int> e[N];
int fa[N][22] = {{0}}, d[N] = {0};
vector<int> res[N];
int dfn[N] = {0}, cd = 0; 
void dfs(int x, int dth) {
	res[d[x] = dth].push_back(x);
	dfn[x] = ++cd;
	for (auto i: e[x])
		dfs(i, dth + 1);
}

int qanc(int x, int num) {
	for (int j = 20; j >= 0; j--)
		if (num >= (1 << j))
			x = fa[x][j], num -= (1 << j);
	return x;
}
int qlca(int x, int y) {
	if (d[x] > d[y])	
		swap(x, y);
//	cout << x << " and " << y << endl;
	y = qanc(y, d[y] - d[x]);
	if (x == y)
		return x;
	for (int j = 20; j >= 0; j--)
		if (fa[x][j] != fa[y][j])	
			x = fa[x][j], y = fa[y][j];
	//cout << "ans = " << fa[x][0] << endl;
	return fa[x][0];
}

int tot = 0;
struct Node {
	int op, x, y, z, f, id;
	Node (int _op = 0, int _x = 0, int _y = 0, int _z = 0, int _f = 0, int _id = 0) :
		op(_op), x(_x), y(_y), z(_z), f(_f), id(_id) {} 
} a[M * 6], b[M * 6];

vector<int> g[N];//虚树节点 

int vt[N * 2] = {0}, lenv = 0;
int tmp[N * 2] = {0}, lent = 0;

bool cmpd(int x, int y) {
	return dfn[x] < dfn[y];
}

void build(int t) {
	lenv = 0;
	vt[++lenv] = rt;
	for (auto i: res[t])
		vt[++lenv] = i;
	sort(vt + 1, vt + lenv + 1, cmpd);
	lent = 0;
	for (int i = 1; i <= lenv; i++) {
		tmp[++lent] = vt[i];
		if (i < lenv)
			tmp[++lent] = qlca(vt[i], vt[i + 1]);
	}
	sort(tmp + 1, tmp + lent + 1, cmpd);
	lent = unique(tmp + 1, tmp + lent + 1) - tmp - 1;
//	cout << lent << endl;
	for (int i = 1; i < lent; i++) {
		g[qlca(tmp[i], tmp[i + 1])].push_back(tmp[i + 1]);
	//	cout << qlca(tmp[i], tmp[i + 1]) << " to " << tmp[i + 1] << endl;
	}
}
void clrvt() {
	for (int i = 1; i <= lent; i++)
		g[tmp[i]].clear();
}

int sz[N] = {0}, chd[N] = {0};
int getsz(int x) {
	sz[x] = g[x].empty(), chd[x] = -1;
	for (auto i: g[x]) {
		sz[x] += getsz(i);
		if (chd[x] == -1 || sz[chd[x]] < sz[i])
			chd[x] = i;
	}
	return sz[x];
}

piii c[M * 2];
int len = 0;

void upd(int x, int t, int cl) {
	if (chd[x] == -1) {
	//	if (x == 82)	
	//		cout << d[x] << " " << (int)g[x].size() << " ?? " << t << " ?? " << cl << endl;
		c[++len] = mp(t, mp(x, cl));
	}
	for (auto i: g[x])
		upd(i, t, cl);
}

int srh(int x, int nw) { 
	if (chd[x] == -1) 
		return x;
	for (auto i: g[x])
		if (i != chd[x])
			srh(i, nw);
	int col = srh(chd[x], nw);
	for (auto i: g[x])
		if (i != chd[x])
			upd(i, nw - d[x], col);
	return col;
}

set<int> pos[N];
int arr[N] = {0};
void initc() {
	cout << len << endl;
	c[++len] = mp(0, mp(rt, 0));
	sort(c + 1, c + len + 1);
	for (int i = 1; i <= n; i++) {
		arr[i] = i, pos[i].insert(0), pos[i].insert(i);
		a[++tot] = Node(0, 0, 0, i, 1);
	}
	for (int i = 1; i <= len; i++) {
		int t = c[i].first, x = c[i].second.first, y = c[i].second.second;
	//	printf("at time %d: mdf %d to %d\n", t, x, y);
		auto cur = pos[arr[x]].lower_bound(x);
		cur--;
		int prx = *cur;
		a[++tot] = Node(0, t, *cur, x, -1);
		cur++;
		cur++;
		if (cur != pos[arr[x]].end()) {
			a[++tot] = Node(0, t, x, *cur, -1);
			a[++tot] = Node(0, t, prx, *cur, 1);
		}
		pos[arr[x]].erase(x);
		arr[x] = y;
		if (arr[x]) {
			pos[arr[x]].insert(x);
			cur = pos[arr[x]].lower_bound(x);
			cur--;
			prx = *cur;
			a[++tot] = Node(0, t, prx, x, 1);
			cur++;
			cur++;
			if (cur != pos[arr[x]].end()) {
				a[++tot] = Node(0, t, prx, *cur, -1);
				a[++tot] = Node(0, t, x, *cur, 1);
			}
		}
	}
//	for (int i = 1; i <= tot; i++)
//		printf("(%d, %d, %d, v:%d)\n", a[i].x, a[i].y, a[i].z, a[i].f);
}

bool cmp(Node u, Node v) {
	if (u.x != v.x)
		return u.x < v.x;
	if (u.y != v.y)
		return u.y < v.y;
	if (u.z != v.z)
		return u.z < v.z;
	return u.op < v.op;
}

int t[N] = {0};
int lowbit(int x) {
	return x & -x; 
}
void mdf(int x, int v) {
	while (x <= n)
		t[x] += v, x += lowbit(x); 
}
int qry(int x) {
	int ans = 0;
	while (x)
		ans += t[x], x -= lowbit(x);
	return ans;
}
void clr(int x) {
	while (x <= n)
		t[x] = 0, x += lowbit(x);
}

int ans[M] = {0};

void slv(int l, int r) {
	if (l + 1 == r)
		return;
	int mid = (l + r) / 2;
	slv(l, mid), slv(mid, r);
	for (int i = mid, j = l; i < r; i++) {
		while (j < mid && a[j].y < a[i].y) {
			if (a[j].op == 0)
				mdf(a[j].z, a[j].f);
			j++;
		}
		if (a[i].op == 1)
			ans[a[i].id] += a[i].f * qry(a[i].z);
	} 
	for (int i = l; i < mid; i++)
		if (a[i].op == 0)
			clr(a[i].z);
	for (int i = l, j = mid, cur = l; i < mid || j < r; )
		if (j == r || (i < mid && a[i].y < a[j].y))
			b[cur++] = a[i++];
		else
			b[cur++] = a[j++];
	for (int i = l; i < r; i++)
		a[i] = b[i];
}

void show(int x) {
	for (auto i: g[x])
		printf("%d -> %d\n", x, i);
	for (auto i: g[x])
		show(i);	
}

int main() {
//	freopen("a.in", "r", stdin);
	scanf("%d%d", &n, &m);
//	if (n > 1000)
//		return 0;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &fa[i][0]);
		if (!fa[i][0])
			rt = i;
		else	
			e[fa[i][0]].push_back(i);//, cout << fa[i] << " -> " << i << endl;
	}
	for (int j = 1; (1 << j) <= n; j++)
		for (int i = 1; i <= n; i++)
			fa[i][j] = fa[fa[i][j - 1]][j - 1];
	dfs(rt, 1);
	for (int i = 1; res[i + 1].size(); i++) {
	//	printf("d:%d\n", i);
		build(i + 1);
		getsz(rt);
		srh(rt, i + 1);
		upd(rt, i + 1, 0);
		clrvt();
	//	show(rt);
	}
//	cout << "good" << endl;
	initc();
//	cout << "good" << endl;
	for (int i = 1, l, r, k; i <= m; i++) {
		scanf("%d%d%d", &l, &r, &k);
		a[++tot] = Node(1, k, l, l - 1, -1, i);
		a[++tot] = Node(1, k, l, r, 1, i);
	}
	sort(a + 1, a + tot + 1, cmp);
	slv(1, tot + 1);
	for (int i = 1; i <= m; i++)
		printf("%d\n", ans[i]);
	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: 12ms
memory: 306044kb

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:

11
2
1
3
3
1

result:

wrong answer 1st numbers differ - expected: '2', found: '11'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #30:

score: 0
Wrong Answer
time: 275ms
memory: 324272kb

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:

49999
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
4310...

result:

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

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Wrong Answer

Test #67:

score: 0
Wrong Answer
time: 967ms
memory: 338536kb

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:

99999
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
2...

result:

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

Subtask #6:

score: 0
Skipped

Dependency #1:

0%