(题面来自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都是一次直接修改,因此要在下推时特判掉空的无效标记。

代码:

  1. #include <bitset>
  2. #include <cstdio>
  3. #include <iostream>
  4. #define maxn 400010
  5. using namespace std;
  6. template <typename T>
  7. void read(T &x) {
  8. x = 0;
  9. int f = 1;
  10. char ch = getchar();
  11. while (!isdigit(ch)) {
  12. if (ch == '-')
  13. f = -1;
  14. ch = getchar();
  15. }
  16. while (isdigit(ch)) {
  17. x = x * 10 + (ch ^ 48);
  18. ch = getchar();
  19. }
  20. x *= f;
  21. return;
  22. }
  23. int n, m;
  24. struct E {
  25. int to, nxt;
  26. } edge[maxn << 1];
  27. int head[maxn], top;
  28. inline void insert(int u, int v) {
  29. edge[++top] = (E) {v, head[u]};
  30. head[u] = top;
  31. }
  32. int color[maxn], val[maxn], dfn[maxn], size[maxn], tmr;
  33. void dfs(int u, int pre) {
  34. dfn[u] = ++tmr;
  35. size[u] = 1;
  36. val[tmr] = color[u];
  37. for (int i = head[u]; i; i = edge[i].nxt) {
  38. int v = edge[i].to;
  39. if (v != pre) {
  40. dfs(v, u);
  41. size[u] += size[v];
  42. }
  43. }
  44. }
  45. namespace Segment_tree {
  46. #define lc (nd<<1)
  47. #define rc ((nd<<1)|1)
  48. #define mid ((l + r) >> 1)
  49. bitset<61> seg[maxn << 2];
  50. bitset<61> tag[maxn << 2];
  51. inline void put_tag(int nd, bitset<61> op) {
  52. if (op.count()) //关键,以及第一个调炸的点
  53. seg[nd] = op,
  54. tag[nd] = op;
  55. }
  56. inline void push_down(int nd) {
  57. put_tag(lc, tag[nd]);
  58. put_tag(rc, tag[nd]);
  59. tag[nd].reset();//第二个调炸的点:忘记清除标记
  60. }
  61. inline void update(int nd) {
  62. seg[nd] = seg[lc] | seg[rc];
  63. }
  64. void build(int nd, int l, int r) {
  65. if (l == r) {
  66. seg[nd].set(val[l], 1);
  67. return;
  68. }
  69. build(lc, l, mid);
  70. build(rc, mid + 1, r);
  71. update(nd);
  72. }
  73. void modify(int nd, int l, int r, int ql, int qr, bitset<61> op) {
  74. if (l >= ql && r <= qr) {
  75. put_tag(nd, op);
  76. return;
  77. } else if (l > qr || r < ql)
  78. return;
  79. push_down(nd);
  80. modify(lc, l, mid, ql, qr, op);
  81. modify(rc, mid + 1, r, ql, qr, op);
  82. update(nd);
  83. }
  84. bitset<61> query(int nd, int l, int r, int ql, int qr) {
  85. if (l >= ql && r <= qr)
  86. return seg[nd];
  87. if (l > qr || r < ql) {
  88. bitset<61> t;
  89. return t;
  90. }
  91. push_down(nd);
  92. return query(lc, l, mid, ql, qr) | query(rc, mid + 1, r, ql, qr);
  93. }
  94. } using namespace Segment_tree;
  95. int main() {
  96. read(n), read(m);
  97. for (int i = 1; i <= n; ++i)
  98. read(color[i]);
  99. int u, v, t, k;
  100. for (int i = 1; i < n; ++i) {
  101. read(u), read(v);
  102. insert(u, v), insert(v, u);
  103. }
  104. dfs(1, 0);
  105. build(1, 1, n);
  106. bitset<61> op;
  107. for (int i = 1; i <= m; ++i) {
  108. read(t), read(v);
  109. if (t == 1) {
  110. read(k);
  111. op.reset();
  112. op.set(k, 1);
  113. modify(1, 1, n, dfn[v], dfn[v] + size[v] - 1, op);
  114. } else {
  115. op = query(1, 1, n, dfn[v], dfn[v] + size[v] - 1);
  116. printf("%d\n", op.count());
  117. }
  118. }
  119. return 0;
  120. }

【CF620E】New Year Tree的更多相关文章

  1. 线段树+Dfs序【CF620E】New Year Tree

    Description 你有一棵以1为根的有根树,有n个点,每个节点初始有一个颜色c[i]. 有两种操作: 1 v c 将以v为根的子树中所有点颜色更改为c 2 v 查询以v为根的子树中的节点有多少种 ...

  2. 【数据结构】B-Tree, B+Tree, B*树介绍 转

    [数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tre ...

  3. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  4. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  5. AC日记——【模板】Link Cut Tree 洛谷 P3690

    [模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...

  6. LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测

    UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...

  7. (RE) luogu P3690 【模板】Link Cut Tree

    二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...

  8. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  9. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

随机推荐

  1. 【Deeplearning】(转)深度学习知识网络

    转自深度学习知识框架,小象牛逼! 图片来自小象学院公开课,下面直接解释几条线 神经网络 线性回归 (+ 非线性激励) → 神经网络 有线性映射关系的数据,找到映射关系,非常简单,只能描述简单的映射关系 ...

  2. Flask常用扩展(Extentions)

    Flask常用扩展(Extentions) 官网;http://flask.pocoo.org/extensions/ 1.Flask-Script ​ 说明: 一个flask终端运行的解析器 安装: ...

  3. IOCP 模型2 AcceptEx

    // IOCP2.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...

  4. nacos、ribbon和feign的简明教程

    nacos简明教程 为什么需要nacos? 在微服务架构中,微服务之间经常要相互通信和调用,而且一个服务往往存在多个实例来降低负荷或保证高可用.我们假定A服务要调用B服务,最简单的方式把B服务的地址和 ...

  5. 对比JAVA、Python、C、Go运行时间,我惊呆了!!!

    对比JAVA.Python.C.Go运行时间,我惊呆了!!! 周末在寝室刷完算法,想放松一下,于是做了一个实验:用现在主流的几种编程语言对0 - (10000000 - 1)求和,结果我惊呆了,话不多 ...

  6. Spring Security 实战干货:OAuth2第三方授权初体验

    1. 前言 Spring Security实战干货系列 现在很多项目都有第三方登录或者第三方授权的需求,而最成熟的方案就是OAuth2.0授权协议.Spring Security也整合了OAuth2. ...

  7. 【Kata Daily 191010】Grasshopper - Summation(加总)

    题目: Summation Write a program that finds the summation of every number from 1 to num. The number wil ...

  8. DCL单例模式中的缺陷及单例模式的其他实现

    DCL:Double Check Lock ,意为双重检查锁.在单例模式中懒汉式中可以使用DCL来保证程序执行的效率. 1 public class SingletonDemo { 2 private ...

  9. NodeJs 加入Windows开机自启动服务

    首先需要到http://nssm.cc/download/?page=download 下载 nssm,下下来之后是压缩包形式的解压之后,在命令行模式下进入到nssm的目录.之后运行:nssm ins ...

  10. 3.3 Spring5源码---循环依赖过程中spring读取不完整bean的最终解决方案

    根据之前解析的循环依赖的源码, 分析了一级缓存,二级缓存,三级缓存的作用以及如何解决循环依赖的. 然而在多线程的情况下, Spring在创建bean的过程中, 可能会读取到不完整的bean. 下面, ...