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 ...
随机推荐
- jupiter的@TempDir 等不生效
jupiter与junit是 完全独立的测试组件,要严防在测试中将二者混用.最好在依赖引入jupiter 时 就将junit的依赖干掉,以防在写测试用例时将二者混用.不会报错,但是会导致 jupite ...
- 线段树&树状数组与离散化的妙用
牛客2019多校联盟Day7 Fine the median 题意: 每次给数组插入区间[Li,Ri] 内的所有数,每操作一次查询中位数. 遇到这题真的算是巧合,然而就是这种冥冥之中的缘分,给了我线 ...
- 【数据库】Function&Procedure&Package
Function/Procedure都是可独立编译并存储在数据库中的,区别是Function有返回值. Package则是数据和过程.函数的集合体. CREATE PROCEDURE dorepeat ...
- js库链接
1.autoHeightTextarea自适应高度的textarea是一款jquery插件,支持链式调用,支持设置最小行数.最小高度.最大行数和最大高度,在输入文字的时候实现textarea的高度自适 ...
- HDU 5285:wyh2000 and pupil
wyh2000 and pupil Accepts: 93 Submissions: 925 Time Limit: 3000/1500 MS (Java/Others) Memory Lim ...
- python语法技巧
目录: 7.python break continue用法 1.三元表达式 2.用列表推导式来取代map和filter 3.函数式编程 4.fluent python 阅读笔记 5.enum库用法 6 ...
- C语言预处理理论
C语言预处理理论1.从源码到可执行文件的过程(1)源码.c->(编译)->elf可执行程序(2)源码.c->(编译)->目标文件.o->(链接)->elf可执行程序 ...
- LeetCode | No.1 两数之和
题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...
- jQuery表格隔行换色和全选的实现
1.下载jQuery,并导入:https://blog.csdn.net/weixin_44718300/article/details/88746796 2.代码实现: <!DOCTYPE h ...
- Android群英传神兵利器读书笔记——第三章:Android Studio奇技淫巧
这篇文章篇幅较长,可以使用版权声明下面的目录,找到感兴趣的进行阅读 3.1 Android Studio使用初探 Project面板 Stucture面板 Android Monitor Keymap ...