CF620E New Year Tree 线段树 dfs序
luogu链接
题目大意:
有一个节点有颜色的树
操作1.修改子树的颜色
操作2.查询子树颜色的种类
注意,颜色种类小于60种
只有子树的操作,dfs序当然是最好的选择
dfs序列是什么,懒得讲了,自己搜吧
然后开两个数组,begin_和end_记录节点子树在dfs序数组中的开头和结尾
begin,end居然在cf是关键字,还好不是ccf,要不就死了
区间修改一个数,区间查询,线段树的傻逼操作
OK
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ls rt<<1
#define rs rt<<1|1
#define ll long long
const int max4 = 2e6 + 7;
const int inf = 0x3f3f3f3f;
int n, m;
int w[max4], a[max4], begin_[max4], end_[max4];
struct node
{
int l, r, size, lazy;
ll z;
void color(int i)
{
z = z | (1LL << (i - 1));
}
void clear()
{
z = 0LL;
}
} e[max4];
struct edge_edge
{
int v, nxt;
} edge[max4];
int head[max4], e_tot;
void add_edge(int u, int v)
{
edge[++e_tot].v = v;
edge[e_tot].nxt = head[u];
head[u] = e_tot;
}
int count(ll z)
{
int js = 0;
for (; z;)
{
if (z & 1) js++;
z >>= 1;
}
return js;
}
int read()
{
int x = 0, f = 1; char s = getchar();
for (; s < '0' || s > '9'; s = getchar()) if (s == '-') f = -1;
for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
return x * f;
}
void pushup(int rt)
{
e[rt].z = e[ls].z | e[rs].z;
}
void pushdown(int rt)
{
if (e[rt].lazy)
{
e[ls].clear();
e[rs].clear();
e[ls].color(e[rt].lazy);
e[rs].color(e[rt].lazy);
e[ls].lazy = e[rt].lazy;
e[rs].lazy = e[rt].lazy;
e[rt].lazy = 0;
}
}
void build(int l, int r, int rt)
{
e[rt].l = l, e[rt].r = r;
if (l == r)
{
e[rt].color(w[a[l]]);
return;
}
int mid = (l + r) >> 1;
build(l, mid, ls);
build(mid + 1, r, rs);
pushup(rt);
}
void update(int L, int R, int k, int rt)
{
if (L <= e[rt].l && e[rt].r <= R)
{
e[rt].clear();
e[rt].color(k);
e[rt].lazy = k;
return;
}
pushdown(rt);
int mid = (e[rt].l + e[rt].r) >> 1;
if (L <= mid) update(L, R, k, ls);
if (R > mid) update(L, R, k, rs);
pushup(rt);
}
ll query(int L, int R, int rt)
{
if (L <= e[rt].l && e[rt].r <= R)
{
return e[rt].z;
}
pushdown(rt);
int mid = (e[rt].l + e[rt].r) >> 1;
ll ans = 0;
if (L <= mid) ans = ans | query(L, R, ls);
if (R > mid) ans = ans | query(L, R, rs);
pushup(rt);
return ans;
}
void debug()
{
printf("debug\n");
printf(" %d\n", count(e[1].z));
printf(" %d %d\n", count(e[2].z), count(e[3].z) );
printf(" %d %d %d %d\n", count(e[4].z), count(e[5].z), count(e[6].z), count(e[7].z) );
printf(" %d %d %d %d %d %d %d %d\n", count(e[8].z),
count(e[9].z), count(e[10].z), count(e[11].z), count(e[12].z), count(e[13].z), count(e[14].z), count(e[15].z));
}
int js;
void dfs(int u, int f)
{
a[++js] = u;
begin_[u] = js;
for (int i = head[u]; i; i = edge[i].nxt)
{
int v = edge[i].v;
if (v == f) continue;
dfs(v, u);
}
end_[u] = js;
}
int main()
{
n = read(), m = read();
for (int i = 1; i <= n; ++i)
{
w[i] = read();
}
for (int i = 1; i < n; ++i)
{
int x = read(), y = read();
add_edge(x, y);
add_edge(y, x);
}
dfs(1, 0);
// cout << js << "\n";
// for (int i = 1; i <= n; ++i)
// {
// printf("%d ", a[i]);
// } puts("");
// for (int i = 1; i <= n; ++i)
// {
// printf("%d => %d~~%d\n", i, begin_[i], end_[i]);
// }
build(1, n, 1);
for (int i = 1; i <= m; ++i)
{
int tmp = read();
if (tmp == 1)
{
int a = read(), b = read();
update(begin_[a], end_[a], b, 1);
}
else
{
int a = read();
ll ans = query(begin_[a], end_[a], 1);
printf("%d\n", count(ans));
}
//debug();
}
return 0;
}
CF620E New Year Tree 线段树 dfs序的更多相关文章
- CF620E New Year Tree 线段树+dfs序+bitset
线段树维护 dfs 序是显然的. 暴力建 60 个线段树太慢,于是用 bitset 优化就好了 ~ code: #include <bits/stdc++.h> #define M 63 ...
- S - Query on a tree HDU - 3804 线段树+dfs序
S - Query on a tree HDU - 3804 离散化+权值线段树 题目大意:给你一棵树,让你求这棵树上询问的点到根节点直接最大小于等于val的长度. 这个题目和之前写的那个给你一棵 ...
- Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序
题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s 内存限制:512.0MB 总提交次数:196 AC次数:65 平均分: ...
- 【XSY2534】【BZOJ4817】树点涂色 LCT 倍增 线段树 dfs序
题目大意 Bob有一棵\(n\)个点的有根树,其中\(1\)号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜 ...
- BZOJ_3252_攻略_线段树+dfs序
BZOJ_3252_攻略_线段树+dfs序 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏< ...
- 【bzoj4817】树点涂色 LCT+线段树+dfs序
Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...
- HDU 5692 线段树+dfs序
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 线段树+Dfs序【CF620E】New Year Tree
Description 你有一棵以1为根的有根树,有n个点,每个节点初始有一个颜色c[i]. 有两种操作: 1 v c 将以v为根的子树中所有点颜色更改为c 2 v 查询以v为根的子树中的节点有多少种 ...
- CF620E New Year Tree(树形+dfs序+线段树+状态压缩)
题目链接 题目大意 \(~~\)给出一棵 nn 个节点的树,根节点为 11.每个节点上有一种颜色 c\(_{i}\) 和m 次操作.操作有两种: \(~~~~\) 1. 1\(~\)u\(~\)c:将 ...
随机推荐
- 如何去除WIN7任务栏项目上右键菜单中的最近访问一栏
运行gpedit.msc-->用户配置-->管理模板-->任务栏和开始菜单-->将"不保留最近打开文档的记录"和"退出系统时清空最近打开文档的记录 ...
- Callable接口解析
1.接口的定义: public interface Callable<V> { V call() throws Exception; } 2.Callable和Runnable的异同 先看 ...
- Shell初学(一)hello world
精简: 1.创建:可以使用 vi/vim 命令来创建文件如: test.sh ,扩展名并不影响脚本执行,写什么都可以. 2.hello_world: #!/bin/bash ...
- xss跨站脚本攻击及xss漏洞防范
xss跨站脚本攻击(Cross Site Scripting,因与css样式表相似故缩写为XSS).恶意攻击者往Web页面里插入恶意Script代码,当用户浏览该页之时,嵌入其中Web里面的Scrip ...
- PAT 1019 General Palindromic Number[简单]
1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...
- Kafka核心组件
一.Kafka核心组件及工作方式 Producer :消息生产者,就是向kafka broker发消息的客户端 Consumer :消息消费者,向kafka broker取消息的客户端 Topic : ...
- 4.keras实现-->生成式深度学习之用变分自编码器VAE生成图像(mnist数据集和名人头像数据集)
变分自编码器(VAE,variatinal autoencoder) VS 生成式对抗网络(GAN,generative adversarial network) 两者不仅适用于图像,还可以 ...
- 关闭WPS启动时显示的在线模板
关闭WPS启动时显示的在线模板 以WPS2016为例,如下图: 操作步骤如下: ①点击在线模板页面右下方的"设置"选项 ②"设置"界面打开后,选择启动WPS默认 ...
- VMWARE安装centos6 http://www.centoscn.com/image-text/setup/2013/0816/1263.html
http://www.centoscn.com/image-text/setup/2013/0816/1263.html
- apache源码安装
1.apr和apr-util,下载地址: http://apr.apache.org/download.cgi yum install gcc yum install libtool yum inst ...