RE#11求条
查看原帖
RE#11求条
716260
SegmentTree_楼主2024/1/29 08:42
#include<bits/stdc++.h>
#define ll long long
#define lid (id << 1)
#define rid (id << 1 | 1)
using namespace std;
const int N = 5e5+5;
struct node{
	int l, r;
	ll ans, lazy;
}tr[N << 2];
int n, q;
int a[N];
int head[N], nxt[N << 1], to[N << 1], val[N], cccnnnttt;
int siz[N], dep[N], fa[N], hson[N], top[N], cnt, dfn[N], rnk[N];
void add(int u, int v){
	to[++cccnnnttt] = v;
	nxt[cccnnnttt] = head[u];
	head[u] = cccnnnttt;
}
void dfs1(int u, int f){
	siz[u] = 1;
	int maxson = -1;
	hson[u] = -1;
	for (int i = head[u];i;i = nxt[i]){
		int v = to[i];
		if (v == f) return;
		fa[v] = u;
		dep[v] = dep[u] + 1;
		dfs1(v, u);
		siz[u] += siz[v];
		if (maxson < siz[v]) maxson = siz[v], hson[u] = v; 
	}
}
void dfs2(int u, int t){
	top[u] = t;
	dfn[u] = ++cnt;
	rnk[cnt] = u;
	a[cnt] = val[u];
	if (hson[u] == -1) return;
	dfs2(hson[u], t);
	for (int i = head[u];i;i = nxt[i]){
		int v = to[i];
		if (fa[u] != v && hson[u] != v) dfs2(v, v);
	}
}
void pushup(int id){
	tr[id].ans = tr[lid].ans | tr[rid].ans;
}
void pushdown(int id){
	if (tr[id].lazy){
		tr[lid].ans = tr[id].lazy;
		tr[rid].ans = tr[id].lazy;
		tr[lid].lazy = tr[id].lazy;
		tr[rid].lazy = tr[id].lazy;
		tr[id].lazy = 0;
		return;
	}
}
void build(int id, int l, int r){
	tr[id].l = l;
	tr[id].r = r;
	tr[id].lazy = 0;
	if (l == r){
		tr[id].ans = 1LL << a[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(lid, l, mid);
	build(rid, mid + 1, r);
	pushup(id);
}
void modify(int id, int l, int r, int k){
	if (l <= tr[id].l && tr[id].r <= r){
		tr[id].ans = 1LL << k;
		tr[id].lazy = 1LL << k;
		return;
	}
	pushdown(id);
	int mid = (tr[id].l + tr[id].r) >> 1;
	if (r <= mid) modify(lid, l, r, k);
	else if (l > mid) modify(rid, l, r, k);
	else modify(lid, l, mid, k), modify(rid, mid + 1, r, k);
	pushup(id);
}
ll query(int id, int l, int r){
	if (l <= tr[id].l && tr[id].r <= r){
		return tr[id].ans; 
	}
	pushdown(id);
	int mid = (tr[id].l + tr[id].r) >> 1;
	if (r <= mid) return query(lid, l, r);
	else if (l > mid) return query(rid, l, r);
	else return query(lid, l, mid) | query(rid, mid + 1, r);
}
int getans(ll x){
	int res = 0;
	for (ll i = x;i;i -= (i & (-i))) ++res;
	return res;
}
void modifysubtree(int x, int c){
	modify(1, dfn[x], dfn[x] + siz[x] - 1, c);
}
int querysubtree(int x){
	return getans(query(1, dfn[x], dfn[x] + siz[x] - 1));
}
int main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> q;
	for (int i = 1;i <= n;++i) cin >> val[i];
	for (int i = 1;i < n;++i){
		int u, v;
		cin >> u >> v;
		add(u, v);
		add(v, u);
	}
	dep[1] = 1;
	dfs1(1, 0);
	dfs2(1, 1);
	build(1, 1, n);
	while (q--){
		int op, u, c;
		cin >> op >> u;
		if (op == 1){
			cin >> c;
			modifysubtree(u, c);
		}
		else{
			cout << querysubtree(u) << '\n';
		}
	}
	return 0;
} 
2024/1/29 08:42
加载中...