有关dfs序的例题,需要有一定的位运算基础

题面

  • 给定一个树,树上有颜色,将某一子树的颜色统一修改,求子树中颜色的数量

Solution

  • 子树修改,子树求和,dfs序的知识(类似区间修改区间求和)
  • 考虑到颜色的个数问题,利用位运算进行表示。
  • 最后答案用二进制表示,\(\ 1\)表示有该种颜色,\(\ 0\)表示没有,
  • 因此还需考虑答案\(\ 1\)的数量。
  • dfs序问题自然用到线段树进行维护。

具体介绍一下位运算,和一些小错误

  • 方案总数不是两个节点维护的方案数的简单相加,而是“|”(或)
  • 答案维护的是颜色的个数,但不是具体数值
  • 关于答案\(\ 1\)的个数,可以利用快速幂
  • 也可利用\(\ lowbit\),作用是得到最后\(\ 1\)的位置上表示的数。
  • 建树的时候特别记住需要\(\ long \ long\)的地方

    更加详细的内容

Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int manx=1e6+10;
const int mamx = 1e7 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
inline int read() {
char c = getchar(); int x = 0, f = 1;
for ( ; !isdigit(c); c = getchar()) if (c == '-') f = -1;
for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return x * f;
}
struct nodee{
int l,r;
ll sum;
ll add;
}e[manx<<2+2];
int clr[manx<<1];
struct node{
int u;
int v;
int nxt;
int w;
}ee[manx];
int head[manx],js,l[manx],r[manx],cnt,n,m,dfn[manx];
int add(int u,int v){
ee[++cnt].u = u;
ee[cnt].nxt = head[u];
ee[cnt].v = v;
//e[cnt].w = w;
head[u] = cnt;
}
inline void init(){
cnt=js=0;
clr(head,-1);
}
//大法师
void dfs(int u, int pre){
js++;
l[u] = js;
dfn[js] = u;
for(int i = head[u];~i;i = ee[i].nxt){
int v = ee[i].v;
if( v == pre ) continue;
dfs(v,u);
}
r[u] = js; //我们可以只记录他的入段,尾端那个不必重复
return ;
}
//线段树
void uploat(int s){//上传
e[s].sum = 0;
if(e[s<<1].l) e[s].sum |=e[s<<1].sum ;
if(e[s<<1|1].l ) e[s].sum |=e[s<<1|1].sum ;
}
void downloat(int i){
if(e[i].add !=0){
ll s = e[i].add ;//不要用int 用ll,作者就在这卡了一天
e[i<<1].sum = s;
e[i<<1].add = s;
e[i<<1|1].add = s;
e[i<<1|1].sum = s;
e[i].add = 0;
}
}
void build_up (int rt,int l,int r){
e[rt].l = l;e[rt].r = r;
if(l == r){
e[rt].sum = (ll)1<<(clr[dfn[l]]);
e[rt].add = 0;
return;
}
int mid = (l+r) >> 1;
build_up(rt<<1,l,mid);
build_up(rt<<1|1,mid+1,r);
e[rt].sum = e[rt<<1].sum | e[rt<<1|1].sum;
}
void updata(int i,int l,int r,int add){
if(e[i].l >= l && e[i].r <= r)
{
e[i].sum = (ll)1<<add;
e[i].add = (ll)1<<add;
return;
}
int mid = (e[i].l + e[i].r ) >> 1;
downloat(i);
if(mid >= r)updata(i<<1,l,r,add);
else if(mid <l)updata(i<<1|1,l,r,add);
else updata(i<<1,l,mid,add),updata(i<<1|1,mid+1,r,add);
uploat(i);
}
ll query(int i,int l,int r)
{
if(e[i].l >= l && e[i].r <= r){
return e[i].sum ;
}
downloat(i);
int mid = (e[i].l +e[i].r ) >> 1;
if(mid >= r)
return query(i<<1,l,r);
else
if(mid<l)
return query(i<<1|1,l,r);//熟悉的操作
return query(i<<1,l,mid)|query(i<<1|1,mid+1,r);
}
ll lowbit(ll x){
return x&-x;//lowbit函数
}
int ans;
int main(){
n = read();
m = read();
init ();
for(int i = 1;i <= n;i ++)
clr[i] = read();
for(int i = 1;i <= n - 1;i ++)
{
int x = read(),y = read();
add(x,y);add(y,x);
}
dfs(1,0);
build_up(1,1,n);//建树
for(int i = 1;i <= m; i++)
{
int x = read();
int y;
int z;
if(x == 1){
y = read();z = read();
updata(1,l[y],r[y],z);
}
else{
ans = 0;y = read();
ll diet = query(1,l[y],r[y]);
while(diet>0){
diet-=lowbit(diet);
ans++;//判断1的个数
}
cout<<ans<<endl;//华丽收场
}
}
return 0;
}

题解 CF620E 【New Year Tree】的更多相关文章

  1. 题解:CF593D Happy Tree Party

    题解:CF593D Happy Tree Party Description Bogdan has a birthday today and mom gave him a tree consistin ...

  2. CF620E New Year Tree(线段树+二进制)

    题解 弱智题,二进制表示位数.合并时用| 就是被1<<x卡了好久. 要写成1ll<<x才行 #include<iostream> #include<cstri ...

  3. 题解-CF429C Guess the Tree

    题面 CF429C Guess the Tree 给一个长度为 \(n\) 的数组 \(a_i\),问是否有一棵树,每个节点要么是叶子要么至少有两个儿子,而且 \(i\) 号点的子树大小是 \(a_i ...

  4. 题解-AtCoder Code-Festival2017 Final-J Tree MST

    Problem \(\mathrm{Code~Festival~2017~Final~J}\) 题意概要:一棵 \(n\) 个节点有点权边权的树.构建一张完全图,对于任意一对点 \((x,y)\),连 ...

  5. PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

    题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...

  6. [LeetCode 题解]: Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  7. [CF620E]New Year Tree

    题目大意:有一棵以$1$为根的有根树,有$n$个点,每个节点初始有颜色$c_i$.有两种操作: $1 v c:$将以$v$为根的子树中所有点颜色更改为$c$ $2 v:$ 查询以$v$为根的子树中的节 ...

  8. leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

  9. 题解 CF383C 【Propagating tree】

    这道题明明没有省选难度啊,为什么就成紫题了QAQ 另:在CF上A了但是洛谷Remote Judge玄学爆零. 思路是DFS序+线段树. 首先这道题直观上可以对于每一次修改用DFS暴力O(n),然后对于 ...

随机推荐

  1. TurtleBot3 Waffle (tx2版华夫)(6)

    重要提示:请在配网通信成功后进行操作,配网后再次开机需要重新验证通信: 重要提示:[Remote PC]代表PC端.[TurtelBot]代表树莓派端: 操作步骤如下: 1)[Remote PC] 启 ...

  2. Node 跨域问题 Access to XMLHttpRequest at 'http://localhost:8080/api/user/login' from origin 'http://localhost:808

    人不可能踏进同一条河流,我可以一天在同一个问题上摔倒两次. 这次是跨域问题,都是泪,教程提供的服务端代码虽然配置了文件,但是依然是没有解决跨域问题,依然报错 Request header field ...

  3. 一行 CSS 代码的魅力

    之前在知乎看到一个很有意思的讨论 一行代码可以做什么? 那么,一行 CSS 代码又能不能搞点事情呢? CSS Battle 首先,这让我想到了,年初的时候沉迷的一个网站 CSS Battle .这个网 ...

  4. mysql锁类型

    mysql锁类型 问题 都有哪些锁 锁与隔离级别的关系 sql语句中涉及都涉及哪些锁 事务中,锁何时释放 死锁检测机制 概要

  5. Hadoop伪分布式模式

    搭建在单一服务器 基于官方文档 http://hadoop.apache.org/docs/r2.7.2/hadoop-project-dist/hadoop-common/SingleCluster ...

  6. git文件操作

    git下载地址: https://git-scm.com/download mac 直接使用brew下载brew install git 1Git一般工作流程: 1.在工作目录创建版本库 2.在工作目 ...

  7. 【Web】CSS中的浮动float

    CSS中的float 文章目录 CSS中的float 1.float浮动属性 2.float文字环绕图片 3.float浮动的真正原因以及副作用分析 4.清除浮动的四种解决方法 5.实际应用 导航效果 ...

  8. 关于SSRF与CSRF漏洞的解释

    目录 SSRF服务端请求伪造(外网访问内网) 1.SSRF形成原因 2.利用SSRF漏洞的目的 3.SSRF漏洞的用途 4.SSRF漏洞的特性 实例 5.如何挖掘SSRF漏洞 6.常用SSRF去做什么 ...

  9. Linux下nginx的安装以及环境配置

    参考链接 https://blog.csdn.net/qq_42815754/article/details/82980326 环境: centos7 .nginx-1.9.14 1.下载 并解压  ...

  10. 二十五:XSS跨站值原理分类及攻击手法

    HTML DOM树 XSS跨站产生原理,危害,特点 本质,产生层面,函数类,漏洞操作对应层,危害影响,浏览器内核版本 XSS是什么? XSS全称跨站脚本(Cross Site Scripting),为 ...