题目链接 New Year Tree

考虑到$ck <= 60$,那么用位运算统计颜色种数

对于每个点,重新标号并算出他对应的进和出的时间,然后区间更新+查询。

用线段树来维护。

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define rep(i, a, b) for (int i(a); i <= (b); ++i)
  6.  
  7. struct node{
  8. long long num, lazy;
  9. } tree[ << ];
  10.  
  11. struct Node{
  12. int l, r;
  13. } e[];
  14.  
  15. vector <int> v[];
  16.  
  17. int n, m;
  18. long long val[], c[];
  19. int Time;
  20. bool vis[];
  21. long long ans, cover;
  22. int op;
  23. int x, y;
  24.  
  25. void dfs(int x, int fa){
  26. e[x].l = ++Time;
  27. val[Time] = c[x];
  28. vis[x] = true;
  29. for (auto u : v[x]){
  30. if (u == fa) continue;
  31. dfs(u, x);
  32. }
  33.  
  34. e[x].r = Time;
  35. }
  36.  
  37. inline void pushup(int i){
  38. tree[i].num = tree[i << ].num | tree[i << | ].num;
  39. }
  40.  
  41. inline void pushdown(int i){
  42. if (tree[i].lazy){
  43. tree[i << ].num = tree[i << | ].num = (1LL << tree[i].lazy);
  44. tree[i << ].lazy = tree[i << | ].lazy = tree[i].lazy;
  45. tree[i].lazy = ;
  46. }
  47. }
  48.  
  49. void build(int i, int l, int r){
  50. tree[i].lazy = ;
  51. if (l == r){
  52. tree[i].num = (1LL << val[l]);
  53. return ;
  54. }
  55.  
  56. int mid = (l + r) >> ;
  57. build(i << , l, mid);
  58. build(i << | , mid + , r);
  59. pushup(i);
  60. }
  61.  
  62. void update(int i, int L, int R, int l, int r, long long cover){
  63. if (l <= L && R <= r){
  64. tree[i].lazy = cover;
  65. tree[i].num = (1LL << cover);
  66. return ;
  67. }
  68.  
  69. int mid = (L + R) >> ;
  70. pushdown(i);
  71. if (l <= mid) update(i << , L, mid, l, r, cover);
  72. if (r > mid) update(i << | , mid + , R, l, r, cover);
  73. pushup(i);
  74. }
  75.  
  76. void solve(int i, int L, int R, int l, int r){
  77. if (l <= L && R <= r){
  78. ans |= tree[i].num;
  79. return;
  80. }
  81.  
  82. pushdown(i);
  83. int mid = (L + R) >> ;
  84. if (l <= mid) solve(i << , L, mid, l, r);
  85. if (r > mid) solve(i << | , mid + , R, l, r);
  86. }
  87.  
  88. int main(){
  89.  
  90. scanf("%d%d", &n, &m);
  91.  
  92. rep(i, , n) v[i].clear();
  93. rep(i, , n) scanf("%lld", c + i);
  94. rep(i, , n - ){
  95. scanf("%d%d", &x, &y);
  96. v[x].push_back(y);
  97. v[y].push_back(x);
  98. }
  99.  
  100. memset(vis, , sizeof vis); Time = ;
  101. dfs(, );
  102. build(, , n);
  103.  
  104. rep(i, , m){
  105. scanf("%d%d", &op, &x);
  106. if (op == ){
  107. scanf("%lld", &cover);
  108. update(, , n, e[x].l, e[x].r, cover);
  109. }
  110.  
  111. else{
  112. ans = ;
  113. solve(, , n, e[x].l, e[x].r);
  114. int ret = ;
  115. for (; ans; ans -= ans & -ans) ++ret;
  116. printf("%d\n", ret);
  117. }
  118. }
  119.  
  120. return ;
  121. }

Codeforces 620E New Year Tree(线段树+位运算)的更多相关文章

  1. CodeForces 620E New Year Tree(线段树的骚操作第二弹)

    The New Year holidays are over, but Resha doesn't want to throw away the New Year tree. He invited h ...

  2. Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)

    链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...

  3. hdu 5023 线段树+位运算

    主要考线段树的区间修改和区间查询,这里有一个问题就是这么把一个区间的多种颜色上传给父亲甚至祖先节点,在这里题目告诉我们最多30颜色,那么我们可以把这30中颜色用二进制储存和传给祖先节点,二进制的每一位 ...

  4. poj 2777 Count Color - 线段树 - 位运算优化

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42472   Accepted: 12850 Description Cho ...

  5. Count Color(线段树+位运算 POJ2777)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...

  6. poj 3225 线段树+位运算

    略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...

  7. POJ 2777 Count Color(线段树+位运算)

    题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an opti ...

  8. poj_2777线段树+位运算

    第一次没想到用位运算,不出意料的T了,,, PS:在床上呆了接近两个月后,我胡汉三又杀回来刷题啦-- #include<iostream> #include<cstdio> # ...

  9. [poj2777] Count Color (线段树 + 位运算) (水题)

    发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...

随机推荐

  1. OwinStartupAttribute出错

    尝试加载应用时出现了以下错误.- 找不到包含 OwinStartupAttribute 的程序集.- 找不到包含 Startup 或 [AssemblyName].Startup 类的程序集.若要禁用 ...

  2. thinkphp5开发restful-api接口学习 笔记二

    目录 第4节 为api项目搭建数据库 第5节 使用markdown书写接口文档 第6节(判断数据库中是否有此用户) 第7节 为项目配置URL 需求分析 配置主域名和二级域名 使用tp5路由进行URL解 ...

  3. Python学习笔记:输入输出,注释,运算符,变量,数字类型,序列,条件和循环控制,函数,迭代器与生成器,异常处理

    输入输出 输入函数input()和raw_input() 在Python3.x中只有input()作为输入函数,会将输入内容自动转换str类型: 在Python2.x中有input()和raw_inp ...

  4. (转)git常见错误

      error: src refspec master does not match any. 引起该错误的原因是,目录中没有文件,空目录是不能提交上去的 error: insufficient pe ...

  5. hdu-2544 最短路(最短路)

    Time limit1000 ms Memory limit32768 kB   在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到 ...

  6. German Collegiate Programming Contest 2015

    // Legacy Code #include <iostream> #include <cstdio> #include <cstring> #include & ...

  7. Mysql登陆、退出、更改环境编码

    登录: mysql -h[数据库地址] -u[username] -p[password] -P[端口]  //大写P表示端口,小写p表示密码 例如:mysql -hlocalhost -uroot ...

  8. 【15】ES6 for Humans: The Latest Standard of JavaScript: ES2015 and Beyond

    [15]ES6 for Humans 共148页: 目前看到:已经全部阅读.   亚马逊地址: 魔芋:总结: 我先看的是阮一峰的在线书籍.这本书的内容很多都与之重复的. 居然卖¥463.也是没谁了. ...

  9. Vue简单了解

    目录 1. 前端概览 2. 现代前端开发方式 3. MVVM开发核心 4. Vue核心 5. Vue优点 6. Vue难点 7. Vue与非Vue项目结合 8. Vue调试 9. Vue与SEO 今天 ...

  10. 令人惊叹的Npm工具包

    1.http-server (简单搭建http服务器) 2.json-server (JSON服务器,快速搭建resful api接口) 3.cssnano (css多功能优化工具) PS:比uncs ...