【Educational Codeforces Round 6E】【线段树 dfs序】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
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<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=4e5+10,M=0,Z=1e9+7,ms63=0x3f3f3f3f;
int n,m,x,y;
int col[N];
int first[N], id; int w[N << 1], nxt[N << 1]; bool vis[N];
int in[N], out[N]; int p[N]; int tim;
LL b[64];
LL C;
struct A
{
	int l,r;
	LL flag;
	LL c;
}a[1<<20];
void ins(int x,int y)
{
	++id;
	w[id]=y;
	nxt[id]=first[x];
	first[x]=id;
}
void dfs(int x)
{
	vis[x]=1;
	in[x]=++tim;
	p[tim]=col[x];
	for(int z=first[x];z;z=nxt[z])
	{
		int y=w[z];
		if(vis[y])continue;
		dfs(y);
	}
	out[x]=tim;
}
void pushdown(int o)
{
	if(a[o].flag)
	{
		a[ls].flag=a[rs].flag=a[o].flag;
		a[ls].c |= a[o].flag;
		a[rs].c |= a[o].flag;
		a[o].flag=0;
	}
}
void pushup(int o)
{
	a[o].c=a[ls].c|a[rs].c;
}
void build(int o,int l,int r)
{
	a[o].l = l;
	a[o].r = r;
	a[o].flag = 0;
	if(l==r)
	{
		a[o].c|=b[ p[l] ];
		return;
	}
	int mid=(l+r)>>1;
	build(ls,l,mid);
	build(rs,mid+1,r);
	pushup(o);
	return;
}
void change(int o,int l,int r,LL v)
{
	if(a[o].l==l&&a[o].r==r)
	{
		a[o].flag = a[o].c = v;
		return;
	}
	pushdown(o);
	int mid=(a[o].l+a[o].r)>>1;
	if(r<=mid)change(ls,l,r,v);
	else if(l>mid)change(rs,l,r,v);
	else
	{
		change(ls,l,mid,v);
		change(rs,mid+1,r,v);
	}
	pushup(o);
}
void update(int o,int l,int r)
{
	if(a[o].flag)
	{
		C |= a[o].flag;
		return;
	}
	if(a[o].l==l&&a[o].r==r)
	{
		C |= a[o].c;
		return;
	}
	int mid=(a[o].l+a[o].r)>>1;
	if(r<=mid)update(ls,l,r);
	else if(l>mid)update(rs,l,r);
	else
	{
		update(ls,l,mid);
		update(rs,mid+1,r);
	}
	pushup(o);
}
int main()
{
	for (int i = 0; i <= 60; ++i)b[i] = 1ll << i;
	while(~scanf("%d%d",&n,&m))
	{
		memset(first,0,n+2<<2);id=1;
		for(int i=1;i<=n;++i)scanf("%d",&col[i]);
		for(int i=1;i<n;++i)
		{
			scanf("%d%d",&x,&y);
			ins(x,y);
			ins(y,x);
		}
		MS(vis,0);
		tim=0;
		dfs(1);
		build(1,1,n);
		for(int i=1;i<=m;++i)
		{
			int o;
			int p,v;
			scanf("%d",&o);
			if(o==1)
			{
				scanf("%d%d",&p,&v);
				change(1,in[p],out[p],b[v]);
			}
			else
			{
				scanf("%d",&p);
				C = 0;
				update(1,in[p],out[p]);
				printf("%d\n", __builtin_popcountll(C));
			}
		}
	}
	return 0;
}
/*
【题意】
给你一棵树,有n(4e5)个节点,每个节点有一个颜色,颜色范围在[1,60]
我们有两种操作。
操作1:把节点p的子树都改为颜色v
操作2:查询以p为根的子树有多少种不同的颜色。

【类型】
线段树
dfs序

【分析】
我们先得到in和out的dfs序,
然后建线段树,每个节点开60个bool数组,表示颜色数
(或者用LL来表示)

然后对于修改操作,使用flag打标记
对于查询操作,做全局或运算,然后用__builtin_popcountll()求1的个数即可

【时间复杂度&&优化】
O(mlogn)

*/


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值