Educational Codeforces Round 6 620E. New Year Tree(DFS序+线段树)
题目链接:点击打开链接
题意:给你一棵树,编号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序+线段树)的更多相关文章
- CodeForces 620E:New Year Tree(dfs序+线段树)
E. New Year Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputout ...
- 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 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 ...
- 数据结构(线段树):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 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 81 (Rated for Div. 2)E(线段树)
预处理把左集划分为大小为1~i-1时,把全部元素都移动到右集的代价,记作sum[i]. 然后枚举终态时左集的大小,更新把元素i 留在/移动到 左集的代价. 树状数组/线段树处理区间修改/区间查询 #d ...
- Educational Codeforces Round 72 (Rated for Div. 2)E(线段树,思维)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;#define BUF_SIZE 100000 ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
随机推荐
- 【Android】无法通过drawable下的selector类型改变背景颜色?
举例 我在darwable目录下创建了selector文件,并设置了如下内容 <?xml version="1.0" encoding="utf-8"?& ...
- 2020ICPC上海 C题(数位dp, 记忆化搜索)
先复习了下之前做的数位DP又做了道新题才看的这道题,对我来说还是一种新类型,涉及到非线性计算,之前做的都是形如 \(dp[x]-dp[y]\)这样的只用处理一个上限做下差即可.一开始想分别枚举 \(x ...
- 耗时三年开源的H5商城,生产级代码实战
waynboot-mall 商城项目是我从疫情开始初期着手准备开发的,到如今 2023 年底,已经过了 3 年多的时间. 从项目初期到现在,一个人持续迭代,修复漏洞,添加功能,经历了前端开发工具从 v ...
- 玩转 K8s 权限控制:RBAC + kubeconfig 搞定 kubectl 权限管理那些事
1. 先抛需求 当一个 K8s 集群需要被多个租户共享时,就涉及到了权限问题,比如你是管理员,这时候你会面临着"给每个用户分配一个 Namespace"类似的需求. 更进一步,可能 ...
- vue-admin-template快速开发框架,菜单栏不显示层级结构
问题现象 路由 页面效果 解决办法 路由添加中配置 alwaysShow: true, // 树形层级显示 页面效果
- Linux查看内存使用情况以及释放内存
查看内存使用情况 一般使用 top 命令即可,命令行输入 top会实时显示内存和cpu等硬件的使用情况,然后输入M即显示内存的使用情况. 但是有些情况下,内存明明被占用了,但是使用top命令依然无法找 ...
- Unity无法显示animator面板,如何解决?
步骤: 点击动画的主体: 右侧Inspector面板找到Animator,双击Controller中的对象: 左上角即可显示animator面板. 总结: 不行就双击!!!!!!!!!!!!!!!!! ...
- Android 蓝牙使用
原文地址: Android 蓝牙使用 - Stars-One的杂货小窝 公司项目需求需要实现监听蓝牙耳机连接,且要获取蓝牙耳机电量功能,翻了不少官方文档,记录下技术调研代码 注:本文没有研究蓝牙配对功 ...
- [Python急救站]含义GUI的学生管理系统
这个管理系统是含有GUI界面的学生管理系统,比较方便. import tkinter as tk class Student: def __init__(self, name, student_id, ...
- 文心一言 VS 讯飞星火 VS chatgpt (36)-- 算法导论5.3 6题
六.请解释如何实现算法 PERMUTE-BY-SORTING,以处理两个或更多优先级相同的情形.也就是说,即使有两个或更多优先级相同,你的算法也应该产生一个均匀随机排列. 文心一言: 算法 PERMU ...