CodeForces 620E:New Year Tree(dfs序+线段树)
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序+线段树)的更多相关文章
- codeforces 620E. New Year Tree dfs序+线段树+bitset
题目链接 给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60. 每个节点一个bitset维护就可以. #in ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- 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 ...
- POJ3321 - Apple Tree DFS序 + 线段树或树状数组
Apple Tree:http://poj.org/problem?id=3321 题意: 告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这 ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- 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 ...
- codechef T6 Pishty and tree dfs序+线段树
PSHTTR: Pishty 和城堡题目描述 Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格 的古堡和非常聪明的熊闻名全国. 胡斯特的镇城之宝是就是这么一座古 ...
- 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 ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
随机推荐
- Qt QThread 线程创建,线程同步,线程通信 实例
1. 继承QThread, 实现run()方法, 即可创建线程. 2. 实例1 代码 myThread.h #ifndef MYTHREAD_H #define MYTHREAD_H #includ ...
- Spring AOP的一个比喻和IOC的作用
aop切面编程就是在常规的执行java类中方法前或执行后加入自定义的方法.比如你本来每天都去打酱油,去,打酱油,回.现在我每天在你打酱油路上等着,你去打酱油的时候我打你一顿,回来的时候给你点糖果吃.你 ...
- iOS审核拒绝苹果官方原因详解
1.1不当内容应用程序不应该包括攻击性,敏感,令人不悦,侮辱或者品味低下的内容.例如: 1.1.1 诽谤或者人格侮辱的内容,包括引用或者评论宗教.种族.性取向.性别或者其他目标群体的内容,特别是该应用 ...
- demo(幸福大转盘)总结
百度推广首页demo 在<head>与</head>之间加入代码 <link rel="shortcut icon" href="favic ...
- 微信开放接口获取用户昵称保存到MySQL中为空白
微信昵称中包含emoji表情标签,某些标签是使用了4字节编码的UTF8. 而大多数MySQL数据库现在使用的是3字节UTF8编码,这样会导致保存为空,且不会提示失败. 解决方法有2个,一个是升级到My ...
- sql处理数据库锁的存储过程
/*--处理死锁 查看当前进程,或死锁进程,并能自动杀掉死进程 因为是针对死的,所以如果有死锁进程,只能查看死锁进程 当然,你可以通过参数控制,不管有没有死锁,都只查看死锁进程 --邹建 2004.4 ...
- DataGrid的组成论述(WPF)
DataGrid:它标示是一个整体概念,是个大容器,包含Row的感念 Column:是表格的组成成分(表格是由列组成的),它包括Header和Cell的概念
- HANA 与BW
本来备件折让有个挺麻烦的.我当时一度以为不能用BW做,其实自己也想用hana做,发现坤哥说的 的很有道理.BW都没有学懂,都是迷迷糊糊的.再去做hana 弄的四不像.先沉下心来,在bw里把这个项目做好 ...
- 15 Python 迭代器和生成器
什么是迭代 (iterable) 字符串.列表.元组.字典.集合都可以被for循环,说明他们都是可迭代的. 可以直接作用于for循环的对象统称为可迭代对象(Iterable). 可以被next()函数 ...
- jira 从数据库 切换到mysql
通过JIRA管理员登录,进入“管理员页面”,“系统”--“导入&导出”,以XML格式备份数据. 在MySQL中创建Schema,命名为jira 关闭JIRA服务 备份一下JIRA的安装目录和数 ...