New Year Tree

New Year Tree

Translation

  • 给出一棵 n n n个节点的树,根节点为 1 1 1。每个节点上有一种颜色 c i c_i ci m m m次操作。操作有两种:
    1. 1 u c:将以 u u u为根的子树上的所有节点的颜色改为 c c c
    2. 2 u:询问以 u u u为根的子树上的所有节点的颜色数量。
  • 1 ≤ n , m ≤ 4 × 1 0 5 1\le n,m\le 4\times 10^5 1n,m4×105 1 ≤ c i , c ≤ 60 1\le c_i,c\le 60 1ci,c60

Description

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 n n vertices and root in the vertex 1 1 1.

You should process the queries of the two types:

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

Format

Input

The first line contains two integers n , m n,m n,m( 1 ≤ n , m ≤ 4 ⋅ 1 0 5 1\le n,m\le 4·10^{5} 1n,m4105) — the number of vertices in the tree and the number of the queries.

The second line contains n n nintegers c i c_{i} ci( 1 ≤ c i ≤ 60 1\le c_{i}\le 60 1ci60) — the colour of the i i i-th vertex.

Each of the next n − 1 n-1 n1lines contains two integers x j , y j x_{j},y_{j} xj,yj( 1 ≤ x j , y j ≤ n 1\le x_{j},y_{j}\le n 1xj,yjn) — the vertices of the j j j-th edge. It is guaranteed that you are given correct undirected tree.

The last m m m lines contains the description of the queries. Each description starts with the integer t k t_{k} tk( 1 ≤ t k ≤ 2 1\le t_{k}\le 2 1tk2) — the type of the k k k-th query. For the queries of the first type then follows two integers v k , c k v_{k},c_{k} vk,ck( 1 ≤ v k ≤ n , 1 ≤ c k ≤ 60 1\le v_{k}\le n,1\le c_{k}\le 60 1vkn,1ck60) — the number of the vertex whose subtree will be recoloured with the colour c k c_{k} ck. For the queries of the second type then follows integer v k v_{k} vk( 1 ≤ v k ≤ n 1\le v_{k}\le n 1vkn) — 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 a 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.

Samples

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

Input

23 30
1 2 2 6 5 3 2 1 1 1 2 4 5 3 4 4 3 3 3 3 3 4 6
1 2
1 3
1 4
2 5
2 6
3 7
3 8
4 9
4 10
4 11
6 12
6 13
7 14
7 15
7 16
8 17
8 18
10 19
10 20
10 21
11 22
11 23
2 1
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 4
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 1
1 21 1
1 22 1
1 23 1
2 1
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 4

Output

6
1
3
3
2
1
2
3
5
5
1
2
2
1
1
1
2
3

解析

数据结构综合题。

对于这种树上子树修改问题,最容易想到的是 dfs 序。因为,这样就可以把一个立体的东西拍平,转换为序列问题,而求解序列问题的手段有很多。

颜色总数只有 60 60 60,可以考虑状态压缩。这题中我使用的是 bitset。bitset 可以处理很多集合问题。求并集直接取或即可。

最先 dfs。对于一个点的子树操作,在时间戳上,根节点出现位置和子树最后一个节点出现位置中进行操作。

线段树中合并区间,就是求并集。我们只需要使用线段树的区间赋值和查询操作。特别注意,下传延迟标记时,因为是赋值,所以需要判掉延迟标记为空的情况。

务必使用 C 风格输入输出,去掉 long long,否则 TLE ON 51

注解

本人代码中 bitset 的各种用法:

样式用法含义
[][i]查询右数第 i i i
any()s.any()如果 s s s 不为空返回 1 1 1
reset()s.reset()清空 s s s
|a|b a , b a,b a,b 的并集
count()s.count() s s s 1 1 1 的个数

代码

// 620E New Year Tree
#include <bits/stdc++.h>
#define SIZE 1000010
#define all(x) x.begin(), x.end()
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;

bool memst;

int n, m;
int d[SIZE];
int dfn[SIZE], siz[SIZE], pos[SIZE];
int cnt=0;
vector<int> a[SIZE];

void dfs(int u, int fa)
{
	siz[u]=1;
	dfn[u]=++cnt;
	pos[cnt]=u;
	for(int v:a[u])	
		if(v!=fa) dfs(v, u), siz[u]+=siz[v];
}

struct segmentTreeNode
{
	int l, r;
	bitset<60> s;
	bitset<60> lt;
};

segmentTreeNode t[SIZE*4];
class segmentTree
{
public:
	void spread(int i)
	{
		if(t[i].lt.any())	// 因为是赋值,所以需要判掉延迟标记为空的情况。
		{
			t[i*2].s=t[i].lt;
			t[i*2+1].s=t[i].lt;
			t[i*2].lt=t[i].lt;
			t[i*2+1].lt=t[i].lt;
			t[i].lt.reset();
		}			
	}

	void build(int p, int l, int r)
	{
		t[p].l=l; t[p].r=r;
		if(l==r)
		{
			t[p].s[d[pos[l]]]=1;
			return;
		}
		int mid=(l+r)/2;
		build(p*2, l, mid);
		build(p*2+1, mid+1, r);
		t[p].s=t[p*2].s|t[p*2+1].s;
	}
	
	void change(int p, int l, int r, int c)
	{
		spread(p);
		if(l<=t[p].l && t[p].r<=r)
		{
			t[p].lt.reset();
			t[p].s.reset();
			t[p].s[c]=1; t[p].lt[c]=1;
			return;
		}
		t[p].s[c]=1;
		int mid=(t[p].l+t[p].r)/2;
		if(l<=mid) change(p*2,   l, r, c);
		if(r>mid)  change(p*2+1, l, r, c);
		t[p].s=t[p*2].s|t[p*2+1].s;
	}
	
	bitset<60> query(int p, int l, int r)
	{
		spread(p);
		if(l<=t[p].l && t[p].r<=r)
			return t[p].s;
		int mid=(t[p].l+t[p].r)/2;
		bitset<60> sum;
		if(l<=mid) sum|=query(p*2,   l, r);
		if(r>mid)  sum|=query(p*2+1, l, r);
		return sum;
	}
};

bool memed;

signed main()
{
	scanf("%d %d", &n, &m);
	for(int i=1; i<=n; i++) scanf("%d", d+i);
	for(int i=1; i<n; i++)
	{
		int u, v; scanf("%d %d", &u, &v);
		a[u].push_back(v);
		a[v].push_back(u);
	}
	dfs(1, 0);
	segmentTree st;
	st.build(1, 1, n);
	while(m--)
	{
		int o; scanf("%d", &o);
		if(o==1)
		{
			int v, c; scanf("%d %d", &v, &c);
			st.change(1, dfn[v], dfn[v]+siz[v]-1, c);
		}
		else
		{
			int v; scanf("%d", &v);
			printf("%d\n", st.query(1, dfn[v], dfn[v]+siz[v]-1).count());
		}
	}

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值