【CF620E】New Year Tree
(题面来自luogu)
题意翻译
你有一棵以1为根的有根树,有n个点,每个节点初始有一个颜色c[i]。
有两种操作:
1 v c 将以v为根的子树中所有点颜色更改为c
2 v 查询以v为根的子树中的节点有多少种不同的颜色
翻译贡献者UID:28455
n、m <= 4e5,ci <= 60。
今天唯一听懂的一道题目,调了两个小时……因为要查询子树信息,考虑构造出dfs序之后,用线段树维护区间颜色。因为颜色只有60种,可以维护在节点上维护bitset或者一个64位整型来压缩状态,每一位上表示以该点为根的子树内是否含有这个颜色。
线段树内的tag是不可以累加的,每个tag都是一次直接修改,因此要在下推时特判掉空的无效标记。
代码:
- #include <bitset>
- #include <cstdio>
- #include <iostream>
- #define maxn 400010
- using namespace std;
- template <typename T>
- void read(T &x) {
- x = 0;
- int f = 1;
- char ch = getchar();
- while (!isdigit(ch)) {
- if (ch == '-')
- f = -1;
- ch = getchar();
- }
- while (isdigit(ch)) {
- x = x * 10 + (ch ^ 48);
- ch = getchar();
- }
- x *= f;
- return;
- }
- int n, m;
- struct E {
- int to, nxt;
- } edge[maxn << 1];
- int head[maxn], top;
- inline void insert(int u, int v) {
- edge[++top] = (E) {v, head[u]};
- head[u] = top;
- }
- int color[maxn], val[maxn], dfn[maxn], size[maxn], tmr;
- void dfs(int u, int pre) {
- dfn[u] = ++tmr;
- size[u] = 1;
- val[tmr] = color[u];
- for (int i = head[u]; i; i = edge[i].nxt) {
- int v = edge[i].to;
- if (v != pre) {
- dfs(v, u);
- size[u] += size[v];
- }
- }
- }
- namespace Segment_tree {
- #define lc (nd<<1)
- #define rc ((nd<<1)|1)
- #define mid ((l + r) >> 1)
- bitset<61> seg[maxn << 2];
- bitset<61> tag[maxn << 2];
- inline void put_tag(int nd, bitset<61> op) {
- if (op.count()) //关键,以及第一个调炸的点
- seg[nd] = op,
- tag[nd] = op;
- }
- inline void push_down(int nd) {
- put_tag(lc, tag[nd]);
- put_tag(rc, tag[nd]);
- tag[nd].reset();//第二个调炸的点:忘记清除标记
- }
- inline void update(int nd) {
- seg[nd] = seg[lc] | seg[rc];
- }
- void build(int nd, int l, int r) {
- if (l == r) {
- seg[nd].set(val[l], 1);
- return;
- }
- build(lc, l, mid);
- build(rc, mid + 1, r);
- update(nd);
- }
- void modify(int nd, int l, int r, int ql, int qr, bitset<61> op) {
- if (l >= ql && r <= qr) {
- put_tag(nd, op);
- return;
- } else if (l > qr || r < ql)
- return;
- push_down(nd);
- modify(lc, l, mid, ql, qr, op);
- modify(rc, mid + 1, r, ql, qr, op);
- update(nd);
- }
- bitset<61> query(int nd, int l, int r, int ql, int qr) {
- if (l >= ql && r <= qr)
- return seg[nd];
- if (l > qr || r < ql) {
- bitset<61> t;
- return t;
- }
- push_down(nd);
- return query(lc, l, mid, ql, qr) | query(rc, mid + 1, r, ql, qr);
- }
- } using namespace Segment_tree;
- int main() {
- read(n), read(m);
- for (int i = 1; i <= n; ++i)
- read(color[i]);
- int u, v, t, k;
- for (int i = 1; i < n; ++i) {
- read(u), read(v);
- insert(u, v), insert(v, u);
- }
- dfs(1, 0);
- build(1, 1, n);
- bitset<61> op;
- for (int i = 1; i <= m; ++i) {
- read(t), read(v);
- if (t == 1) {
- read(k);
- op.reset();
- op.set(k, 1);
- modify(1, 1, n, dfn[v], dfn[v] + size[v] - 1, op);
- } else {
- op = query(1, 1, n, dfn[v], dfn[v] + size[v] - 1);
- printf("%d\n", op.count());
- }
- }
- return 0;
- }
【CF620E】New Year Tree的更多相关文章
- 线段树+Dfs序【CF620E】New Year Tree
Description 你有一棵以1为根的有根树,有n个点,每个节点初始有一个颜色c[i]. 有两种操作: 1 v c 将以v为根的子树中所有点颜色更改为c 2 v 查询以v为根的子树中的节点有多少种 ...
- 【数据结构】B-Tree, B+Tree, B*树介绍 转
[数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tre ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- LG3690 【模板】Link Cut Tree (动态树)
题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...
- AC日记——【模板】Link Cut Tree 洛谷 P3690
[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...
- LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测
UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...
- (RE) luogu P3690 【模板】Link Cut Tree
二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
随机推荐
- 本地文件r如何上传到github上
来源:http://www.cnblogs.com/shenchanghui/p/7184101.html 来源:http://blog.csdn.net/zamamiro/article/detai ...
- python中拿不到字典value值得问题解决
在python项目的练习中,根据字典的key值get不到value值,如图所示: 最后,将user = self.allUsers.get(cardNum)改成user = self.allUsers ...
- 栈的C++实现
数据结构c++实现系列第一篇. 话不多说,直接上代码. sichstack.h (头文件) 1 #pragma once 2 #include<string> 3 4 namespace ...
- SpringBook+Lombok 使用教程
什么是Lombok? Lombok项目是一个Java库,它会自动插入编辑器和构建工具中,Lombok提供了一组有用的注释,用来消除Java类中的大量样板代码. Lombok也存在一定风险,在一些开发工 ...
- 【论文阅读】分布一致性算法Paxos 《The Part-Time Parliament》
论文原文.翻译稿.PPt:https://1drv.ms/u/s!Ak-jGl23kTuMimOZzV-MyLQUYmsN?e=DL1xHD
- ABAP-字符串常用处理方法
字符串处理 SPLIT dobj AT sep INTO { {result1 result2 ...} | {TABLE result_tab} } 必须指定足够目标字段.否则,用字段dobj的剩余 ...
- PHP 将数组转换为JSON字符串<兼容中文>
1 /************************************************************** 2 * 3 * 使用特定function对数组中所有元素做处理 4 ...
- Charles使用part5——模拟慢网络
一.配置参数解析: bandwidth -- 带宽,即上行.下行数据传输速度utilisation -- 带宽可用率,大部分modern是100%round-trip latency -- 第一个请求 ...
- 处理textarea里Enter(回车换行符)
Enter换行符 如果包含有回车换行符,在字符串中表现为"\n": 会返回一条字符串: 原文章:https://blog.csdn.net/shenlf_bk/article/de ...
- Elasticsearch原理解析与性能调优
基本概念 定义 一个分布式的实时文档存储,每个字段 可以被索引与搜索 一个分布式实时分析搜索引擎 能胜任上百个服务节点的扩展,并支持 PB 级别的结构化或者非结构化数据 用途 全文检索 结构化搜索 分 ...