Codeforces 620E New Year Tree (哈希 + 线段树)

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

                       New Year Tree

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.
Sample test(s)
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


一棵无向树(以1为根)。放对一个节点染色的时候,该节点的子树都被染成这个颜色。多次询问,每个询问为输入该树的某个节点然后输出它和它的子树有多少种颜色。

先将树dfs一次,把节点用dfs序表达出来。哈希成一段连续的序列。记住每个节点的区间范围。

我写这篇博客的目的是 因为这个地方WA n次:
由于颜色有60种,用二进制的 1<<60表示。
但是这样写 1 << col会WA
正确的姿势为: 1LL << col …..


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define lson l,mid,i<<1
#define rson mid+1,r,i<<1|1

const int N = 4 * 1e5 + 500;

struct Node
{
    int next,to;
    Node(){}
    Node(int a,int b):next(a),to(b){}
};
struct Node edge[N*2]; int head[N];  int nedges = -1;
void add(int a,int b)
{
    edge[++nedges] = Node(head[a],b);
    head[a] = nedges;
    edge[++nedges] = Node(head[b],a);
    head[b] = nedges;
}

bool vis[N];  int has[N]; int a[N]; int in[N];  int out[N]; int mark = 1;

void dfs(int u)
{
    in[u] = mark; has[mark] = u; vis[u] = true;
    for(int i=head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if( vis[v] ) continue;
        mark++; dfs(v);
    }
    out[u] = mark;
}
long long ans;
struct tree 
{
    long long val;
    int flag;
}tr[N*4];

void built(int l,int r,int i)
{
    if(l==r)
    {
        tr[i].val = 1LL<<(a[has[l]]);  // !!!!!
        tr[i].flag=0;
        return;
    }

    int mid=(l+r)>>1;
    built(lson);
    built(rson);
    tr[i].val = tr[i<<1].val | tr[i<<1|1].val ;
    return;
}
void pushdown(int i,int col)
{
    tr[i<<1].flag=tr[i<<1|1].flag=col;
    tr[i<<1].val=tr[i<<1|1].val=1LL<<col;
    tr[i].flag=0;
    return;
}
void update(int l,int r,int i,int a,int b,int col)
{
    if(l>=a&&r<=b)
    {
        tr[i].val = 1LL<<col;
        tr[i].flag=col;
        return;
    }
    int mid=(l+r)>>1;
    if(tr[i].flag) pushdown(i,tr[i].flag);
    if(mid>=a) update(lson,a,b,col);
    if(mid<b) update(rson,a,b,col);
    tr[i].val = tr[i<<1].val | tr[i<<1|1].val;
    return;
}
void query(int l,int r,int i,int a,int b)
{
    if(l>=a&&r<=b)
    {
        ans|= tr[i].val;
        return;
    }

    int mid=(l+r)>>1;
    if(tr[i].flag) pushdown(i,tr[i].flag);
    if(mid>=a) query(lson,a,b);
    if(mid<b) query(rson,a,b);
    return;
}

int main()
{
    int n,m; scanf("%d%d",&n,&m);
    memset(vis,false,sizeof(vis));
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<n;i++)
    {
        int u,v; scanf("%d%d",&u,&v);
        add(u,v);
    }
    dfs(1);
    built(1,n,1);
    while(m--)
    {
       int op;  scanf("%d",&op);
       if(op==1){
           int x,col; scanf("%d%d",&x,&col);
           update(1,n,1,in[x],out[x],col);
       }
       else{
           int x;  scanf("%d",&x);
           ans = 0;
           query(1,n,1,in[x],out[x]);
           int sum = 0;
           for(int i=1;i<=60;i++)
               if( (ans>>i) & 1) sum++;
               printf("%d\n",sum);
       }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值