题目链接

题意:

  一棵以1为根的树,树上每个节点有颜色标记(<=60),有两种操作:

  1. 可以把某个节点的子树的节点(包括本身)都改成某种颜色

  2. 查询某个节点的子树上(包括本身)有多少个不同的颜色

思路:

  和2012年多校第7场的G题是同类题,DFS序处理出每个节点管辖的管辖范围[L[u], R[u]],其中L[u]就是子树根节点u所在的位置,用线段树成端更新颜色变化,注意到颜色(<=60),可以用bitset<60>,0表示没有这个颜色,1表示有,异或就能区间合并,最后count一下不同颜色的个数。

另外:

  以前这种题是做不了的,现在都能秒掉了,说明在进步:)

#include <bits/stdc++.h>

const int N = 4e5 + 5;
int a[N];
std::vector<int> edge[N];
int L[N], R[N], id[N];
int tim; #define lson l, mid, o << 1
#define rson mid + 1, r, o << 1 | 1
struct Node {
std::bitset<60> color;
int lazy;
};
Node node[N<<2]; void push_up(int o) {
node[o].color = node[o<<1].color | node[o<<1|1].color;
} void push_down(int o) {
if (node[o].lazy != -1) {
node[o<<1].lazy = node[o<<1|1].lazy = node[o].lazy;
node[o<<1].color.reset ();
node[o<<1].color.set (node[o].lazy);
node[o<<1|1].color.reset ();
node[o<<1|1].color.set (node[o].lazy);
node[o].lazy = -1;
}
} void build(int l, int r, int o) {
node[o].lazy = -1;
node[o].color.reset (); //clear to 0
if (l == r) {
node[o].color.set (a[id[l]]); //set to 1
return ;
}
int mid = l + r >> 1;
build (lson);
build (rson);
push_up (o);
} void updata(int ql, int qr, int c, int l, int r, int o) {
if (ql <= l && r <= qr) {
node[o].lazy = c;
node[o].color.reset ();
node[o].color.set (c);
return ;
}
push_down (o);
int mid = l + r >> 1;
if (ql <= mid) {
updata (ql, qr, c, lson);
}
if (qr > mid) {
updata (ql, qr, c, rson);
}
push_up (o);
} std::bitset<60> query(int ql, int qr, int l, int r, int o) {
if (ql <= l && r <= qr) {
return node[o].color;
}
push_down (o);
int mid = l + r >> 1;
std::bitset<60> ret;
if (ql <= mid) {
ret |= query (ql, qr, lson);
}
if (qr > mid) {
ret |= query (ql, qr, rson);
}
return ret;
} void DFS(int u, int fa) {
L[u] = ++tim; id[tim] = u;
for (auto v: edge[u]) {
if (v != fa) {
DFS (v, u);
}
}
R[u] = tim;
} int main() {
int n, m;
scanf ("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
scanf ("%d", a+i);
a[i]--;
}
for (int i=1; i<n; ++i) {
int x, y;
scanf ("%d%d", &x, &y);
edge[x].push_back (y);
edge[y].push_back (x);
}
tim = 0;
DFS (1, 0);
build (1, tim, 1); for (int i=0; i<m; ++i) {
int type, v;
scanf ("%d%d", &type, &v);
if (type == 1) {
int c;
scanf ("%d", &c);
c--;
updata (L[v], R[v], c, 1, tim, 1);
} else {
std::bitset<60> ans = query (L[v], R[v], 1, tim, 1);
printf ("%d\n", ans.count ());
}
}
return 0;
}

  

DFS序+线段树+bitset CF 620E New Year Tree(圣诞树)的更多相关文章

  1. Codeforces633G(SummerTrainingDay06-I dfs序+线段树+bitset)

    G. Yash And Trees time limit per test:4 seconds memory limit per test:512 megabytes input:standard i ...

  2. Manthan, Codefest 16 G. Yash And Trees dfs序+线段树+bitset

    G. Yash And Trees 题目连接: http://www.codeforces.com/contest/633/problem/G Description Yash loves playi ...

  3. DFS序+线段树 hihoCoder 1381 Little Y's Tree(树的连通块的直径和)

    题目链接 #1381 : Little Y's Tree 时间限制:24000ms 单点时限:4000ms 内存限制:512MB 描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每 ...

  4. codeforces 620E. New Year Tree dfs序+线段树+bitset

    题目链接 给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60. 每个节点一个bitset维护就可以. #in ...

  5. 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 ...

  6. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  7. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  8. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  9. BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)

    题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...

随机推荐

  1. C语言操作注册表 写入 读取信息

    #include <stdio.h>#include <windows.h>int main(void){char regname[]="Software\\Micr ...

  2. PHP代码 如何网页获取用户的openid

    public function getOpenid($appid, $appsecret) { $SERVER_NAME = $_SERVER['SERVER_NAME']; $REQUEST_URI ...

  3. Android 数字签名

    一个ApK如果要安装到手机上,必须要一个数字签名,不过你是debug也好,release也好,这个数字签名来源一个叫做证书的东西,在我们debug的时候,开发工具已经帮我们生成了一个叫做debug.k ...

  4. memcache服务器端及PHP memcache扩展的安装(转载)

    memcache服务器端的安装(windows版)    1.下载memcached软件 32位下载地址: memcached-win32-1.4.4-14.zip(直接下载) 下载页面: 64位下载 ...

  5. intellij idea Maven 创建项目时出现的一些问题

    1.关于maven仓库的问题 在下载资源的时候特别慢,原因是因为天朝的网络你们都懂的.解决方式使用国内镜像,原本可以用的OSChina的镜像,由于其服务器关闭,现在无法使用. 解决方案是使用阿里云的m ...

  6. HTML5本地存储

    之前对这个还不太熟悉,项目中在账号登录时,获取账号的信息,存储在本地然后随时调用//存储localStorage.setItem('data',值); //获取var information = lo ...

  7. C++编译期间字节序判断

    当前常用的字节序一般就两种,大端序和小端序. 下面列出四种字节序的表达方式.在对应平台下,内存布局为{0x,00,0x01,0x02,0x03}的四字节,表示为十六进制的值就如下面代码所示的. END ...

  8. JSPatch热更新的利器.

    如果用一句话来描述JSPatch,就是利用系统自带的JavaScriptCore.framework配合RunTime机制,进行实时的代码下载与运行.. 而且使用也很简单,启动,加载JS,运行... ...

  9. linux 比较两个文件是否一致

    diff source target 如果一致不弹出任何信息

  10. Python全栈开发【基础三】

    Python全栈开发[基础三]  本节内容: 函数(全局与局部变量) 递归 内置函数 函数 一.定义和使用 函数最重要的是减少代码的重用性和增强代码可读性 def 函数名(参数): ... 函数体 . ...