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'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
随机推荐
- iOS 浅谈:深.浅拷贝与copy.strong
深.浅拷贝 copy mutableCopy NSString NSString *string = @"汉斯哈哈哈"; // 没有产生新对象 NSString *copyStri ...
- Id.value与document.getElementById("Id").value的区别
如果标签Id在Form表单里面的话,直接Id.value就不能用了,而是要用Form.Id.value来取值或设置值 所以最好用document.getElementById("Id&quo ...
- 安卓中AIDL的使用方法快速入门
1.AIDL是什么? AIDL全称是Android Interface Definition Language,即安卓接口定义语言. 2.AIDL是用来做什么的?(为什么要有AIDL) AIDL是用来 ...
- HTML5 中的 canvas 画布(一)
---恢复内容开始--- 在HTML5中新添加的元素,canvas 现在支持 IE9+的版本 注意:HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript ...
- iOS音频解码表格
- PYTHON 随机验证码生成
# 生成一个六位随机验证码 import random # random 生成随机数 temp = '' for i in range(6): num = random.randrange(0,6) ...
- erlang 虚机CPU 占用高排查
-问题起因 近期线上一组服务中,个别节点服务器CPU使用率很低,只有其他1/4.排除业务不均,曾怀疑是系统top统计错误,从Erlang调度器的利用率调查 找到通过erlang:statistics( ...
- 《A Convolutional Neural Network Cascade for Face Detection》
文章链接: http://pan.baidu.com/s/1bQBJMQ 密码:4772 作者在这里提出了基于神经网络的Cascade方法,Cascade最早可追溯到Haar Feature提取 ...
- ABAP 常见查询问题解决方法
在ABAP 编程的时候会遇到查询单条语句的时候数能取对 但是条目数多了的话 会出现数不准确的问题 原因可能出现在查询使用了二分法查询方式 二分法查询下必须按排序的字段排序 还得按照排序的字段 ...
- Linux 添加完硬盘后,如何挂载和分区、以及其他的分区不足,如何从新的硬盘上挂载借用
挂载好新硬盘后输入fdisk -l命令看当前磁盘信息 可以看到除了当前的第一块硬盘外还有一块sdb的第二块硬盘,然后用fdisk /dev/sdb 进行分区 进入fdisk命令,输入h可以看到该命令的 ...