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. 生信基础概念之unique reads VS multi-mapping reads

    unique reads:在参考组上只有一个匹配点 multi-mapping reads:在参考组上有多个匹配点 下面是tophat的一个结果案例: Reads: Input : Mapped : ...

  2. 获取CPU利用率

    #define MB (1024 * 1024) MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex); GlobalMemoryStatu ...

  3. SQL Server technical bulletin - How to resolve a deadlock

    https://support.microsoft.com/en-us/help/832524/sql-server-technical-bulletin-how-to-resolve-a-deadl ...

  4. Apache Phoenix的子查询

    Phoenix现在支持在WHERE 和FROM 中使用子查询.子查询可以被指定在很多地方,比如 IN/NOT IN, EXISTS/NOTEXISTS等. Subqueries with INor N ...

  5. Linux嵌入式 -- 内核 - 内核定时器

    1.  度量时间差 时钟中断由系统的定时硬件以周期性的时间间隔产生,这个间隔(即频率)由内核根据HZ来确定,HZ是一个与体系结构无关的常数,可配置(50-1200),在X86平台,默认值为1000(每 ...

  6. BZOJ4199/UOJ131 [Noi2015]品酒大会

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  7. hibernate报错org.hibernate.HibernateException: No CurrentSessionContext configured!

    org.hibernate.HibernateException: No CurrentSessionContext configured! at org.hibernate.internal.Ses ...

  8. php攻击漏洞总结

    1.两字节编码(gbk)都存在宽字节攻击问题[character_set_client=gbk] 案例:http://www.cnblogs.com/lcamry/articles/5625276.h ...

  9. window.name 跨域数据传输

    通过window.name可以实现跨域数据传输. 要解决的功能:  www.a.com/a.html 需要获取到 www.b.com/b.html页面内容的数据 需要3个页面 www.a.com/a. ...

  10. hzau 1205 Sequence Number(二分)

    G. Sequence Number In Linear algebra, we have learned the definition of inversion number: Assuming A ...