Codeforces620E New Year Tree
挺好的一道题
Description
给一棵树,每个点有颜色 \(c_i\) 为点权,需要实现以下两种操作:
子树修改颜色(覆盖),查询子树颜色种类
\(n \leq 4 \times 10^5,c_i \leq 60\)
Solution
\]
首先看到子树和修改啥的,直接思考 \(dfs\) 序加线段树(从树剖学来的)
我们看到如果对于每一个点开桶进行统计,可能不太现实
然后审题的关键点就来了:\(c_i \leq 60\)
可以开 $long $ \(long\) 状压
然后就成了单点进行答案统计
最后把对应 \(query\) 搞个 \(lowbit\) 什么的整一下 \(1\) 就好了(从树状数组剽来的)
要注意 \(1ll<<\)的问题(蒟蒻去年没有遇到这种问题,因为\(D1T1\)写的暴力……)
\]
\(P.S.\)博主知道应该是\(QED\)
Code
#include <bits/stdc++.h>
using namespace std;
#define int long long
namespace yspm {
inline int read() {
int res = 0, f = 1;
char k;
while (!isdigit(k = getchar()))
if (k == '-')
f = -1;
while (isdigit(k)) res = res * 10 + k - '0', k = getchar();
return res * f;
}
const int N = 4e5 + 10;
int a[N], fa[N], dep[N], head[N], cnt, in[N], out[N], tim, opt, n, m, id[N];
struct node {
int nxt, to;
} e[N << 2];
inline void add1(int u, int v) {
e[++cnt].nxt = head[u];
e[cnt].to = v;
return head[u] = cnt, void();
}
struct tree {
int l, r, sum, add;
#define l(p) t[p].l
#define r(p) t[p].r
#define sum(p) t[p].sum
#define add(p) t[p].add
} t[N << 2];
inline void push_up(int p) {
sum(p) = sum(p << 1) | sum(p << 1 | 1);
return;
}
inline void build(int p, int l, int r) {
l(p) = l;
r(p) = r;
if (l == r)
return sum(p) = 1ll << a[id[l]], void();
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
return push_up(p);
}
inline int lowbit(int x) { return x & (-x); }
inline void spread(int p) {
if (add(p)) {
sum(p << 1) = add(p);
sum(p << 1 | 1) = add(p);
add(p << 1) = add(p);
add(p << 1 | 1) = add(p);
}
return add(p) = 0, void();
}
inline void update(int p, int l, int r, int c) {
if (l <= l(p) && r(p) <= r)
return add(p) = 1ll << c, sum(p) = 1ll << c, void();
spread(p);
int mid = (l(p) + r(p)) >> 1;
if (l <= mid)
update(p << 1, l, r, c);
if (r > mid)
update(p << 1 | 1, l, r, c);
return push_up(p);
}
inline int query(int p, int l, int r) {
if (l <= l(p) && r(p) <= r)
return sum(p);
spread(p);
int ans = 0, mid = (l(p) + r(p)) >> 1;
if (l <= mid)
ans |= query(p << 1, l, r);
if (r > mid)
ans |= query(p << 1 | 1, l, r);
return ans;
}
inline int ask1(int x) {
int ret = 0;
for (; x; x -= lowbit(x)) ret++;
return ret;
}
inline void dfs(int x, int f) {
in[x] = ++tim;
id[tim] = x;
fa[x] = f;
for (int i = head[x]; i; i = e[i].nxt) {
int t = e[i].to;
if (t == f)
continue;
dfs(t, x);
}
return out[x] = tim, void();
}
signed main() {
n = read(), m = read();
for (int i = 1; i <= n; ++i) a[i] = read();
for (int i = 1, u, v; i < n; ++i) u = read(), v = read(), add1(u, v), add1(v, u);
dfs(1, 0);
build(1, 1, n);
while (m--) {
opt = read();
if (opt == 1) {
int x = read(), c = read();
update(1, in[x], out[x], c);
} else {
int x = read();
int tmp = query(1, in[x], out[x]);
printf("%lld\n", ask1(tmp));
}
}
return 0;
}
} // namespace yspm
signed main() { return yspm::main(); }
Codeforces620E New Year Tree的更多相关文章
- 2019.03.09 codeforces620E. New Year Tree(线段树+状态压缩)
传送门 题意:给一棵带颜色的树,可以给子树染色或者问子树里有几种不同的颜色,颜色值不超过606060. 思路:颜色值很小,因此状压一个区间里的颜色用线段树取并集即可. 代码: #include< ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- Django中出现no such table: django_session
这个错误跟Session的机制有关, 既然要从Web服务器端来记录用户信息, 那么一定要有存放用户session id对应信息的地方才行. 所以,我们需要创建django_session表. Djan ...
- java基础源码 (5)--reflect包-AccessibleObject类
学习参考博客:https://blog.csdn.net/benjaminzhang666/article/details/9664585AccessibleObject类基本作用 1.将反射的对象标 ...
- 设置此div的子元素居中显示
下面样式设置到父div上: .modal { display: flex; align-items: center; /*竖直居中 垂直居中*/ justify-content: center; /* ...
- Linux重要命令练习之bc
- IO_课堂测试
IO_课堂测试 一,用户需求 英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?<飘> 中最常用的短语是什么,等等. (1)要求1 ...
- HDU - 1114 Piggy-Bank(完全背包讲解)
题意:背包重量为F-E,有N种硬币,价值为Pi,重量为Wi,硬币个数enough(无穷多个),问若要将背包完全塞满,最少需要多少钱,若塞不满输出“This is impossible.”. 分析:完全 ...
- UVA - 11491 Erasing and Winning(奖品的价值)(贪心)
题意:有一个n位整数(不以0开头),要求删除其中的d个数字,使结果尽量大.(1<=d<n<=10^5) 分析: 1.从头扫一遍,如果当前填的数字小于n-d,则将当前数字填上. 2.如 ...
- 使用FragmentStatePagerAdapter时发现的内存泄露问题
这篇文章想说的并非是由于使用 FragmentStatePagerAdapter 而导致的内存泄漏,内存泄漏的真正原因和 FragmentStaePagerAdapter 并无直接关联,但是使用 Fr ...
- c++ 字符串转数字或数字转字符串
在C++中字符串转换为数字,或数字转换为字符串,用到如下函数: _itoa atoi.atof.itoa.itow _itoa_s 1.整形转换为字符串: wchar_t * _itot(int _V ...
- maven项目中WEB-INF的父目录必须叫webapp吗?
这个并不是必须的,可以在pom配置文件中修改,如下所示: <webappDirectory>src/main/WebContent</webappDirectory> ...