有关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. 运行springboot项目报错 Error running 'ResourceApplication': Command line is too long. Shorten comma

    方法1 IDEA 运行报错:Error running '***': Command line is too long 技术标签: IDEA     Error running 'Test': Com ...

  2. OAuth2.0的四种授权模式(转)

    1. OAuth2简易实战(一)-四种模式 1.1. 隐式授权模式(Implicit Grant) 第一步:用户访问页面时,重定向到认证服务器. 第二步:认证服务器给用户一个认证页面,等待用户授权. ...

  3. 有关em的个人理解

    个人的感觉关键就是在那个font-size,  对于一开始没有设置font-size的可以默认那就是16px  后面的所有的基础都应该是在前面距离他最近的那个font-size的大小作为1em进行设置 ...

  4. 论JAVA实现MYSQL 行级锁(分布式锁实现方案一)

    @Override @Transactional public String getCustomerId() { // return String.valueOf(getSequenceId(SEQ_ ...

  5. git基础-远程仓库的使用

    远程仓库的使用 为了能在任意 Git 项目上协作,你需要知道如何管理自己的远程仓库. 远程仓库是指托管在因特网或其他网络中的你的项目的版本库. 你可以有好几个远程仓库,通常有些仓库对你只读,有些则可以 ...

  6. GitHub基础使用指南

    引言: 只要进行了一段时间的软件或者编程相关知识的学习,相信大家或多或少都会见过这只"猫猫",这可不是什么宠物店铺的商标,身为即将成为程序猿/媛的你,或者已经是一位程序猿/媛的你, ...

  7. Lagom 官方文档之随手记

    引言 Lagom是出品Akka的Lightbend公司推出的一个微服务框架,目前最新版本为1.6.2.Lagom一词出自瑞典语,意为"适量". https://www.lagomf ...

  8. 第8章 控制对象的访问(setter、getter、proxy)

    目录 1. 使用getter和setter控制属性访问 1.1 定义getter与setter 通过对象字面量定义,或在ES6的class中定义 通过使用内置的Object.definePropert ...

  9. Oracle误删数据的恢复

    Oracle误删数据的恢复,分为两种方法:SCN和时间戳两种方法恢复. 一.通过SCN恢复删除且已提交的数据 1.获得当前数据库的SCN号 select current_scn from v$data ...

  10. 微信小程序request请求的封装

    目录 1,前言 2,实现思路 3,实现过程 3.1,request的封装 3.2,api的封装 4,实际使用 1,前言 在开发微信小程序的过程中,避免不了和服务端请求数据,微信小程序给我们提供了wx. ...