Codeforces 620E New Year Tree dfs序+线段树+状态压缩

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·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
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

Source

Educational Codeforces Round 6


My Solution

题意:给定一棵树,每个节点都有颜色,然后询问子树上有多少种不同的颜色。


dfs序+线段树+状态压缩
由于只有60种颜色(2^60 < 2^63),所以可以直接用二进制压位。

即sum[Ind]维护的是该区间的一个状态,从右向左第i位表示第i种颜色在该区间是否出现。

然后用上线段树区间修改+区间查询即可。

时间复杂度 O(nlogn)

空间复杂度 O(4*n)


#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 4e5 + 8;

vector<int> sons[MAXN];
int color[MAXN];
//dfs序
int p1[MAXN], p2[MAXN], ti = 0;
int dfsnum[MAXN];  //这个按情况是否需要。
inline void get_dfs_list(int u, int fa){
    p1[u] = ++ti;
    dfsnum[ti] = u; //
    int sz = sons[u].size(), i, v;
    for(i = 0; i < sz; i++){
        v = sons[u][i];
        if(v == fa) continue;
        get_dfs_list(v, u);
    }
    p2[u] = ti;
}
//线段树
LL sum[4*MAXN], lazy[4*MAXN];
int size;
inline void pushup(int Ind){
    sum[Ind] = sum[Ind<<1] | sum[(Ind<<1)|1];
}

inline void pushdown(int Ind){
    sum[Ind<<1] = lazy[Ind];
    sum[(Ind<<1)|1] = lazy[Ind];
    lazy[Ind<<1] = lazy[Ind];
    lazy[(Ind<<1)|1] = lazy[Ind];
    lazy[Ind] = 0;
}

inline LL _Query(int a, int b, int l, int r, int Ind){
    if(a <= l && b >= r) return sum[Ind];
    int mid = (l+r)>>1;
    if(lazy[Ind]) pushdown(Ind);
    LL ret = 0;
    if(a <= mid) ret |= _Query(a, b, l, mid, Ind<<1);
    if(b > mid) ret |= _Query(a, b, mid + 1, r, (Ind<<1)|1);
    //pushup(Ind);
    return ret;
}

inline void _Modify(int a, int b, int l, int r, int Ind, LL d){
     if(a <= l && b >= r){
        sum[Ind] = d;
        lazy[Ind] = d;
        return;
    }
    int mid = (l+r)>>1;
    if(lazy[Ind]) pushdown(Ind);
    if(a <= mid) _Modify(a, b, l, mid, Ind<<1, d);
    if(b > mid) _Modify(a, b, mid + 1, r, (Ind<<1)|1, d);
    pushup(Ind);
}

inline void _build(int l, int r, int Ind){
    if(l == r){
        sum[Ind] = 1ll << (color[dfsnum[l]] - 1);
        return;
    }
    int mid = (l+r)>>1;
    _build(l, mid, Ind<<1);
    _build(mid + 1, r, (Ind<<1)|1);
    pushup(Ind);
}

inline LL Query(int a, int b) {return _Query(a, b, 1, size, 1);}
inline void Modify(int a, int b, LL d){return _Modify(a, b, 1, size, 1, d);}


int main()
{
    #ifdef LOCAL
    freopen("c.txt", "r", stdin);
    //freopen("c.out", "w", stdout);
    int T = 3;
    while(T--){
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    int n, m, i, u, v, root, type, xi, ci, ans;
    LL val;
    cin >> n >> m;
    for(i = 1; i <= n; i++){
        cin >> color[i];
    }
    for(i = 1; i < n; i++){
        cin >> u >> v;
        sons[u].push_back(v);
        sons[v].push_back(u);
    }
    root = 1;
    ti = 0;
    get_dfs_list(root, -1);
    size = ti;
    _build(1, size, 1);
    while(m--){
        cin >> type;
        if(type == 1){
            cin >> xi >> ci;
            Modify(p1[xi], p2[xi], 1ll <<(ci-1));
        }
        else{
            cin >> xi;
            val = Query(p1[xi], p2[xi]);
            ans = 0;
            while(val){
                if(val & 1){
                    ans++;
                }
                val >>= 1;
            }
            cout << ans << "\n";
        }
    }


    #ifdef LOCAL
    for(i = 1; i <= n; i++){
        sons[i].clear();
    }
    memset(sum, 0, sizeof sum);
    memset(lazy, 0, sizeof lazy);
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}


                                                                                                                                             ------from ProLights

  Thank you!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值