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
题意:
给定一棵n个节点的树,1节点一定是根节点,每个节点初始都被涂有颜色,有两种操作
一是将某个节点的子树全部涂为某个颜色(不包括当前节点)
二是询问某个节点的子树上的所有颜色的种类数(包括当前节点)
思路:
一共有60种颜色,可以用longlong的二进制存储,每个1代表一种颜色
用dfs+时间戳记录每个节点,这样每个子树节点都在一起
线段树维护时间戳
用按位或运算来求
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#define max_ 401000
#define inf 0x3f3f3f3f
#define ll long long
#define les 1e-8
using namespace std;
struct node
{
int l,r;
ll w;
int f;//标记
};
struct node tree[max_*4];
int n,m,ti=0;
int col[max_],in[max_],out[max_],num[max_];
bool vis[max_];
vector<int>v[max_];
ll ans;
void built(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
tree[i].f=0;
if(l==r)
{
tree[i].w=(1ll<<col[num[l]]);
return;
}
int mid=(l+r)>>1;
built(i<<1,l,mid);
built(i<<1|1,mid+1,r);
tree[i].w=(tree[i<<1].w|tree[i<<1|1].w);
}
void updata(int i,int l,int r,int v)
{
if(tree[i].l==l&&tree[i].r==r)
{
tree[i].f=1;
tree[i].w=(1ll<<v);
return;
}
if(tree[i].f)
{
tree[i].f=0;
tree[i<<1].f=tree[i<<1|1].f=1;
tree[i<<1].w=tree[i<<1|1].w=tree[i].w;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(r<=mid)
updata(i<<1,l,r,v);
else if(l>mid)
updata(i<<1|1,l,r,v);
else
{
updata(i<<1,l,mid,v);
updata(i<<1|1,mid+1,r,v);
}
tree[i].w=(tree[i<<1].w|tree[i<<1|1].w);
}
void query(int i,int l,int r)
{
if(tree[i].l==l&&tree[i].r==r)
{
ans|=tree[i].w;
return;
}
if(tree[i].f)
{
tree[i].f=0;
tree[i<<1].f=tree[i<<1|1].f=1;
tree[i<<1].w=tree[i<<1|1].w=tree[i].w;
}
int mid=(tree[i].l+tree[i].r)>>1;
if(r<=mid)
query(i<<1,l,r);
else if(l>mid)
query(i<<1|1,l,r);
else
{
query(i<<1,l,mid);
query(i<<1|1,mid+1,r);
}
}
void dfs(int x)
{
in[x]=++ti;//in储存进入节点的时间
num[ti]=x;//记录每个时间进入节点的值,方便更新
for(int i=0;i<v[x].size();i++)
{
if(vis[v[x][i]]==false)
{
vis[v[x][i]]=true;
dfs(v[x][i]);
}
}
out[x]=ti;//out储存退出节点的时间
}
int main(int argc, char const *argv[]) {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&col[i]);//每个的初始颜色
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
vis[1]=true;
dfs(1);
built(1,1,n);
while(m--)
{
int tp;
scanf("%d",&tp);
if(tp==1)
{
int x,y;
scanf("%d%d",&x,&y);
updata(1,in[x],out[x],y);
}
else
{
int x;
scanf("%d",&x);
ans=0;
query(1,in[x],out[x]);
int sum=0;
for(;ans;ans-=ans&(-ans))//每次ans-=ans&(-ans)都把最低位的1去掉了 即lowbit函数
sum++;
printf("%d\n",sum );
}
}
return 0;
}