题目链接

给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60.

每个节点一个bitset维护就可以。

  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <map>
  8. #include <set>
  9. #include <string>
  10. #include <queue>
  11. #include <stack>
  12. #include <bitset>
  13. using namespace std;
  14. #define pb(x) push_back(x)
  15. #define ll long long
  16. #define mk(x, y) make_pair(x, y)
  17. #define lson l, m, rt<<1
  18. #define mem(a) memset(a, 0, sizeof(a))
  19. #define rson m+1, r, rt<<1|1
  20. #define mem1(a) memset(a, -1, sizeof(a))
  21. #define mem2(a) memset(a, 0x3f, sizeof(a))
  22. #define rep(i, n, a) for(int i = a; i<n; i++)
  23. #define fi first
  24. #define se second
  25. typedef pair<int, int> pll;
  26. const double PI = acos(-1.0);
  27. const double eps = 1e-;
  28. const int mod = 1e9+;
  29. const int inf = ;
  30. const int dir[][] = { {-, }, {, }, {, -}, {, } };
  31. const int maxn = 4e5+;
  32. struct node
  33. {
  34. int to, nextt;
  35. }e[maxn*];
  36. int lazy[maxn<<], a[maxn], in[maxn], out[maxn], head[maxn*], num, clock;
  37. bitset <> s[maxn<<];
  38. void add(int u, int v) {
  39. e[num].to = v, e[num].nextt = head[u], head[u] = num++;
  40. }
  41. void pushUp(int rt) {
  42. s[rt] = s[rt<<]|s[rt<<|];
  43. }
  44. void pushDown(int rt) {
  45. if(lazy[rt]) {
  46. lazy[rt<<] = lazy[rt<<|] = lazy[rt];
  47. s[rt<<].reset();
  48. s[rt<<|].reset();
  49. s[rt<<][lazy[rt]] = s[rt<<|][lazy[rt]] = ;
  50. lazy[rt] = ;
  51. return ;
  52. }
  53. }
  54. void update(int val, int L, int R, int l, int r, int rt) {
  55. if(L<=l&&R>=r) {
  56. s[rt].reset();
  57. s[rt][val] = ;
  58. lazy[rt] = val;
  59. return ;
  60. }
  61. pushDown(rt);
  62. int m = l+r>>;
  63. if(L<=m)
  64. update(val, L, R, lson);
  65. if(R>m)
  66. update(val, L, R, rson);
  67. pushUp(rt);
  68. }
  69. void dfs(int u, int fa) {
  70. in[u] = ++clock;
  71. for(int i = head[u]; ~i; i = e[i].nextt) {
  72. int v = e[i].to;
  73. if(v == fa)
  74. continue;
  75. dfs(v, u);
  76. }
  77. out[u] = clock;
  78. }
  79. bitset<> query(int L, int R, int l, int r, int rt) {
  80. if(L<=l&&R>=r) {
  81. return s[rt];
  82. }
  83. pushDown(rt);
  84. bitset<> tmp;
  85. tmp.reset();
  86. int m = l+r>>;
  87. if(L<=m)
  88. tmp |= query(L, R, lson);
  89. if(R>m)
  90. tmp |= query(L, R, rson);
  91. return tmp;
  92. }
  93. int main()
  94. {
  95. int q, n, m, x, y;
  96. int ch;
  97. memset(head, -, sizeof(head));
  98. cin>>n>>m;
  99. for(int i = ; i<=n; i++)
  100. scanf("%d", &a[i]);
  101. for(int i = ; i<n; i++) {
  102. scanf("%d%d", &x, &y);
  103. add(x, y);
  104. add(y, x);
  105. }
  106. dfs(, );
  107. for(int i = ; i<=n; i++) {
  108. update(a[i], in[i], in[i], , n, );
  109. }
  110. while(m--) {
  111. scanf("%d%d", &ch, &x);
  112. if(ch == )
  113. printf("%d\n", query(in[x], out[x], , n, ).count());
  114. else {
  115. int z;
  116. scanf("%d", &z);
  117. update(z, in[x], out[x], , n, );
  118. }
  119. }
  120. return ;
  121. }

codeforces 620E. New Year Tree dfs序+线段树+bitset的更多相关文章

  1. CodeForces 620E:New Year Tree(dfs序+线段树)

    E. New Year Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputout ...

  2. codeforces 633G. Yash And Trees dfs序+线段树+bitset

    题目链接 G. Yash And Trees time limit per test 4 seconds memory limit per test 512 megabytes input stand ...

  3. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  4. DFS序+线段树+bitset CF 620E New Year Tree(圣诞树)

    题目链接 题意: 一棵以1为根的树,树上每个节点有颜色标记(<=60),有两种操作: 1. 可以把某个节点的子树的节点(包括本身)都改成某种颜色 2. 查询某个节点的子树上(包括本身)有多少个不 ...

  5. Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树

    题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  6. POJ3321 - Apple Tree DFS序 + 线段树或树状数组

    Apple Tree:http://poj.org/problem?id=3321 题意: 告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这 ...

  7. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  8. codeforces 916E Jamie and Tree dfs序列化+线段树+LCA

    E. Jamie and Tree time limit per test 2.5 seconds memory limit per test 256 megabytes input standard ...

  9. codechef T6 Pishty and tree dfs序+线段树

    PSHTTR: Pishty 和城堡题目描述 Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格 的古堡和非常聪明的熊闻名全国. 胡斯特的镇城之宝是就是这么一座古 ...

随机推荐

  1. 九一八-->我逝去的青春

    九一八纪念馆 十二年前 30元一张门票 我毫不犹豫掏钱进去参观 你们笑我 钱少人傻 在东北的四年 从2001到2005 每年都感慨这一天 北国的秋色里 警钟长鸣 长鸣声中 有我逝去的青春 如今 三十而 ...

  2. 【从零学习Python】Ubuntu14.10下Python开发环境配置

    1. 前言 近期在研究计算机视觉的一些算法,也刚開始接触linux,试着在ubuntu下用qt+openCV进行开发,感觉还行.可是Python作为在学术领域广为应用的高级解释性语言.其在计算机视觉的 ...

  3. 原生js下拉刷新

    <!DOCTYPE html><html> <head>        <meta charset="UTF-8">         ...

  4. 最近用的到的一些js的常用方法(简单的)

    由于新的项目开始了,是使用MVC 5.0 开发的,前端使用了两个主流的框架 UIKIT,Ignite UI(收费) 因为是mvc主要用json来交互,不能避免要对前端脚本进行操作,所以就将能用到的方法 ...

  5. iOS合并静态库文件

    具体命令如下(在控制台输入如下命令): lipo -create 其中一个要合并的静态库 另一个要合并的静态库 -output 合并后的静态库

  6. Assertion failure in -[UIView layoutSublayersOfLayer:]

    Assertion failure in -[UIView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.137/UIView.m:8 ...

  7. 在C++中如何使用C

    如下代码: /*C语言头文件:Max.h*/ #ifndef _MAX_H_ #define _MAX_H_ int Max(int nA,int nB) #endif /*C语言实现文件:Max.c ...

  8. IWebBrowser隐藏滚动条

    刚才在项目里看到一个IWebBrowser2,竟然需要通过MoveWindow的方式把滚动条遮挡,如果要缩小IWebBrowser2控件的显示大小呢?这种方法至少我用不习惯,起码也得从源头解决这样的问 ...

  9. BZOJ 3224: Tyvj 1728 普通平衡树(BST)

    treap,算是模板题了...我中间还一次交错题... -------------------------------------------------------------------- #in ...

  10. 5.6.3.7 localeCompare() 方法

    与操作字符串有关的最后一个方法是localeCompare(),这个方法比较两个字符串,并返回下列值中的一个: 如果字符串在字母表中应该排在字符串参数之前,则返回一个负数(大多数情况下是-1,具体的值 ...