DFS序+线段树+bitset CF 620E New Year Tree(圣诞树)
题意:
一棵以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(圣诞树)的更多相关文章
- Codeforces633G(SummerTrainingDay06-I dfs序+线段树+bitset)
G. Yash And Trees time limit per test:4 seconds memory limit per test:512 megabytes input:standard i ...
- 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 ...
- DFS序+线段树 hihoCoder 1381 Little Y's Tree(树的连通块的直径和)
题目链接 #1381 : Little Y's Tree 时间限制:24000ms 单点时限:4000ms 内存限制:512MB 描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每 ...
- codeforces 620E. New Year Tree dfs序+线段树+bitset
题目链接 给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60. 每个节点一个bitset维护就可以. #in ...
- 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 ...
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
- 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 339 Solved: 130[Submit][Status][Discuss] D ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)
题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
随机推荐
- Oracle 如何扩展表空间
第一步:查看ORACLE表空间的使用情况:SELECT DBF.TABLESPACE_NAME, DBF.TOTALSPACE "总量(M)", DBF.T ...
- mysql语句入门
show databases; 展示可以用的数据库: use employees; 进入数据库: show tables; 展示可用的列表. sho ...
- GSM07.10协议中串口复用的注意事项
DLCI:0通道(地址域中DLCI==0)是控制通道,用来传输管理信息.逻辑通道的建立和关闭,睡眠模式的启动和唤醒,流量控制等控制信息都是用该通道. DLCI:1~n通道是逻辑通道,用来传输用户数据. ...
- Deep Residual Learning
最近在做一个分类的任务,输入为3通道车型图片,输出要求将这些图片对车型进行分类,最后分类类别总共是30个. 开始是试用了实验室师姐的方法采用了VGGNet的模型对车型进行分类,据之前得实验结果是训练后 ...
- 耿丹CS16-2班第六次作业汇总
Deadline: 2016-11-13 11:59 作业内容 第六次作业总结 00.本次题目分值最高为**6分/题 × 7题 + 5分/篇 × 1篇 = 47分**,其中有新解法者每题加原创分**2 ...
- Gerrit增加SSL证书
在http的基础上增加SSL 配置gerrit.config文件 [gerrit] basePath = git canonicalWebUrl = https://172.16.99.212/ .. ...
- struts2使用配置文件中使用json-default的问题
使用Struts2实现异步验证数据: 与Struts2相关的jar包如下: freemarker-2.3.13.jar ognl-2.6.11.jar struts2-core-2.1.6.jar s ...
- Linux远程复制命令SCP
scp test.json savo@yourdomain.com:/usr/share/nginx/test # 以上命令需配置好ssh
- 【Kubernetes】K8S网络方案--最近在看的
K8S网络-最近在看的 Create a Minikube cluster - Kubernetes Kubernetes Documentation - Kubernetes Kubernetes ...
- 《转载》使用org.w3c.dom.Element的setTextContent()、getTextContent()方法时出现编译错误
今天在更新项目后进行编译时,出现如下错误一堆: 编译错误 Google之,在stackoverflow上看到如下的解决方法: I came here with the same problem. Ev ...