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 ...
随机推荐
- winfrom窗体的透明度
在VS中创建一个Winform项目,其默认的窗体名称为 Form1. 在VS设计界面中对 Form1 的 Opacity 属性值设置为 50%. 没错,就这样就可以了. 方法2: ...
- Docker PHP 例子
版权所有,未经许可,禁止转载 章节 Docker 介绍 Docker 和虚拟机的区别 Docker 安装 Docker Hub Docker 镜像(image) Docker 容器(container ...
- Day3-T2
原题目 奶牛Bessie的电脑总是无缘无故地被 FJ 关掉,奶牛 Bessie 非常苦恼,也非常生气.FJ 却发现了 一个很神奇的规律(别问是怎么知道的),发现 Bessie 每吃一次草,她的生气值会 ...
- xxe
XXE xml external entity injection xml外部实体注入 概述 xml是可扩展的标记语言,涉及被用来传输.存储数据 html被用来显示数据 其中xml的标签没有预定义的, ...
- git修改已经push的commit message
git中修改上一次提交的commit的message git commit --amend -m "你的新的注释" git push -f 多个commit https://www ...
- CF1217B Zmei Gorynich
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like rept ...
- 基于图灵api的Python机器人
一.注册图灵机器人 先注册并登录图灵机器人官网: 点击创建机器人 复制机器人的key 二.搭建Python机器人 Python版本:3.6 注意替换第三行代码的apikey import reques ...
- MYSQL连接不上100061错误
有界面的情况下启动MYSQL 无界面 https://blog.csdn.net/qq_22233621/article/details/72673176 参考
- webservice wsdl文件标签讲解
<?xml version="1.0" encoding="utf8"?> <wsdl:definitions targetNamespace ...
- Linq------连表查询
1 List<Student> list = new List<Student>() { ,sex="男"}, ,sex="男"}, , ...