[题目链接]

https://codeforces.com/contest/620/problem/E

[算法]

显然 , 一棵子树的DFS序必然为连续的一段

用线段树维护颜色数即可

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + ; struct edge
{
int to , nxt;
} e[MAXN << ]; int n , m , tot , timer;
int a[MAXN],dfn[MAXN],pos[MAXN],size[MAXN],head[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
struct SegmentTree
{
struct Node
{
int l , r , tag;
long long value;
} Tree[MAXN << ];
inline void update(int index)
{
Tree[index].value = Tree[index << ].value | Tree[index << | ].value;
}
inline void build(int index,int l,int r)
{
Tree[index].l = l;
Tree[index].r = r;
Tree[index].tag = -;
Tree[index].value = ;
if (l == r)
{
Tree[index].value = 1ll * << a[pos[l]];
return;
}
int mid = (l + r) >> ;
build(index << ,l,mid);
build(index << | ,mid + ,r);
update(index);
}
inline void pushdown(int index)
{
if (Tree[index].tag != -)
{
Tree[index << ].tag = Tree[index << | ].tag = Tree[index].tag;
Tree[index << ].value = Tree[index << | ].value = 1ll * << Tree[index].tag;
Tree[index].tag = -;
}
}
inline void modify(int index,int l,int r,int c)
{
if (Tree[index].l == l && Tree[index].r == r)
{
Tree[index].value = 1ll * << c;
Tree[index].tag = c;
return;
}
pushdown(index);
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) modify(index << ,l,r,c);
else if (mid + <= l) modify(index << | ,l,r,c);
else
{
modify(index << ,l,mid,c);
modify(index << | ,mid + ,r,c);
}
update(index);
}
inline long long query(int index,int l,int r)
{
if (Tree[index].l == l && Tree[index].r == r) return Tree[index].value;
pushdown(index);
int mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) return query(index << ,l,r);
else if (mid + <= l) return query(index << | ,l,r);
else return query(index << ,l,mid) | query(index << | ,mid + ,r);
}
} T; inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
}
inline void dfs(int u,int fa)
{
dfn[u] = ++timer;
pos[timer] = u;
size[u] = ;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == fa) continue;
dfs(v,u);
size[u] += size[v];
}
}
inline int calc(long long x)
{
int ret = ;
while (x > )
{
ret += x & ;
x >>= ;
}
return ret;
} int main()
{ read(n); read(m);
for (int i = ; i <= n; i++)
{
read(a[i]);
a[i]--;
}
for (int i = ; i < n; i++)
{
int x , y;
read(x); read(y);
addedge(x,y);
addedge(y,x);
}
dfs(,-);
T.build(,,n);
while (m--)
{
int op;
read(op);
if (op == )
{
int v , col;
read(v); read(col);
T.modify(,dfn[v],dfn[v] + size[v] - ,--col);
} else
{
int v;
read(v);
printf("%d\n",calc(T.query(,dfn[v],dfn[v] + size[v] - )));
}
}
return ; }

[Codeforces Education Round 6E] New Year Tree的更多相关文章

  1. Codeforces Education Round 11

    A(模拟+数学) 题意:在一个数列当中最少添加多少个数可以使它们两两互质,并打印出添加以后的数列 #include <iostream> #include <cstdio> # ...

  2. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  3. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  4. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  5. Codeforces Beta Round #57 (Div. 2)

    Codeforces Beta Round #57 (Div. 2) http://codeforces.com/contest/61 A #include<bits/stdc++.h> ...

  6. Codeforces Beta Round 84 (Div. 2 Only)

    layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: ...

  7. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  8. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  9. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

随机推荐

  1. oracle亲手安装过程

    适用于centos6 radhat6版本 1.检查依赖库: rpm -q binutils compat-libcap1 compat-libstdc++ compat-libstdc++.i686 ...

  2. 五段实用的js高级技巧

    技巧一之setTimeout. 应用案例:比如你想一个函数循环执行10次,怎么办?以前通常是先setInterval,然后clearInterval,技巧一就是克服这个问题 复制代码 代码如下: (f ...

  3. 简述HTTP报文请求方法和状态响应码

    1. Method 请求方法,表明客户端希望服务器对资源执行的动作: 1.1 GET 向服务器请求资源. 1.2 HEAD 和GET方法的行为类似,但服务器在响应中只返回首部,不会返回实体的主体部分. ...

  4. GROUP函数

    GROUP_ID 首先我们看看官方的解释: 大意是GROUP_ID用于区分相同分组标准的分组统计结果. 解释起来比较抽象,下面我们来看看具体的案例. 例1:单一分组 SQL> select gr ...

  5. list嵌套,int与str的用法,replace

    #*************************replace(待改,改动值),返回很重要 A = [['libai',89]] A[0][0]=A[0][0].replace('a','af') ...

  6. echarts的简单应用之(一)柱形图

    前段时间做项目需要绘制一些图表来展示信息,从网上资料来看用的比较多的是百度开源的echarts.echarts的官网上有API以及demo.上面的图形都是非常炫的,不过都是静态 数据,我们做项目时不可 ...

  7. Quartz.Net 学习之路01 安装Quartz.Net

    Quartz.Net 系列文章的第一篇,至于Quartz.Net 是做什么的我就不介绍了,相信要用到它的都知道它是用来干嘛的: Quartz.Net安装方法: 1.打开项目,在VS“工具”菜单选中“库 ...

  8. [NOIP2008] 提高组 洛谷P1125 笨小猴

    题目描述 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设maxn是单词中出现次数最 ...

  9. JSP发送电子邮件

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/sending-email.html: 发送一个简单的电子邮件 给出一个简单的例子,从机器上发送一个简单的 ...

  10. ubuntu12.04+cuda6.0+opencv2.4.9

    更新了cuda之后,opencv的gpu模块又要重新编译了,这个地方有一个疑问,我对cuda6.0装了两次,第一次装好之后,没有配一个bumblebee,重装了cuda6.0之后,发现原来编译的ope ...