Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset
Z - New Year Tree
这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset,
首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树。
然后就是一个区间修改和区间查询。这个区间查询时查询这个区间的种类数。
这个之前写过几个题目也是查区间种类数的
莫队和权值线段树应该都是不支持修改的,所以这个题目用不上,
然后就是这个高端压位卡常容器bitset 我觉得这个题目应该可以用到。
#include <cstring>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
#include <bitset>
#include <algorithm>
#include <map>
#include <vector>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
typedef bitset<> bit;
const int maxn = 4e5 + ;
vector<int>G[maxn];
ll el[maxn], er[maxn], a[maxn], tot, num[maxn]; void dfs(int u,int pre)
{
el[u] = ++tot;
num[tot] = u;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if (v == pre) continue;
dfs(v, u);
}
er[u] = tot;
} bit tree[maxn * ];
ll lazy[maxn * ]; void build(int id,int l,int r)
{
lazy[id] = ;
if(l==r)
{
tree[id].reset();
tree[id] = (1ll << (a[num[l]] - ));
return;
}
int mid=(l + r) >> ;
build(id << , l, mid);
build(id << | , mid + , r);
tree[id] = tree[id << ] | tree[id << | ];
} void push_down(int id)
{
if(lazy[id]!=)
{
tree[id << ].reset(); tree[id << | ].reset();
tree[id << ] = (1ll << (lazy[id] - ));
tree[id << | ] = (1ll << (lazy[id] - ));
lazy[id << ] = lazy[id << | ] = lazy[id];
lazy[id] = ;
}
} void update(int id,int l,int r,int x,int y,int val)
{
if(x<=l&&y>=r)
{
tree[id].reset();
tree[id] = (1ll << (val - ));
lazy[id] = val;
return;
}
push_down(id);
int mid = (l + r) >> ;
if (x <= mid) update(id << , l, mid, x, y, val);
if (y > mid) update(id << | , mid + , r, x, y, val);
tree[id] = tree[id << ] | tree[id << | ];
} bit query(int id,int l,int r,int x,int y)
{
if (x <= l && y >= r) return tree[id];
int mid = (l + r) >> ;
bit ans;
ans.reset();
push_down(id);
if (x <= mid) ans = query(id << , l, mid, x, y);
if (y > mid) ans |= query(id << | , mid + , r, x, y);
return ans;
} int main() {
int n, m;
tot = ;
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) scanf("%lld", &a[i]);
for (int i = ; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(, -);
build(, , n);
while (m--) {
int opt, v, x;
scanf("%d", &opt);
if (opt == ) {
scanf("%d%d", &v, &x);
update(, , n, el[v], er[v], x);
}
else {
scanf("%d", &v);
bit ans = query(, , n, el[v], er[v]);
printf("%d\n", ans.count());
}
}
return ;
}
Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset的更多相关文章
- Alyona and a tree CodeForces - 739B (线段树合并)
大意: 给定有根树, 每个点$x$有权值$a_x$, 对于每个点$x$, 求出$x$子树内所有点$y$, 需要满足$dist(x,y)<=a_y$. 刚开始想错了, 直接打线段树合并了..... ...
- Vasya and a Tree CodeForces - 1076E (线段树 + dfs)
题面 Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 writ ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)
2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)
题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...
- hdu1698 线段树区间更新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
随机推荐
- **********Prometheus(三)******************
通过centos7.x来部署Prometheus ####同步时间,否则后面监控会有异常!!!!!####### 1. 创建文件夹,上传软件包.解压并将prometheus promtool两个命令复 ...
- 设计模式中巧记I/O
一.I/O 1. I/O操作中的设计模式 概要 以设计模式角度,自顶向下理解I/O源码结构 理解字节与字符的关系 1.1 装饰者模式(输入流为例) 背景:通过继承扩展对象耦合度高,使用装饰者扩展可以在 ...
- python从零开始基础入门——开发环境搭建
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:山海皆可平z PS:如有需要Python学习资料的小伙伴可以加点击下方 ...
- Python 类学习的一些Tips
这里不详细介绍类,只总结一些小萌新在学习python 类时会有的一些疑点. 类的私有性 在python中,属性和方法的访问权限只有两种,公开的,和私有的.在给属性命名时用两个“__”下划线作为开头,就 ...
- [javascript] js实现小数的算术运算方法
/** ** 加法函数,用来得到精确的加法结果 ** 说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显.这个函数返回较为精确的加法结果. ** 调用:accAdd(arg ...
- orcale 多列转一行显示
强大的数据库有个自带函数wm_concat() wm_concat()这个函数放的是需要汇总的列 select wm_concat(name) name from tablename
- 科学计算包Numpy
Numpy 用于科学计算的python模块,提供了Python中没有的数组对象,支持N维数组运算.处理大型矩阵.成熟的广播函数库.矢量运算.线性代数.傅里叶变换以及随机数生成等功能,并可与C++.FO ...
- 用Python的Plotly画出炫酷的数据可视化(含各类图介绍,附代码)
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 我被狗咬了 在谈及数据可视化的时候,我们通常都会使用到matplo ...
- phpstudy xdebug 配置
来源:https://baijiahao.baidu.com/s?id=1607680791440431678&wfr=spider&for=pc https://www.cnblog ...
- Selenium常见报错问题(1)- 先来认识下selenium常见异常类
如果你在跑selenium脚本时,需要某些异常不知道怎么解决时,可以看看这一系列的文章,看看有没有你需要的答案 https://www.cnblogs.com/poloyy/category/1749 ...