首页 > 其他分享 >CF620E New Year Tree

CF620E New Year Tree

时间:2024-02-02 17:55:06浏览次数:29  
标签:CF620E modify rep Tree mid dfs cin int New

CF620E New Year Tree

题意:给出一棵 n 个节点的树,根节点为 1。每个节点上有一种颜色 ci​。m 次操作。操作有两种:

  • 1 u c:将以 u 为根的子树上的所有节点的颜色改为 c。
  • 2 u:询问以 u 为根的子树上的所有节点的颜色数量。
    1 <= c <= 60。

由于 c 的范围,可以用一个整数来表示每棵子树的状态。
将树上问题转化为序列问题,当然可以树剖,但本题只对子树操作,维护 in 为进树时的时间戳,out 为出树时的时间戳,再在 dfs 序上用线段树维护即可。

#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
#define per(i, a, b) for(int i = (a); i >= (b); -- i)
#define pb emplace_back
#define All(X) X.begin(), X.end()
#define ls x << 1
#define rs ls | 1
using namespace std;
using ll = long long;
constexpr int N = 4e5 + 5;

int n, m, c[N], in[N], out[N], timestamp;
vector<int> G[N];

struct Node {
	ll v, tag;
} t[N << 2];


void pushup(int x) {
	t[x].v = t[ls].v | t[rs].v;
}

void pushdown(int x) {
	if(t[x].tag) {
		t[ls].v = t[ls].tag = t[x].tag;
		t[rs].v = t[rs].tag = t[x].tag;
		t[x].tag = 0;
	}
}

void modify(int L, int R, int v, int x = 1, int l = 1, int r = n) {
	if(L <= l && r <= R) {
		t[x].v = t[x].tag = 1ll << v;
		return;
	}
	pushdown(x);
	int mid = l + r >> 1;
	if(mid >= L) modify(L, R, v, ls, l, mid);
	if(mid < R) modify(L, R, v, rs, mid + 1, r);
	pushup(x);
}

ll query(int L, int R, int x = 1, int l = 1, int r = n) {
	if(L <= l && r <= R) return t[x].v;
	pushdown(x);
	int mid = l + r >> 1;
	ll ret = 0;
	if(mid >= L) ret |= query(L, R, ls, l, mid);
	if(mid < R) ret |= query(L, R, rs, mid + 1, r);
	return ret;
}


void dfs(int x, int fa) {
	in[x] = ++ timestamp;
	modify(in[x], in[x], c[x]);
	for(int y : G[x]) {
		if(y != fa) {
			dfs(y, x);
		}
	}
	out[x] = timestamp;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
	cin >> n >> m;
	rep(i, 1, n) cin >> c[i];
	rep(i, 2, n) {
		int x, y; cin >> x >> y;
		G[x].pb(y);
		G[y].pb(x);
	}
	dfs(1, 0);
	rep(i, 1, m) {
		int op, x; cin >> op >> x;
		if(op == 1) {
			int col; cin >> col;
			modify(in[x], out[x], col);
		}
		else cout << __popcount(query(in[x], out[x])) << '\n';
	}
	return 0;
}

标签:CF620E,modify,rep,Tree,mid,dfs,cin,int,New
From: https://www.cnblogs.com/Luxinze/p/18003607

相关文章

  • Java 中的HashSet 和 TreeSet
    HashSetHashSet集合:无序不可重复方法HashSet集合的元素实际上是放到HashMap集合的Key中importjava.util.HashSet;importjava.util.Set;/**HashSet集合:无序不可重复**/publicclassHashSetTest{publicstaticvoidmain(String[]args){//演示一......
  • CF620E New Year Tree 题解
    题目链接:CF或者洛谷这题很简单,看到颜色数,HH的项链?树,树上的HH的项链?带修,树上的镜中的昆虫?\(c_i\le60\),噢,easy了。考虑一类信息,表示有和无,对于某种颜色来讲,\(0/1\)表示无或者有,而或运算让我们从小区间的有无情况反映到大区间的有无情况。一种暴力的想法,为每种颜色建立一棵有......
  • 聊聊ClickHouse MergeTree引擎的固定/自适应索引粒度
    ​ 前言我们在刚开始学习ClickHouse的MergeTree引擎时,就会发现建表语句的末尾总会有SETTINGSindex_granularity=8192这句话(其实不写也可以),表示索引粒度为8192。在每个datapart中,索引粒度参数的含义有二:每隔index_granularity行对主键组的数据进行采样,形成稀疏索引,并存储......
  • [转帖]Open JDK 8.0_152-b16 崩溃 : [libzip.so+0x12522] newEntry+0x62
    一.问题描述在执行spark任务的时候,JVM崩溃.崩溃dump日志:##AfatalerrorhasbeendetectedbytheJavaRuntimeEnvironment:##SIGBUS(0x7)atpc=0x00007f9adacb9522,pid=107874,tid=0x00007f9add417700##JREversion:Java(TM)SERuntimeEnvironme......
  • k-D Tree
    作用维护\(k\)维空间上的点的数据结构,树高严格\(log\),空间\(O(n)\)。支持插入,并如替罪羊树一般根据“不平衡度”\(\alpha\)重构。可以用线段树式或平衡树式实现。建树对于一个给定序列,类似线段树建树,使用nth_element可以总复杂度\(n\log\)建树。由于需要分割,要么轮......
  • 【侯捷C++面向对象笔记】补充5-new & delete重载
    平时所使用的new和delete操作,称之为表达式,一般由好几个步骤组成。如上图所示,new表达式会被编译器转化为三个步骤。new表达式不能重载,但其中operatornew是可以重载的。➡️全局::operatornew的重载why不能放在namespace内?因为全局operatornew是放在defaultglobalnamespac......
  • no new changes
    nonewchangeshttps://gerrit-documentation.storage.googleapis.com/Documentation/2.16.4/error-no-new-changes.htmlWiththiserrormessageGerritrejectstopushacommitifthepushedcommitwasalreadysuccessfullypushedtoGerritinprojectscope.In......
  • Treeview
    usingDBHelper;usingSunny.UI;usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Windows.Forms;usingSystem.Linq;usingSystem.Text;namespaceMES{publicpartialclassRolePower:UIForm{......
  • Git、.gitinore、SourceTree使用介绍
    Git使用教程Git是分布式版本控制系统,也可以叫内容管理系统(CMS),工作管理系统。Git安装本文档后半部分会介绍SourceTree,SourceTree内置有Git所以这里不介绍其他Git安装方式。Git工作流程克隆Git资源到本地仓库(文件夹)在本地仓库中添加或修改文件。获取Git其他人的修改信......
  • CF337E Divisor Tree
    题意简述定义DivisorTree为一棵树:叶子上的数为质数。非叶子上的数为其所有儿子上的数的乘积。给定\(n\)个数\(a_i\),你需要让每个\(a_i\)都在DivisorTree上出现,并最小化DivisorTree的节点数量。\(n\le8,a_i\le10^6\)。分析显然DivisorTree上只能有质数......