题目链接:点击打开链接

题意:给你一棵树,编号1~n,告诉你根结点是1。 每次有两个操作:

1,将以v为根的子树的结点全部染成颜色c

2,问以v为根的紫书的结点的颜色种类。

思路:如果这是一条线段的话, 那么这就是线段树的区间更新问题,而现在是一棵树。

因为告诉了根结点是1, 那么这棵树的任意一个结点的子树就是确定的, 所以我们可以用DFS的先序遍历,将所有结点重新编号,因为先序遍历的话, 任意一个结点和其子树的编号就是一条连续的线段了,在这其中维护每个结点的新编号, 和这个结点的子树中的最大编号即可。

然后就是线段树区间更新了, 由于颜色数最大60, 用long long通过位运算的 | 操作就行了, 注意对1左移的时候应该先将1转成long long再进行操作。

细节参见代码:

// Author : RioTian
// Time : 21/01/09
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 1000000000 + 7;
const int INF = 1000000000;
const int maxn = 400000 + 10;
int T, n, m, u, v, id[maxn], a[maxn], cnt, last[maxn], b[maxn], setv[maxn << 2];
bool vis[maxn];
ll sum[maxn << 2];
vector<int> g[maxn];
void dfs(int root) {
id[root] = ++cnt;
vis[root] = true;
int len = g[root].size();
for (int i = 0; i < len; i++) {
int v = g[root][i];
if (!vis[v]) {
dfs(v);
}
}
last[root] = cnt;
}
void PushUp(int o) {
sum[o] = sum[o << 1] | sum[o << 1 | 1];
}
void pushdown(int l, int r, int o) {
if (setv[o]) {
setv[o << 1] = setv[o << 1 | 1] = setv[o];
sum[o << 1] = sum[o << 1 | 1] = (1LL << setv[o]);
setv[o] = 0;
}
}
void build(int l, int r, int o) {
int m = (l + r) >> 1;
setv[o] = 0;
if (l == r) {
sum[o] = 1LL << b[++cnt];
return;
}
build(l, m, o << 1);
build(m + 1, r, o << 1 | 1);
PushUp(o);
}
void update(int L, int R, int v, int l, int r, int o) {
int m = (l + r) >> 1;
if (L <= l && r <= R) {
setv[o] = v;
sum[o] = (1LL << v);
return;
}
pushdown(l, r, o);
if (L <= m)
update(L, R, v, l, m, o << 1);
if (m < R)
update(L, R, v, m + 1, r, o << 1 | 1);
PushUp(o);
}
ll query(int L, int R, int l, int r, int o) {
int m = (l + r) >> 1;
if (L <= l && r <= R) {
return sum[o];
}
pushdown(l, r, o);
ll ans = 0;
if (L <= m)
ans |= query(L, R, l, m, o << 1);
if (m < R)
ans |= query(L, R, m + 1, r, o << 1 | 1);
PushUp(o);
return ans;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
g[u].clear();
}
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
memset(vis, 0, sizeof(vis));
cnt = 0;
dfs(1);
for (int i = 1; i <= n; i++) {
b[id[i]] = a[i];
}
cnt = 0;
build(1, n, 1);
int res, v, c;
while (m--) {
scanf("%d", &res);
if (res == 1) {
scanf("%d%d", &v, &c);
update(id[v], last[v], c, 1, n, 1);
} else {
scanf("%d", &v);
ll ans = query(id[v], last[v], 1, n, 1);
int cc = 0;
for (int i = 1; i <= 61; i++) {
if (ans & (1LL << i))
cc++;
}
printf("%d\n", cc);
}
}
return 0;
}

Educational Codeforces Round 6 620E. New Year Tree(DFS序+线段树)的更多相关文章

  1. CodeForces 620E:New Year Tree(dfs序+线段树)

    E. New Year Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputout ...

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

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

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

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

  4. CodeForces 877E Danil and a Part-time Job(dfs序+线段树)

    Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...

  5. 数据结构(线段树):Educational Codeforces Round 6 620E. New Year Tree

    E. New Year Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  6. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

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

  8. Educational Codeforces Round 81 (Rated for Div. 2)E(线段树)

    预处理把左集划分为大小为1~i-1时,把全部元素都移动到右集的代价,记作sum[i]. 然后枚举终态时左集的大小,更新把元素i 留在/移动到 左集的代价. 树状数组/线段树处理区间修改/区间查询 #d ...

  9. Educational Codeforces Round 72 (Rated for Div. 2)E(线段树,思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;#define BUF_SIZE 100000 ...

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

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

随机推荐

  1. 2019-2020 ICPC Southwestern European Regional Programming Contest (SWERC 2019-20) L题(SG+状压)

    题意:给定一个N*N的表格,其上有三种类型的方格:坚实的地面.潮湿的区域和保护区.连通的湿区方格形成湿区,当两个正方形共用一条边时,它们被认为是连接的.每个湿区必须连接到网格的左右两侧,并且不包含超过 ...

  2. WPF应用框架中工作流模块的介绍

    在前面的随笔,我对我们开发的审批工作流做了不少的介绍,其中有包括WInform的.Vue+Element.Bootstrap Asp.net的,在各个框架上,我们都尽量争取界面能够一致化,以便客户能够 ...

  3. 用最清爽的方式开发dotNet

    用最清爽的方式开发dotNet 不管是官方自带模板还是其他开源搞的,总是一来一大堆,如果你也嫌弃这些过于臃肿,不如看看我这个方式 前提 假设我要做一个简单的api 方式 想到清爽,那肯定是简单方便,脑 ...

  4. 【UniApp】-uni-app概述

    前言 好,经过我们前面文章的编写,大家可以了解到微信小程序开发相关的知识,了解完了这个微信小程序开发和云开发之后,从这篇文章开始,我再来给大家写一下关于 Uniapp 的知识. 好,开始,那什么是 U ...

  5. Java连接phoenix问题

    这个错误的原因是找不到文件,所以要在本地配置一下hadoop的环境变量 下载 将其中hadoop 这个文件放在c盘windows下system32目录下就ok了

  6. Spring系列:基于Spring-AOP和Spring-Aspects实现AOP切面编程

    目录 一.概念及相关术语 概念 相关术语 ①横切关注点 ②通知(增强) ③切面 ④目标 ⑤代理 ⑥连接点 ⑦切入点 作用 二.基于注解的AOP 技术说明 准备工作 创建切面类并配置 各种通知 切入点表 ...

  7. Json Schema简介和Json Schema的.net实现库 LateApexEarlySpeed.Json.Schema

    什么是Json Schema ? Json Schema是一种声明式语言,它可以用来标识Json的结构,数据类型和数据的具体限制,它提供了描述期望Json结构的标准化方法. 利用Json Schema ...

  8. NC65主键含义

    最简单的办法,调用用友的类 import nc.jdbc.framework.generator.SequenceGenerator; IdGenerator idGenerator = new Se ...

  9. NC65单据模板公式使用

    单据模板公式使用 (一) 公式使用场景 用户使用产品时,往往对单据上的字段取值有各种不同的需求.为此单据模板提供 了模板公式功能,可以让实施顾问或者用户通过配置各种公式,并且不用修改代码,从 而满足用 ...

  10. [VMware]ESXI下硬盘的两种直通方式

    文章来自:https://rmbz.net/archives/vmware-esxi-passthrough 最近再搞ESXI,把原来的"黑群晖"改成ESXI:因为群晖里有数据,为 ...