树剖WA*17+T*3求救
查看原帖
树剖WA*17+T*3求救
307542
yyyhy楼主2024/11/20 17:17

link

#include <cstdio> 
#include <vector>
#include <cstring>
#include <algorithm>
#define maxn 100000
#define int long long

inline int read() {
	char ch = getchar(); int num = 0;
	while (ch < '0' || ch > '9') ch = getchar();
	while (ch >= '0' && ch <= '9') num = (num << 1) + (num << 3) + (ch ^ '0'), ch = getchar();
	return num;
}
inline void print(int x) {
	if (x > 10) print(x / 10);
	putchar(x % 10 | '0');
	return;
}

char ch[20];
std :: vector <int> S[maxn + 10];
int n, q, x, mytimer, a[maxn + 10], f[maxn + 10], h[maxn + 10], dfn[maxn + 10];
int siz[maxn + 10], hson[maxn + 10], las[maxn + 10];
int ad[maxn + 10];

int tr[maxn << 2], tag[maxn << 2]; 

inline void dfs1(int i) {
//	printf("i = %d\n", i);
	siz[i] = 1;
	for (int j : S[i]) {
		dfs1(j);
		if (siz[j] > siz[hson[i]]) hson[i] = j;
		siz[i] += siz[j];
	}
	return;
}
inline void dfs2(int i, int fa) {
//	printf("i = %d, fa = %d\n", i, fa);
	dfn[i] = las[i] = ++mytimer; h[i] = fa;
	if (hson[i]) dfs2(hson[i], fa), las[i] = las[hson[i]];
	for (int j : S[i]) {
		if (j == f[i] || j == hson[i]) continue;
		dfs2(j, j);
		las[i] = las[j];
	}
	return;
}
inline void pushdown(int i, int l, int r) {
	int mid = l + r >> 1, ls = i << 1, rs = ls + 1;
	if (tag[i] == 1) tag[ls] = tag[rs] = 1, tr[ls] = mid - l + 1, tr[rs] = r - mid;
	else tag[ls] = tag[rs] = 2, tr[ls] = tr[rs] = 0;
	tag[i] = 0;
	return;
}
inline int query1(int i, int l, int r, int ll, int rr) {
//	printf("1 %d %d %d\n", l, r, tr[i]);
	if (l >= ll && r <= rr) 
		return tr[i];
	if (tag[i]) pushdown(i, l, r);
	int re = 0, mid = l + r >> 1, ls = i << 1, rs = ls + 1;
	if (ll <= mid) re += query1(ls, l, mid, ll, rr);
	if (rr > mid) re += query1(rs, mid + 1, r, ll, rr);
	return re;
}
inline void add1(int i, int l, int r, int ll, int rr) {
//	printf("2 %d %d %d\n", l, r, tr[i]);
	if (l >= ll && r <= rr) {
		tag[i] = 1, tr[i] = r - l + 1;
		return;
	}
	if (tag[i]) pushdown(i, l, r);
	int mid = l + r >> 1, ls = i << 1, rs = ls + 1;
	if (ll <= mid) add1(ls, l, mid, ll, rr);
	if (rr > mid) add1(rs, mid + 1, r, ll, rr);
	tr[i] = tr[ls] + tr[rs];
	return;
}
inline void add0(int i, int l, int r, int ll, int rr) {
//	printf("%d %d\n", l, r);
	if (l >= ll && r <= rr) {
		tag[i] = 2, tr[i] = 0;
		return;
	}
	if (tag[i]) pushdown(i, l, r);
	int mid = l + r >> 1, ls = i << 1, rs = ls + 1;
	if (ll <= mid) add0(ls, l, mid, ll, rr);
	if (rr > mid) add0(rs, mid + 1, r, ll, rr);
	tr[i] = tr[ls] + tr[rs];
	return;
}

signed main(void) {
	freopen("in.in", "r", stdin);
	n = read(); f[0] = n;
	for (int i = 1; i < n; i++)
		f[i] = read(), S[f[i]].push_back(i); 
	dfs1(0);
	dfs2(0, 0);
//	for (int i = 0; i < n; i++)
//		printf("%d ", dfn[i]);
//	putchar('\n');
	q = read();
	while (q--) {
		scanf("%s", ch); x = read();
//		printf("x = %d\n", x);
		if (!strcmp(ch, "install")) {
			int ans = 0;
			while (dfn[x]) {
//				printf("x = %d\n", x);
				ans += dfn[x] - dfn[h[x]] + 1 - query1(1, 1, n, dfn[h[x]], dfn[x]);
//				printf("ans = %d\n", ans);
				add1(1, 1, n, dfn[h[x]], dfn[x]);
				x = f[h[x]];
			}
			print(ans), putchar('\n');
		} else {
//			printf("%d %d %d\n", x, dfn[x], las[x]);
			print(query1(1, 1, n, dfn[x], las[x])), putchar('\n');
			add0(1, 1, n, dfn[x], las[x]);
		}
	}
	
	return 0;
}
2024/11/20 17:17
加载中...