【BZOJ4196】[Noi2015]软件包管理器【树链剖分】【线段树】

【题目链接】

1A了。。

裸树剖,没啥说的...

/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 100005;

int n, head[maxn], cnt;

struct _edge {
	int v, next;
} g[maxn << 1];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline void add(int u, int v) {
	g[cnt] = (_edge){v, head[u]};
	head[u] = cnt++;
}

int son[maxn], size[maxn], top[maxn], pre[maxn], depth[maxn], dfn[maxn], clo;
int sum[maxn << 2], setv[maxn << 2];

inline void dfs1(int x) {
	size[x] = 1; son[x] = 0;
	for(int i = head[x]; ~i; i = g[i].next) if(g[i].v ^ pre[x]) {
		depth[g[i].v] = depth[x] + 1;
		pre[g[i].v] = x;
		dfs1(g[i].v);
		size[x] += size[g[i].v];
		if(size[son[x]] < size[g[i].v]) son[x] = g[i].v;
	}
}

inline void dfs2(int x, int tp) {
	top[x] = tp;
	dfn[x] = ++clo;
	if(son[x]) dfs2(son[x], tp);
	for(int i = head[x]; ~i; i = g[i].next) if(g[i].v ^ pre[x] && g[i].v ^ son[x])
		dfs2(g[i].v, g[i].v);
}

inline void pushdown(int p, int l, int mid, int r) {
	if(~setv[p]) {
		setv[p << 1] = setv[p]; setv[p << 1 | 1] = setv[p];
		sum[p << 1] = (mid - l + 1) * setv[p]; sum[p << 1 | 1] = (r - mid) * setv[p];
		setv[p] = -1;
	}
}

inline void pushup(int p) {
	sum[p] = sum[p << 1] + sum[p << 1 | 1];
}

inline int query(int p, int l, int r, int x, int y) {
	if(x <= l && r <= y) return sum[p];
	int mid = l + r >> 1, ans = 0;
	pushdown(p, l, mid, r);
	if(x <= mid) ans += query(p << 1, l, mid, x, y);
	if(y > mid) ans += query(p << 1 | 1, mid + 1, r, x, y);
	return ans;
}

inline void set(int p, int l, int r, int x, int y, int c) {
	if(x <= l && r <= y) {
		sum[p] = (r - l + 1) * c;
		setv[p] = c;
		return;
	}
	int mid = l + r >> 1;
	pushdown(p, l, mid, r);
	if(x <= mid) set(p << 1, l, mid, x, y, c);
	if(y > mid) set(p << 1 | 1, mid + 1, r, x, y, c);
	pushup(p);
}

inline int querychain(int x) {
	int ans = 0;
	for(; x; x = pre[top[x]]) {
		ans += query(1, 1, clo, dfn[top[x]], dfn[x]);
		set(1, 1, clo, dfn[top[x]], dfn[x], 1);
	}
	return ans;
}

inline int querytree(int x) {
	int ans = query(1, 1, clo, dfn[x], dfn[x] + size[x] - 1);
	set(1, 1, clo, dfn[x], dfn[x] + size[x] - 1, 0);
	return ans;
}

int main() {
	n = iread();
	for(int i = 1; i <= n; i++) head[i] = -1; cnt = 0;
	for(int i = 2; i <= n; i++) {
		int x = iread(); x++;
		add(x, i); add(i, x);
	}

	depth[1] = 1;
	dfs1(1); dfs2(1, 1);

	for(int i = (maxn << 2) - 1; i >= 0; i--) setv[i] = -1;

	for(int q = iread(); q; q--) {
		char ch = getchar(); for(; ch != 'i' && ch != 'u'; ch = getchar());
		int x = iread(); x++;
		if(ch == 'i') printf("%d\n", depth[x] - querychain(x));
		else if(ch == 'u') printf("%d\n", querytree(x));
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值