Codeforces - 620E New Year Tree(dfs序列+线段树染色)

 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
Copy
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
Copy
2
3
4
5
1
2
题意:给出一个无向树和每个顶点的颜色,然后进行q次操作,1.修改区间颜色,2.求顶点x为根所在子树的颜色数
思路:dfs序(+线段树(常规染色操作)

#include<bits/stdc++.h>
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pb push_back
#define P pair<int,int>
using namespace std;
const int maxn = 4*1e5+7;
ll sum[maxn<<2];
int head[maxn],Next[maxn*20],To[maxn*20],cnt;
int in[maxn],out[maxn],pos[maxn],tot,c[maxn],lazy[maxn<<2];
void add(int u,int v)
{
    Next[++cnt]=head[u];
    head[u]=cnt;
    To[cnt]=v;
}
void dfs(int u,int fa)
{
    in[u]=++tot;
    pos[tot]=u;//记录下当前的节点,后面建树对应c[pos[l]]
    for(int i=head[u]; i!=-1; i=Next[i])
    {
        if(To[i]==fa) continue;
        dfs(To[i],u);
    }
    out[u]=tot;
}
void push_up(int rt)
{
    sum[rt]=sum[rt<<1]|sum[rt<<1|1];
}
void push_down(int rt)
{
    if(lazy[rt])
    {
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        sum[rt<<1]=sum[rt<<1|1]=(1LL<<lazy[rt]);
        lazy[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=1LL<<c[pos[l]];
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt);
}
void updata(int l,int r,int rt,int L,int R,int val)
{
  if(L<=l&&r<=R)
  {
      sum[rt]=1LL<<val;
      lazy[rt]=val;
      return;
  }
  push_down(rt);
  int m=(l+r)>>1;
  if(L<=m) updata(lson,L,R,val);
  if(R>m) updata(rson,L,R,val);
  push_up(rt);
}
ll query(int l,int r,int rt,int L,int R)
{
    if(L<=l&&r<=R) return sum[rt];
    push_down(rt);
    int m=(l+r)>>1;
    ll ans=0;
    if(L<=m) ans|=query(lson,L,R);
    if(R>m) ans|=query(rson,L,R);
    return ans;
}
int main()
{
    int n,m,u,v;
    scanf("%d %d",&n,&m);
    for(int i=1; i<=n; i++) scanf("%d",&c[i]);
    memset(head,-1,sizeof(head));
    for(int i=1; i<n; i++)
    {
        scanf("%d %d",&u,&v);
        add(u,v);
        add(v,u);
    }
    dfs(1,-1);
    build(1,n,1);
    while(m--)
    {
        int op,val,x;
        scanf("%d",&op);
        if(op==1)
        {
            scanf("%d %d",&x,&val);
            updata(1,n,1,in[x],out[x],val);
        }
        else
        {
            scanf("%d",&x);
            ll ans=query(1,n,1,in[x],out[x]);
            int ct=0;
            while(ans)
            {
                ct+=(ans%2);
                ans/=2;
            }
            printf("%d\n",ct);
        }
    }

}
View Code

 

 

 

PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~
 

转载于:https://www.cnblogs.com/MengX/p/9498315.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值