首页 > 其他分享 >New Year Tree

New Year Tree

时间:2022-10-02 09:33:58浏览次数:35  
标签:10 le int Tree lt Year New

New Year Tree

Translation

  • 给出一棵 \(n\)个节点的树,根节点为 \(1\)。每个节点上有一种颜色 \(c_i\)。\(m\)次操作。操作有两种:
    1. 1 u c:将以 \(u\)为根的子树上的所有节点的颜色改为 \(c\)。
    2. 2 u:询问以 \(u\)为根的子树上的所有节点的颜色数量。
  • \(1\le n,m\le 4\times 10^5\),\(1\le c_i,c\le 60\)。

Description

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\).

Format

Input

The first line contains two integers \(n,m\)( \(1\le n,m\le 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\le c_{i}\le 60\)) — the colour of the \(i\)-th vertex.

Each of the next \(n-1\)lines contains two integers \(x_{j},y_{j}\)( \(1\le x_{j},y_{j}\le 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\le t_{k}\le 2\)) — the type of the \(k\)-th query. For the queries of the first type then follows two integers \(v_{k},c_{k}\)( \(1\le v_{k}\le n,1\le c_{k}\le 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\le v_{k}\le 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.

Samples

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

解析

数据结构综合题。

对于这种树上子树修改问题,最容易想到的是 dfs 序。因为,这样就可以把一个立体的东西拍平,转换为序列问题,而求解序列问题的手段有很多。

颜色总数只有 \(60\),可以考虑状态压缩。这题中我使用的是 bitset。bitset 可以处理很多集合问题。求并集直接取或即可。

最先 dfs。对于一个点的子树操作,在时间戳上,根节点出现位置和子树最后一个节点出现位置中进行操作。

线段树中合并区间,就是求并集。我们只需要使用线段树的区间赋值和查询操作。特别注意,下传延迟标记时,因为是赋值,所以需要判掉延迟标记为空的情况。

务必使用 C 风格输入输出,去掉 long long,否则 TLE ON 51

注解

本人代码中 bitset 的各种用法:

样式 用法 含义
[] [i] 查询右数第 \(i\) 位
any() s.any() 如果 \(s\) 不为空返回 \(1\)
reset() s.reset() 清空 \(s\)
| a|b 求 \(a,b\) 的并集
count() s.count() \(s\) 中 \(1\) 的个数

代码

// 620E New Year Tree
#include <bits/stdc++.h>
#define SIZE 1000010
#define all(x) x.begin(), x.end()
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;

bool memst;

int n, m;
int d[SIZE];
int dfn[SIZE], siz[SIZE], pos[SIZE];
int cnt=0;
vector<int> a[SIZE];

void dfs(int u, int fa)
{
	siz[u]=1;
	dfn[u]=++cnt;
	pos[cnt]=u;
	for(int v:a[u])	
		if(v!=fa) dfs(v, u), siz[u]+=siz[v];
}

struct segmentTreeNode
{
	int l, r;
	bitset<60> s;
	bitset<60> lt;
};

segmentTreeNode t[SIZE*4];
class segmentTree
{
public:
	void spread(int i)
	{
		if(t[i].lt.any())	// 因为是赋值,所以需要判掉延迟标记为空的情况。
		{
			t[i*2].s=t[i].lt;
			t[i*2+1].s=t[i].lt;
			t[i*2].lt=t[i].lt;
			t[i*2+1].lt=t[i].lt;
			t[i].lt.reset();
		}			
	}

	void build(int p, int l, int r)
	{
		t[p].l=l; t[p].r=r;
		if(l==r)
		{
			t[p].s[d[pos[l]]]=1;
			return;
		}
		int mid=(l+r)/2;
		build(p*2, l, mid);
		build(p*2+1, mid+1, r);
		t[p].s=t[p*2].s|t[p*2+1].s;
	}
	
	void change(int p, int l, int r, int c)
	{
		spread(p);
		if(l<=t[p].l && t[p].r<=r)
		{
			t[p].lt.reset();
			t[p].s.reset();
			t[p].s[c]=1; t[p].lt[c]=1;
			return;
		}
		t[p].s[c]=1;
		int mid=(t[p].l+t[p].r)/2;
		if(l<=mid) change(p*2,   l, r, c);
		if(r>mid)  change(p*2+1, l, r, c);
		t[p].s=t[p*2].s|t[p*2+1].s;
	}
	
	bitset<60> query(int p, int l, int r)
	{
		spread(p);
		if(l<=t[p].l && t[p].r<=r)
			return t[p].s;
		int mid=(t[p].l+t[p].r)/2;
		bitset<60> sum;
		if(l<=mid) sum|=query(p*2,   l, r);
		if(r>mid)  sum|=query(p*2+1, l, r);
		return sum;
	}
};

bool memed;

signed main()
{
	scanf("%d %d", &n, &m);
	for(int i=1; i<=n; i++) scanf("%d", d+i);
	for(int i=1; i<n; i++)
	{
		int u, v; scanf("%d %d", &u, &v);
		a[u].push_back(v);
		a[v].push_back(u);
	}
	dfs(1, 0);
	segmentTree st;
	st.build(1, 1, n);
	while(m--)
	{
		int o; scanf("%d", &o);
		if(o==1)
		{
			int v, c; scanf("%d %d", &v, &c);
			st.change(1, dfn[v], dfn[v]+siz[v]-1, c);
		}
		else
		{
			int v; scanf("%d", &v);
			printf("%d\n", st.query(1, dfn[v], dfn[v]+siz[v]-1).count());
		}
	}

    return 0;
}

标签:10,le,int,Tree,lt,Year,New
From: https://www.cnblogs.com/jerrywang2009/p/16748268.html

相关文章