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:
- Change the colours of all vertices in the subtree of the vertex v to the colour c.
- Find the number of different colours in the subtree of the vertex v.
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.
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.
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
2 3 4 5 1 2
思路:建立一棵线段上,用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;
}