天天看點

coderforce Educational Codeforces Round 6 E.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 vertex1.

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 thei-th vertex.

Each of the next n - 1 lines contains two integersxj, 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 integertk (1 ≤ tk ≤ 2) — the type of the k-th query. For the queries of the first type then follows two integersvk, 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 integervk (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
      

給你一棵樹每次更新或者查詢子樹的顔色種類,或者更新某一棵子樹上面的顔色為某一種,顔色總數為60,那麼一看就類似與區間更新用一個longlong的來儲存該數裡面的顔色,也就是狀壓,但是這是樹上面的操作,可以用DFS把一顆樹轉為一個長線段,L和R數字表示以該節點為根的子樹的區間範圍,那麼就轉換成了一個普通的區間更新,最後注意細節和long long的維護。

#include<bits/stdc++.h>
//#include<windows.h>
using namespace std;
#define maxn 400050
int n,m,tid;
int c[maxn];
int L[maxn],R[maxn];
int ID[maxn];
vector<int>mp[maxn];
struct node
{
    int l;
    int r;
    long long flag;
    long long cur;
}tree[maxn*8];
void build(int id,int l,int r)
{
    tree[id].l=l;
    tree[id].r=r;
    tree[id].flag=0;
    if(l==r)
    {
        tree[id].cur=(long long)((long long)1<<c[ID[l]]);
        return ;
    }
    int mid=(l+r)/2;
    build(id*2,l,mid);
    build(id*2+1,mid+1,r);
    tree[id].cur=tree[id*2].cur|tree[id*2+1].cur;
}
void pushup(int id)
{
    if(tree[id].flag!=0)
    {
        tree[id*2].flag=tree[id].flag;
        tree[id*2+1].flag=tree[id].flag;

        tree[id*2].cur=tree[id*2].flag;
        tree[id*2+1].cur=tree[id*2+1].flag;

        tree[id].flag=0;
    }
}
void update(int id,int x,int y,long long z)
{
    int l=tree[id].l;
    int r=tree[id].r;
    if(r<x||l>y)
        return ;
    if(l>=x&&r<=y)
    {
        pushup(id);
        tree[id].flag=z;
        tree[id].cur=z;
        return ;
    }
    pushup(id);
    update(id*2,x,y,z);
    update(id*2+1,x,y,z);
    tree[id].cur=tree[id*2].cur|tree[id*2+1].cur;
}
long long query(int id,int x,int y)
{
    int l=tree[id].l;
    int r=tree[id].r;
    int mid=(l+r)/2;
    if(r<x||l>y)
        return 0;
    if(l>=x&&r<=y)
    {
        pushup(id);
        return tree[id].cur;
    }
    pushup(id);
    return query(id*2,x,y)|query(id*2+1,x,y);
}
int getsum(long long cur)
{
    long long tmp=cur;
    int sum=0;
    while(tmp)
    {
        sum+=tmp%2;
        tmp/=2;
    }
    return sum;
}
void dfs(int now)
{
    if(L[now]!=0)
        return ;
    L[now]=++tid;
    int len=mp[now].size();
    for(int i=0;i<len;i++)
        dfs(mp[now][i]);
    R[now]=tid;
}
int main()
{
    scanf("%d%d",&n,&m);
    tid=0;
    memset(L,0,sizeof(L));
    for(int i=1;i<=n;i++)
        scanf("%d",&c[i]);
    for(int i=1;i<=n-1;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        mp[x].push_back(y);
        mp[y].push_back(x);
    }
    dfs(1);
    for(int i=1;i<=n;i++)
        ID[L[i]]=i;
    build(1,1,n);
    while(m--)
    {
        int t;
        scanf("%d",&t);
        if(t==1)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            update(1,L[x],R[x],(long long)1<<y);

        }
        else
        {
            int z;
            scanf("%d",&z);
            long long ans=query(1,L[z],R[z]);
            printf("%d\n",getsum(ans));
        }
    }
return 0;
}