您的位置:首页 > 其它

Codeforces 620E New Year Tree【DFS序+线段树区间染色+二进制思维+位运算】

2018-02-01 23:56 435 查看
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:

Change the colours of all vertices in the subtree of the vertex v to the colour c.

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

题意:

给你n个点,每个点都有一个初始颜色,然后搭建成一棵根节点为1的树,下面m个操作,操作共两种:1 x y 把以x节点为根节点的子树全染成y色,2 x 查询以x节点为根节点的子树颜色总数.

分析:

这道题关键是二进制的思维,把60种颜色用二进制枚举出来然后进行或运算,最后统计二进制中1的个数就是颜色总数。其次就是,DFS序预处理注意建树,用线段树维护区间染色,复杂度:O(4∗105∗60∗log[4∗105])≈O(5∗108)O(4∗105∗60∗log[4∗105])≈O(5∗108) .

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;

const LL MAXN = 4e5 + 5;
LL col[MAXN << 2], lazy[MAXN << 2];
LL in[MAXN], out[MAXN], vis[MAXN], num[MAXN];
vector<LL> G[MAXN];
LL time = 0;

void dfs(LL x, LL fa) {
in[x] = ++time;
num[time] = x;//把线性结构记录下来
for(LL i = 0; i < G[x].size(); i++) {
LL cnt = G[x][i];
if(cnt == fa) continue;
dfs(cnt, x);
}
out[x] = time;
}

void pushup(LL root) {
col[root] = col[root << 1] | col[root << 1 | 1];
}

void pushdown(LL root) {
if(lazy[root]) {
lazy[root << 1] = lazy[root];
lazy[root << 1 | 1] = lazy[root];
col[root << 1] = lazy[root];
col[root << 1 | 1] = lazy[root];
lazy[root] = 0;
}
}

void build(LL root, LL L, LL R) {
if(L == R) {
col[root] = vis[num[L]]; //建树时注意这一步,因为经过了DFS序的预处理
return ;
}
LL mid = (L + R) >> 1;
build(root << 1, L, mid);
build(root << 1 | 1, mid + 1, R);
pushup(root);
}

void updata(LL root, LL L, LL R, LL l, LL r, LL c) {
if(l <= L && R <= r) {
col[root] = c;
lazy[root] = c;
return ;
}
pushdown(root);
LL mid = (L + R) >> 1;
if(l <= mid) updata(root << 1, L, mid, l, r, c);
if(r > mid) updata(root << 1 | 1, mid + 1, R, l, r, c);
pushup(root);
}

LL query(LL root, LL L, LL R, LL l, LL r) {
if(l <= L && R <= r) {
return col[root];
}
pushdown(root);
LL ans = 0;
LL mid = (L + R) >> 1;
if(l <= mid) ans |= query(root << 1, L, mid, l, r);
if(r > mid) ans |= query(root << 1 | 1, mid + 1, R, l, r);
return ans;
}

LL judge(LL x) {
LL ans = 0;
while(x) {
if(x % 2 == 1) ans++;
x /= 2;
}
return ans;
}

int main() {
LL n, m;
memset(col, 0, sizeof(col));
memset(lazy, 0, sizeof(lazy));
scanf("%lld %lld", &n, &m);
for(LL i = 1; i <= n; i++) {
LL c;
scanf("%lld", &c);
vis[i] = 1LL << (c - 1); //注意longlong中1LL
}
for(LL i = 1; i < n; i++) {
LL x, y;
scanf("%lld %lld", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, -1);
build(1, 1, n); //后续建树
// for(LL i = 1; i <= n; i++) {
//     printf("%lld -> %lld\n", i, in[i]);
// }
while(m--) {
LL op, x, c;
scanf("%lld", &op);
if(op == 1) {
scanf("%lld %lld", &x, &c);
LL L = in[x];
LL R = out[x];
c = 1LL << (c - 1);
updata(1, 1, n, L, R, c);
}
else {
scanf("%lld", &x);
LL L = in[x];
LL R = out[x];
printf("%lld\n", judge(query(1, 1, n, L, R)));
}
}
return 0;
}

/*这组数据卡1LL,确实不容忽视
10 10
39 50 50 7 39 7 46 7 39 7
10 7
7 3
3 5
3 4
6 4
1 4
1 8
8 2
2 9
2 8
1 6 50
2 4
2 6
1 7 39
1 3 39
2 9
1 1 15
2 7
1 10 7
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: