Loading

BZOJ2908 又是nand (线段树)

BZOJ2908 又是nand

首先手玩需要计算的值,发现既不满足交换律也不满足结合律,不好维护。

对于位运算,常见的考虑分开每一位计算贡献,对于单独一位,计算较为简单。

既然计算的值只能按顺序计算,那我们只能考虑树剖(其他数据结构不好维护顺序)。给每一位建一棵线段树,在线段树上维护。

注意到 「树上从 a 到 b 的费用为 0 与路径上的点的权值顺次 nand」,这里我们可以考虑维护 \(t_{c,x}\) 表示 \(c\) 顺次 nand 区间 \(x\) 的值,此时容易发现这样是可以区间合并的

但是我们发现这样子做还不太够,因为计算路径时,有的段需要从左到右,有的段需要从右往左,所以分别维护 \(tl\)\(tr\) 即可。

#include <bits/stdc++.h>
#define pii std::pair<int, int>
#define fi first
#define se second

typedef long long i64;
const int N = 100010;
i64 n, m, k;
struct node {
	i64 to, nxt;
} e[N << 1];
i64 a[N];
i64 h[N], cnt;
void add(i64 u, i64 v) {
	e[++cnt].to = v;
	e[cnt].nxt = h[u];
	h[u] = cnt;
}
i64 idx;
i64 dep[N], sz[N], fa[N], id[N], tp[N], wt[N], son[N]; 
void dfs1(i64 u, i64 f, i64 deep) {
	dep[u] = deep;
	sz[u] = 1;
	fa[u] = f;
	i64 maxson = -1;
	for(i64 i = h[u]; i; i = e[i].nxt) {
		i64 v = e[i].to;
		if(v == f) continue;

		dfs1(v, u, deep + 1);

		sz[u] += sz[v];
		if(maxson < sz[v]) son[u] = v, maxson = sz[v];
	}
}
void dfs2(i64 u, i64 topf) {
	id[u] = ++idx;
	tp[u] = topf;
	wt[idx] = a[u];
	if(!son[u]) return;
	dfs2(son[u], topf);
	for(i64 i = h[u]; i; i = e[i].nxt) {
		i64 v = e[i].to;

		if(v == fa[u] || v == son[u]) continue;

		dfs2(v, v);
	} 
}

struct seg {
	bool wl[2][N << 2], wr[2][N << 2];
	void pushup(i64 u) {
		wl[0][u] = wl[wl[0][u << 1]][u << 1 | 1];
		wl[1][u] = wl[wl[1][u << 1]][u << 1 | 1];
		wr[0][u] = wr[wr[0][u << 1 | 1]][u << 1];
		wr[1][u] = wr[wr[1][u << 1 | 1]][u << 1];
	}
	void build(i64 u, i64 l, i64 r, i64 x) {
		if(l == r) {
			wl[0][u] = wr[0][u] = 1, wl[1][u] = wr[1][u] = !((wt[l] >> x) & 1);
			return;
		}
		i64 mid = (l + r) >> 1;
		build(u << 1, l, mid, x), build(u << 1 | 1, mid + 1, r, x);
		pushup(u);
	}
	void update(i64 u, i64 l, i64 r, i64 x, i64 y) {
		if(l == r) {
			wl[0][u] = wr[0][u] = 1, wl[1][u] = wr[1][u] = !y;
			return;
		}
		i64 mid = (l + r) >> 1;
		if(x <= mid) update(u << 1, l, mid, x, y);
		else update(u << 1 | 1, mid + 1, r, x, y);
		pushup(u);
	} 
	bool ql(i64 u, i64 l, i64 r, i64 L, i64 R, bool c) {
		if(L <= l && r <= R) {
			return wl[c][u];
		}
		i64 mid = (l + r) >> 1;
		if(R <= mid) return ql(u << 1, l, mid, L, R, c);
		if(L > mid) return ql(u << 1 | 1, mid + 1, r, L, R, c);
		return ql(u << 1 | 1, mid + 1, r, L, R, ql(u << 1, l, mid, L, R, c));
	}
	bool qr(i64 u, i64 l, i64 r, i64 L, i64 R, bool c) {
		if(L <= l && r <= R) {
			return wr[c][u];
		}
		i64 mid = (l + r) >> 1;
		if(R <= mid) return qr(u << 1, l, mid, L, R, c);
		if(L > mid) return qr(u << 1 | 1, mid + 1, r, L, R, c);
		return qr(u << 1, l, mid, L, R, qr(u << 1 | 1, mid + 1, r, L, R, c));
	}
} t[35];
i64 st[N], tot;
void solve(i64 u, i64 v) {
	tot = 0;
	i64 ret = 0;
	while(tp[u] != tp[v]) {
		if(dep[tp[u]] >= dep[tp[v]]) {
			for(i64 i = 0; i < k; i++) ret = ret - (ret & (1 << i)) + (1ll * t[i].qr(1, 1, n, id[tp[u]], id[u], (ret >> i) & 1) << i); 
			u = fa[tp[u]];
		}	
		else {
			st[++tot] = v;
			v = fa[tp[v]]; 
		}
	}
	if(dep[u] < dep[v]) for(i64 i = 0; i < k; i++) ret = ret - (ret & (1 << i)) + (1ll * t[i].ql(1, 1, n, id[u], id[v], (ret >> i) & 1) << i); 
	else for(i64 i = 0; i < k; i++) ret = ret - (ret & (1 << i)) + (1ll * t[i].qr(1, 1, n, id[v], id[u], (ret >> i) & 1) << i);
	for(i64 i = tot; i >= 1; i--) {
		i64 x = st[i];
		for(i64 i = 0; i < k; i++) ret = ret - (ret & (1 << i)) + (1ll * t[i].ql(1, 1, n, id[tp[x]], id[x], (ret >> i) & 1) << i); 
	}
	std::cout << ret << "\n";
}
void Solve() {
	std::cin >> n >> m >> k;
	for(i64 i = 1; i <= n; i++) {
		std::cin >> a[i];
	}

	for(i64 i = 1; i < n; i++) {
		i64 u, v;
		std::cin >> u >> v;
		add(u, v), add(v, u);
	}

	dfs1(1, 0, 1);
	dfs2(1, 1);
	for(i64 i = 0; i < k; i++) t[i].build(1, 1, n, i);

	while(m--) {
		std::string s;
		std::cin >> s;
		i64 a, b;
		std::cin >> a >> b;
		if(s == "Replace") {
			for(i64 i = 0; i < k; i++) t[i].update(1, 1, n, id[a], (b >> i) & 1);
		}
		else solve(a, b);
	}
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
	Solve();

	return 0;
}
posted @ 2024-03-23 20:48  Fire_Raku  阅读(9)  评论(0编辑  收藏  举报