CodeForces 620E New Year Tree 线段树

E. New Year Tree
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The New Year holidays are over, but Resha doesn't want to throw away the New Year tree. He invited his best friends Kerim and Gural to help him to redecorate the New Year tree.

The New Year tree is an undirected tree with n vertices and root in the vertex 1.

You should process the queries of the two types:

  1. Change the colours of all vertices in the subtree of the vertex v to the colour c.
  2. Find the number of different colours in the subtree of the vertex v.
Input

The first line contains two integers n, m (1 ≤ n, m ≤ 4·105) — the number of vertices in the tree and the number of the queries.

The second line contains n integers ci (1 ≤ ci ≤ 60) — the colour of the i-th vertex.

Each of the next n - 1 lines contains two integers xj, yj (1 ≤ xj, yj ≤ n) — the vertices of the j-th edge. It is guaranteed that you are given correct undirected tree.

The last m lines contains the description of the queries. Each description starts with the integer tk (1 ≤ tk ≤ 2) — the type of the k-th query. For the queries of the first type then follows two integers vk, ck (1 ≤ vk ≤ n, 1 ≤ ck ≤ 60) — the number of the vertex whose subtree will be recoloured with the colour ck. For the queries of the second type then follows integer vk (1 ≤ vk ≤ n) — the number of the vertex for which subtree you should find the number of different colours.

Output

For each query of the second type print the integer a — the number of different colours in the subtree of the vertex given in the query.

Each of the numbers should be printed on a separate line in order of query appearing in the input.

Examples
Input
7 10
1 1 1 1 1 1 1
1 2
1 3
1 4
3 5
3 6
3 7
1 3 2
2 1
1 4 3
2 1
1 2 5
2 1
1 6 4
2 1
2 2
2 3
Output
2
3
4
5
1
2
题意:一棵树上面的点都有颜色,最多61种,有两种操作,一种是把这个点以及它子树的所有节点都涂成那个颜色,另一个操作是问这个节点和它的子树里颜色数

思路:建立一棵线段上,用dfs序记录下子树范围,然后维护线段树的时候,用二进制来表示某个颜色是否存在,有61种颜色,也就是2^61次方就可以表示完所有情况,对于一个点维护出来的数,每一位都去对比,然后记录颜色数就可以了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define lowbit(x) (x & (-x))
#define maxn 500005
vector<int>vec[maxn];
int vis[maxn],le[maxn],ri[maxn],a[maxn],cnt;
ll ans;

struct node{
	int le,ri;
	ll num,lazy;
}tree[maxn * 4];

void dfs(int x){  // dfs序,建立各个点到其所有子树 的编号 
	vis[x] = 1;
	le[x] = ++cnt;
	for(int i = 0;i < vec[x].size();i++){
		int v = vec[x][i];
		if(vis[v])
			continue;
		dfs(v);
	}
	ri[x] = cnt;
}
void push_down(int i){
	if(!tree[i].lazy)
		return ;
	tree[i * 2].num = tree[i * 2 + 1].num = (1ll << tree[i].lazy);
	tree[i * 2].lazy = tree[i * 2 + 1].lazy = tree[i].lazy;
	tree[i].lazy = 0;
}
void push_up(int i){
	tree[i].num = tree[i * 2].num | tree[i * 2 + 1].num;
}
void build(int i,int l,int r){
	tree[i].lazy = 0;
	tree[i].le = l;
	tree[i].ri = r;
	if(l == r)
		return ;
	int mid = (l + r) >> 1;
	build(i * 2,l,mid);
	build(i * 2 + 1,mid + 1,r);
}
void get_pos(int i,int aim,int w){
	if(tree[i].le == aim && tree[i].ri == aim){
		tree[i].num = (1ll << w);
		return ;
	}
	push_down(i);
	int mid = (tree[i].le + tree[i].ri) >> 1;
	if(aim <= mid)
		get_pos(i * 2,aim,w);
	if(aim > mid)
		get_pos(i * 2 + 1,aim,w);
	push_up(i); 
}
void update(int i,int l,int r,ll w){
	if(tree[i].le == l && tree[i].ri == r){
		tree[i].lazy = w;
		tree[i].num = (1ll << w); //更新颜色 
		return ;
	}
	push_down(i);
	int mid = (tree[i].le + tree[i].ri) >> 1;
	if(r <= mid)
		update(i * 2,l,r,w);
	else if(l > mid)
		update(i * 2 + 1,l,r,w);
	else{
		update(i * 2,l,mid,w);
		update(i * 2 + 1,mid + 1,r,w);
	}
	push_up(i);
	return ;
}
void query(int i,int l,int r){
	if(tree[i].le == l && tree[i].ri == r){
		ans |= tree[i].num;
		return ;
	}
	push_down(i); //懒惰数组,查询到了再更新 
	int mid = (tree[i].le + tree[i].ri) >> 1;
	if(r <= mid){
		query(i * 2,l,r);
	}else if(l > mid){
		query(i * 2 + 1,l,r);
	}else{
		query(i * 2,l,mid);
		query(i * 2 + 1,mid + 1,r);
	}
	push_up(i);
}
int main(){
	int n,m,u,v;
	scanf("%d %d",&n,&m);
	for(int i = 1;i <= n;i++)
		scanf("%d",&a[i]);
	for(int i = 1;i < n;i++){
		scanf("%d %d",&u,&v);
		vec[u].push_back(v);
		vec[v].push_back(u);
	}
	cnt = 0;
	dfs(1);
	build(1,1,cnt);
	for(int i = 1;i <= n;i++){
		get_pos(1,le[i],a[i]); //把每个点的颜色上传,且转化为二进制保存在线段树里 
	}
	while(m--){
		int opt,x,y;
		scanf("%d",&opt);
		if(opt == 1){
			scanf("%d %d",&x,&y);
			update(1,le[x],ri[x],y);
		}else{
			ans = 0;
			scanf("%d",&x);
			query(1,le[x],ri[x]); // 查询得到一个ans 
			int tmp = 0;
			for(int i = 1;i <= 61;i++){	//去与61个位一一比较,统计颜色数 
				if(ans & (1ll << i))
					tmp++;
			}
			printf("%d\n",tmp);
		}
	}
	return 0;
} 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值