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 ...
随机推荐
- 058-PHP中goto语句的使用
<?php for($i=1;$i<=5;$i++){ //使用for循环循环输出1~5 if($i==3) goto ECH; //当$i为3时候跳出for循环 echo "$ ...
- 课程作业02-1-课后作业1-(1)使用组合数公式利用n!来计算
1.设计思想:运用递归阶乘的函数,依次求出n!.k!.(n-k)!,再根据组合数的公式计算(n!/(k!*(n-k)!)). 2.程序流程图: 3.源程序代码: //信1605-3 20163429 ...
- ntpdate更新系统时间时报错Can't find host ntp1.aliyun.com: Servname not supported for ai_socktype (-8)
ntpdate更新系统时间时报错Can't find host ntp1.aliyun.com: Servname not supported for ai_socktype (-8) 所报错误: [ ...
- Docker 和虚拟机的区别
版权所有,未经许可,禁止转载 章节 Docker 介绍 Docker 和虚拟机的区别 Docker 安装 Docker Hub Docker 镜像(image) Docker 容器(container ...
- BlackArch Linux 2019.06.01 宣布发布
导读 BlackArch Linux是一个基于Arch Linux的发行版,专为渗透测试人员和安全研究人员设计,并包含大量渗透测试和安全实用程序,已宣布发布2019.06.01版本. BlackArc ...
- 【pwnable.kr】 flag
pwnable从入门到放弃 第四题 Download : http://pwnable.kr/bin/flag 下载下来的二进制文件,对着它一脸懵逼,题目中说是逆向题,这我哪会啊... 用ida打开看 ...
- yarn storm spark
单机zookeeper http://coolxing.iteye.com/blog/1871009 storm http://os.51cto.com/art/201309/411003_2.htm ...
- ABP which was not registered.
ABP 错误: 'AoLongData.Finances.FinanceService' is waiting for the following dependencies:- Service 'Ab ...
- DBUtils模版CRUD
准备:导包 1.创建c3p0-config.xml配置文件放在src下 <?xml version="1.0" encoding="UTF-8"?> ...
- Swift轮播控件快速入门——FSPagerView
2018年03月01日 19:17:42 https://blog.csdn.net/sinat_21886795/article/details/79416068 今天介绍一个IOS的轮播控件FSP ...