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

来源:互联网 发布:淘宝开店店宝宝可靠吗 编辑:程序博客网 时间:2024/06/07 07:11

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 101 1 1 1 1 1 11 21 31 43 53 63 71 3 22 11 4 32 11 2 52 11 6 42 12 22 3
output
234512
input
23 301 2 2 6 5 3 2 1 1 1 2 4 5 3 4 4 3 3 3 3 3 4 61 21 31 42 52 63 73 84 94 104 116 126 137 147 157 168 178 1810 1910 2010 2111 2211 232 12 52 62 72 82 92 102 112 41 12 11 13 11 14 11 15 11 16 11 17 11 18 11 19 11 20 11 21 11 22 11 23 12 12 52 62 72 82 92 102 112 4
output
613321235512211123

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!

阅读全文
0 0
原创粉丝点击