有关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. 将Maven镜像更换为国内阿里云仓库

    1.国内访问maven默认远程中央镜像特别慢 2.用阿里的镜像替代远程中央镜像 3.大部分jar包都可以在阿里镜像中找到,部分jar包在阿里镜像中没有,需要单独配置镜像 换为国内镜像,让你感受飞一般的 ...

  2. Java发送企业微信应用消息

    1.发送消息与被动回复消息 (1)流程不同:发送消息是第三方服务器主动通知微信服务器向用户发消息.而被动回复消息是 用户发送消息之后,微信服务器将消息传递给 第三方服务器,第三方服务器接收到消息后,再 ...

  3. hibernate连接数据库中文乱码

    4.做完这两步还是不行,需要修改hibernate的配置文件hibernate.cfg.xml,在配置文件配置hibernate.connection.url属性.示例: <property n ...

  4. Java学习日报8.3

    package car;class Person{ private String name; private int age; private Car car; public Person(Strin ...

  5. 学习一下 SpringCloud (三)-- 服务调用、负载均衡 Ribbon、OpenFeign

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  6. Let’s Encrypt 通配符证书,泛域名证书申请配置

    首先你可以查看下官方提供的支持申请通配符证书的客户端列表:https://letsencrypt.org/docs/client-options/. 参考链接:https://github.com/N ...

  7. TR2021_0000偶发数据库连接异常问题排查

    [问题描述] 数据库连接异常是很难排查的一类问题.因为它牵涉到应用端,网络层和服务器端.任何一个组件异常,都会导致数据库连接失败.开发遇到数据库连接不上的问题,都会第一时间找DBA来协助查看,DBA除 ...

  8. 了解一下ajax

    AJAX:是一种无需重新加载页面的情况下能够更新部分(局部更新)网页的技术. 1. 概念:ASychronous JavaScript And XML 异步的JavaScript和XML 首先了解一下 ...

  9. ClickHouse安装使用(单机、集群、高可用)

    Clickhouse版本:20.3.6.40-2 安装包地址:https://repo.yandex.ru/clickhouse/rpm/stable/x86_64/ 一.单机版 1.安装依赖 yum ...

  10. 【Spring】Spring中的Bean - 2、Baen的实例化 (构造器、静态工厂、实例工厂)

    Bean的实例化 文章目录 Bean的实例化 构造器实例化 静态工厂方式实例化 实例工厂方式实例化 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-S ...