WA #51 求助
查看原帖
WA #51 求助
570700
CznTree楼主2024/1/6 15:33

rt,代码:

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr) 
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lc(x) x << 1ll 
#define rc(x) x << 1ll | 1ll
#define lowbit(x) ((x) & (-x))
#define LEN_LC (tree[lc(i)].r - tree[lc(i)].l + 1) 
#define LEN_RC (tree[rc(i)].r - tree[rc(i)].l + 1) 
#define LEN (tree[i].r - tree[i].l + 1)  
#define int long long 
const int N = 5e5 +7;
int L[N], R[N]; 
int k[N]; 
int a[N]; 
int n, m; 
struct Edge{ 
	int next, to; 
}edge[N]; 
struct tree{
    int l, r; 
    int sum, lazy; 
}tree[N << 2]; 
int head[N]; 
int cnt; 
void add(int u, int v) {
	cnt++; 
	edge[cnt].to = v; 
	edge[cnt].next = head[u]; 
	head[u] = cnt;  
}
int tot;  
void dfs(int u, int fa) {
	tot++; 
	L[u] = tot; 
	k[tot] = u; 
	for (int i = head[u]; i ; i = edge[i].next) {
		int now = edge[i].to; 
		if(now == fa) continue; 
		dfs(now, u); 
	} 
	R[u] = tot;  
}

void push_up(int i) {
	tree[i].sum = tree[lc(i)].sum | tree[rc(i)].sum;
}

void push_down(int i) {
	if(tree[i].lazy) {
		tree[lc(i)].sum = tree[i].lazy; 
		tree[lc(i)].lazy = tree[i].lazy; 
		tree[rc(i)].sum = tree[i].lazy; 
		tree[rc(i)].lazy = tree[i].lazy;
		tree[i].lazy = 0; 		 
	}
}

void build(int i, int L, int R) {
	tree[i].l = L, tree[i].r = R; 
	if(L == R) {
		tree[i].sum = 1ll << a[k[L]];  
		return ; 
	}
	int mid = (L + R) >> 1; 
	build(lc(i), L, mid); 
	build(rc(i), mid + 1, R); 
	push_up(i); 	
}

void modify(int i, int L, int R, int k) {
	if(L <= tree[i].l && tree[i].r <= R) {
		tree[i].sum = 1ll << k; 
		tree[i].lazy = 1ll << k; 
		return ; 
	}
	push_down(i); 
	if(tree[lc(i)].r >= L) modify(lc(i), L, R, k); 
	if(tree[rc(i)].l <= R) modify(rc(i), L, R, k); 
	push_up(i);
}
int query(int i, int L, int R) {
	if(L <= tree[i].l && tree[i].r <= R) {	
		return tree[i].sum;
	}
	push_down(i); 
	int res = 0; 
	if(tree[lc(i)].r >= L) res |= query(lc(i), L, R); 
	if(tree[rc(i)].l <= R) res |= query(rc(i), L, R); 
	return res; 
}

void solve() {
	std::cin >> n >> m; 
	rep(i, 1, n) {
		std::cin >> a[i];  
	} 
	rep(i, 1, n - 1) {
		int u, v; 
		std::cin >> u >> v;
		add(u, v); 
		add(v, u); 
	}
	dfs(1, -1);
	build(1, 1, n); 
	while(m--) {
		int opt; 
		std::cin >> opt; 
		if(opt == 1) {
			int u, v; 
			std::cin >> u >> v; 
			modify(1, L[u], R[u], v); 
		}
		if(opt == 2) {
			int x; 
			std::cin >> x;
			std::cout << __builtin_popcountll(query(1, L[x], R[x])) << std::endl; 
		}
	}
}
signed main() {
	IOS; 
	solve(); 
	return 0; 
}
2024/1/6 15:33
加载中...