codeforces 620E. New Year Tree dfs序+线段树+bitset
给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60.
每个节点一个bitset维护就可以。
- #include <iostream>
- #include <vector>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <map>
- #include <set>
- #include <string>
- #include <queue>
- #include <stack>
- #include <bitset>
- using namespace std;
- #define pb(x) push_back(x)
- #define ll long long
- #define mk(x, y) make_pair(x, y)
- #define lson l, m, rt<<1
- #define mem(a) memset(a, 0, sizeof(a))
- #define rson m+1, r, rt<<1|1
- #define mem1(a) memset(a, -1, sizeof(a))
- #define mem2(a) memset(a, 0x3f, sizeof(a))
- #define rep(i, n, a) for(int i = a; i<n; i++)
- #define fi first
- #define se second
- typedef pair<int, int> pll;
- const double PI = acos(-1.0);
- const double eps = 1e-;
- const int mod = 1e9+;
- const int inf = ;
- const int dir[][] = { {-, }, {, }, {, -}, {, } };
- const int maxn = 4e5+;
- struct node
- {
- int to, nextt;
- }e[maxn*];
- int lazy[maxn<<], a[maxn], in[maxn], out[maxn], head[maxn*], num, clock;
- bitset <> s[maxn<<];
- void add(int u, int v) {
- e[num].to = v, e[num].nextt = head[u], head[u] = num++;
- }
- void pushUp(int rt) {
- s[rt] = s[rt<<]|s[rt<<|];
- }
- void pushDown(int rt) {
- if(lazy[rt]) {
- lazy[rt<<] = lazy[rt<<|] = lazy[rt];
- s[rt<<].reset();
- s[rt<<|].reset();
- s[rt<<][lazy[rt]] = s[rt<<|][lazy[rt]] = ;
- lazy[rt] = ;
- return ;
- }
- }
- void update(int val, int L, int R, int l, int r, int rt) {
- if(L<=l&&R>=r) {
- s[rt].reset();
- s[rt][val] = ;
- lazy[rt] = val;
- return ;
- }
- pushDown(rt);
- int m = l+r>>;
- if(L<=m)
- update(val, L, R, lson);
- if(R>m)
- update(val, L, R, rson);
- pushUp(rt);
- }
- void dfs(int u, int fa) {
- in[u] = ++clock;
- for(int i = head[u]; ~i; i = e[i].nextt) {
- int v = e[i].to;
- if(v == fa)
- continue;
- dfs(v, u);
- }
- out[u] = clock;
- }
- bitset<> query(int L, int R, int l, int r, int rt) {
- if(L<=l&&R>=r) {
- return s[rt];
- }
- pushDown(rt);
- bitset<> tmp;
- tmp.reset();
- int m = l+r>>;
- if(L<=m)
- tmp |= query(L, R, lson);
- if(R>m)
- tmp |= query(L, R, rson);
- return tmp;
- }
- int main()
- {
- int q, n, m, x, y;
- int ch;
- memset(head, -, sizeof(head));
- cin>>n>>m;
- for(int i = ; i<=n; i++)
- scanf("%d", &a[i]);
- for(int i = ; i<n; i++) {
- scanf("%d%d", &x, &y);
- add(x, y);
- add(y, x);
- }
- dfs(, );
- for(int i = ; i<=n; i++) {
- update(a[i], in[i], in[i], , n, );
- }
- while(m--) {
- scanf("%d%d", &ch, &x);
- if(ch == )
- printf("%d\n", query(in[x], out[x], , n, ).count());
- else {
- int z;
- scanf("%d", &z);
- update(z, in[x], out[x], , n, );
- }
- }
- return ;
- }
codeforces 620E. New Year Tree dfs序+线段树+bitset的更多相关文章
- CodeForces 620E:New Year Tree(dfs序+线段树)
E. New Year Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputout ...
- 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 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- DFS序+线段树+bitset CF 620E New Year Tree(圣诞树)
题目链接 题意: 一棵以1为根的树,树上每个节点有颜色标记(<=60),有两种操作: 1. 可以把某个节点的子树的节点(包括本身)都改成某种颜色 2. 查询某个节点的子树上(包括本身)有多少个不 ...
- Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树
题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- POJ3321 - Apple Tree DFS序 + 线段树或树状数组
Apple Tree:http://poj.org/problem?id=3321 题意: 告诉你一棵树,每棵树开始每个点上都有一个苹果,有两种操作,一种是计算以x为根的树上有几个苹果,一种是转换x这 ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- codeforces 916E Jamie and Tree dfs序列化+线段树+LCA
E. Jamie and Tree time limit per test 2.5 seconds memory limit per test 256 megabytes input standard ...
- codechef T6 Pishty and tree dfs序+线段树
PSHTTR: Pishty 和城堡题目描述 Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格 的古堡和非常聪明的熊闻名全国. 胡斯特的镇城之宝是就是这么一座古 ...
随机推荐
- 九一八-->我逝去的青春
九一八纪念馆 十二年前 30元一张门票 我毫不犹豫掏钱进去参观 你们笑我 钱少人傻 在东北的四年 从2001到2005 每年都感慨这一天 北国的秋色里 警钟长鸣 长鸣声中 有我逝去的青春 如今 三十而 ...
- 【从零学习Python】Ubuntu14.10下Python开发环境配置
1. 前言 近期在研究计算机视觉的一些算法,也刚開始接触linux,试着在ubuntu下用qt+openCV进行开发,感觉还行.可是Python作为在学术领域广为应用的高级解释性语言.其在计算机视觉的 ...
- 原生js下拉刷新
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- 最近用的到的一些js的常用方法(简单的)
由于新的项目开始了,是使用MVC 5.0 开发的,前端使用了两个主流的框架 UIKIT,Ignite UI(收费) 因为是mvc主要用json来交互,不能避免要对前端脚本进行操作,所以就将能用到的方法 ...
- iOS合并静态库文件
具体命令如下(在控制台输入如下命令): lipo -create 其中一个要合并的静态库 另一个要合并的静态库 -output 合并后的静态库
- Assertion failure in -[UIView layoutSublayersOfLayer:]
Assertion failure in -[UIView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.137/UIView.m:8 ...
- 在C++中如何使用C
如下代码: /*C语言头文件:Max.h*/ #ifndef _MAX_H_ #define _MAX_H_ int Max(int nA,int nB) #endif /*C语言实现文件:Max.c ...
- IWebBrowser隐藏滚动条
刚才在项目里看到一个IWebBrowser2,竟然需要通过MoveWindow的方式把滚动条遮挡,如果要缩小IWebBrowser2控件的显示大小呢?这种方法至少我用不习惯,起码也得从源头解决这样的问 ...
- BZOJ 3224: Tyvj 1728 普通平衡树(BST)
treap,算是模板题了...我中间还一次交错题... -------------------------------------------------------------------- #in ...
- 5.6.3.7 localeCompare() 方法
与操作字符串有关的最后一个方法是localeCompare(),这个方法比较两个字符串,并返回下列值中的一个: 如果字符串在字母表中应该排在字符串参数之前,则返回一个负数(大多数情况下是-1,具体的值 ...