New Year Tree CodeForces - 620E

http://codeforces.com/problemset/problem/620/E

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.

Example
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

#include<cstdio>
#include<cstring>
#include<algorithm> 
#include<cmath>
#include<vector> 
#define ls(o) (o<<1)
#define rs(o) (o<<1|1)
#define debug(x) printf("%d***\n",x)
typedef long long ll;
using namespace std;
const int maxn=4e5+10;
struct Tre{
	int l,r;
	ll sum,tag;
}tr[maxn*4];
int color[maxn],val[maxn];
struct ID{
	int l,r;
}id[maxn];
vector<int> vec[maxn];
int cnt;
/*
这道题的精髓在于转换,把树型的结构,转换为一个数组来表示,
当然了,那么对应点的坐标就发生了变化,比如说
一个节点的管理范围是[l,r],那么如果我们去树中找他,我们知道他的管理范围是[l,r]; 
可是如果让你去更新,你应该怎么来做呢,肯定不能还是原来的节点,dfs我们都是把节点的[l,]的l 
设置成自己,这样的话,我们就可以把【l,这个l,当做他在线段树中的坐标,这样的话,就实现了
完美的转换 

还有就是颜色的统计,利用long long 64位实现最后的结果 
*/
void dfs(int o,int fa){
	id[o].l=++cnt;
	val[cnt]=color[o];//实现一种转换 
	for(int i=0;i<vec[o].size();i++){
		int v=vec[o][i];
		if(v!=fa)
			dfs(v,o);
	}
	id[o].r=cnt;
}

void build(int l,int r,int o){
	tr[o].l=l,tr[o].r=r;
	tr[o].tag=0;
	if(l==r){
		tr[o].sum=(1ll<<val[l]);
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,ls(o));
	build(mid+1,r,rs(o));
	tr[o].sum=tr[ls(o)].sum|tr[rs(o)].sum;
}

void pushdown(int o){
	if(tr[o].tag){
		tr[ls(o)].sum=tr[rs(o)].sum=(1ll<<tr[o].tag);
		tr[ls(o)].tag=tr[rs(o)].tag=tr[o].tag;
		tr[o].tag=0;
	}
} 

void update(int l,int r,int c,int o){
	if(l<=tr[o].l&&tr[o].r<=r){
		tr[o].tag=c;
		tr[o].sum=(1ll<<c);
		return;
	}
	pushdown(o);
	int mid=(tr[o].l+tr[o].r)>>1;
	if(r<=mid)
		update(l,r,c,ls(o));
	else if(l>mid)
		update(l,r,c,rs(o));
	else{
		update(l,mid,c,ls(o));
		update(mid+1,r,c,rs(o));
	}
	tr[o].sum=tr[ls(o)].sum|tr[rs(o)].sum;
}
ll query(int l,int r,int o){
	if(l<=tr[o].l&&tr[o].r<=r)
		return tr[o].sum;
	pushdown(o);
	int mid=(tr[o].l+tr[o].r)>>1;
	if(r<=mid)
		return query(l,r,ls(o));
	else if(l>mid)
		return query(l,r,rs(o));
	else{
		ll ans1=query(l,mid,ls(o));
		ll ans2=query(mid+1,r,rs(o));
		return ans1|ans2;
	}
}
int main(){
	//freopen("123.txt","w",stdout);
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&color[i]);
	}
	for(int i=1;i<n;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		vec[a].push_back(b);
		vec[b].push_back(a);
	}
	cnt=0;
	dfs(1,-1);
	build(1,n,1);
	for(int i=1;i<=m;i++){
		int type;
		scanf("%d",&type);
		if(type==1){
			int pos, c;
			scanf("%d %d",&pos,&c);
			update(id[pos].l,id[pos].r,c,1);
		}
		else{
			int pos;
			scanf("%d",&pos);
			ll ans=query(id[pos].l,id[pos].r,1);
			int sum=0;
			//printf("******%lld\n",ans);
			while(ans>0){
				ans-=ans&(-ans);
				sum++;
			}
			printf("%d\n",sum);
		}
	}
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值