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'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
随机推荐
- acm小知识
__builtin_popcount(i); __builtin_popcountll(i) ;//计算i的二进制表示中1的个数 int a[M] , b[M] ; memcpy(a+i , b+j ...
- HTML和XHTML的区别
引言:XHTML是用XML语言重写了HTML,也可以理解为XHTML是HTML的过渡语言,它比HTML语言更严谨,基本语言都还是沿用HTML的标签. XHTML和HTML的差别主要分为两大类: 功能上 ...
- PAP认证方式原理和实现
PAP认证协议 基本描述: Password Authentication Protocol 口令认证协议 PAP认证过程非常简单,二次握手机制,使用明文格式发送用户名和密码,发起方为被认证方,可以做 ...
- ES6学习笔记一
块级作用域:在ES5中只有全局作用域与函数作用域,ES6中新增的块级作用域避免变量的覆盖与泄露.考虑到代码运行环境,在块级作用域中应避免声明函数,最好用函数表达式的方式声明. let与const:le ...
- java模拟post请求发送json
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...
- RBAC权限模型
RBAC 现在大多数的管理系统都是基于RBAC开发的组织机构权限框架.所有的操作都是基于角色(Role)来完成的.我们先从需求的角度出发,来了解关于系统权限管理. 用户A和用户B都属于研发部,我们可以 ...
- angularJS select
ng-options指令 在angularJS中创建select下拉是使用ng-options创建下拉项 ng-options="val as label for element in ar ...
- hdu1045 DFS
#include<stdio.h> #include<string.h> int n; int maxx; ][]; ]={,-,,}; ]={,,,-}; ][][];//炮 ...
- [MySQL]MySQL之权限管理
一.MySQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你全力以内的事情,不可以越界.比如只允许你执行select操作,那么你就不能执行update操作.只允许你从某台机器上连接m ...
- LINUX下YUM源配置
转自:http://www.cnblogs.com/phoebus0501/archive/2010/12/14/1906144.html 1.确保RHEL5中已经安装了yum [root@lvs-m ...