zoukankan      html  css  js  c++  java
  • Codeforces

     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
    ",ct);
            }
        }
    
    }
    View Code
    PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~
     
  • 相关阅读:
    mysql常用方法案例
    springboot整合mybatis
    mysql自定义函数统计订单状态:GET_ORDER_STATUS()
    mysql计算时间差-本例为计算分钟差然后/60计算小时保留一位小数,由于直接得小时只会取整
    mysql字段值为null时排序问题
    对象与内存(一)
    java基础提升(关于数组)
    项目的部署
    myeclipse中ssm的搭建
    ui自动化笔记 selenium_webdriver,ui自动化框架(web)
  • 原文地址:https://www.cnblogs.com/MengX/p/9498315.html
Copyright © 2011-2022 走看看