• codeforces CF620E New Year Tree DFS序 线段树 状压


    $ Rightarrow $ 戳我进CF原题

    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 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·10^5) $
    — the number of vertices in the tree and the number of the queries.

    The second line contains $ n $ integers $ c_i (1 ≤ ci ≤ 60) $ — the colour of the $ i $ -th vertex.

    Each of the next $ n - 1 $ lines contains two integers $ x_j, y_j (1 ≤ x_j, y_j ≤ 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 $ t_k (1 ≤ t_k ≤ 2) $ — the type of the $ k $ -th query.
    For the queries of the first type then follows two integers $ v_k, c_k (1 ≤ v_k ≤ n, 1 ≤ c_k ≤ 60) $
    — the number of the vertex whose subtree will be recoloured with the colour $ c_k $ .
    For the queries of the second type then follows integer $ v_k (1 ≤ v_k ≤ 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

    input1

     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
    

    output1

     2
     3
     4
     5
     1
     2
    

    input2

     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
    

    output2

     6
     1
     3
     3
     2
     1
     2
     3
     5
     5
     1
     2
     2
     1
     1
     1
     2
     3
    

    题目大意

    • 给你一棵树,每个结点刚开始的时候都有一个颜色,

    • 现有操作

    1. $ u quad col $ :给这个结点及其子树染上 $ col $ 这种颜色
    2. $ u $ :查询以 $ u $ 为根节点的子树的所有颜色种类
    • 颜色数 $ le 60 quad n le 10^5 $

    思路

    • 我们看到这道题的颜色最多只有 $ 60 $ 种,所以理所应当的想到突破口就是颜色,
      $ 60 $ 的范围可以考虑状压,其实我们在乎的只是有或者没有该颜色,所以我们可以用或运算来合并两个块

    • 只有修改子树或者查询子树的操作,所以直接 $ dfs $ 序就可以

    • 对 $ dfs $ 序维护一个线段树,支持区间修改区间或,统计区间或之后得到的数在二进制下 $ 1 $ 的个数,即为答案

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    #define int long long  
    #define N 400005
    inline int read() {
        register char ch;
        while(!isdigit(ch=getchar()));
        register int x=ch^'0';
        while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
        return x;
    }
    vector<int>e[N];
    int n,m,c[N],id[N],in[N],out[N],tim,sum[N<<2],lzy[N<<2];
    void dfs(int u,int fa){
    	id[in[u]=++tim]=u;
    	for(int i=0;i<e[u].size();++i){
    		int v=e[u][i];
    		if(v!=fa) dfs(v,u);
    	}
    	out[u]=tim;
    }
    void build(int o,int l,int r){
    	if(l==r){
    		sum[o]=c[id[l]];
    		return;
    	}
    	int mid=l+r>>1;
    	build(o<<1,l,mid); build(o<<1|1,mid+1,r);
    	sum[o]=sum[o<<1]|sum[o<<1|1]; 
    }
    inline void pushdown(int o){
    	sum[o<<1]=sum[o<<1|1]=lzy[o<<1]=lzy[o<<1|1]=lzy[o];
    	lzy[o]=0;
    }
    void updata(int o,int l,int r,int L,int R,int val){
    	if(L<=l&&r<=R){
    		sum[o]=val;
    		lzy[o]=val;
    		return;
    	}
    	if(lzy[o]) pushdown(o);
    	int mid=l+r>>1;
    	if(L>mid) updata(o<<1|1,mid+1,r,L,R,val);
    	else if(R<=mid) updata(o<<1,l,mid,L,R,val);
    	else {
    		updata(o<<1,l,mid,L,R,val);
    		updata(o<<1|1,mid+1,r,L,R,val);
    	}
    	sum[o]=sum[o<<1]|sum[o<<1|1];
    }
    int query(int o,int l,int r,int L,int R){
    	if(L<=l&&r<=R) return sum[o];
    	if(lzy[o]) pushdown(o);
    	int mid=l+r>>1;
    	if(L>mid) return query(o<<1|1,mid+1,r,L,R);
    	else if(R<=mid) return query(o<<1,l,mid,L,R);
    	else return query(o<<1,l,mid,L,R)|query(o<<1|1,mid+1,r,L,R);
    	sum[o]=sum[o<<1]|sum[o<<1|1];
    }
    inline int calc(int x){
    	int res=0; 
    	while(x){ ++res; x-=x&(-x); }
    	return res;
    }
    signed main(){
    	n=read(); m=read();
    	for(int i=1;i<=n;++i) c[i]=1ll<<(read()-1);
    	for(int i=1;i<n;++i){
    		int u=read(),v=read();
    		e[u].push_back(v);
    		e[v].push_back(u);
    	}
    	dfs(1,0);
    	build(1,1,n);
    	while(m--){
    		int opt=read(),u=read();
    		if(opt==1) updata(1,1,n,in[u],out[u],1ll<<(read()-1));
    		else printf("%lld
    ",calc(query(1,1,n,in[u],out[u])));
    	}
    	return 0;
    }
    
  • 相关阅读:
    网络爬虫学习软件篇-Python(一)下载安装(超详细教程,傻瓜式说明)
    Vue.js+BootStrap+.Net Core开发后台管理系统 初次接触学习记录(11-7)
    形参 ref 到底是做什么用的?
    SweetAlert拒绝单一的弹出警告框
    轻松学习C语言编程之函数知识详解
    C语言的这个小知识点,竟然连开发多年的老司机都了解的不完全
    「C语言」编程学习—控制语句goto语句解析!
    数学思维+C语言画小猪佩奇,来试试?
    抖音很火的告白编程程序,C语言一样也能做
    世界最强的编程语言:C语言
  • 原文地址:https://www.cnblogs.com/PotremZ/p/CF620E.html
Copyright © 2020-2023  润新知