【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 ...
随机推荐
- 【Deeplearning】(转)深度学习知识网络
转自深度学习知识框架,小象牛逼! 图片来自小象学院公开课,下面直接解释几条线 神经网络 线性回归 (+ 非线性激励) → 神经网络 有线性映射关系的数据,找到映射关系,非常简单,只能描述简单的映射关系 ...
- Flask常用扩展(Extentions)
Flask常用扩展(Extentions) 官网;http://flask.pocoo.org/extensions/ 1.Flask-Script 说明: 一个flask终端运行的解析器 安装: ...
- IOCP 模型2 AcceptEx
// IOCP2.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...
- nacos、ribbon和feign的简明教程
nacos简明教程 为什么需要nacos? 在微服务架构中,微服务之间经常要相互通信和调用,而且一个服务往往存在多个实例来降低负荷或保证高可用.我们假定A服务要调用B服务,最简单的方式把B服务的地址和 ...
- 对比JAVA、Python、C、Go运行时间,我惊呆了!!!
对比JAVA.Python.C.Go运行时间,我惊呆了!!! 周末在寝室刷完算法,想放松一下,于是做了一个实验:用现在主流的几种编程语言对0 - (10000000 - 1)求和,结果我惊呆了,话不多 ...
- Spring Security 实战干货:OAuth2第三方授权初体验
1. 前言 Spring Security实战干货系列 现在很多项目都有第三方登录或者第三方授权的需求,而最成熟的方案就是OAuth2.0授权协议.Spring Security也整合了OAuth2. ...
- 【Kata Daily 191010】Grasshopper - Summation(加总)
题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...
- DCL单例模式中的缺陷及单例模式的其他实现
DCL:Double Check Lock ,意为双重检查锁.在单例模式中懒汉式中可以使用DCL来保证程序执行的效率. 1 public class SingletonDemo { 2 private ...
- NodeJs 加入Windows开机自启动服务
首先需要到http://nssm.cc/download/?page=download 下载 nssm,下下来之后是压缩包形式的解压之后,在命令行模式下进入到nssm的目录.之后运行:nssm ins ...
- 3.3 Spring5源码---循环依赖过程中spring读取不完整bean的最终解决方案
根据之前解析的循环依赖的源码, 分析了一级缓存,二级缓存,三级缓存的作用以及如何解决循环依赖的. 然而在多线程的情况下, Spring在创建bean的过程中, 可能会读取到不完整的bean. 下面, ...