E. New Year Tree
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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:

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

因为只有60种颜色,所以用二进制数表示颜色的种类。

#include <cstdio>
#include <string.h>
using namespace std;
const int MAXN=;
typedef long long LL;
struct Edge{
int to,net;
}es[MAXN+MAXN];
int head[MAXN],tot;
int n,m;
int val[MAXN],Hash[MAXN];
void addedge(int u,int v)
{
es[tot].to=v;
es[tot].net=head[u];
head[u]=tot++;
}
int lch[MAXN],rch[MAXN],key;
void dfs(int u,int fa)
{
lch[u]=++key;
Hash[lch[u]]=val[u];
for(int i=head[u];i!=-;i=es[i].net)
{
if(es[i].to!=fa)
{
dfs(es[i].to,u);
}
}
rch[u]=key;
}
struct Node{
int l,r;
LL color,lazy;
}a[MAXN*];
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].lazy=;
if(l==r)
{
a[rt].color=1LL<<Hash[l];
return ;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
a[rt].color=(a[rt<<].color | a[(rt<<)|].color);
}
void pushDown(int rt)
{
a[rt<<].color=a[rt].color;
a[(rt<<)|].color=a[rt].color;
a[rt<<].lazy=a[rt].lazy;
a[(rt<<)|].lazy=a[rt].lazy;
a[rt].lazy=;
}
void update(int rt,int l,int r,int v)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].color=1LL<<v;
a[rt].lazy=1LL<<v;
return ;
}
if(a[rt].lazy!=)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
{
update(rt<<,l,r,v);
}
else if(mid<l)
{
update((rt<<)|,l,r,v);
}
else
{
update(rt<<,l,mid,v);
update((rt<<)|,mid+,r,v);
}
a[rt].color=(a[rt<<].color | a[(rt<<)|].color);
}
LL res;
void query(int rt,int l,int r)
{
if(a[rt].l==l&&a[rt].r==r)
{
res|=a[rt].color;
return ;
}
if(a[rt].lazy!=)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
{
query(rt<<,l,r);
}
else if(mid<l)
{
query((rt<<)|,l,r);
}
else
{
query(rt<<,l,mid);
query((rt<<)|,mid+,r);
}
a[rt].color=(a[rt<<].color | a[(rt<<)|].color);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-,sizeof(head));
tot=;
key=;
for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
}
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs(,-);
build(,,n);
for(int i=;i<m;i++)
{
int type;
scanf("%d",&type);
if(type==)
{
int x,y;
scanf("%d%d",&x,&y);
update(,lch[x],rch[x],y);
}
else
{
int x;
scanf("%d",&x);
res=;
query(,lch[x],rch[x]);
int sum=;
while(res>)
{
if(res&) sum++;
res>>=;
}
printf("%d\n",sum);
}
}
}
return ;
}

CodeForces 620E:New Year Tree(dfs序+线段树)的更多相关文章

  1. codeforces 620E. New Year Tree dfs序+线段树+bitset

    题目链接 给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60. 每个节点一个bitset维护就可以. #in ...

  2. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  3. Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树

    题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  4. POJ3321 - Apple Tree DFS序 + 线段树或树状数组

    Apple Tree:http://poj.org/problem?id=3321 题意: 告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这 ...

  5. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  6. codeforces 916E Jamie and Tree dfs序列化+线段树+LCA

    E. Jamie and Tree time limit per test 2.5 seconds memory limit per test 256 megabytes input standard ...

  7. codechef T6 Pishty and tree dfs序+线段树

    PSHTTR: Pishty 和城堡题目描述 Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格 的古堡和非常聪明的熊闻名全国. 胡斯特的镇城之宝是就是这么一座古 ...

  8. codeforces 633G. Yash And Trees dfs序+线段树+bitset

    题目链接 G. Yash And Trees time limit per test 4 seconds memory limit per test 512 megabytes input stand ...

  9. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

随机推荐

  1. Python字符串格式转换

    转换类型 转换类型 说明 d, i 带符号十进制 b 无符号二进制 o 无符号八进制 u 无符号十进制 x 无符号十六进制(小写) X 无符号十六进制(大写) e 科学计数法表示的浮点数(小写) E ...

  2. nodejs文件追加内容

    const fs = require("fs"); // fs.appendFile 追加文件内容 // 1, 参数1:表示要向那个文件追加内容,只一个文件的路径 // 2, 参数 ...

  3. 克隆VMware虚拟机

    虚拟机使用过程需要用到多个进行实验.重新安装时间又太长,通过vmware虚拟机自带软件能够很好的快速克隆出完全相同的系统. 环境:关闭VMware,不要在开启状态哦~ 步骤: 选中需要被克隆的系统 菜 ...

  4. python安装包

    1. python常用包下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy numpy包下载地址:https://pypi.python.org ...

  5. 【bzoj3747】Kinoman[POI2015](线段树)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3747 对于这种题,考虑固定区间的右端点为r,设区间左端点为l能取得的好看值总和为a[l] ...

  6. SpringMVC中响应json数据(异步传送)

    1.首先导入3个jar包: jackson-annotations-2.1.5.jar jackson-core-2.1.5.jar jackson-databind-2.1.5.jar JSON所需 ...

  7. 泛型学习第一天:List与IList的区别 (一)

    先看代码: using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace L ...

  8. mysql在表的某一位置增加一列的命令

    如果想在一个已经建好的表中添加一列,可以用诸如: alter table t1 add column addr varchar(20) not null; 这条语句会向已有的表t1中加入一列addr, ...

  9. Spring Boot入门——json数据处理

    1.引入fastJson插件 <!-- 引入fastjson插件 --> <dependency> <groupId>com.alibaba</groupId ...

  10. 利用Swoole实现PHP+websocket直播,即使通讯代码,及linux下swoole安装基本配置

    swoole安装基本配置 php安装swoole 1. 下载swoole安装 wget http://pecl.php.net/get/swoole-1.9.1.tgz tar -zxvf swool ...